2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
21 #include "php_streams.h"
22 #include "php_output.h"
23 #include "ext/standard/md5.h"
24 #include "ext/standard/sha1.h"
27 #include "php_http_std_defs.h"
28 #include "php_http_api.h"
29 #include "php_http_cache_api.h"
30 #include "php_http_send_api.h"
31 #include "php_http_date_api.h"
33 #ifdef HTTP_HAVE_MHASH
37 ZEND_EXTERN_MODULE_GLOBALS(http
);
39 PHP_MINIT_FUNCTION(http_cache
)
41 HTTP_LONG_CONSTANT("HTTP_ETAG_MD5", HTTP_ETAG_MD5
);
42 HTTP_LONG_CONSTANT("HTTP_ETAG_SHA1", HTTP_ETAG_SHA1
);
43 HTTP_LONG_CONSTANT("HTTP_ETAG_CRC32", HTTP_ETAG_CRC32
);
45 #ifdef HTTP_HAVE_HASH_EXT
46 HTTP_LONG_CONSTANT("HTTP_ETAG_SHA256", HTTP_ETAG_SHA256
);
47 HTTP_LONG_CONSTANT("HTTP_ETAG_SHA384", HTTP_ETAG_SHA384
);
48 HTTP_LONG_CONSTANT("HTTP_ETAG_SHA512", HTTP_ETAG_SHA512
);
49 HTTP_LONG_CONSTANT("HTTP_ETAG_RIPEMD128", HTTP_ETAG_RIPEMD128
);
50 HTTP_LONG_CONSTANT("HTTP_ETAG_RIPEMD160", HTTP_ETAG_RIPEMD160
);
53 #ifdef HTTP_HAVE_MHASH
55 int l
, i
, c
= mhash_count();
57 for (i
= 0; i
<= c
; ++i
) {
58 char const_name
[256] = {0};
59 const char *hash_name
= mhash_get_hash_name_static(i
);
62 l
= snprintf(const_name
, 255, "HTTP_ETAG_MHASH_%s", hash_name
);
63 zend_register_long_constant(const_name
, l
+ 1, i
, CONST_CS
|CONST_PERSISTENT
, module_number TSRMLS_CC
);
72 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
73 PHP_HTTP_API
char *_http_etag(const void *data_ptr
, size_t data_len
, http_send_mode data_mode TSRMLS_DC
)
75 void *ctx
= http_etag_init();
77 if (data_mode
== SEND_DATA
) {
78 http_etag_update(ctx
, data_ptr
, data_len
);
81 php_stream_statbuf ssb
;
83 if (data_mode
== SEND_RSRC
) {
84 ss
= php_stream_stat((php_stream
*) data_ptr
, &ssb
);
86 ss
= php_stream_stat_path((char *) data_ptr
, &ssb
);
94 char ssb_buf
[128] = {0};
96 ssb_len
= snprintf(ssb_buf
, 127, "%ld=%ld=%ld", (long) ssb
.sb
.st_mtime
,
98 (long) ssb
.sb
.st_size
);
99 http_etag_update(ctx
, ssb_buf
, ssb_len
);
103 return http_etag_finish(&ctx
);
107 /* {{{ time_t http_last_modified(void *, http_send_mode) */
108 PHP_HTTP_API
time_t _http_last_modified(const void *data_ptr
, http_send_mode data_mode TSRMLS_DC
)
110 php_stream_statbuf ssb
;
114 case SEND_DATA
: return time(NULL
);
115 case SEND_RSRC
: return php_stream_stat((php_stream
*) data_ptr
, &ssb
) ? 0 : ssb
.sb
.st_mtime
;
116 default: return php_stream_stat_path((char *) data_ptr
, &ssb
) ? 0 : ssb
.sb
.st_mtime
;
121 /* {{{ zend_bool http_match_last_modified(char *, time_t) */
122 PHP_HTTP_API zend_bool
_http_match_last_modified_ex(const char *entry
, time_t t
, zend_bool enforce_presence TSRMLS_DC
)
126 char *modified
, *chr_ptr
;
128 HTTP_GSC(zmodified
, entry
, !enforce_presence
);
130 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
131 if ((chr_ptr
= strrchr(modified
, ';'))) {
134 retval
= (t
<= http_parse_date(modified
));
140 /* {{{ zend_bool http_match_etag(char *, char *) */
141 PHP_HTTP_API zend_bool
_http_match_etag_ex(const char *entry
, const char *etag
, zend_bool enforce_presence TSRMLS_DC
)
147 HTTP_GSC(zetag
, entry
, !enforce_presence
);
149 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
153 spprintf("ed_etag
, 0, "\"%s\"", etag
);
154 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
155 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
157 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
165 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
166 PHP_HTTP_API STATUS
_http_cache_last_modified(time_t last_modified
,
167 time_t send_modified
, const char *cache_control
, size_t cc_len TSRMLS_DC
)
169 char *sent_header
= NULL
;
171 if (SG(headers_sent
)) {
175 if (cc_len
&& (SUCCESS
!= http_send_cache_control(cache_control
, cc_len
))) {
179 if (SUCCESS
!= http_send_last_modified_ex(send_modified
, &sent_header
)) {
183 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
184 http_exit_ex(304, sent_header
, NULL
, 0);
186 STR_FREE(sent_header
);
193 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
194 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, size_t etag_len
,
195 const char *cache_control
, size_t cc_len TSRMLS_DC
)
197 char *sent_header
= NULL
;
199 if (SG(headers_sent
)) {
203 if (cc_len
&& (SUCCESS
!= http_send_cache_control(cache_control
, cc_len
))) {
208 if (SUCCESS
!= http_send_etag_ex(etag
, etag_len
, &sent_header
)) {
211 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
212 http_exit_ex(304, sent_header
, NULL
, 0);
214 STR_FREE(sent_header
);
219 /* start ob_etaghandler */
220 return http_start_ob_etaghandler();
224 PHP_HTTP_API STATUS
_http_start_ob_etaghandler(TSRMLS_D
)
226 /* already running? */
227 if (php_ob_handler_used("ob_etaghandler" TSRMLS_CC
)) {
228 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "ob_etaghandler can only be used once");
232 HTTP_G(etag
).started
= 1;
233 return php_start_ob_buffer_named("ob_etaghandler", HTTP_G(send
).buffer_size
, 1 TSRMLS_CC
);
236 PHP_HTTP_API zend_bool
_http_interrupt_ob_etaghandler(TSRMLS_D
)
238 if (HTTP_G(etag
).started
) {
239 HTTP_G(etag
).started
= 0;
240 http_etag_free(&HTTP_G(etag
).ctx
);
246 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
247 void _http_ob_etaghandler(char *output
, uint output_len
,
248 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
251 *handled_output_len
= output_len
;
252 *handled_output
= estrndup(output
, output_len
);
254 /* are we supposed to run? */
255 if (HTTP_G(etag
).started
) {
256 /* initialize the etag context */
257 if (mode
& PHP_OUTPUT_HANDLER_START
) {
258 HTTP_G(etag
).ctx
= http_etag_init();
262 http_etag_update(HTTP_G(etag
).ctx
, output
, output_len
);
265 if (mode
& PHP_OUTPUT_HANDLER_END
) {
266 char *sent_header
= NULL
;
267 char *etag
= http_etag_finish(&HTTP_G(etag
).ctx
);
269 http_send_cache_control(HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
));
270 http_send_etag_ex(etag
, strlen(etag
), &sent_header
);
272 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
273 http_exit_ex(304, sent_header
, etag
, 0);
276 STR_FREE(sent_header
);
288 * vim600: sw=4 ts=4 fdm=marker