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 +----------------------------------------------------------------------+
24 #include "php_streams.h"
25 #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_flush() _http_flush(TSRMLS_C)
39 /* {{{ static inline void http_flush() */
40 static inline void _http_flush(TSRMLS_D
)
42 php_end_ob_buffer(1, 1 TSRMLS_CC
);
47 #define http_sleep() _http_sleep(TSRMLS_C)
48 /* {{{ static inline void http_sleep() */
49 static inline void _http_sleep(TSRMLS_D
)
51 #define HTTP_MSEC(s) (s * 1000)
52 #define HTTP_USEC(s) (HTTP_MSEC(s) * 1000)
53 #define HTTP_NSEC(s) (HTTP_USEC(s) * 1000)
54 #define HTTP_NANOSEC (1000 * 1000 * 1000)
55 #define HTTP_DIFFSEC (0.001)
57 if (HTTP_G(send
).throttle_delay
>= HTTP_DIFFSEC
) {
58 #if defined(PHP_WIN32)
59 Sleep((DWORD
) HTTP_MSEC(HTTP_G(send
).throttle_delay
));
60 #elif defined(HAVE_USLEEP)
61 usleep(HTTP_USEC(HTTP_G(send
).throttle_delay
));
62 #elif defined(HAVE_NANOSLEEP)
63 struct timespec req
, rem
;
65 req
.tv_sec
= (time_t) HTTP_G(send
).throttle_delay
;
66 req
.tv_nsec
= HTTP_NSEC(HTTP_G(send
).throttle_delay
) % HTTP_NANOSEC
;
68 while (nanosleep(&req
, &rem
) && (errno
== EINTR
) && (HTTP_NSEC(rem
.tv_sec
) + rem
.tv_nsec
) > HTTP_NSEC(HTTP_DIFFSEC
))) {
69 req
.tv_sec
= rem
.tv_sec
;
70 req
.tv_nsec
= rem
.tv_nsec
;
77 #define HTTP_CHUNK_AVAIL(len) ((len -= HTTP_G(send).buffer_size) >= 0)
78 #define HTTP_CHUNK_WRITE(data, l, dofree, dosleep) \
80 long size = (long) l; \
82 if ((1 > size) || (size - PHPWRITE(data, size))) { \
95 #define http_send_chunk(d, b, e, m) _http_send_chunk((d), (b), (e), (m) TSRMLS_CC)
96 /* {{{ static STATUS http_send_chunk(const void *, size_t, size_t, http_send_mode) */
97 static STATUS
_http_send_chunk(const void *data
, size_t begin
, size_t end
, http_send_mode mode TSRMLS_DC
)
99 long len
= end
- begin
;
106 php_stream
*s
= (php_stream
*) data
;
108 if (php_stream_seek(s
, begin
, SEEK_SET
)) {
112 buf
= emalloc(HTTP_G(send
).buffer_size
);
114 while (HTTP_CHUNK_AVAIL(len
)) {
115 HTTP_CHUNK_WRITE(buf
, php_stream_read(s
, buf
, HTTP_G(send
).buffer_size
), 1, 1);
118 /* read & write left over */
120 HTTP_CHUNK_WRITE(buf
, php_stream_read(s
, buf
, HTTP_G(send
).buffer_size
+ len
), 1, 0);
129 char *s
= (char *) data
+ begin
;
131 while (HTTP_CHUNK_AVAIL(len
)) {
132 HTTP_CHUNK_WRITE(s
, HTTP_G(send
).buffer_size
, 0, 1);
133 s
+= HTTP_G(send
).buffer_size
;
136 /* write left over */
138 HTTP_CHUNK_WRITE(s
, HTTP_G(send
).buffer_size
+ len
, 0, 0);
151 /* {{{ STATUS http_send_header(char *, char *, zend_bool) */
152 PHP_HTTP_API STATUS
_http_send_header_ex(const char *name
, size_t name_len
, const char *value
, size_t value_len
, zend_bool replace
, char **sent_header TSRMLS_DC
)
155 size_t header_len
= sizeof(": ") + name_len
+ value_len
+ 1;
156 char *header
= emalloc(header_len
+ 1);
158 header
[header_len
] = '\0';
159 header_len
= snprintf(header
, header_len
, "%s: %s", name
, value
);
160 ret
= http_send_header_string_ex(header
, header_len
, replace
);
162 *sent_header
= header
;
170 /* {{{ STATUS http_send_status_header(int, char *) */
171 PHP_HTTP_API STATUS
_http_send_status_header_ex(int status
, const char *header
, size_t header_len
, zend_bool replace TSRMLS_DC
)
174 sapi_header_line h
= {(char *) header
, header_len
, status
};
175 if (SUCCESS
!= (ret
= sapi_header_op(replace
? SAPI_HEADER_REPLACE
: SAPI_HEADER_ADD
, &h TSRMLS_CC
))) {
176 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Could not send header: %s (%d)", header
, status
);
182 /* {{{ STATUS http_send_last_modified(int) */
183 PHP_HTTP_API STATUS
_http_send_last_modified_ex(time_t t
, char **sent_header TSRMLS_DC
)
186 char *date
= http_date(t
);
192 ret
= http_send_header_ex("Last-Modified", lenof("Last-Modified"), date
, strlen(date
), 1, sent_header
);
196 HTTP_G(send
).last_modified
= t
;
202 /* {{{ STATUS http_send_etag(char *, size_t) */
203 PHP_HTTP_API STATUS
_http_send_etag_ex(const char *etag
, size_t etag_len
, char **sent_header TSRMLS_DC
)
209 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Attempt to send empty ETag (previous: %s)\n", HTTP_G(send
).unquoted_etag
);
214 STR_SET(HTTP_G(send
).unquoted_etag
, estrndup(etag
, etag_len
));
216 etag_len
= spprintf(&etag_header
, 0, "ETag: \"%s\"", etag
);
217 status
= http_send_header_string_ex(etag_header
, etag_len
, 1);
220 *sent_header
= etag_header
;
229 /* {{{ STATUS http_send_content_type(char *, size_t) */
230 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
, size_t ct_len TSRMLS_DC
)
232 HTTP_CHECK_CONTENT_TYPE(content_type
, return FAILURE
);
234 /* remember for multiple ranges */
235 STR_FREE(HTTP_G(send
).content_type
);
236 HTTP_G(send
).content_type
= estrndup(content_type
, ct_len
);
238 return http_send_header_ex("Content-Type", lenof("Content-Type"), content_type
, ct_len
, 1, NULL
);
242 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
243 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
, size_t f_len
, zend_bool send_inline TSRMLS_DC
)
249 cd_header
= ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len
);
250 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
252 cd_header
= ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
);
253 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
256 status
= http_send_header_string(cd_header
);
262 /* {{{ STATUS http_send_ranges(HashTable *, void *, size_t, http_send_mode) */
263 PHP_HTTP_API STATUS
_http_send_ranges(HashTable
*ranges
, const void *data
, size_t size
, http_send_mode mode TSRMLS_DC
)
265 zval
**zbegin
, **zend
, **zrange
;
268 if (zend_hash_num_elements(ranges
) == 1) {
269 char range_header
[256] = {0};
271 if (SUCCESS
!= zend_hash_index_find(ranges
, 0, (void **) &zrange
) ||
272 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &zbegin
) ||
273 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &zend
)) {
274 http_send_status(500);
278 /* Send HTTP 206 Partial Content */
279 http_send_status(206);
281 /* send content range header */
282 snprintf(range_header
, 255, "Content-Range: bytes %ld-%ld/%lu", Z_LVAL_PP(zbegin
), Z_LVAL_PP(zend
), (ulong
) size
);
283 http_send_header_string(range_header
);
285 /* send requested chunk */
286 return http_send_chunk(data
, Z_LVAL_PP(zbegin
), Z_LVAL_PP(zend
) + 1, mode
);
292 char bound
[23] = {0}, preface
[1024] = {0},
293 multi_header
[68] = "Content-Type: multipart/byteranges; boundary=";
295 /* Send HTTP 206 Partial Content */
296 http_send_status(206);
298 /* send multipart/byteranges header */
299 snprintf(bound
, 22, "--%lu%0.9f", (ulong
) time(NULL
), php_combined_lcg(TSRMLS_C
));
300 strncat(multi_header
, bound
+ 2, 21);
301 http_send_header_string(multi_header
);
303 /* send each requested chunk */
304 FOREACH_HASH_VAL(ranges
, zrange
) {
305 if (SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 0, (void **) &zbegin
) ||
306 SUCCESS
!= zend_hash_index_find(Z_ARRVAL_PP(zrange
), 1, (void **) &zend
)) {
310 preface_len
= snprintf(preface
, 1023,
312 HTTP_CRLF
"Content-Type: %s"
313 HTTP_CRLF
"Content-Range: bytes %ld-%ld/%lu"
318 HTTP_G(send
).content_type
? HTTP_G(send
).content_type
: "application/x-octetstream",
324 PHPWRITE(preface
, preface_len
);
325 http_send_chunk(data
, Z_LVAL_PP(zbegin
), Z_LVAL_PP(zend
) + 1, mode
);
328 /* write boundary once more */
329 PHPWRITE(HTTP_CRLF
, lenof(HTTP_CRLF
));
330 PHPWRITE(bound
, strlen(bound
));
331 PHPWRITE("--", lenof("--"));
338 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
339 PHP_HTTP_API STATUS
_http_send_ex(const void *data_ptr
, size_t data_size
, http_send_mode data_mode
, zend_bool no_cache TSRMLS_DC
)
342 http_range_status range_status
;
352 /* stop on-the-fly etag generation */
353 cache_etag
= http_interrupt_ob_etaghandler();
355 /* enable partial dl and resume */
356 http_send_header_string("Accept-Ranges: bytes");
358 zend_hash_init(&ranges
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
359 range_status
= http_get_request_ranges(&ranges
, data_size
);
361 if (range_status
== RANGE_ERR
) {
362 zend_hash_destroy(&ranges
);
363 http_send_status(416);
367 /* Range Request - only send ranges if entity hasn't changed */
368 if ( range_status
== RANGE_OK
&&
369 http_match_etag_ex("HTTP_IF_MATCH", HTTP_G(send
).unquoted_etag
, 0) &&
370 http_match_last_modified_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(send
).last_modified
, 0) &&
371 http_match_last_modified_ex("HTTP_UNLESS_MODIFIED_SINCE", HTTP_G(send
).last_modified
, 0)) {
372 STATUS result
= http_send_ranges(&ranges
, data_ptr
, data_size
, data_mode
);
373 zend_hash_destroy(&ranges
);
377 zend_hash_destroy(&ranges
);
379 /* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */
380 if (!no_cache
&& cache_etag
) {
383 if (etag
= http_etag(data_ptr
, data_size
, data_mode
)) {
384 char *sent_header
= NULL
;
386 http_send_etag_ex(etag
, strlen(etag
), &sent_header
);
387 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
388 return http_exit_ex(304, sent_header
, NULL
, 0);
390 STR_FREE(sent_header
);
396 /* send 304 Not Modified if last modified matches */
397 if (!no_cache
&& http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G(send
).last_modified
)) {
398 char *sent_header
= NULL
;
399 http_send_last_modified_ex(HTTP_G(send
).last_modified
, &sent_header
);
400 return http_exit_ex(304, sent_header
, NULL
, 0);
403 /* emit a content-length header */
404 if (!php_ob_handler_used("ob_gzhandler" TSRMLS_CC
)) {
406 spprintf(&cl
, 0, "Content-Length: %lu", (unsigned long) data_size
);
407 http_send_header_string(cl
);
410 /* send full entity */
411 return http_send_chunk(data_ptr
, 0, data_size
, data_mode
);
415 /* {{{ STATUS http_send_stream(php_stream *) */
416 PHP_HTTP_API STATUS
_http_send_stream_ex(php_stream
*file
, zend_bool close_stream
, zend_bool no_cache TSRMLS_DC
)
419 php_stream_statbuf ssb
;
421 if ((!file
) || php_stream_stat(file
, &ssb
)) {
425 status
= http_send_ex(file
, ssb
.sb
.st_size
, SEND_RSRC
, no_cache
);
428 php_stream_close(file
);
440 * vim600: sw=4 ts=4 fdm=marker