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