- avoid lots of useless ops
[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
47 #ifdef HTTP_HAVE_MHASH
48 {
49 int l, i, c = mhash_count();
50
51 for (i = 0; i < c; ++i) {
52 char const_name[256] = {0};
53 const char *hash_name = mhash_get_hash_name_static(i);
54
55 if (hash_name) {
56 l = snprintf(const_name, 255, "HTTP_ETAG_MHASH_%s", hash_name);
57 zend_register_long_constant(const_name, l + 1, i, CONST_CS|CONST_PERSISTENT, module_number TSRMLS_CC);
58 }
59 }
60 }
61 #endif
62
63 return SUCCESS;
64 }
65
66 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
67 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC)
68 {
69 php_stream_statbuf ssb;
70 char ssb_buf[128] = {0};
71 size_t ssb_len;
72 void *ctx = http_etag_init();
73
74 switch (data_mode)
75 {
76 case SEND_DATA:
77 http_etag_update(ctx, data_ptr, data_len);
78 break;
79
80 case SEND_RSRC:
81 {
82 if (php_stream_stat((php_stream *) data_ptr, &ssb)) {
83 efree(ctx);
84 return NULL;
85 }
86 ssb_len = snprintf(ssb_buf, 127, "%ld=%ld=%ld", ssb.sb.st_mtime, ssb.sb.st_ino, ssb.sb.st_size);
87 http_etag_update(ctx, ssb_buf, ssb_len);
88 }
89 break;
90
91 default:
92 {
93 if (php_stream_stat_path((char *) data_ptr, &ssb)) {
94 efree(ctx);
95 return NULL;
96 }
97 ssb_len = snprintf(ssb_buf, 127, "%ld=%ld=%ld", ssb.sb.st_mtime, ssb.sb.st_ino, ssb.sb.st_size);
98 http_etag_update(ctx, ssb_buf, ssb_len);
99 }
100 break;
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 quoted_etag = (char *) emalloc(strlen(etag) + 3);
154 sprintf(quoted_etag, "\"%s\"", etag);
155
156 if (!strchr(Z_STRVAL_P(zetag), ',')) {
157 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
158 } else {
159 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
160 }
161 efree(quoted_etag);
162 return result;
163 }
164 /* }}} */
165
166 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
167 PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified,
168 time_t send_modified, const char *cache_control, size_t cc_len TSRMLS_DC)
169 {
170 char *sent_header = NULL;
171
172 if (cc_len && (SUCCESS != http_send_cache_control(cache_control, cc_len))) {
173 return FAILURE;
174 }
175
176 if (SUCCESS != http_send_last_modified_ex(send_modified, &sent_header)) {
177 return FAILURE;
178 }
179
180 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", last_modified)) {
181 http_exit_ex(304, sent_header, NULL, 0);
182 } else {
183 STR_FREE(sent_header);
184 }
185
186 return SUCCESS;
187 }
188 /* }}} */
189
190 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
191 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len,
192 const char *cache_control, size_t cc_len TSRMLS_DC)
193 {
194 char *sent_header = NULL;
195
196 if (cc_len && (SUCCESS != http_send_cache_control(cache_control, cc_len))) {
197 return FAILURE;
198 }
199
200 if (etag_len) {
201 if (SUCCESS != http_send_etag_ex(etag, etag_len, &sent_header)) {
202 return FAILURE;
203 }
204 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
205 http_exit_ex(304, sent_header, NULL, 0);
206 } else {
207 STR_FREE(sent_header);
208 }
209 return SUCCESS;
210 }
211
212 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
213 if (HTTP_G(etag).started) {
214 return SUCCESS;
215 }
216
217 if (HTTP_G(etag).started = (SUCCESS == php_start_ob_buffer_named("ob_etaghandler", HTTP_SENDBUF_SIZE, 1 TSRMLS_CC))) {
218 return SUCCESS;
219 } else {
220 return FAILURE;
221 }
222 }
223 /* }}} */
224
225 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
226 PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len,
227 char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
228 {
229 if (mode & PHP_OUTPUT_HANDLER_START) {
230 if (HTTP_G(etag).started) {
231 http_error(HE_WARNING, HTTP_E_RUNTIME, "ob_etaghandler can only be used once");
232 return;
233 }
234 HTTP_G(etag).started = 1;
235 HTTP_G(etag).ctx = http_etag_init();
236 }
237
238 http_etag_update(HTTP_G(etag).ctx, output, output_len);
239
240 if (mode & PHP_OUTPUT_HANDLER_END) {
241 char *etag = http_etag_finish(HTTP_G(etag).ctx);
242
243 /* just do that if desired */
244 if (HTTP_G(etag).started) {
245 char *sent_header = NULL;
246
247 http_send_cache_control(HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL));
248 http_send_etag_ex(etag, strlen(etag), &sent_header);
249
250 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
251 efree(etag);
252 http_exit_ex(304, sent_header, NULL, 0);
253 } else {
254 STR_FREE(sent_header);
255 }
256 }
257 efree(etag);
258 }
259
260 *handled_output_len = output_len;
261 *handled_output = estrndup(output, output_len);
262 }
263 /* }}} */
264
265 /*
266 * Local variables:
267 * tab-width: 4
268 * c-basic-offset: 4
269 * End:
270 * vim600: sw=4 ts=4 fdm=marker
271 * vim<600: sw=4 ts=4
272 */
273