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 +--------------------------------------------------------------------+
22 #include "php_output.h"
23 #include "php_streams.h"
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"
30 ZEND_EXTERN_MODULE_GLOBALS(http
);
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
)
35 void *ctx
= http_etag_init();
37 if (data_mode
== SEND_DATA
) {
38 http_etag_update(ctx
, data_ptr
, data_len
);
41 php_stream_statbuf ssb
;
43 if (data_mode
== SEND_RSRC
) {
44 ss
= php_stream_stat((php_stream
*) data_ptr
, &ssb
);
46 ss
= php_stream_stat_path((char *) data_ptr
, &ssb
);
54 char ssb_buf
[128] = {0};
56 ssb_len
= snprintf(ssb_buf
, 127, "%ld=%ld=%ld", (long) ssb
.sb
.st_mtime
,
58 (long) ssb
.sb
.st_size
);
59 http_etag_update(ctx
, ssb_buf
, ssb_len
);
63 return http_etag_finish(ctx
);
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
)
70 php_stream_statbuf ssb
;
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
;
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
)
86 char *modified
, *chr_ptr
;
88 HTTP_GSC(zmodified
, entry
, !enforce_presence
);
90 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
91 if ((chr_ptr
= strrchr(modified
, ';'))) {
94 retval
= (t
<= http_parse_date(modified
));
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
)
107 HTTP_GSC(zetag
, entry
, !enforce_presence
);
109 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
113 spprintf("ed_etag
, 0, "\"%s\"", etag
);
114 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
115 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
117 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
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
)
129 char *sent_header
= NULL
;
131 if (SG(headers_sent
)) {
135 if (cc_len
&& (SUCCESS
!= http_send_cache_control(cache_control
, cc_len
))) {
139 if (SUCCESS
!= http_send_last_modified_ex(send_modified
, &sent_header
)) {
143 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
144 http_exit_ex(304, sent_header
, NULL
, 0);
146 STR_FREE(sent_header
);
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
)
157 char *sent_header
= NULL
;
159 if (SG(headers_sent
)) {
163 if (cc_len
&& (SUCCESS
!= http_send_cache_control(cache_control
, cc_len
))) {
168 if (SUCCESS
!= http_send_etag_ex(etag
, etag_len
, &sent_header
)) {
171 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
172 http_exit_ex(304, sent_header
, NULL
, 0);
174 STR_FREE(sent_header
);
179 /* start ob_etaghandler */
180 return http_start_ob_etaghandler();
184 PHP_HTTP_API STATUS
_http_start_ob_etaghandler(TSRMLS_D
)
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");
192 HTTP_G(etag
).started
= 1;
193 return php_start_ob_buffer_named("ob_etaghandler", HTTP_G(send
).buffer_size
, 1 TSRMLS_CC
);
196 PHP_HTTP_API zend_bool
_http_interrupt_ob_etaghandler(TSRMLS_D
)
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
;
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
)
214 *handled_output_len
= output_len
;
215 *handled_output
= estrndup(output
, output_len
);
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();
225 http_etag_update(HTTP_G(etag
).ctx
, output
, output_len
);
228 if (mode
& PHP_OUTPUT_HANDLER_END
) {
229 char *sent_header
= NULL
;
230 char *etag
= http_etag_finish(HTTP_G(etag
).ctx
);
232 HTTP_G(etag
).ctx
= NULL
;
234 http_send_cache_control(HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
));
235 http_send_etag_ex(etag
, strlen(etag
), &sent_header
);
237 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
238 http_exit_ex(304, sent_header
, etag
, 0);
241 STR_FREE(sent_header
);
253 * vim600: sw=4 ts=4 fdm=marker