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