- fix HTTP_ETAG_CRC32
[m6w6/ext-http] / php_http_cache_api.h
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifndef PHP_HTTP_CACHE_API_H
19 #define PHP_HTTP_CACHE_API_H
20
21 #include "zend_ini.h"
22
23 #include "ext/standard/md5.h"
24 #include "ext/standard/sha1.h"
25 #include "ext/standard/crc32.h"
26
27 #include "php_http_std_defs.h"
28 #include "php_http.h"
29 #include "php_http_api.h"
30 #include "php_http_send_api.h"
31
32 #ifdef HTTP_HAVE_MHASH
33 # include <mhash.h>
34 #endif
35
36 ZEND_EXTERN_MODULE_GLOBALS(http);
37
38 #define http_cache_global_init() _http_cache_global_init(INIT_FUNC_ARGS_PASSTHRU)
39 extern STATUS _http_cache_global_init(INIT_FUNC_ARGS);
40
41 typedef enum {
42 HTTP_ETAG_CRC32 = -3,
43 HTTP_ETAG_MD5 = -2,
44 HTTP_ETAG_SHA1 = -1,
45 } http_etag_mode;
46
47 #ifdef HTTP_HAVE_MHASH
48 static void *http_etag_alloc_mhash_digest(size_t size)
49 {
50 return emalloc(size);
51 }
52 #endif
53
54 #define http_etag_digest(d, l) _http_etag_digest((d), (l) TSRMLS_CC)
55 static inline char *_http_etag_digest(const unsigned char *digest, int len TSRMLS_DC)
56 {
57 int i;
58 char *hex = emalloc(len * 2 + 1);
59 char *ptr = hex;
60
61 /* optimize this --
62 look at apache's make_etag */
63 for (i = 0; i < len; ++i) {
64 sprintf(ptr, "%02x", digest[i]);
65 ptr += 2;
66 }
67 *ptr = '\0';
68
69 return hex;
70 }
71
72 #define http_etag_init() _http_etag_init(TSRMLS_C)
73 static inline void *_http_etag_init(TSRMLS_D)
74 {
75 void *ctx = NULL;
76 long mode = HTTP_G(etag).mode;
77
78 switch (mode)
79 {
80 case HTTP_ETAG_CRC32:
81 ctx = emalloc(sizeof(unsigned int));
82 *((unsigned int *) ctx) = ~0;
83 break;
84
85 case HTTP_ETAG_SHA1:
86 PHP_SHA1Init(ctx = emalloc(sizeof(PHP_SHA1_CTX)));
87 break;
88
89 case HTTP_ETAG_MD5:
90 #ifndef HTTP_HAVE_MHASH
91 default:
92 #endif
93 PHP_MD5Init(ctx = emalloc(sizeof(PHP_MD5_CTX)));
94 break;
95
96 #ifdef HTTP_HAVE_MHASH
97 default:
98 if ((mode < 0) || ((ulong)mode > mhash_count()) || (!(ctx = mhash_init(mode)))) {
99 http_error_ex(HE_ERROR, HTTP_E_RUNTIME, "Invalid ETag mode: %ld", mode);
100 }
101 break;
102 #endif
103 }
104
105 return ctx;
106 }
107
108 #define http_etag_free(cp) _http_etag_free((cp) TSRMLS_CC)
109 static inline void _http_etag_free(void **ctx_ptr TSRMLS_DC)
110 {
111 long mode = HTTP_G(etag).mode;
112
113 switch (mode)
114 {
115 case HTTP_ETAG_CRC32:
116 if (*((unsigned int **) ctx_ptr)) {
117 efree(*((unsigned int **) ctx_ptr));
118 *((unsigned int **) ctx_ptr) = NULL;
119 }
120 break;
121
122 case HTTP_ETAG_SHA1:
123 if (*((PHP_SHA1_CTX **) ctx_ptr)) {
124 efree(*((PHP_SHA1_CTX **) ctx_ptr));
125 *((PHP_SHA1_CTX **) ctx_ptr) = NULL;
126 }
127 break;
128
129 case HTTP_ETAG_MD5:
130 #ifndef HTTP_HAVE_MHASH
131 default:
132 #endif
133 if (*((PHP_MD5_CTX **) ctx_ptr)) {
134 efree(*((PHP_MD5_CTX **) ctx_ptr));
135 *((PHP_MD5_CTX **) ctx_ptr) = NULL;
136 }
137 break;
138
139 #ifdef HTTP_HAVE_MHASH
140 default:
141 /* mhash gets already freed in http_etag_finish() */
142 if (*((MHASH *) ctx_ptr)) {
143 mhash_deinit(*((MHASH *) ctx_ptr), NULL);
144 *((MHASH *) ctx_ptr) = NULL;
145 }
146 break;
147 #endif
148 }
149 }
150
151 #define http_etag_finish(c) _http_etag_finish((c) TSRMLS_CC)
152 static inline char *_http_etag_finish(void **ctx_ptr TSRMLS_DC)
153 {
154 char *etag = NULL;
155 unsigned char digest[20];
156 long mode = HTTP_G(etag).mode;
157
158 switch (mode)
159 {
160 case HTTP_ETAG_CRC32:
161 **((unsigned int **) ctx_ptr) = ~**((unsigned int **) ctx_ptr);
162 etag = http_etag_digest(*((const unsigned char **) ctx_ptr), sizeof(unsigned int));
163 break;
164
165 case HTTP_ETAG_SHA1:
166 PHP_SHA1Final(digest, *((PHP_SHA1_CTX **) ctx_ptr));
167 etag = http_etag_digest(digest, 20);
168 break;
169
170 case HTTP_ETAG_MD5:
171 #ifndef HTTP_HAVE_MHASH
172 default:
173 #endif
174 PHP_MD5Final(digest, *((PHP_MD5_CTX **) ctx_ptr));
175 etag = http_etag_digest(digest, 16);
176 break;
177
178 #ifdef HTTP_HAVE_MHASH
179 default:
180 {
181 unsigned char *mhash_digest = mhash_end_m(*((MHASH *) ctx_ptr), http_etag_alloc_mhash_digest);
182 etag = http_etag_digest(mhash_digest, mhash_get_block_size(mode));
183 efree(mhash_digest);
184 /* avoid double free */
185 *((MHASH *) ctx_ptr) = NULL;
186 }
187 break;
188 #endif
189 }
190
191 http_etag_free(ctx_ptr);
192
193 return etag;
194 }
195
196 #define http_etag_update(c, d, l) _http_etag_update((c), (d), (l) TSRMLS_CC)
197 static inline void _http_etag_update(void *ctx, const char *data_ptr, size_t data_len TSRMLS_DC)
198 {
199 switch (INI_INT("http.etag_mode"))
200 {
201 case HTTP_ETAG_CRC32:
202 {
203 unsigned int i, c = *((unsigned int *) ctx);
204
205 for (i = 0; i < data_len; ++i) {
206 c = CRC32(c, data_ptr[i]);
207 }
208 *((unsigned int *)ctx) = c;
209 }
210 break;
211
212 case HTTP_ETAG_SHA1:
213 PHP_SHA1Update(ctx, (const unsigned char *) data_ptr, data_len);
214 break;
215
216 case HTTP_ETAG_MD5:
217 #ifndef HTTP_HAVE_MHASH
218 default:
219 #endif
220 PHP_MD5Update(ctx, (const unsigned char *) data_ptr, data_len);
221 break;
222
223 #ifdef HTTP_HAVE_MHASH
224 default:
225 mhash(ctx, data_ptr, data_len);
226 break;
227 #endif
228 }
229 }
230
231 #define http_ob_etaghandler(o, l, ho, hl, m) _http_ob_etaghandler((o), (l), (ho), (hl), (m) TSRMLS_CC)
232 extern void _http_ob_etaghandler(char *output, uint output_len, char **handled_output, uint *handled_output_len, int mode TSRMLS_DC);
233
234 #define http_etag(p, l, m) _http_etag((p), (l), (m) TSRMLS_CC)
235 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC);
236
237 #define http_last_modified(p, m) _http_last_modified((p), (m) TSRMLS_CC)
238 PHP_HTTP_API time_t _http_last_modified(const void *data_ptr, http_send_mode data_mode TSRMLS_DC);
239
240 #define http_match_last_modified(entry, modified) _http_match_last_modified_ex((entry), (modified), 1 TSRMLS_CC)
241 #define http_match_last_modified_ex(entry, modified, ep) _http_match_last_modified_ex((entry), (modified), (ep) TSRMLS_CC)
242 PHP_HTTP_API zend_bool _http_match_last_modified_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC);
243
244 #define http_match_etag(entry, etag) _http_match_etag_ex((entry), (etag), 1 TSRMLS_CC)
245 #define http_match_etag_ex(entry, etag, ep) _http_match_etag_ex((entry), (etag), (ep) TSRMLS_CC)
246 PHP_HTTP_API zend_bool _http_match_etag_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC);
247
248 #define http_cache_last_modified(l, s, cc, ccl) _http_cache_last_modified((l), (s), (cc), (ccl) TSRMLS_CC)
249 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);
250
251 #define http_cache_etag(e, el, cc, ccl) _http_cache_etag((e), (el), (cc), (ccl) TSRMLS_CC)
252 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len, const char *cache_control, size_t cc_len TSRMLS_DC);
253
254 #define http_start_ob_etaghandler() _http_start_ob_etaghandler(TSRMLS_C)
255 PHP_HTTP_API STATUS _http_start_ob_etaghandler(TSRMLS_D);
256 #define http_interrupt_ob_etaghandler() _http_interrupt_ob_etaghandler(TSRMLS_C)
257 PHP_HTTP_API zend_bool _http_interrupt_ob_etaghandler(TSRMLS_D);
258
259 #endif
260
261 /*
262 * Local variables:
263 * tab-width: 4
264 * c-basic-offset: 4
265 * End:
266 * vim600: noet sw=4 ts=4 fdm=marker
267 * vim<600: noet sw=4 ts=4
268 */
269