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