- remove ambigious OPT
[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
22 #include "php.h"
23 #include "php_streams.h"
24 #include "php_output.h"
25 #include "ext/standard/md5.h"
26
27 #include "SAPI.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 ZEND_EXTERN_MODULE_GLOBALS(http);
37
38 /* {{{ STATUS http_cache_exit(char *, zend_bool) */
39 STATUS _http_cache_exit_ex(char *cache_token, zend_bool etag, zend_bool free_token TSRMLS_DC)
40 {
41 if (HTTP_G(cache_log) && strlen(HTTP_G(cache_log))) {
42 php_stream *log = php_stream_open_wrapper(HTTP_G(cache_log), "ab", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL);
43
44 if (log) {
45 time_t now;
46 struct tm nowtm;
47 char datetime[128];
48
49 time(&now);
50 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now, &nowtm));
51 php_stream_printf(log TSRMLS_CC, "%s [%s] %32s %s\n", datetime, etag ? "ETAG":"LMOD", cache_token, SG(request_info).request_uri);
52 php_stream_close(log);
53 }
54 }
55 if (free_token && cache_token) {
56 efree(cache_token);
57 }
58 return http_exit_ex(304, NULL, 0);
59 }
60 /* }}} */
61
62 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
63 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC)
64 {
65 char ssb_buf[128] = {0};
66 unsigned char digest[16];
67 PHP_MD5_CTX ctx;
68 char *new_etag = ecalloc(1, 33);
69
70 PHP_MD5Init(&ctx);
71
72 switch (data_mode)
73 {
74 case SEND_DATA:
75 PHP_MD5Update(&ctx, data_ptr, data_len);
76 break;
77
78 case SEND_RSRC:
79 if (!HTTP_G(ssb).sb.st_ino) {
80 if (php_stream_stat((php_stream *) data_ptr, &HTTP_G(ssb))) {
81 return NULL;
82 }
83 }
84 snprintf(ssb_buf, 127, "%ld=%ld=%ld",
85 HTTP_G(ssb).sb.st_mtime,
86 HTTP_G(ssb).sb.st_ino,
87 HTTP_G(ssb).sb.st_size
88 );
89 PHP_MD5Update(&ctx, ssb_buf, strlen(ssb_buf));
90 break;
91
92 default:
93 efree(new_etag);
94 return NULL;
95 break;
96 }
97
98 PHP_MD5Final(digest, &ctx);
99 make_digest(new_etag, digest);
100
101 return new_etag;
102 }
103 /* }}} */
104
105 /* {{{ time_t http_last_modified(void *, http_send_mode) */
106 PHP_HTTP_API time_t _http_last_modified(const void *data_ptr, http_send_mode data_mode TSRMLS_DC)
107 {
108 switch (data_mode)
109 {
110 case SEND_DATA:
111 {
112 return time(NULL);
113 }
114
115 case SEND_RSRC:
116 {
117 php_stream_stat((php_stream *) data_ptr, &HTTP_G(ssb));
118 return HTTP_G(ssb).sb.st_mtime;
119 }
120
121 default:
122 {
123 php_stream_stat_path(Z_STRVAL_P((zval *) data_ptr), &HTTP_G(ssb));
124 return HTTP_G(ssb).sb.st_mtime;
125 }
126 }
127 }
128 /* }}} */
129
130 /* {{{ zend_bool http_match_last_modified(char *, time_t) */
131 PHP_HTTP_API zend_bool _http_match_last_modified_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC)
132 {
133 zend_bool retval;
134 zval *zmodified;
135 char *modified, *chr_ptr;
136
137 HTTP_GSC(zmodified, entry, !enforce_presence);
138
139 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
140 if (chr_ptr = strrchr(modified, ';')) {
141 chr_ptr = 0;
142 }
143 retval = (t <= http_parse_date(modified));
144 efree(modified);
145 return retval;
146 }
147 /* }}} */
148
149 /* {{{ zend_bool http_match_etag(char *, char *) */
150 PHP_HTTP_API zend_bool _http_match_etag_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC)
151 {
152 zval *zetag;
153 char *quoted_etag;
154 zend_bool result;
155
156 HTTP_GSC(zetag, entry, !enforce_presence);
157
158 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
159 return 1;
160 }
161
162 quoted_etag = (char *) emalloc(strlen(etag) + 3);
163 sprintf(quoted_etag, "\"%s\"", etag);
164
165 if (!strchr(Z_STRVAL_P(zetag), ',')) {
166 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
167 } else {
168 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
169 }
170 efree(quoted_etag);
171 return result;
172 }
173 /* }}} */
174
175 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
176 PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified,
177 time_t send_modified, const char *cache_control, size_t cc_len TSRMLS_DC)
178 {
179 if (cc_len && (SUCCESS != http_send_cache_control(cache_control, cc_len))) {
180 return FAILURE;
181 }
182
183 if (SUCCESS != http_send_last_modified(send_modified)) {
184 return FAILURE;
185 }
186
187 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", last_modified)) {
188 return http_cache_exit(http_date(last_modified), 0);
189 }
190
191 return SUCCESS;
192 }
193 /* }}} */
194
195 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
196 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len,
197 const char *cache_control, size_t cc_len TSRMLS_DC)
198 {
199 if (cc_len && (SUCCESS != http_send_cache_control(cache_control, cc_len))) {
200 return FAILURE;
201 }
202
203 if (etag_len) {
204 if (SUCCESS != http_send_etag(etag, etag_len)) {
205 return FAILURE;
206 }
207 if (!http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
208 return SUCCESS;
209 }
210 return http_cache_exit_ex((char *)etag, 1, 0);
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_SENDBUF_SIZE, 1 TSRMLS_CC))) {
219 return SUCCESS;
220 } else {
221 return FAILURE;
222 }
223
224 }
225 /* }}} */
226
227 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
228 PHP_HTTP_API void _http_ob_etaghandler(char *output, uint output_len,
229 char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
230 {
231 char etag[33] = { 0 };
232 unsigned char digest[16];
233
234 if (mode & PHP_OUTPUT_HANDLER_START) {
235 HTTP_G(etag_started) = 1;
236 PHP_MD5Init(&HTTP_G(etag_md5));
237 }
238
239 PHP_MD5Update(&HTTP_G(etag_md5), output, output_len);
240
241 if (mode & PHP_OUTPUT_HANDLER_END) {
242 PHP_MD5Final(digest, &HTTP_G(etag_md5));
243
244 /* just do that if desired */
245 if (HTTP_G(etag_started)) {
246 make_digest(etag, digest);
247 http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL);
248 http_send_etag(etag, 32);
249
250 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
251 http_cache_exit_ex(etag, 1, 0);
252 }
253 }
254 }
255
256 *handled_output_len = output_len;
257 *handled_output = estrndup(output, output_len);
258 }
259 /* }}} */
260
261 /*
262 * Local variables:
263 * tab-width: 4
264 * c-basic-offset: 4
265 * End:
266 * vim600: sw=4 ts=4 fdm=marker
267 * vim<600: sw=4 ts=4
268 */
269