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-2010, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
15 #define HTTP_WANT_SAPI
18 #include "php_output.h"
19 #include "php_streams.h"
21 #include "php_http_api.h"
22 #include "php_http_cache_api.h"
23 #include "php_http_date_api.h"
24 #include "php_http_send_api.h"
26 /* {{{ char *http_etag(void *, size_t, http_send_mode) */
27 PHP_HTTP_API
char *_http_etag(const void *data_ptr
, size_t data_len
, http_send_mode data_mode TSRMLS_DC
)
29 void *ctx
= http_etag_init();
31 if (data_mode
== SEND_DATA
) {
32 http_etag_update(ctx
, data_ptr
, data_len
);
35 php_stream_statbuf ssb
;
37 if (data_mode
== SEND_RSRC
) {
38 ss
= php_stream_stat((php_stream
*) data_ptr
, &ssb
);
40 ss
= php_stream_stat_path((char *) data_ptr
, &ssb
);
50 ssb_len
= snprintf(ssb_buf
, sizeof(ssb_buf
), "%ld=%ld=%ld", (long) ssb
.sb
.st_mtime
,
52 (long) ssb
.sb
.st_size
);
53 http_etag_update(ctx
, ssb_buf
, ssb_len
);
57 return http_etag_finish(ctx
);
61 /* {{{ time_t http_last_modified(void *, http_send_mode) */
62 PHP_HTTP_API
time_t _http_last_modified(const void *data_ptr
, http_send_mode data_mode TSRMLS_DC
)
64 php_stream_statbuf ssb
;
67 case SEND_DATA
: return HTTP_G
->request
.time
;
68 case SEND_RSRC
: return php_stream_stat((php_stream
*) data_ptr
, &ssb
) ? 0 : ssb
.sb
.st_mtime
;
69 default: return php_stream_stat_path((char *) data_ptr
, &ssb
) ? 0 : ssb
.sb
.st_mtime
;
74 /* {{{ zend_bool http_match_last_modified(char *, time_t) */
75 PHP_HTTP_API zend_bool
_http_match_last_modified_ex(const char *entry
, time_t t
, zend_bool enforce_presence TSRMLS_DC
)
79 char *modified
, *chr_ptr
;
81 if (!(zmodified
= http_get_server_var(entry
, 1))) {
82 return !enforce_presence
;
85 modified
= estrndup(Z_STRVAL_P(zmodified
), Z_STRLEN_P(zmodified
));
86 if ((chr_ptr
= strrchr(modified
, ';'))) {
90 retval
= (t
<= http_parse_date_ex(modified
, 1));
96 /* {{{ zend_bool http_match_etag(char *, char *) */
97 PHP_HTTP_API zend_bool
_http_match_etag_ex(const char *entry
, const char *etag
, zend_bool enforce_presence TSRMLS_DC
)
103 if (!(zetag
= http_get_server_var_ex(entry
, strlen(entry
)+1, 1))) {
104 return !enforce_presence
;
107 if (NULL
!= strchr(Z_STRVAL_P(zetag
), '*')) {
111 spprintf("ed_etag
, 0, "\"%s\"", etag
);
112 if (!strchr(Z_STRVAL_P(zetag
), ',')) {
113 result
= !strcmp(Z_STRVAL_P(zetag
), quoted_etag
);
115 result
= (NULL
!= strstr(Z_STRVAL_P(zetag
), quoted_etag
));
123 /* {{{ STATUS http_cache_last_modified(time_t, time_t, char *, size_t) */
124 PHP_HTTP_API STATUS
_http_cache_last_modified(time_t last_modified
,
125 time_t send_modified
, const char *cache_control
, size_t cc_len TSRMLS_DC
)
127 char *sent_header
= NULL
;
129 if (SG(headers_sent
)) {
133 if (cc_len
&& (SUCCESS
!= http_send_cache_control(cache_control
, cc_len
))) {
137 if (SUCCESS
!= http_send_last_modified_ex(send_modified
, &sent_header
)) {
141 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", last_modified
)) {
142 http_exit_ex(304, sent_header
, NULL
, 0);
144 STR_FREE(sent_header
);
151 /* {{{ STATUS http_cache_etag(char *, size_t, char *, size_t) */
152 PHP_HTTP_API STATUS
_http_cache_etag(const char *etag
, size_t etag_len
,
153 const char *cache_control
, size_t cc_len TSRMLS_DC
)
155 char *sent_header
= NULL
;
157 if (SG(headers_sent
)) {
161 if (cc_len
&& (SUCCESS
!= http_send_cache_control(cache_control
, cc_len
))) {
166 if (SUCCESS
!= http_send_etag_ex(etag
, etag_len
, &sent_header
)) {
169 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
170 http_exit_ex(304, sent_header
, NULL
, 0);
172 STR_FREE(sent_header
);
177 /* start ob_etaghandler */
178 return http_start_ob_etaghandler();
182 PHP_HTTP_API STATUS
_http_start_ob_etaghandler(TSRMLS_D
)
184 /* already running? */
185 #ifdef PHP_OUTPUT_NEWAPI
188 if (php_output_handler_conflict(ZEND_STRL("ob_etaghandler"), ZEND_STRL("ob_etaghandler") TSRMLS_CC
)) {
192 if (php_ob_handler_used("ob_etaghandler" TSRMLS_CC
)) {
193 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "ob_etaghandler can only be used once");
197 HTTP_G
->etag
.started
= 1;
198 #ifdef PHP_OUTPUT_NEWAPI
199 return php_output_start_internal(ZEND_STRL("ob_etaghandler"), _http_ob_etaghandler
, HTTP_G
->send
.buffer_size
, 0 TSRMLS_CC
);
201 return php_start_ob_buffer_named("ob_etaghandler", HTTP_G
->send
.buffer_size
, 0 TSRMLS_CC
);
205 PHP_HTTP_API zend_bool
_http_interrupt_ob_etaghandler(TSRMLS_D
)
207 if (HTTP_G
->etag
.started
) {
208 HTTP_G
->etag
.started
= 0;
209 if (HTTP_G
->etag
.ctx
) {
210 efree(HTTP_G
->etag
.ctx
);
211 HTTP_G
->etag
.ctx
= NULL
;
218 /* {{{ void http_ob_etaghandler(char *, uint, char **, uint *, int) */
219 void _http_ob_etaghandler(char *output
, uint output_len
,
220 char **handled_output
, uint
*handled_output_len
, int mode TSRMLS_DC
)
223 *handled_output_len
= output_len
;
224 *handled_output
= estrndup(output
, output_len
);
226 /* are we supposed to run? */
227 if (HTTP_G
->etag
.started
) {
228 /* initialize the etag context */
229 if (mode
& PHP_OUTPUT_HANDLER_START
) {
230 HTTP_G
->etag
.ctx
= http_etag_init();
234 http_etag_update(HTTP_G
->etag
.ctx
, output
, output_len
);
237 if (mode
& PHP_OUTPUT_HANDLER_END
) {
238 char *sent_header
= NULL
;
239 char *etag
= http_etag_finish(HTTP_G
->etag
.ctx
);
241 HTTP_G
->etag
.ctx
= NULL
;
243 http_send_cache_control(HTTP_DEFAULT_CACHECONTROL
, lenof(HTTP_DEFAULT_CACHECONTROL
));
244 http_send_etag_ex(etag
, strlen(etag
), &sent_header
);
246 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
247 /* force exit; ob within ob does not work */
248 HTTP_G
->force_exit
= 1;
249 http_exit_ex(304, sent_header
, etag
, 0);
252 STR_FREE(sent_header
);
264 * vim600: sw=4 ts=4 fdm=marker