- changelog
[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 php_stream_statbuf ssb;
71 char ssb_buf[128] = {0};
72 size_t ssb_len;
73 void *ctx = http_etag_init();
74
75 switch (data_mode)
76 {
77 case SEND_DATA:
78 http_etag_update(ctx, data_ptr, data_len);
79 break;
80
81 case SEND_RSRC:
82 {
83 if (php_stream_stat((php_stream *) data_ptr, &ssb)) {
84 efree(ctx);
85 return NULL;
86 }
87 ssb_len = snprintf(ssb_buf, 127, "%ld=%ld=%ld", ssb.sb.st_mtime, ssb.sb.st_ino, ssb.sb.st_size);
88 http_etag_update(ctx, ssb_buf, ssb_len);
89 }
90 break;
91
92 default:
93 {
94 if (php_stream_stat_path((char *) data_ptr, &ssb)) {
95 efree(ctx);
96 return NULL;
97 }
98 ssb_len = snprintf(ssb_buf, 127, "%ld=%ld=%ld", ssb.sb.st_mtime, ssb.sb.st_ino, ssb.sb.st_size);
99 http_etag_update(ctx, ssb_buf, ssb_len);
100 }
101 break;
102 }
103
104 return http_etag_finish(ctx);
105 }
106 /* }}} */
107
108 /* {{{ time_t http_last_modified(void *, http_send_mode) */
109 PHP_HTTP_API time_t _http_last_modified(const void *data_ptr, http_send_mode data_mode TSRMLS_DC)
110 {
111 php_stream_statbuf ssb;
112
113 switch (data_mode)
114 {
115 case SEND_DATA: return time(NULL);
116 case SEND_RSRC: return php_stream_stat((php_stream *) data_ptr, &ssb) ? 0 : ssb.sb.st_mtime;
117 default: return php_stream_stat_path((char *) data_ptr, &ssb) ? 0 : ssb.sb.st_mtime;
118 }
119 }
120 /* }}} */
121
122 /* {{{ zend_bool http_match_last_modified(char *, time_t) */
123 PHP_HTTP_API zend_bool _http_match_last_modified_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC)
124 {
125 zend_bool retval;
126 zval *zmodified;
127 char *modified, *chr_ptr;
128
129 HTTP_GSC(zmodified, entry, !enforce_presence);
130
131 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
132 if (chr_ptr = strrchr(modified, ';')) {
133 chr_ptr = 0;
134 }
135 retval = (t <= http_parse_date(modified));
136 efree(modified);
137 return retval;
138 }
139 /* }}} */
140
141 /* {{{ zend_bool http_match_etag(char *, char *) */
142 PHP_HTTP_API zend_bool _http_match_etag_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC)
143 {
144 zval *zetag;
145 char *quoted_etag;
146 zend_bool result;
147
148 HTTP_GSC(zetag, entry, !enforce_presence);
149
150 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
151 return 1;
152 }
153
154 quoted_etag = (char *) emalloc(strlen(etag) + 3);
155 sprintf(quoted_etag, "\"%s\"", etag);
156
157 if (!strchr(Z_STRVAL_P(zetag), ',')) {
158 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
159 } else {
160 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
161 }
162 efree(quoted_etag);
163 return result;
164 }
165 /* }}} */
166
167 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
168 PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified,
169 time_t send_modified, const char *cache_control, size_t cc_len TSRMLS_DC)
170 {
171 char *sent_header = NULL;
172
173 if (cc_len && (SUCCESS != http_send_cache_control(cache_control, cc_len))) {
174 return FAILURE;
175 }
176
177 if (SUCCESS != http_send_last_modified_ex(send_modified, &sent_header)) {
178 return FAILURE;
179 }
180
181 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", last_modified)) {
182 http_exit_ex(304, sent_header, NULL, 0);
183 } else {
184 STR_FREE(sent_header);
185 }
186
187 return SUCCESS;
188 }
189 /* }}} */
190
191 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
192 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len,
193 const char *cache_control, size_t cc_len TSRMLS_DC)
194 {
195 char *sent_header = NULL;
196
197 if (cc_len && (SUCCESS != http_send_cache_control(cache_control, cc_len))) {
198 return FAILURE;
199 }
200
201 if (etag_len) {
202 if (SUCCESS != http_send_etag_ex(etag, etag_len, &sent_header)) {
203 return FAILURE;
204 }
205 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
206 http_exit_ex(304, sent_header, NULL, 0);
207 } else {
208 STR_FREE(sent_header);
209 }
210 return SUCCESS;
211 }
212
213 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
214 if (HTTP_G(etag).started) {
215 return SUCCESS;
216 }
217
218 if (HTTP_G(etag).started = (SUCCESS == php_start_ob_buffer_named("ob_etaghandler", HTTP_G(send).buffer_size, 1 TSRMLS_CC))) {
219 return SUCCESS;
220 } else {
221 return FAILURE;
222 }
223 }
224 /* }}} */
225
226 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
227 PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len,
228 char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
229 {
230 if (mode & PHP_OUTPUT_HANDLER_START) {
231 if (HTTP_G(etag).started) {
232 http_error(HE_WARNING, HTTP_E_RUNTIME, "ob_etaghandler can only be used once");
233 return;
234 }
235 HTTP_G(etag).started = 1;
236 HTTP_G(etag).ctx = http_etag_init();
237 }
238
239 http_etag_update(HTTP_G(etag).ctx, output, output_len);
240
241 if (mode & PHP_OUTPUT_HANDLER_END) {
242 char *etag = http_etag_finish(HTTP_G(etag).ctx);
243
244 /* just do that if desired */
245 if (HTTP_G(etag).started) {
246 char *sent_header = NULL;
247
248 http_send_cache_control(HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL));
249 http_send_etag_ex(etag, strlen(etag), &sent_header);
250
251 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
252 efree(etag);
253 http_exit_ex(304, sent_header, NULL, 0);
254 } else {
255 STR_FREE(sent_header);
256 }
257 }
258 efree(etag);
259 }
260
261 *handled_output_len = output_len;
262 *handled_output = estrndup(output, output_len);
263 }
264 /* }}} */
265
266 /*
267 * Local variables:
268 * tab-width: 4
269 * c-basic-offset: 4
270 * End:
271 * vim600: sw=4 ts=4 fdm=marker
272 * vim<600: sw=4 ts=4
273 */
274