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