963bb7e1d0c1aecac81b10047662715b9407006c
[m6w6/ext-http] / http_cache_api.c
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 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18 #include "php.h"
19
20 #include "SAPI.h"
21 #include "php_streams.h"
22 #include "php_output.h"
23 #include "ext/standard/md5.h"
24 #include "ext/standard/sha1.h"
25
26 #include "php_http.h"
27 #include "php_http_std_defs.h"
28 #include "php_http_api.h"
29 #include "php_http_cache_api.h"
30 #include "php_http_send_api.h"
31 #include "php_http_date_api.h"
32
33 #ifdef HTTP_HAVE_MHASH
34 # include <mhash.h>
35 #endif
36
37 ZEND_EXTERN_MODULE_GLOBALS(http);
38
39 PHP_MINIT_FUNCTION(http_cache)
40 {
41 HTTP_LONG_CONSTANT("HTTP_ETAG_MD5", HTTP_ETAG_MD5);
42 HTTP_LONG_CONSTANT("HTTP_ETAG_SHA1", HTTP_ETAG_SHA1);
43 HTTP_LONG_CONSTANT("HTTP_ETAG_CRC32", HTTP_ETAG_CRC32);
44
45 #ifdef HTTP_HAVE_HASH_EXT
46 HTTP_LONG_CONSTANT("HTTP_ETAG_SHA256", HTTP_ETAG_SHA256);
47 HTTP_LONG_CONSTANT("HTTP_ETAG_SHA384", HTTP_ETAG_SHA384);
48 HTTP_LONG_CONSTANT("HTTP_ETAG_SHA512", HTTP_ETAG_SHA512);
49 HTTP_LONG_CONSTANT("HTTP_ETAG_RIPEMD128", HTTP_ETAG_RIPEMD128);
50 HTTP_LONG_CONSTANT("HTTP_ETAG_RIPEMD160", HTTP_ETAG_RIPEMD160);
51 #endif
52
53 #ifdef HTTP_HAVE_MHASH
54 {
55 int l, i, c = mhash_count();
56
57 for (i = 0; i <= c; ++i) {
58 char const_name[256] = {0};
59 const char *hash_name = mhash_get_hash_name_static(i);
60
61 if (hash_name) {
62 l = snprintf(const_name, 255, "HTTP_ETAG_MHASH_%s", hash_name);
63 zend_register_long_constant(const_name, l + 1, i, CONST_CS|CONST_PERSISTENT, module_number TSRMLS_CC);
64 }
65 }
66 }
67 #endif
68
69 return SUCCESS;
70 }
71
72 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
73 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC)
74 {
75 void *ctx = http_etag_init();
76
77 if (data_mode == SEND_DATA) {
78 http_etag_update(ctx, data_ptr, data_len);
79 } else {
80 STATUS ss = FAILURE;
81 php_stream_statbuf ssb;
82
83 if (data_mode == SEND_RSRC) {
84 ss = php_stream_stat((php_stream *) data_ptr, &ssb);
85 } else {
86 ss = php_stream_stat_path((char *) data_ptr, &ssb);
87 }
88
89 if (SUCCESS != ss) {
90 http_etag_free(&ctx);
91 return NULL;
92 } else {
93 size_t ssb_len;
94 char ssb_buf[128] = {0};
95
96 ssb_len = snprintf(ssb_buf, 127, "%ld=%ld=%ld", (long) ssb.sb.st_mtime,
97 (long) ssb.sb.st_ino,
98 (long) ssb.sb.st_size);
99 http_etag_update(ctx, ssb_buf, ssb_len);
100 }
101 }
102
103 return http_etag_finish(&ctx);
104 }
105 /* }}} */
106
107 /* {{{ time_t http_last_modified(void *, http_send_mode) */
108 PHP_HTTP_API time_t _http_last_modified(const void *data_ptr, http_send_mode data_mode TSRMLS_DC)
109 {
110 php_stream_statbuf ssb;
111
112 switch (data_mode)
113 {
114 case SEND_DATA: return time(NULL);
115 case SEND_RSRC: return php_stream_stat((php_stream *) data_ptr, &ssb) ? 0 : ssb.sb.st_mtime;
116 default: return php_stream_stat_path((char *) data_ptr, &ssb) ? 0 : ssb.sb.st_mtime;
117 }
118 }
119 /* }}} */
120
121 /* {{{ zend_bool http_match_last_modified(char *, time_t) */
122 PHP_HTTP_API zend_bool _http_match_last_modified_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC)
123 {
124 zend_bool retval;
125 zval *zmodified;
126 char *modified, *chr_ptr;
127
128 HTTP_GSC(zmodified, entry, !enforce_presence);
129
130 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
131 if ((chr_ptr = strrchr(modified, ';'))) {
132 chr_ptr = 0;
133 }
134 retval = (t <= http_parse_date(modified));
135 efree(modified);
136 return retval;
137 }
138 /* }}} */
139
140 /* {{{ zend_bool http_match_etag(char *, char *) */
141 PHP_HTTP_API zend_bool _http_match_etag_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC)
142 {
143 zval *zetag;
144 char *quoted_etag;
145 zend_bool result;
146
147 HTTP_GSC(zetag, entry, !enforce_presence);
148
149 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
150 return 1;
151 }
152
153 spprintf(&quoted_etag, 0, "\"%s\"", etag);
154 if (!strchr(Z_STRVAL_P(zetag), ',')) {
155 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
156 } else {
157 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
158 }
159 efree(quoted_etag);
160
161 return result;
162 }
163 /* }}} */
164
165 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
166 PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified,
167 time_t send_modified, const char *cache_control, size_t cc_len TSRMLS_DC)
168 {
169 char *sent_header = NULL;
170
171 if (SG(headers_sent)) {
172 return FAILURE;
173 }
174
175 if (cc_len && (SUCCESS != http_send_cache_control(cache_control, cc_len))) {
176 return FAILURE;
177 }
178
179 if (SUCCESS != http_send_last_modified_ex(send_modified, &sent_header)) {
180 return FAILURE;
181 }
182
183 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", last_modified)) {
184 http_exit_ex(304, sent_header, NULL, 0);
185 } else {
186 STR_FREE(sent_header);
187 }
188
189 return SUCCESS;
190 }
191 /* }}} */
192
193 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
194 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len,
195 const char *cache_control, size_t cc_len TSRMLS_DC)
196 {
197 char *sent_header = NULL;
198
199 if (SG(headers_sent)) {
200 return FAILURE;
201 }
202
203 if (cc_len && (SUCCESS != http_send_cache_control(cache_control, cc_len))) {
204 return FAILURE;
205 }
206
207 if (etag_len) {
208 if (SUCCESS != http_send_etag_ex(etag, etag_len, &sent_header)) {
209 return FAILURE;
210 }
211 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
212 http_exit_ex(304, sent_header, NULL, 0);
213 } else {
214 STR_FREE(sent_header);
215 }
216 return SUCCESS;
217 }
218
219 /* start ob_etaghandler */
220 return http_start_ob_etaghandler();
221 }
222 /* }}} */
223
224 PHP_HTTP_API STATUS _http_start_ob_etaghandler(TSRMLS_D)
225 {
226 /* already running? */
227 if (php_ob_handler_used("ob_etaghandler" TSRMLS_CC)) {
228 http_error(HE_WARNING, HTTP_E_RUNTIME, "ob_etaghandler can only be used once");
229 return FAILURE;
230 }
231
232 HTTP_G(etag).started = 1;
233 return php_start_ob_buffer_named("ob_etaghandler", HTTP_G(send).buffer_size, 1 TSRMLS_CC);
234 }
235
236 PHP_HTTP_API zend_bool _http_interrupt_ob_etaghandler(TSRMLS_D)
237 {
238 if (HTTP_G(etag).started) {
239 HTTP_G(etag).started = 0;
240 http_etag_free(&HTTP_G(etag).ctx);
241 return 1;
242 }
243 return 0;
244 }
245
246 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
247 void _http_ob_etaghandler(char *output, uint output_len,
248 char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
249 {
250 /* passthru */
251 *handled_output_len = output_len;
252 *handled_output = estrndup(output, output_len);
253
254 /* are we supposed to run? */
255 if (HTTP_G(etag).started) {
256 /* initialize the etag context */
257 if (mode & PHP_OUTPUT_HANDLER_START) {
258 HTTP_G(etag).ctx = http_etag_init();
259 }
260
261 /* update */
262 http_etag_update(HTTP_G(etag).ctx, output, output_len);
263
264 /* finish */
265 if (mode & PHP_OUTPUT_HANDLER_END) {
266 char *sent_header = NULL;
267 char *etag = http_etag_finish(&HTTP_G(etag).ctx);
268
269 http_send_cache_control(HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL));
270 http_send_etag_ex(etag, strlen(etag), &sent_header);
271
272 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
273 http_exit_ex(304, sent_header, etag, 0);
274 }
275
276 STR_FREE(sent_header);
277 STR_FREE(etag);
278 }
279 }
280 }
281 /* }}} */
282
283 /*
284 * Local variables:
285 * tab-width: 4
286 * c-basic-offset: 4
287 * End:
288 * vim600: sw=4 ts=4 fdm=marker
289 * vim<600: sw=4 ts=4
290 */
291