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-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #ifndef PHP_HTTP_CACHE_API_H
16 #define PHP_HTTP_CACHE_API_H
20 #include "ext/standard/md5.h"
21 #include "ext/standard/sha1.h"
22 #include "ext/standard/crc32.h"
24 #include "php_http_std_defs.h"
26 #include "php_http_api.h"
27 #include "php_http_send_api.h"
29 #if defined(HAVE_HASH_EXT) && !defined(COMPILE_DL_HASH) && defined(HTTP_HAVE_HASH_EXT_INCLUDES)
30 # define HTTP_HAVE_HASH_EXT
31 # include "php_hash.h"
32 # include "php_hash_sha.h"
33 # include "php_hash_ripemd.h"
36 #ifdef HTTP_HAVE_MHASH
40 ZEND_EXTERN_MODULE_GLOBALS(http
);
43 #ifdef HTTP_HAVE_HASH_EXT
44 HTTP_ETAG_RIPEMD160
= -8,
45 HTTP_ETAG_RIPEMD128
= -7,
46 HTTP_ETAG_SHA512
= -6,
47 HTTP_ETAG_SHA384
= -5,
48 HTTP_ETAG_SHA256
= -4,
55 extern PHP_MINIT_FUNCTION(http_cache
);
57 #ifdef HTTP_HAVE_MHASH
58 static void *http_etag_alloc_mhash_digest(size_t size
)
64 #define http_etag_digest(d, l) _http_etag_digest((d), (l) TSRMLS_CC)
65 static inline char *_http_etag_digest(const unsigned char *digest
, int len TSRMLS_DC
)
67 static const char hexdigits
[16] = "0123456789abcdef";
69 char *hex
= emalloc(len
* 2 + 1);
72 for (i
= 0; i
< len
; ++i
) {
73 *ptr
++ = hexdigits
[digest
[i
] >> 4];
74 *ptr
++ = hexdigits
[digest
[i
] & 0xF];
81 #undef CASE_HTTP_ETAG_HASH
82 #define CASE_HTTP_ETAG_HASH(HASH) \
83 case HTTP_ETAG_##HASH: \
84 PHP_##HASH##Init(ctx = emalloc(sizeof(PHP_##HASH##_CTX))); \
86 #define http_etag_init() _http_etag_init(TSRMLS_C)
87 static inline void *_http_etag_init(TSRMLS_D
)
90 long mode
= HTTP_G(etag
).mode
;
95 ctx
= emalloc(sizeof(uint
));
99 #ifdef HTTP_HAVE_HASH_EXT
100 CASE_HTTP_ETAG_HASH(RIPEMD160
);
101 CASE_HTTP_ETAG_HASH(RIPEMD128
);
102 CASE_HTTP_ETAG_HASH(SHA512
);
103 CASE_HTTP_ETAG_HASH(SHA384
);
104 CASE_HTTP_ETAG_HASH(SHA256
);
106 CASE_HTTP_ETAG_HASH(SHA1
);
107 #ifndef HTTP_HAVE_MHASH
110 CASE_HTTP_ETAG_HASH(MD5
);
112 #ifdef HTTP_HAVE_MHASH
114 if ((mode
< 0) || ((ulong
)mode
> mhash_count()) || (!(ctx
= mhash_init(mode
)))) {
115 http_error_ex(HE_ERROR
, HTTP_E_RUNTIME
, "Invalid ETag mode: %ld", mode
);
124 #undef CASE_HTTP_ETAG_HASH
125 #define CASE_HTTP_ETAG_HASH(HASH) \
126 case HTTP_ETAG_##HASH: \
127 if (*((PHP_##HASH##_CTX **) ctx_ptr)) { \
128 efree(*((PHP_##HASH##_CTX **) ctx_ptr)); \
129 *((PHP_##HASH##_CTX **) ctx_ptr) = NULL; \
132 #define http_etag_free(cp) _http_etag_free((cp) TSRMLS_CC)
133 static inline void _http_etag_free(void **ctx_ptr TSRMLS_DC
)
135 switch (HTTP_G(etag
).mode
)
137 case HTTP_ETAG_CRC32
:
138 if (*((uint
**) ctx_ptr
)) {
139 efree(*((uint
**) ctx_ptr
));
140 *((uint
**) ctx_ptr
) = NULL
;
144 #ifdef HTTP_HAVE_HASH_EXT
145 CASE_HTTP_ETAG_HASH(RIPEMD160
);
146 CASE_HTTP_ETAG_HASH(RIPEMD128
);
147 CASE_HTTP_ETAG_HASH(SHA512
);
148 CASE_HTTP_ETAG_HASH(SHA384
);
149 CASE_HTTP_ETAG_HASH(SHA256
);
151 CASE_HTTP_ETAG_HASH(SHA1
);
152 #ifndef HTTP_HAVE_MHASH
155 CASE_HTTP_ETAG_HASH(MD5
);
157 #ifdef HTTP_HAVE_MHASH
159 /* mhash gets already freed in http_etag_finish() */
160 if (*((MHASH
*) ctx_ptr
)) {
161 mhash_deinit(*((MHASH
*) ctx_ptr
), NULL
);
162 *((MHASH
*) ctx_ptr
) = NULL
;
169 #undef CASE_HTTP_ETAG_HASH
170 #define CASE_HTTP_ETAG_HASH(HASH, len) \
171 case HTTP_ETAG_##HASH##: \
172 PHP_##HASH##Final(digest, *((PHP_##HASH##_CTX **) ctx_ptr)); \
173 etag = http_etag_digest(digest, len); \
175 #define http_etag_finish(c) _http_etag_finish((c) TSRMLS_CC)
176 static inline char *_http_etag_finish(void **ctx_ptr TSRMLS_DC
)
179 unsigned char digest
[128];
180 long mode
= HTTP_G(etag
).mode
;
184 case HTTP_ETAG_CRC32
:
185 **((uint
**) ctx_ptr
) = ~**((uint
**) ctx_ptr
);
186 etag
= http_etag_digest(*((const unsigned char **) ctx_ptr
), sizeof(uint
));
189 #ifdef HTTP_HAVE_HASH_EXT
190 CASE_HTTP_ETAG_HASH(RIPEMD160
, 20);
191 CASE_HTTP_ETAG_HASH(RIPEMD128
, 16);
192 CASE_HTTP_ETAG_HASH(SHA512
, 64);
193 CASE_HTTP_ETAG_HASH(SHA384
, 48);
194 CASE_HTTP_ETAG_HASH(SHA256
, 32);
196 CASE_HTTP_ETAG_HASH(SHA1
, 20);
197 #ifndef HTTP_HAVE_MHASH
200 CASE_HTTP_ETAG_HASH(MD5
, 16);
202 #ifdef HTTP_HAVE_MHASH
205 unsigned char *mhash_digest
= mhash_end_m(*((MHASH
*) ctx_ptr
), http_etag_alloc_mhash_digest
);
206 etag
= http_etag_digest(mhash_digest
, mhash_get_block_size(mode
));
208 /* avoid double free */
209 *((MHASH
*) ctx_ptr
) = NULL
;
215 http_etag_free(ctx_ptr
);
220 #undef CASE_HTTP_ETAG_HASH
221 #define CASE_HTTP_ETAG_HASH(HASH) \
222 case HTTP_ETAG_##HASH: \
223 PHP_##HASH##Update(ctx, (const unsigned char *) data_ptr, data_len); \
225 #define http_etag_update(c, d, l) _http_etag_update((c), (d), (l) TSRMLS_CC)
226 static inline void _http_etag_update(void *ctx
, const char *data_ptr
, size_t data_len TSRMLS_DC
)
228 switch (HTTP_G(etag
).mode
)
230 case HTTP_ETAG_CRC32
:
232 uint i
, c
= *((uint
*) ctx
);
234 for (i
= 0; i
< data_len
; ++i
) {
235 c
= CRC32(c
, data_ptr
[i
]);
241 #ifdef HTTP_HAVE_HASH_EXT
242 CASE_HTTP_ETAG_HASH(RIPEMD160
);
243 CASE_HTTP_ETAG_HASH(RIPEMD128
);
244 CASE_HTTP_ETAG_HASH(SHA512
);
245 CASE_HTTP_ETAG_HASH(SHA384
);
246 CASE_HTTP_ETAG_HASH(SHA256
);
248 CASE_HTTP_ETAG_HASH(SHA1
);
249 #ifndef HTTP_HAVE_MHASH
252 CASE_HTTP_ETAG_HASH(MD5
);
254 #ifdef HTTP_HAVE_MHASH
256 mhash(ctx
, data_ptr
, data_len
);
262 #define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC)
263 extern void _http_ob_etaghandler(char *output
, uint output_len
, char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
);
265 #define http_etag(p, l, m) _http_etag((p), (l), (m) TSRMLS_CC)
266 PHP_HTTP_API
char *_http_etag(const void *data_ptr
, size_t data_len
, http_send_mode data_mode TSRMLS_DC
);
268 #define http_last_modified(p, m) _http_last_modified((p), (m) TSRMLS_CC)
269 PHP_HTTP_API
time_t _http_last_modified(const void *data_ptr
, http_send_mode data_mode TSRMLS_DC
);
271 #define http_match_last_modified(entry, modified) _http_match_last_modified_ex((entry), (modified), 1 TSRMLS_CC)
272 #define http_match_last_modified_ex(entry, modified, ep) _http_match_last_modified_ex((entry), (modified), (ep) TSRMLS_CC)
273 PHP_HTTP_API zend_bool
_http_match_last_modified_ex(const char *entry
, time_t t
, zend_bool enforce_presence TSRMLS_DC
);
275 #define http_match_etag(entry, etag) _http_match_etag_ex((entry), (etag), 1 TSRMLS_CC)
276 #define http_match_etag_ex(entry, etag, ep) _http_match_etag_ex((entry), (etag), (ep) TSRMLS_CC)
277 PHP_HTTP_API zend_bool
_http_match_etag_ex(const char *entry
, const char *etag
, zend_bool enforce_presence TSRMLS_DC
);
279 #define http_cache_last_modified(l, s, cc, ccl) _http_cache_last_modified((l), (s), (cc), (ccl) TSRMLS_CC)
280 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
);
282 #define http_cache_etag(e, el, cc, ccl) _http_cache_etag((e), (el), (cc), (ccl) TSRMLS_CC)
283 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, size_t etag_len
, const char *cache_control
, size_t cc_len TSRMLS_DC
);
285 #define http_start_ob_etaghandler() _http_start_ob_etaghandler(TSRMLS_C)
286 PHP_HTTP_API STATUS
_http_start_ob_etaghandler(TSRMLS_D
);
287 #define http_interrupt_ob_etaghandler() _http_interrupt_ob_etaghandler(TSRMLS_C)
288 PHP_HTTP_API zend_bool
_http_interrupt_ob_etaghandler(TSRMLS_D
);
297 * vim600: noet sw=4 ts=4 fdm=marker
298 * vim<600: noet sw=4 ts=4