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/php_lcg.h"
28 #include "php_http_std_defs.h"
29 #include "php_http_api.h"
30 #include "php_http_send_api.h"
31 #include "php_http_headers_api.h"
32 #include "php_http_date_api.h"
33 #include "php_http_cache_api.h"
35 ZEND_EXTERN_MODULE_GLOBALS(http
);
37 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
38 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t, http_send_mode) */
39 static STATUS
_http_send_chunk(const void *data
, size_t begin
, size_t end
, http_send_mode mode TSRMLS_DC
)
41 long len
= end
- begin
;
49 php_stream
*s
= (php_stream
*) data
;
51 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
55 buf
= (char *) ecalloc(1, HTTP_SENDBUF_SIZE
);
56 /* read into buf and write out */
57 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
58 if (!(read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
))) {
62 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
66 /* ob_flush() && flush() */
67 php_end_ob_buffer(1, 1 TSRMLS_CC
);
71 /* read & write left over */
73 if (read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
+ len
)) {
74 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
82 /* ob_flush() & flush() */
83 php_end_ob_buffer(1, 1 TSRMLS_CC
);
92 char *s
= (char *) data
+ begin
;
94 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
95 if (HTTP_SENDBUF_SIZE
- php_body_write(s
, HTTP_SENDBUF_SIZE TSRMLS_CC
)) {
98 s
+= HTTP_SENDBUF_SIZE
;
99 /* ob_flush() & flush() */
100 php_end_ob_buffer(1, 1 TSRMLS_CC
);
101 sapi_flush(TSRMLS_C
);
104 /* write left over */
106 if (HTTP_SENDBUF_SIZE
+ len
- php_body_write(s
, HTTP_SENDBUF_SIZE
+ len TSRMLS_CC
)) {
109 /* ob_flush() & flush() */
110 php_end_ob_buffer(1, 1 TSRMLS_CC
);
111 sapi_flush(TSRMLS_C
);
124 /* {{{ STATUS http_send_status_header(int, char *) */
125 PHP_HTTP_API STATUS
_http_send_status_header(int status
, const char *header TSRMLS_DC
)
128 sapi_header_line h
= {(char *) header
, strlen(header
), status
};
129 if (SUCCESS
!= (ret
= sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
))) {
130 http_error_ex(E_WARNING
, HTTP_E_HEADER
, "Could not send header: %s (%d)", header
, status
);
136 /* {{{ STATUS http_send_last_modified(int) */
137 PHP_HTTP_API STATUS
_http_send_last_modified(time_t t TSRMLS_DC
)
140 if (date
= http_date(t
)) {
141 char modified
[96] = "Last-Modified: ";
142 strcat(modified
, date
);
148 return http_send_header(modified
);
154 /* {{{ STATUS http_send_etag(char *, size_t) */
155 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
, size_t etag_len TSRMLS_DC
)
161 http_error_ex(E_WARNING
, HTTP_E_HEADER
, "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag
));
169 HTTP_G(etag
) = estrdup(etag
);
171 etag_header
= ecalloc(1, sizeof("ETag: \"\"") + etag_len
);
172 sprintf(etag_header
, "ETag: \"%s\"", etag
);
173 status
= http_send_header(etag_header
);
179 /* {{{ STATUS http_send_cache_control(char *, size_t) */
180 PHP_HTTP_API STATUS
_http_send_cache_control(const char *cache_control
, size_t cc_len TSRMLS_DC
)
183 char *cc_header
= ecalloc(1, sizeof("Cache-Control: ") + cc_len
);
185 sprintf(cc_header
, "Cache-Control: %s", cache_control
);
186 status
= http_send_header(cc_header
);
192 /* {{{ STATUS http_send_content_type(char *, size_t) */
193 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
, size_t ct_len TSRMLS_DC
)
198 if (!strchr(content_type
, '/')) {
199 http_error_ex(E_WARNING
, HTTP_E_PARAM
, "Content-Type '%s' doesn't seem to consist of a primary and a secondary part", content_type
);
203 /* remember for multiple ranges */
205 efree(HTTP_G(ctype
));
207 HTTP_G(ctype
) = estrndup(content_type
, ct_len
);
209 ct_header
= ecalloc(1, sizeof("Content-Type: ") + ct_len
);
210 sprintf(ct_header
, "Content-Type: %s", content_type
);
211 status
= http_send_header(ct_header
);
217 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
218 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
, size_t f_len
, zend_bool send_inline TSRMLS_DC
)
224 cd_header
= ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len
);
225 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
227 cd_header
= ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
);
228 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
231 status
= http_send_header(cd_header
);
237 /* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
238 PHP_HTTP_API STATUS
_http_send_ranges(HashTable
*ranges
, const void *data
, size_t size
, http_send_mode mode TSRMLS_DC
)
244 if (zend_hash_num_elements(ranges
) == 1) {
245 char range_header
[256] = {0};
247 if (SUCCESS
!= zend_hash_index_find(ranges
, 0, (void **) &zrange
) ||
248 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
249 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
253 /* Send HTTP 206 Partial Content */
254 http_send_status(206);
256 /* send content range header */
257 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
258 http_send_header(range_header
);
260 /* send requested chunk */
261 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
266 char bound
[23] = {0}, preface
[1024] = {0},
267 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
269 /* Send HTTP 206 Partial Content */
270 http_send_status(206);
272 /* send multipart/byteranges header */
273 snprintf(bound
, 22, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
274 strncat(multi_header
, bound
+ 2, 21);
275 http_send_header(multi_header
);
277 /* send each requested chunk */
278 FOREACH_HASH_VAL(ranges
, zrange
) {
279 if (SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
280 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
284 snprintf(preface
, 1023,
286 HTTP_CRLF
"Content-Type: %s"
287 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%ld"
292 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
298 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
299 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
302 /* write boundary once more */
303 php_body_write(HTTP_CRLF
, sizeof(HTTP_CRLF
) - 1 TSRMLS_CC
);
304 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
305 php_body_write("--", 2 TSRMLS_CC
);
312 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
313 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, size_t data_size
, http_send_mode data_mode TSRMLS_DC
)
316 http_range_status range_status
;
326 /* stop on-the-fly etag generation */
327 if (cache_etag
= HTTP_G(etag_started
)) {
328 /* interrupt ob_etaghandler */
329 HTTP_G(etag_started
) = 0;
332 zend_hash_init(&ranges
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
333 range_status
= http_get_request_ranges(&ranges
, data_size
);
335 if (range_status
== RANGE_ERR
) {
336 zend_hash_destroy(&ranges
);
337 http_send_status(416);
341 /* Range Request - only send ranges if entity hasn't changed */
342 if ( range_status
== RANGE_OK
&&
343 http_match_etag_ex("HTTP_IF_MATCH", HTTP_G(etag
), 0) &&
344 http_match_last_modified_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
), 0)) {
345 STATUS result
= http_send_ranges(&ranges
, data_ptr
, data_size
, data_mode
);
346 zend_hash_destroy(&ranges
);
350 zend_hash_destroy(&ranges
);
352 /* send 304 Not Modified if etag matches */
356 if (!(etag
= http_etag(data_ptr
, data_size
, data_mode
))) {
359 if (SUCCESS
!= http_send_etag(etag
, 32)) {
363 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
365 return http_cache_exit();
370 /* send 304 Not Modified if last modified matches */
371 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
372 return http_cache_exit();
375 /* send full entity */
376 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
380 /* {{{ STATUS http_send_stream(php_stream *) */
381 PHP_HTTP_API STATUS
_http_send_stream_ex(php_stream
*file
, zend_bool close_stream TSRMLS_DC
)
385 if ((!file
) || php_stream_stat(file
, &HTTP_G(ssb
))) {
389 status
= http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
392 php_stream_close(file
);
405 * vim600: sw=4 ts=4 fdm=marker