2 +----------------------------------------------------------------------+
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 +----------------------------------------------------------------------+
24 #include "php_streams.h"
25 #include "php_output.h"
26 #include "ext/standard/md5.h"
29 #include "php_http_std_defs.h"
30 #include "php_http_api.h"
31 #include "php_http_cache_api.h"
32 #include "php_http_send_api.h"
33 #include "php_http_date_api.h"
35 ZEND_EXTERN_MODULE_GLOBALS(http
);
37 /* {{{ STATUS http_cache_exit(char *, zend_bool) */
38 STATUS
_http_cache_exit_ex(char *cache_token
, zend_bool etag
, zend_bool free_token TSRMLS_DC
)
40 if (HTTP_G(log
).cache
&& strlen(HTTP_G(log
).cache
)) {
41 php_stream
*log
= php_stream_open_wrapper(HTTP_G(log
).cache
, "ab", REPORT_ERRORS
|ENFORCE_SAFE_MODE
, NULL
);
49 strftime(datetime
, sizeof(datetime
), "%Y-%m-%d %H:%M:%S", php_localtime_r(&now
, &nowtm
));
50 php_stream_printf(log TSRMLS_CC
, "%s [%s] %32s %s\n", datetime
, etag
? "ETAG":"LMOD", cache_token
, SG(request_info
).request_uri
);
51 php_stream_close(log
);
54 if (free_token
&& cache_token
) {
57 return http_exit_ex(304, NULL
, 0);
61 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
62 PHP_HTTP_API
char *_http_etag(const void *data_ptr
, size_t data_len
, http_send_mode data_mode TSRMLS_DC
)
64 char ssb_buf
[128] = {0};
65 unsigned char digest
[16];
67 char *new_etag
= ecalloc(1, 33);
74 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
79 php_stream_statbuf ssb
;
81 if (php_stream_stat((php_stream
*) data_ptr
, &ssb
)) {
84 snprintf(ssb_buf
, 127, "%ld=%ld=%ld", ssb
.sb
.st_mtime
, ssb
.sb
.st_ino
, ssb
.sb
.st_size
);
85 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
95 PHP_MD5Final(digest
, &ctx
);
96 make_digest(new_etag
, digest
);
102 /* {{{ time_t http_last_modified(void *, http_send_mode) */
103 PHP_HTTP_API
time_t _http_last_modified(const void *data_ptr
, http_send_mode data_mode TSRMLS_DC
)
105 php_stream_statbuf ssb
;
109 case SEND_DATA
: return time(NULL
);
110 case SEND_RSRC
: return php_stream_stat((php_stream
*) data_ptr
, &ssb
) ? 0 : ssb
.sb
.st_mtime
;
111 default: return php_stream_stat_path(Z_STRVAL_P((zval
*) data_ptr
), &ssb
) ? 0 : ssb
.sb
.st_mtime
;
116 /* {{{ zend_bool http_match_last_modified(char *, time_t) */
117 PHP_HTTP_API zend_bool
_http_match_last_modified_ex(const char *entry
, time_t t
, zend_bool enforce_presence TSRMLS_DC
)
121 char *modified
, *chr_ptr
;
123 HTTP_GSC(zmodified
, entry
, !enforce_presence
);
125 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
126 if (chr_ptr
= strrchr(modified
, ';')) {
129 retval
= (t
<= http_parse_date(modified
));
135 /* {{{ zend_bool http_match_etag(char *, char *) */
136 PHP_HTTP_API zend_bool
_http_match_etag_ex(const char *entry
, const char *etag
, zend_bool enforce_presence TSRMLS_DC
)
142 HTTP_GSC(zetag
, entry
, !enforce_presence
);
144 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
148 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
149 sprintf(quoted_etag
, "\"%s\"", etag
);
151 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
152 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
154 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
161 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
162 PHP_HTTP_API STATUS
_http_cache_last_modified(time_t last_modified
,
163 time_t send_modified
, const char *cache_control
, size_t cc_len TSRMLS_DC
)
165 if (cc_len
&& (SUCCESS
!= http_send_cache_control(cache_control
, cc_len
))) {
169 if (SUCCESS
!= http_send_last_modified(send_modified
)) {
173 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
174 return http_cache_exit(http_date(last_modified
), 0);
181 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
182 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, size_t etag_len
,
183 const char *cache_control
, size_t cc_len TSRMLS_DC
)
185 if (cc_len
&& (SUCCESS
!= http_send_cache_control(cache_control
, cc_len
))) {
190 if (SUCCESS
!= http_send_etag(etag
, etag_len
)) {
193 if (!http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
196 return http_cache_exit_ex((char *)etag
, 1, 0);
199 /* if no etag is given and we didn't already start ob_etaghandler -- start it */
200 if (HTTP_G(etag
).started
) {
204 if (HTTP_G(etag
).started
= (SUCCESS
== php_start_ob_buffer_named("ob_etaghandler", HTTP_SENDBUF_SIZE
, 1 TSRMLS_CC
))) {
212 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
213 PHP_HTTP_API
void _http_ob_etaghandler(char *output
, uint output_len
,
214 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
216 char etag
[33] = { 0 };
217 unsigned char digest
[16];
219 if (mode
& PHP_OUTPUT_HANDLER_START
) {
220 HTTP_G(etag
).started
= 1;
221 PHP_MD5Init(&HTTP_G(etag
).md5ctx
);
224 PHP_MD5Update(&HTTP_G(etag
).md5ctx
, output
, output_len
);
226 if (mode
& PHP_OUTPUT_HANDLER_END
) {
227 PHP_MD5Final(digest
, &HTTP_G(etag
).md5ctx
);
229 /* just do that if desired */
230 if (HTTP_G(etag
).started
) {
231 make_digest(etag
, digest
);
232 http_send_header("Cache-Control: " HTTP_DEFAULT_CACHECONTROL
);
233 http_send_etag(etag
, 32);
235 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
236 http_cache_exit_ex(etag
, 1, 0);
241 *handled_output_len
= output_len
;
242 *handled_output
= estrndup(output
, output_len
);
251 * vim600: sw=4 ts=4 fdm=marker