2 +--------------------------------------------------------------------+
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #ifndef PHP_HTTP_CACHE_API_H
16 #define PHP_HTTP_CACHE_API_H
18 #include "php_http_send_api.h"
20 #include "ext/standard/crc32.h"
21 #include "ext/standard/sha1.h"
22 #include "ext/standard/md5.h"
24 #if defined(HTTP_HAVE_PHP_HASH_H) && HTTP_HAVE_EXT(HASH)
25 # define HTTP_HAVE_EXT_HASH 1
26 # include "php_hash.h"
28 # define HTTP_HAVE_EXT_HASH 0
31 #define http_etag_digest(d, l) _http_etag_digest((d), (l))
32 static inline char *_http_etag_digest(const unsigned char *digest
, int len
)
34 static const char hexdigits
[17] = "0123456789abcdef";
36 char *hex
= emalloc(len
* 2 + 1);
39 for (i
= 0; i
< len
; ++i
) {
40 *ptr
++ = hexdigits
[digest
[i
] >> 4];
41 *ptr
++ = hexdigits
[digest
[i
] & 0xF];
48 #define http_etag_init() _http_etag_init(TSRMLS_C)
49 static inline void *_http_etag_init(TSRMLS_D
)
52 char *mode
= HTTP_G
->etag
.mode
;
54 #if HTTP_HAVE_EXT_HASH
55 php_hash_ops
*eho
= NULL
;
57 if (mode
&& (eho
= php_hash_fetch_ops(mode
, strlen(mode
)))) {
58 ctx
= emalloc(eho
->context_size
);
62 if (mode
&& ((!strcasecmp(mode
, "crc32")) || (!strcasecmp(mode
, "crc32b")))) {
63 ctx
= emalloc(sizeof(uint
));
65 } else if (mode
&& !strcasecmp(mode
, "sha1")) {
66 PHP_SHA1Init(ctx
= emalloc(sizeof(PHP_SHA1_CTX
)));
68 PHP_MD5Init(ctx
= emalloc(sizeof(PHP_MD5_CTX
)));
74 #define http_etag_finish(c) _http_etag_finish((c) TSRMLS_CC)
75 static inline char *_http_etag_finish(void *ctx TSRMLS_DC
)
77 unsigned char digest
[128] = {0};
78 char *etag
= NULL
, *mode
= HTTP_G
->etag
.mode
;
80 #if HTTP_HAVE_EXT_HASH
81 php_hash_ops
*eho
= NULL
;
83 if (mode
&& (eho
= php_hash_fetch_ops(mode
, strlen(mode
)))) {
84 eho
->hash_final(digest
, ctx
);
85 etag
= http_etag_digest(digest
, eho
->digest_size
);
88 if (mode
&& ((!strcasecmp(mode
, "crc32")) || (!strcasecmp(mode
, "crc32b")))) {
89 *((uint
*) ctx
) = ~*((uint
*) ctx
);
90 etag
= http_etag_digest((const unsigned char *) ctx
, sizeof(uint
));
91 } else if (mode
&& (!strcasecmp(mode
, "sha1"))) {
92 PHP_SHA1Final(digest
, ctx
);
93 etag
= http_etag_digest(digest
, 20);
95 PHP_MD5Final(digest
, ctx
);
96 etag
= http_etag_digest(digest
, 16);
103 #define http_etag_update(c, d, l) _http_etag_update((c), (d), (l) TSRMLS_CC)
104 static inline void _http_etag_update(void *ctx
, const char *data_ptr
, size_t data_len TSRMLS_DC
)
106 char *mode
= HTTP_G
->etag
.mode
;
107 #if HTTP_HAVE_EXT_HASH
108 php_hash_ops
*eho
= NULL
;
110 if (mode
&& (eho
= php_hash_fetch_ops(mode
, strlen(mode
)))) {
111 eho
->hash_update(ctx
, (const unsigned char *) data_ptr
, data_len
);
114 if (mode
&& ((!strcasecmp(mode
, "crc32")) || (!strcasecmp(mode
, "crc32b")))) {
115 uint i
, c
= *((uint
*) ctx
);
116 for (i
= 0; i
< data_len
; ++i
) {
117 CRC32(c
, data_ptr
[i
]);
120 } else if (mode
&& (!strcasecmp(mode
, "sha1"))) {
121 PHP_SHA1Update(ctx
, (const unsigned char *) data_ptr
, data_len
);
123 PHP_MD5Update(ctx
, (const unsigned char *) data_ptr
, data_len
);
127 #define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC)
128 extern void _http_ob_etaghandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
);
130 #define http_etag(p, l, m) _http_etag((p), (l), (m) TSRMLS_CC)
131 PHP_HTTP_API
char *_http_etag(const void *data_ptr
, size_t data_len
, http_send_mode data_mode TSRMLS_DC
);
133 #define http_last_modified(p, m) _http_last_modified((p), (m) TSRMLS_CC)
134 PHP_HTTP_API
time_t _http_last_modified(const void *data_ptr
, http_send_mode data_mode TSRMLS_DC
);
136 #define http_match_last_modified(entry, modified) _http_match_last_modified_ex((entry), (modified), 1 TSRMLS_CC)
137 #define http_match_last_modified_ex(entry, modified, ep) _http_match_last_modified_ex((entry), (modified), (ep) TSRMLS_CC)
138 PHP_HTTP_API zend_bool
_http_match_last_modified_ex(const char *entry
, time_t t
, zend_bool enforce_presence TSRMLS_DC
);
140 #define http_match_etag(entry, etag) _http_match_etag_ex((entry), (etag), 1 TSRMLS_CC)
141 #define http_match_etag_ex(entry, etag, ep) _http_match_etag_ex((entry), (etag), (ep) TSRMLS_CC)
142 PHP_HTTP_API zend_bool
_http_match_etag_ex(const char *entry
, const char *etag
, zend_bool enforce_presence TSRMLS_DC
);
144 #define http_cache_last_modified(l, s, cc, ccl) _http_cache_last_modified((l), (s), (cc), (ccl) TSRMLS_CC)
145 PHP_HTTP_API STATUS
_http_cache_last_modified(time_t last_modified
, time_t send_modified
, const char *cache_control
, size_t cc_len TSRMLS_DC
);
147 #define http_cache_etag(e, el, cc, ccl) _http_cache_etag((e), (el), (cc), (ccl) TSRMLS_CC)
148 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, size_t etag_len
, const char *cache_control
, size_t cc_len TSRMLS_DC
);
150 #define http_start_ob_etaghandler() _http_start_ob_etaghandler(TSRMLS_C)
151 PHP_HTTP_API STATUS
_http_start_ob_etaghandler(TSRMLS_D
);
152 #define http_interrupt_ob_etaghandler() _http_interrupt_ob_etaghandler(TSRMLS_C)
153 PHP_HTTP_API zend_bool
_http_interrupt_ob_etaghandler(TSRMLS_D
);
162 * vim600: noet sw=4 ts=4 fdm=marker
163 * vim<600: noet sw=4 ts=4