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