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 +--------------------------------------------------------------------+
19 #define HTTP_WANT_ZLIB
20 #define HTTP_WANT_MAGIC
24 #include "php_streams.h"
25 #include "ext/standard/php_lcg.h"
27 #include "php_http_api.h"
28 #include "php_http_cache_api.h"
29 #include "php_http_date_api.h"
30 #include "php_http_encoding_api.h"
31 #include "php_http_headers_api.h"
32 #include "php_http_send_api.h"
34 ZEND_EXTERN_MODULE_GLOBALS(http
);
36 #define http_flush(d, l) _http_flush((d), (l) TSRMLS_CC)
37 /* {{{ static inline void http_flush() */
38 static inline void _http_flush(const char *data
, size_t data_len TSRMLS_DC
)
40 PHPWRITE(data
, data_len
);
41 php_end_ob_buffer(1, 1 TSRMLS_CC
);
44 #define HTTP_MSEC(s) (s * 1000)
45 #define HTTP_USEC(s) (HTTP_MSEC(s) * 1000)
46 #define HTTP_NSEC(s) (HTTP_USEC(s) * 1000)
47 #define HTTP_NANOSEC (1000 * 1000 * 1000)
48 #define HTTP_DIFFSEC (0.001)
50 if (HTTP_G(send
).throttle_delay
>= HTTP_DIFFSEC
) {
51 #if defined(PHP_WIN32)
52 Sleep((DWORD
) HTTP_MSEC(HTTP_G(send
).throttle_delay
));
53 #elif defined(HAVE_USLEEP)
54 usleep(HTTP_USEC(HTTP_G(send
).throttle_delay
));
55 #elif defined(HAVE_NANOSLEEP)
56 struct timespec req
, rem
;
58 req
.tv_sec
= (time_t) HTTP_G(send
).throttle_delay
;
59 req
.tv_nsec
= HTTP_NSEC(HTTP_G(send
).throttle_delay
) % HTTP_NANOSEC
;
61 while (nanosleep(&req
, &rem
) && (errno
== EINTR
) && (HTTP_NSEC(rem
.tv_sec
) + rem
.tv_nsec
) > HTTP_NSEC(HTTP_DIFFSEC
))) {
62 req
.tv_sec
= rem
.tv_sec
;
63 req
.tv_nsec
= rem
.tv_nsec
;
70 /* {{{ http_send_response_start */
71 #define http_send_response_start(b, cl) _http_send_response_start((b), (cl) TSRMLS_CC)
72 static inline void _http_send_response_start(void **buffer
, size_t content_length TSRMLS_DC
)
76 if ((encoding
= http_encoding_response_start(content_length
))) {
79 *buffer
= http_encoding_deflate_stream_init(NULL
,
80 (encoding
== HTTP_ENCODING_GZIP
) ?
81 HTTP_DEFLATE_TYPE_GZIP
: HTTP_DEFLATE_TYPE_ZLIB
);
87 /* {{{ http_send_response_data_plain */
88 #define http_send_response_data_plain(b, d, dl) _http_send_response_data_plain((b), (d), (dl) TSRMLS_CC)
89 static inline void _http_send_response_data_plain(void **buffer
, const char *data
, size_t data_len TSRMLS_DC
)
91 if (HTTP_G(send
).gzip_encoding
) {
95 http_encoding_stream
*s
= *((http_encoding_stream
**) buffer
);
97 http_encoding_deflate_stream_update(s
, data
, data_len
, &encoded
, &encoded_len
);
98 phpstr_chunked_output((phpstr
**) &s
->storage
, encoded
, encoded_len
, HTTP_G(send
).buffer_size
, _http_flush TSRMLS_CC
);
101 http_error(HE_ERROR
, HTTP_E_RESPONSE
, "Attempt to send GZIP response despite being able to do so; please report this bug");
104 phpstr_chunked_output((phpstr
**) buffer
, data
, data_len
, HTTP_G(send
).buffer_size
, _http_flush TSRMLS_CC
);
109 #define HTTP_CHUNK_AVAIL(len, cs) ((len -= cs) >= 0)
110 /* {{{ http_send_response_data_fetch */
111 #define http_send_response_data_fetch(b, d, l, m, s, e) _http_send_response_data_fetch((b), (d), (l), (m), (s), (e) TSRMLS_CC)
112 static inline void _http_send_response_data_fetch(void **buffer
, const void *data
, size_t data_len
, http_send_mode mode
, size_t begin
, size_t end TSRMLS_DC
)
114 long len
= end
- begin
, chunk_size
= 40960;
120 php_stream
*s
= (php_stream
*) data
;
122 if (SUCCESS
== php_stream_seek(s
, begin
, SEEK_SET
)) {
123 char *buf
= emalloc(chunk_size
);
125 while (HTTP_CHUNK_AVAIL(len
, chunk_size
)) {
126 http_send_response_data_plain(buffer
, buf
, php_stream_read(s
, buf
, chunk_size
));
128 /* read & write left over */
130 http_send_response_data_plain(buffer
, buf
, php_stream_read(s
, buf
, chunk_size
+ len
));
140 char *s
= (char *) data
+ begin
;
142 while (HTTP_CHUNK_AVAIL(len
, chunk_size
)) {
143 http_send_response_data_plain(buffer
, s
, chunk_size
);
146 /* write left over */
148 http_send_response_data_plain(buffer
, s
, chunk_size
+ len
);
153 EMPTY_SWITCH_DEFAULT_CASE();
158 /* {{{ http_send_response_finish */
159 #define http_send_response_finish(b) _http_send_response_finish((b) TSRMLS_CC)
160 static inline void _http_send_response_finish(void **buffer TSRMLS_DC
)
162 if (HTTP_G(send
).gzip_encoding
) {
163 #ifdef HTTP_HAVE_ZLIB
164 char *encoded
= NULL
;
165 size_t encoded_len
= 0;
166 http_encoding_stream
*s
= *((http_encoding_stream
**) buffer
);
168 http_encoding_deflate_stream_finish(s
, &encoded
, &encoded_len
);
169 phpstr_chunked_output((phpstr
**) &s
->storage
, encoded
, encoded_len
, 0, _http_flush TSRMLS_CC
);
170 http_encoding_deflate_stream_free(&s
);
173 http_error(HE_ERROR
, HTTP_E_RESPONSE
, "Attempt to send GZIP response despite being able to do so; please report this bug");
176 phpstr_chunked_output((phpstr
**) buffer
, NULL
, 0, 0, _http_flush TSRMLS_CC
);
182 /* {{{ STATUS http_send_header(char *, char *, zend_bool) */
183 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
)
186 size_t header_len
= sizeof(": ") + name_len
+ value_len
+ 1;
187 char *header
= emalloc(header_len
+ 1);
189 header
[header_len
] = '\0';
190 header_len
= snprintf(header
, header_len
, "%s: %s", name
, value
);
191 ret
= http_send_header_string_ex(header
, header_len
, replace
);
193 *sent_header
= header
;
201 /* {{{ STATUS http_send_status_header(int, char *) */
202 PHP_HTTP_API STATUS
_http_send_status_header_ex(int status
, const char *header
, size_t header_len
, zend_bool replace TSRMLS_DC
)
205 sapi_header_line h
= {(char *) header
, header_len
, status
};
206 if (SUCCESS
!= (ret
= sapi_header_op(replace
? SAPI_HEADER_REPLACE
: SAPI_HEADER_ADD
, &h TSRMLS_CC
))) {
207 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Could not send header: %s (%d)", header
, status
);
213 /* {{{ STATUS http_send_last_modified(int) */
214 PHP_HTTP_API STATUS
_http_send_last_modified_ex(time_t t
, char **sent_header TSRMLS_DC
)
217 char *date
= http_date(t
);
223 ret
= http_send_header_ex("Last-Modified", lenof("Last-Modified"), date
, strlen(date
), 1, sent_header
);
227 HTTP_G(send
).last_modified
= t
;
233 /* {{{ STATUS http_send_etag(char *, size_t) */
234 PHP_HTTP_API STATUS
_http_send_etag_ex(const char *etag
, size_t etag_len
, char **sent_header TSRMLS_DC
)
240 http_error_ex(HE_WARNING
, HTTP_E_HEADER
, "Attempt to send empty ETag (previous: %s)\n", HTTP_G(send
).unquoted_etag
);
245 STR_SET(HTTP_G(send
).unquoted_etag
, estrndup(etag
, etag_len
));
247 etag_len
= spprintf(&etag_header
, 0, "ETag: \"%s\"", etag
);
248 status
= http_send_header_string_ex(etag_header
, etag_len
, 1);
251 *sent_header
= etag_header
;
260 /* {{{ STATUS http_send_content_type(char *, size_t) */
261 PHP_HTTP_API STATUS
_http_send_content_type(const char *content_type
, size_t ct_len TSRMLS_DC
)
263 HTTP_CHECK_CONTENT_TYPE(content_type
, return FAILURE
);
265 /* remember for multiple ranges */
266 STR_FREE(HTTP_G(send
).content_type
);
267 HTTP_G(send
).content_type
= estrndup(content_type
, ct_len
);
269 return http_send_header_ex("Content-Type", lenof("Content-Type"), content_type
, ct_len
, 1, NULL
);
273 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
274 PHP_HTTP_API STATUS
_http_send_content_disposition(const char *filename
, size_t f_len
, zend_bool send_inline TSRMLS_DC
)
280 cd_header
= ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len
);
281 sprintf(cd_header
, "Content-Disposition: inline; filename=\"%s\"", filename
);
283 cd_header
= ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len
);
284 sprintf(cd_header
, "Content-Disposition: attachment; filename=\"%s\"", filename
);
287 status
= http_send_header_string(cd_header
);
293 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
294 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
)
298 http_range_status range_status
;
299 int cache_etag
= http_interrupt_ob_etaghandler();
308 /* enable partial dl and resume */
309 http_send_header_string("Accept-Ranges: bytes");
311 zend_hash_init(&ranges
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
312 range_status
= http_get_request_ranges(&ranges
, data_size
);
314 switch (range_status
)
318 zend_hash_destroy(&ranges
);
319 http_send_status(416);
324 /* Range Request - only send ranges if entity hasn't changed */
325 if ( http_match_etag_ex("HTTP_IF_MATCH", HTTP_G(send
).unquoted_etag
, 0) &&
326 http_match_last_modified_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(send
).last_modified
, 0) &&
327 http_match_last_modified_ex("HTTP_UNLESS_MODIFIED_SINCE", HTTP_G(send
).last_modified
, 0)) {
329 if (zend_hash_num_elements(&ranges
) == 1) {
331 zval
**range
, **begin
, **end
;
333 if ( SUCCESS
== zend_hash_index_find(&ranges
, 0, (void **) &range
) &&
334 SUCCESS
== zend_hash_index_find(Z_ARRVAL_PP(range
), 0, (void **) &begin
) &&
335 SUCCESS
== zend_hash_index_find(Z_ARRVAL_PP(range
), 1, (void **) &end
)) {
336 char range_header_str
[256];
337 size_t range_header_len
;
339 range_header_len
= snprintf(range_header_str
, lenof(range_header_str
), "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin
), Z_LVAL_PP(end
), data_size
);
340 http_send_status_header_ex(206, range_header_str
, range_header_len
, 1);
341 http_send_response_start(&s
, Z_LVAL_PP(end
)-Z_LVAL_PP(begin
)+1);
342 http_send_response_data_fetch(&s
, data_ptr
, data_size
, data_mode
, Z_LVAL_PP(begin
), Z_LVAL_PP(end
) + 1);
343 http_send_response_finish(&s
);
344 zend_hash_destroy(&ranges
);
350 zval
**range
, **begin
, **end
;
351 const char *content_type
= HTTP_G(send
).content_type
;
352 char boundary_str
[32], range_header_str
[256];
353 size_t boundary_len
, range_header_len
;
355 boundary_len
= snprintf(boundary_str
, lenof(boundary_str
), "%lu%0.9f", (ulong
) time(NULL
), (float) php_combined_lcg(TSRMLS_C
));
356 range_header_len
= snprintf(range_header_str
, lenof(range_header_str
), "Content-Type: multipart/byteranges; boundary=%s", boundary_str
);
358 http_send_status_header_ex(206, range_header_str
, range_header_len
, 1);
359 http_send_response_start(&s
, 0);
362 content_type
= "application/x-octetstream";
365 FOREACH_HASH_VAL(pos
, &ranges
, range
) {
366 if ( SUCCESS
== zend_hash_index_find(Z_ARRVAL_PP(range
), 0, (void **) &begin
) &&
367 SUCCESS
== zend_hash_index_find(Z_ARRVAL_PP(range
), 1, (void **) &end
)) {
368 char preface_str
[512];
371 #define HTTP_RANGE_PREFACE \
373 HTTP_CRLF "Content-Type: %s" \
374 HTTP_CRLF "Content-Range: bytes %ld-%ld/%zu" \
377 preface_len
= snprintf(preface_str
, lenof(preface_str
), HTTP_RANGE_PREFACE
, boundary_str
, content_type
, Z_LVAL_PP(begin
), Z_LVAL_PP(end
), data_size
);
378 http_send_response_data_plain(&s
, preface_str
, preface_len
);
379 http_send_response_data_fetch(&s
, data_ptr
, data_size
, data_mode
, Z_LVAL_PP(begin
), Z_LVAL_PP(end
) + 1);
383 http_send_response_data_plain(&s
, HTTP_CRLF
"--", lenof(HTTP_CRLF
"--"));
384 http_send_response_data_plain(&s
, boundary_str
, boundary_len
);
385 http_send_response_data_plain(&s
, "--", lenof("--"));
387 http_send_response_finish(&s
);
388 zend_hash_destroy(&ranges
);
395 zend_hash_destroy(&ranges
);
397 /* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */
398 if (!no_cache
&& cache_etag
) {
401 if ((etag
= http_etag(data_ptr
, data_size
, data_mode
))) {
402 char *sent_header
= NULL
;
404 http_send_etag_ex(etag
, strlen(etag
), &sent_header
);
405 if (http_match_etag("HTTP_IF_NONE_MATCH", etag
)) {
406 return http_exit_ex(304, sent_header
, NULL
, 0);
408 STR_FREE(sent_header
);
414 /* send 304 Not Modified if last modified matches */
415 if (!no_cache
&& http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G(send
).last_modified
)) {
416 char *sent_header
= NULL
;
417 http_send_last_modified_ex(HTTP_G(send
).last_modified
, &sent_header
);
418 return http_exit_ex(304, sent_header
, NULL
, 0);
421 /* send full response */
422 http_send_response_start(&s
, data_size
);
423 http_send_response_data_fetch(&s
, data_ptr
, data_size
, data_mode
, 0, data_size
);
424 http_send_response_finish(&s
);
432 /* {{{ STATUS http_send_stream(php_stream *) */
433 PHP_HTTP_API STATUS
_http_send_stream_ex(php_stream
*file
, zend_bool close_stream
, zend_bool no_cache TSRMLS_DC
)
436 php_stream_statbuf ssb
;
438 if ((!file
) || php_stream_stat(file
, &ssb
)) {
442 status
= http_send_ex(file
, ssb
.sb
.st_size
, SEND_RSRC
, no_cache
);
445 php_stream_close(file
);
452 /* {{{ char *http_guess_content_type(char *magic_file, long magic_mode, void *data, size_t size, http_send_mode mode) */
453 PHP_HTTP_API
char *_http_guess_content_type(const char *magicfile
, long magicmode
, void *data_ptr
, size_t data_len
, http_send_mode data_mode TSRMLS_DC
)
457 #ifdef HTTP_HAVE_MAGIC
458 struct magic_set
*magic
;
460 HTTP_CHECK_OPEN_BASEDIR(magicfile
, return NULL
);
462 /* magic_load() fails if MAGIC_MIME is set because it
463 cowardly adds .mime to the file name */
464 magic
= magic_open(magicmode
&~ MAGIC_MIME
);
467 http_error_ex(HE_WARNING
, HTTP_E_INVALID_PARAM
, "Invalid magic mode: %ld", magicmode
);
468 } else if (-1 == magic_load(magic
, magicfile
)) {
469 http_error_ex(HE_WARNING
, HTTP_E_RUNTIME
, "Failed to load magic database '%s' (%s)", magicfile
, magic_error(magic
));
471 const char *ctype
= NULL
;
473 magic_setflags(magic
, magicmode
);
482 b_len
= php_stream_copy_to_mem(data_ptr
, &buffer
, 65536, 0);
483 ctype
= magic_buffer(magic
, buffer
, b_len
);
489 ctype
= magic_buffer(magic
, data_ptr
, data_len
);
493 HTTP_CHECK_OPEN_BASEDIR(data_ptr
, magic_close(magic
); return NULL
);
494 ctype
= magic_file(magic
, data_ptr
);
501 http_error_ex(HE_WARNING
, HTTP_E_RUNTIME
, "Failed to guess Content-Type: %s", magic_error(magic
));
508 http_error(HE_WARNING
, HTTP_E_RUNTIME
, "Cannot guess Content-Type; libmagic not available");
520 * vim600: sw=4 ts=4 fdm=marker