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 +----------------------------------------------------------------------+
23 #include "php_streams.h"
24 #include "ext/standard/md5.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"
33 ZEND_EXTERN_MODULE_GLOBALS(http
);
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
)
38 char ssb_buf
[128] = {0};
39 unsigned char digest
[16];
41 char *new_etag
= ecalloc(1, 33);
48 PHP_MD5Update(&ctx
, data_ptr
, data_len
);
52 if (!HTTP_G(ssb
).sb
.st_ino
) {
53 if (php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
))) {
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
62 PHP_MD5Update(&ctx
, ssb_buf
, strlen(ssb_buf
));
71 PHP_MD5Final(digest
, &ctx
);
72 make_digest(new_etag
, digest
);
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
)
90 php_stream_stat((php_stream
*) data_ptr
, &HTTP_G(ssb
));
91 return HTTP_G(ssb
).sb
.st_mtime
;
96 php_stream_stat_path(Z_STRVAL_P((zval
*) data_ptr
), &HTTP_G(ssb
));
97 return HTTP_G(ssb
).sb
.st_mtime
;
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
)
108 char *modified
, *chr_ptr
;
110 HTTP_GSC(zmodified
, entry
, !enforce_presence
);
112 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
113 if (chr_ptr
= strrchr(modified
, ';')) {
116 retval
= (t
<= http_parse_date(modified
));
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
)
129 HTTP_GSC(zetag
, entry
, !enforce_presence
);
131 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
135 quoted_etag
= (char *) emalloc(strlen(etag
) + 3);
136 sprintf(quoted_etag
, "\"%s\"", etag
);
138 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
139 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
141 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
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
)
153 http_send_cache_control(cache_control
, cc_len
);
156 if (http_modified_match("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
157 if (SUCCESS
== http_send_status(304)) {
160 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
164 return http_send_last_modified(send_modified
);
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
)
173 http_send_cache_control(cache_control
, cc_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)) {
182 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not send 304 Not Modified");
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;
194 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not start ob_etaghandler");
208 * vim600: sw=4 ts=4 fdm=marker