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