- renamed response.status to response.code
[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
22 #include "php.h"
23 #include "php_streams.h"
24 #include "ext/standard/md5.h"
25
26 #include "php_http.h"
27 #include "php_http_std_defs.h"
28 #include "php_http_cache_api.h"
29 #include "php_http_send_api.h"
30 #include "php_http_api.h"
31 #include "php_http_date_api.h"
32
33 ZEND_EXTERN_MODULE_GLOBALS(http);
34
35 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
36 PHP_HTTP_API char *_http_etag(const void *data_ptr, size_t data_len, http_send_mode data_mode TSRMLS_DC)
37 {
38 char ssb_buf[128] = {0};
39 unsigned char digest[16];
40 PHP_MD5_CTX ctx;
41 char *new_etag = ecalloc(1, 33);
42
43 PHP_MD5Init(&ctx);
44
45 switch (data_mode)
46 {
47 case SEND_DATA:
48 PHP_MD5Update(&ctx, data_ptr, data_len);
49 break;
50
51 case SEND_RSRC:
52 if (!HTTP_G(ssb).sb.st_ino) {
53 if (php_stream_stat((php_stream *) data_ptr, &HTTP_G(ssb))) {
54 return NULL;
55 }
56 }
57 snprintf(ssb_buf, 127, "%ld=%ld=%ld",
58 HTTP_G(ssb).sb.st_mtime,
59 HTTP_G(ssb).sb.st_ino,
60 HTTP_G(ssb).sb.st_size
61 );
62 PHP_MD5Update(&ctx, ssb_buf, strlen(ssb_buf));
63 break;
64
65 default:
66 efree(new_etag);
67 return NULL;
68 break;
69 }
70
71 PHP_MD5Final(digest, &ctx);
72 make_digest(new_etag, digest);
73
74 return new_etag;
75 }
76 /* }}} */
77
78 /* {{{ time_t http_lmod(void *, http_send_mode) */
79 PHP_HTTP_API time_t _http_lmod(const void *data_ptr, http_send_mode data_mode TSRMLS_DC)
80 {
81 switch (data_mode)
82 {
83 case SEND_DATA:
84 {
85 return time(NULL);
86 }
87
88 case SEND_RSRC:
89 {
90 php_stream_stat((php_stream *) data_ptr, &HTTP_G(ssb));
91 return HTTP_G(ssb).sb.st_mtime;
92 }
93
94 default:
95 {
96 php_stream_stat_path(Z_STRVAL_P((zval *) data_ptr), &HTTP_G(ssb));
97 return HTTP_G(ssb).sb.st_mtime;
98 }
99 }
100 }
101 /* }}} */
102
103 /* {{{ zend_bool http_modified_match(char *, time_t) */
104 PHP_HTTP_API zend_bool _http_modified_match_ex(const char *entry, time_t t, zend_bool enforce_presence TSRMLS_DC)
105 {
106 zend_bool retval;
107 zval *zmodified;
108 char *modified, *chr_ptr;
109
110 HTTP_GSC(zmodified, entry, !enforce_presence);
111
112 modified = estrndup(Z_STRVAL_P(zmodified), Z_STRLEN_P(zmodified));
113 if (chr_ptr = strrchr(modified, ';')) {
114 chr_ptr = 0;
115 }
116 retval = (t <= http_parse_date(modified));
117 efree(modified);
118 return retval;
119 }
120 /* }}} */
121
122 /* {{{ zend_bool http_etag_match(char *, char *) */
123 PHP_HTTP_API zend_bool _http_etag_match_ex(const char *entry, const char *etag, zend_bool enforce_presence TSRMLS_DC)
124 {
125 zval *zetag;
126 char *quoted_etag;
127 zend_bool result;
128
129 HTTP_GSC(zetag, entry, !enforce_presence);
130
131 if (NULL != strchr(Z_STRVAL_P(zetag), '*')) {
132 return 1;
133 }
134
135 quoted_etag = (char *) emalloc(strlen(etag) + 3);
136 sprintf(quoted_etag, "\"%s\"", etag);
137
138 if (!strchr(Z_STRVAL_P(zetag), ',')) {
139 result = !strcmp(Z_STRVAL_P(zetag), quoted_etag);
140 } else {
141 result = (NULL != strstr(Z_STRVAL_P(zetag), quoted_etag));
142 }
143 efree(quoted_etag);
144 return result;
145 }
146 /* }}} */
147
148 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
149 PHP_HTTP_API STATUS _http_cache_last_modified(time_t last_modified,
150 time_t send_modified, const char *cache_control, size_t cc_len TSRMLS_DC)
151 {
152 if (cc_len) {
153 http_send_cache_control(cache_control, cc_len);
154 }
155
156 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified)) {
157 if (SUCCESS == http_send_status(304)) {
158 zend_bailout();
159 } else {
160 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
161 return FAILURE;
162 }
163 }
164 return http_send_last_modified(send_modified);
165 }
166 /* }}} */
167
168 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
169 PHP_HTTP_API STATUS _http_cache_etag(const char *etag, size_t etag_len,
170 const char *cache_control, size_t cc_len TSRMLS_DC)
171 {
172 if (cc_len) {
173 http_send_cache_control(cache_control, cc_len);
174 }
175
176 if (etag_len) {
177 http_send_etag(etag, etag_len);
178 if (http_etag_match("HTTP_IF_NONE_MATCH", etag)) {
179 if (SUCCESS == http_send_status(304)) {
180 zend_bailout();
181 } else {
182 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not send 304 Not Modified");
183 return FAILURE;
184 }
185 }
186 }
187
188 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
189 if (!HTTP_G(etag_started)) {
190 if (SUCCESS == http_start_ob_handler(_http_ob_etaghandler, "ob_etaghandler", 4096, 1)) {
191 HTTP_G(etag_started) = 1;
192 return SUCCESS;
193 } else {
194 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not start ob_etaghandler");
195 return FAILURE;
196 }
197 }
198 return SUCCESS;
199 }
200 /* }}} */
201
202
203 /*
204 * Local variables:
205 * tab-width: 4
206 * c-basic-offset: 4
207 * End:
208 * vim600: sw=4 ts=4 fdm=marker
209 * vim<600: sw=4 ts=4
210 */
211