9fce65059988eebaea0606b7c462e5b7ea17e665
[m6w6/ext-http] / http_cache_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #include "php_http.h"
17
18 #include "php_output.h"
19 #include "php_streams.h"
20
21 #include "php_http_api.h"
22 #include "php_http_cache_api.h"
23 #include "php_http_date_api.h"
24 #include "php_http_send_api.h"
25
26 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
27 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC)
28 {
29 void *ctx = http_etag_init();
30
31 if (data_mode == SEND_DATA) {
32 http_etag_update(ctx, data_ptr, data_len);
33 } else {
34 STATUS ss = FAILURE;
35 php_stream_statbuf ssb;
36
37 if (data_mode == SEND_RSRC) {
38 ss = php_stream_stat((php_stream *) data_ptr, &ssb);
39 } else {
40 ss = php_stream_stat_path((char *) data_ptr, &ssb);
41 }
42
43 if (SUCCESS != ss) {
44 efree(ctx);
45 return NULL;
46 } else {
47 size_t ssb_len;
48 char ssb_buf[128] = {0};
49
50 ssb_len = snprintf(ssb_buf, 127, "%ld=%ld=%ld", (long) ssb.sb.st_mtime,
51 (long) ssb.sb.st_ino,
52 (long) ssb.sb.st_size);
53 http_etag_update(ctx, ssb_buf, ssb_len);
54 }
55 }
56
57 return http_etag_finish(ctx);
58 }
59 /* }}} */
60
61 /* {{{ time_t http_last_modified(void *, http_send_mode) */
62 PHP_HTTP_API time_t _http_last_modified(const void *data_ptr, http_send_mode data_mode TSRMLS_DC)
63 {
64 php_stream_statbuf ssb;
65
66 switch (data_mode)
67 {
68 case SEND_DATA: return HTTP_GET_REQUEST_TIME();
69 case SEND_RSRC: return php_stream_stat((php_stream *) data_ptr, &ssb) ? 0 : ssb.sb.st_mtime;
70 default: return php_stream_stat_path((char *) data_ptr, &ssb) ? 0 : ssb.sb.st_mtime;
71 }
72 }
73 /* }}} */
74
75 /* {{{ zend_bool http_match_last_modified(char *, time_t) */
76 PHP_HTTP_API zend_bool _http_match_last_modified_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC)
77 {
78 zend_bool retval;
79 zval *zmodified;
80 char *modified, *chr_ptr;
81
82 HTTP_GSC(zmodified, entry, !enforce_presence);
83
84 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
85 if ((chr_ptr = strrchr(modified, ';'))) {
86 chr_ptr = 0;
87 }
88
89 retval = (t <= http_parse_date(modified));
90 efree(modified);
91 return retval;
92 }
93 /* }}} */
94
95 /* {{{ zend_bool http_match_etag(char *, char *) */
96 PHP_HTTP_API zend_bool _http_match_etag_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC)
97 {
98 zval *zetag;
99 char *quoted_etag;
100 zend_bool result;
101
102 HTTP_GSC(zetag, entry, !enforce_presence);
103
104 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
105 return 1;
106 }
107
108 spprintf(&quoted_etag, 0, "\"%s\"", etag);
109 if (!strchr(Z_STRVAL_P(zetag), ',')) {
110 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
111 } else {
112 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
113 }
114 efree(quoted_etag);
115
116 return result;
117 }
118 /* }}} */
119
120 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
121 PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified,
122 time_t send_modified, const char *cache_control, size_t cc_len TSRMLS_DC)
123 {
124 char *sent_header = NULL;
125
126 if (SG(headers_sent)) {
127 return FAILURE;
128 }
129
130 if (cc_len && (SUCCESS != http_send_cache_control(cache_control, cc_len))) {
131 return FAILURE;
132 }
133
134 if (SUCCESS != http_send_last_modified_ex(send_modified, &sent_header)) {
135 return FAILURE;
136 }
137
138 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", last_modified)) {
139 http_exit_ex(304, sent_header, NULL, 0);
140 } else {
141 STR_FREE(sent_header);
142 }
143
144 return SUCCESS;
145 }
146 /* }}} */
147
148 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
149 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len,
150 const char *cache_control, size_t cc_len TSRMLS_DC)
151 {
152 char *sent_header = NULL;
153
154 if (SG(headers_sent)) {
155 return FAILURE;
156 }
157
158 if (cc_len && (SUCCESS != http_send_cache_control(cache_control, cc_len))) {
159 return FAILURE;
160 }
161
162 if (etag_len) {
163 if (SUCCESS != http_send_etag_ex(etag, etag_len, &sent_header)) {
164 return FAILURE;
165 }
166 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
167 http_exit_ex(304, sent_header, NULL, 0);
168 } else {
169 STR_FREE(sent_header);
170 }
171 return SUCCESS;
172 }
173
174 /* start ob_etaghandler */
175 return http_start_ob_etaghandler();
176 }
177 /* }}} */
178
179 PHP_HTTP_API STATUS _http_start_ob_etaghandler(TSRMLS_D)
180 {
181 /* already running? */
182 if (php_ob_handler_used("ob_etaghandler" TSRMLS_CC)) {
183 http_error(HE_WARNING, HTTP_E_RUNTIME, "ob_etaghandler can only be used once");
184 return FAILURE;
185 }
186
187 HTTP_G->etag.started = 1;
188 return php_start_ob_buffer_named("ob_etaghandler", HTTP_G->send.buffer_size, 0 TSRMLS_CC);
189 }
190
191 PHP_HTTP_API zend_bool _http_interrupt_ob_etaghandler(TSRMLS_D)
192 {
193 if (HTTP_G->etag.started) {
194 HTTP_G->etag.started = 0;
195 if (HTTP_G->etag.ctx) {
196 efree(HTTP_G->etag.ctx);
197 HTTP_G->etag.ctx = NULL;
198 }
199 return 1;
200 }
201 return 0;
202 }
203
204 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
205 void _http_ob_etaghandler(char *output, uint output_len,
206 char **handled_output, uint *handled_output_len, int mode TSRMLS_DC)
207 {
208 /* passthru */
209 *handled_output_len = output_len;
210 *handled_output = estrndup(output, output_len);
211
212 /* are we supposed to run? */
213 if (HTTP_G->etag.started) {
214 /* initialize the etag context */
215 if (mode & PHP_OUTPUT_HANDLER_START) {
216 HTTP_G->etag.ctx = http_etag_init();
217 }
218
219 /* update */
220 http_etag_update(HTTP_G->etag.ctx, output, output_len);
221
222 /* finish */
223 if (mode & PHP_OUTPUT_HANDLER_END) {
224 char *sent_header = NULL;
225 char *etag = http_etag_finish(HTTP_G->etag.ctx);
226
227 HTTP_G->etag.ctx = NULL;
228
229 http_send_cache_control(HTTP_DEFAULT_CACHECONTROL, lenof(HTTP_DEFAULT_CACHECONTROL));
230 http_send_etag_ex(etag, strlen(etag), &sent_header);
231
232 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
233 /* force exit; ob within ob does not work */
234 HTTP_G->force_exit = 1;
235 http_exit_ex(304, sent_header, etag, 0);
236 }
237
238 STR_FREE(sent_header);
239 STR_FREE(etag);
240 }
241 }
242 }
243 /* }}} */
244
245 /*
246 * Local variables:
247 * tab-width: 4
248 * c-basic-offset: 4
249 * End:
250 * vim600: sw=4 ts=4 fdm=marker
251 * vim<600: sw=4 ts=4
252 */
253