- add preliminary ext/hash support (currently only for Win32)
[m6w6/ext-http] / php_http_cache_api.h
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifndef PHP_HTTP_CACHE_API_H
16 #define PHP_HTTP_CACHE_API_H
17
18 #include "zend_ini.h"
19
20 #include "ext/standard/md5.h"
21 #include "ext/standard/sha1.h"
22 #include "ext/standard/crc32.h"
23
24 #include "php_http_std_defs.h"
25 #include "php_http.h"
26 #include "php_http_api.h"
27 #include "php_http_send_api.h"
28
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"
34 #endif
35
36 #ifdef HTTP_HAVE_MHASH
37 # include <mhash.h>
38 #endif
39
40 ZEND_EXTERN_MODULE_GLOBALS(http);
41
42 typedef enum {
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,
49 #endif
50 HTTP_ETAG_CRC32 = -3,
51 HTTP_ETAG_MD5 = -2,
52 HTTP_ETAG_SHA1 = -1,
53 } http_etag_mode;
54
55 extern PHP_MINIT_FUNCTION(http_cache);
56
57 #ifdef HTTP_HAVE_MHASH
58 static void *http_etag_alloc_mhash_digest(size_t size)
59 {
60 return emalloc(size);
61 }
62 #endif
63
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)
66 {
67 static const char hexdigits[16] = "0123456789abcdef";
68 int i;
69 char *hex = emalloc(len * 2 + 1);
70 char *ptr = hex;
71
72 for (i = 0; i < len; ++i) {
73 *ptr++ = hexdigits[digest[i] >> 4];
74 *ptr++ = hexdigits[digest[i] & 0xF];
75 }
76 *ptr = '\0';
77
78 return hex;
79 }
80
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))); \
85 break;
86 #define http_etag_init() _http_etag_init(TSRMLS_C)
87 static inline void *_http_etag_init(TSRMLS_D)
88 {
89 void *ctx = NULL;
90 long mode = HTTP_G(etag).mode;
91
92 switch (mode)
93 {
94 case HTTP_ETAG_CRC32:
95 ctx = emalloc(sizeof(uint));
96 *((uint *) ctx) = ~0;
97 break;
98
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);
105 #endif
106 CASE_HTTP_ETAG_HASH(SHA1);
107 #ifndef HTTP_HAVE_MHASH
108 default:
109 #endif
110 CASE_HTTP_ETAG_HASH(MD5);
111
112 #ifdef HTTP_HAVE_MHASH
113 default:
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);
116 }
117 break;
118 #endif
119 }
120
121 return ctx;
122 }
123
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; \
130 } \
131 break;
132 #define http_etag_free(cp) _http_etag_free((cp) TSRMLS_CC)
133 static inline void _http_etag_free(void **ctx_ptr TSRMLS_DC)
134 {
135 switch (HTTP_G(etag).mode)
136 {
137 case HTTP_ETAG_CRC32:
138 if (*((uint **) ctx_ptr)) {
139 efree(*((uint **) ctx_ptr));
140 *((uint **) ctx_ptr) = NULL;
141 }
142 break;
143
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);
150 #endif
151 CASE_HTTP_ETAG_HASH(SHA1);
152 #ifndef HTTP_HAVE_MHASH
153 default:
154 #endif
155 CASE_HTTP_ETAG_HASH(MD5);
156
157 #ifdef HTTP_HAVE_MHASH
158 default:
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;
163 }
164 break;
165 #endif
166 }
167 }
168
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); \
174 break;
175 #define http_etag_finish(c) _http_etag_finish((c) TSRMLS_CC)
176 static inline char *_http_etag_finish(void **ctx_ptr TSRMLS_DC)
177 {
178 char *etag = NULL;
179 unsigned char digest[128];
180 long mode = HTTP_G(etag).mode;
181
182 switch (mode)
183 {
184 case HTTP_ETAG_CRC32:
185 **((uint **) ctx_ptr) = ~**((uint **) ctx_ptr);
186 etag = http_etag_digest(*((const unsigned char **) ctx_ptr), sizeof(uint));
187 break;
188
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);
195 #endif
196 CASE_HTTP_ETAG_HASH(SHA1, 20);
197 #ifndef HTTP_HAVE_MHASH
198 default:
199 #endif
200 CASE_HTTP_ETAG_HASH(MD5, 16);
201
202 #ifdef HTTP_HAVE_MHASH
203 default:
204 {
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));
207 efree(mhash_digest);
208 /* avoid double free */
209 *((MHASH *) ctx_ptr) = NULL;
210 }
211 break;
212 #endif
213 }
214
215 http_etag_free(ctx_ptr);
216
217 return etag;
218 }
219
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); \
224 break;
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)
227 {
228 switch (HTTP_G(etag).mode)
229 {
230 case HTTP_ETAG_CRC32:
231 {
232 uint i, c = *((uint *) ctx);
233
234 for (i = 0; i < data_len; ++i) {
235 c = CRC32(c, data_ptr[i]);
236 }
237 *((uint *)ctx) = c;
238 }
239 break;
240
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);
247 #endif
248 CASE_HTTP_ETAG_HASH(SHA1);
249 #ifndef HTTP_HAVE_MHASH
250 default:
251 #endif
252 CASE_HTTP_ETAG_HASH(MD5);
253
254 #ifdef HTTP_HAVE_MHASH
255 default:
256 mhash(ctx, data_ptr, data_len);
257 break;
258 #endif
259 }
260 }
261
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);
264
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);
267
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);
270
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);
274
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);
278
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);
281
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);
284
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);
289
290 #endif
291
292 /*
293 * Local variables:
294 * tab-width: 4
295 * c-basic-offset: 4
296 * End:
297 * vim600: noet sw=4 ts=4 fdm=marker
298 * vim<600: noet sw=4 ts=4
299 */
300