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_date_api.h"
31 #include "php_http_send_api.h"
32 #include "php_http_headers_api.h"
33 #include "php_http_date_api.h"
34 #include "php_http_cache_api.h"
36 ZEND_EXTERN_MODULE_GLOBALS(http
);
38 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
39 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t, http_send_mode) */
40 static STATUS
_http_send_chunk(const void *data
, size_t begin
, size_t end
, http_send_mode mode TSRMLS_DC
)
42 long len
= end
- begin
;
50 php_stream
*s
= (php_stream
*) data
;
52 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
56 buf
= (char *) ecalloc(1, HTTP_SENDBUF_SIZE
);
57 /* read into buf and write out */
58 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
59 if (!(read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
))) {
63 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
67 /* ob_flush() && flush() */
68 php_end_ob_buffer(1, 1 TSRMLS_CC
);
72 /* read & write left over */
74 if (read
= php_stream_read(s
, buf
, HTTP_SENDBUF_SIZE
+ len
)) {
75 if (read
- php_body_write(buf
, read TSRMLS_CC
)) {
83 /* ob_flush() & flush() */
84 php_end_ob_buffer(1, 1 TSRMLS_CC
);
93 char *s
= (char *) data
+ begin
;
95 while ((len
-= HTTP_SENDBUF_SIZE
) >= 0) {
96 if (HTTP_SENDBUF_SIZE
- php_body_write(s
, HTTP_SENDBUF_SIZE TSRMLS_CC
)) {
99 s
+= HTTP_SENDBUF_SIZE
;
100 /* ob_flush() & flush() */
101 php_end_ob_buffer(1, 1 TSRMLS_CC
);
102 sapi_flush(TSRMLS_C
);
105 /* write left over */
107 if (HTTP_SENDBUF_SIZE
+ len
- php_body_write(s
, HTTP_SENDBUF_SIZE
+ len TSRMLS_CC
)) {
110 /* ob_flush() & flush() */
111 php_end_ob_buffer(1, 1 TSRMLS_CC
);
112 sapi_flush(TSRMLS_C
);
125 /* {{{ STATUS http_send_status_header(int, char *) */
126 PHP_HTTP_API STATUS
_http_send_status_header(int status
, const char *header TSRMLS_DC
)
129 sapi_header_line h
= {(char *) header
, header
? strlen(header
) : 0, status
};
130 if (SUCCESS
!= (ret
= sapi_header_op(SAPI_HEADER_REPLACE
, &h TSRMLS_CC
))) {
131 http_error_ex(E_WARNING
, HTTP_E_HEADER
, "Could not send header: %s (%d)", header
, status
);
137 /* {{{ STATUS http_send_last_modified(int) */
138 PHP_HTTP_API STATUS
_http_send_last_modified(time_t t TSRMLS_DC
)
141 if (date
= http_date(t
)) {
142 char modified
[96] = "Last-Modified: ";
143 strcat(modified
, date
);
149 return http_send_header(modified
);
155 /* {{{ STATUS http_send_etag(char *, size_t) */
156 PHP_HTTP_API STATUS
_http_send_etag(const char *etag
, size_t etag_len TSRMLS_DC
)
162 http_error_ex(E_WARNING
, HTTP_E_HEADER
, "Attempt to send empty ETag (previous: %s)\n", HTTP_G(etag
));
170 HTTP_G(etag
) = estrdup(etag
);
172 etag_header
= ecalloc(1, sizeof("ETag: \"\"") + etag_len
);
173 sprintf(etag_header
, "ETag: \"%s\"", etag
);
174 status
= http_send_header(etag_header
);
180 /* {{{ STATUS http_send_cache_control(char *, size_t) */
181 PHP_HTTP_API STATUS
_http_send_cache_control(const char *cache_control
, size_t cc_len TSRMLS_DC
)
184 char *cc_header
= ecalloc(1, sizeof("Cache-Control: ") + cc_len
);
186 sprintf(cc_header
, "Cache-Control: %s", cache_control
);
187 status
= http_send_header(cc_header
);
193 /* {{{ STATUS http_send_content_type(char *, size_t) */
194 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
, size_t ct_len TSRMLS_DC
)
199 if (!strchr(content_type
, '/')) {
200 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
);
204 /* remember for multiple ranges */
206 efree(HTTP_G(ctype
));
208 HTTP_G(ctype
) = estrndup(content_type
, ct_len
);
210 ct_header
= ecalloc(1, sizeof("Content-Type: ") + ct_len
);
211 sprintf(ct_header
, "Content-Type: %s", content_type
);
212 status
= http_send_header(ct_header
);
218 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
219 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
, size_t f_len
, zend_bool send_inline TSRMLS_DC
)
225 cd_header
= ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len
);
226 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
228 cd_header
= ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
);
229 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
232 status
= http_send_header(cd_header
);
238 /* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
239 PHP_HTTP_API STATUS
_http_send_ranges(HashTable
*ranges
, const void *data
, size_t size
, http_send_mode mode TSRMLS_DC
)
245 if (zend_hash_num_elements(ranges
) == 1) {
246 char range_header
[256] = {0};
248 if (SUCCESS
!= zend_hash_index_find(ranges
, 0, (void **) &zrange
) ||
249 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
250 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
254 /* Send HTTP 206 Partial Content */
255 http_send_status(206);
257 /* send content range header */
258 snprintf(range_header
, 255, "Content-Range: bytes %d-%d/%d", **begin
, **end
, size
);
259 http_send_header(range_header
);
261 /* send requested chunk */
262 return http_send_chunk(data
, **begin
, **end
+ 1, mode
);
267 char bound
[23] = {0}, preface
[1024] = {0},
268 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
270 /* Send HTTP 206 Partial Content */
271 http_send_status(206);
273 /* send multipart/byteranges header */
274 snprintf(bound
, 22, "--%d%0.9f", time(NULL
), php_combined_lcg(TSRMLS_C
));
275 strncat(multi_header
, bound
+ 2, 21);
276 http_send_header(multi_header
);
278 /* send each requested chunk */
279 FOREACH_HASH_VAL(ranges
, zrange
) {
280 if (SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &begin
) ||
281 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &end
)) {
285 snprintf(preface
, 1023,
287 HTTP_CRLF
"Content-Type: %s"
288 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%ld"
293 HTTP_G(ctype
) ? HTTP_G(ctype
) : "application/x-octetstream",
299 php_body_write(preface
, strlen(preface
) TSRMLS_CC
);
300 http_send_chunk(data
, **begin
, **end
+ 1, mode
);
303 /* write boundary once more */
304 php_body_write(HTTP_CRLF
, sizeof(HTTP_CRLF
) - 1 TSRMLS_CC
);
305 php_body_write(bound
, strlen(bound
) TSRMLS_CC
);
306 php_body_write("--", 2 TSRMLS_CC
);
313 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
314 PHP_HTTP_API STATUS
_http_send(const void *data_ptr
, size_t data_size
, http_send_mode data_mode TSRMLS_DC
)
317 http_range_status range_status
;
327 /* stop on-the-fly etag generation */
328 if (cache_etag
= HTTP_G(etag_started
)) {
329 /* interrupt ob_etaghandler */
330 HTTP_G(etag_started
) = 0;
333 /* enable partial dl and resume */
334 http_send_header("Accept-Ranges: bytes");
336 zend_hash_init(&ranges
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
337 range_status
= http_get_request_ranges(&ranges
, data_size
);
339 if (range_status
== RANGE_ERR
) {
340 zend_hash_destroy(&ranges
);
341 http_send_status(416);
345 /* Range Request - only send ranges if entity hasn't changed */
346 if ( range_status
== RANGE_OK
&&
347 http_match_etag_ex("HTTP_IF_MATCH", HTTP_G(etag
), 0) &&
348 http_match_last_modified_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(lmod
), 0)) {
349 STATUS result
= http_send_ranges(&ranges
, data_ptr
, data_size
, data_mode
);
350 zend_hash_destroy(&ranges
);
354 zend_hash_destroy(&ranges
);
356 /* send 304 Not Modified if etag matches */
360 if (!(etag
= http_etag(data_ptr
, data_size
, data_mode
))) {
363 if (SUCCESS
!= http_send_etag(etag
, 32)) {
367 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
368 return http_cache_exit_ex(etag
, 1, 1);
373 /* send 304 Not Modified if last modified matches */
374 if (http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G(lmod
))) {
375 return http_cache_exit_ex(http_date(HTTP_G(lmod
)), 0, 1);
378 /* send full entity */
379 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
383 /* {{{ STATUS http_send_stream(php_stream *) */
384 PHP_HTTP_API STATUS
_http_send_stream_ex(php_stream
*file
, zend_bool close_stream TSRMLS_DC
)
388 if ((!file
) || php_stream_stat(file
, &HTTP_G(ssb
))) {
392 status
= http_send(file
, HTTP_G(ssb
).sb
.st_size
, SEND_RSRC
);
395 php_stream_close(file
);
408 * vim600: sw=4 ts=4 fdm=marker