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