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