c376b0500a2d954e8fe504472c91e5f3bc3e0025
[m6w6/ext-http] / http_send_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
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 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18
19 #define HTTP_WANT_ZLIB
20 #define HTTP_WANT_MAGIC
21 #include "php_http.h"
22
23 #include "SAPI.h"
24 #include "php_streams.h"
25 #include "ext/standard/php_lcg.h"
26
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"
33
34 ZEND_EXTERN_MODULE_GLOBALS(http);
35
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)
39 {
40 PHPWRITE(data, data_len);
41 php_end_ob_buffer(1, 1 TSRMLS_CC);
42 sapi_flush(TSRMLS_C);
43
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)
49
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;
57
58 req.tv_sec = (time_t) HTTP_G(send).throttle_delay;
59 req.tv_nsec = HTTP_NSEC(HTTP_G(send).throttle_delay) % HTTP_NANOSEC;
60
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;
64 }
65 #endif
66 }
67 }
68 /* }}} */
69
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)
73 {
74 if (http_encoding_response_start(content_length)) {
75 #ifdef HTTP_HAVE_ZLIB
76 char *encoded;
77 size_t encoded_len;
78 int gzip = (HTTP_G(send).gzip_encoding == HTTP_ENCODING_GZIP);
79 http_encoding_stream *s = emalloc(sizeof(http_encoding_stream));
80
81 http_encoding_stream_init(s, gzip?HTTP_ENCODING_STREAM_GZIP_HEADER:0, -1, &encoded, &encoded_len);
82 phpstr_chunked_output((phpstr **) &s->storage, encoded, encoded_len, HTTP_G(send).buffer_size, _http_flush TSRMLS_CC);
83 STR_FREE(encoded);
84 *buffer = s;
85 #endif
86 }
87 }
88 /* }}} */
89
90 /* {{{ http_send_response_data_plain */
91 #define http_send_response_data_plain(b, d, dl) _http_send_response_data_plain((b), (d), (dl) TSRMLS_CC)
92 static inline void _http_send_response_data_plain(void **buffer, const char *data, size_t data_len TSRMLS_DC)
93 {
94 if (HTTP_G(send).gzip_encoding) {
95 #ifdef HTTP_HAVE_ZLIB
96 char *encoded;
97 size_t encoded_len;
98 http_encoding_stream *s = *((http_encoding_stream **) buffer);
99
100 http_encoding_stream_update(s, data, data_len, &encoded, &encoded_len);
101 phpstr_chunked_output((phpstr **) &s->storage, encoded, encoded_len, HTTP_G(send).buffer_size, _http_flush TSRMLS_CC);
102 efree(encoded);
103 #else
104 http_error(HE_ERROR, HTTP_E_RESPONSE, "Attempt to send GZIP response despite being able to do so; please report this bug");
105 #endif
106 } else {
107 phpstr_chunked_output((phpstr **) buffer, data, data_len, HTTP_G(send).buffer_size, _http_flush TSRMLS_CC);
108 }
109 }
110 /* }}} */
111
112 #define HTTP_CHUNK_AVAIL(len, cs) ((len -= cs) >= 0)
113 /* {{{ http_send_response_data_fetch */
114 #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)
115 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)
116 {
117 long len = end - begin, chunk_size = 40960;
118
119 switch (mode)
120 {
121 case SEND_RSRC:
122 {
123 php_stream *s = (php_stream *) data;
124
125 if (SUCCESS == php_stream_seek(s, begin, SEEK_SET)) {
126 char *buf = emalloc(chunk_size);
127
128 while (HTTP_CHUNK_AVAIL(len, chunk_size)) {
129 http_send_response_data_plain(buffer, buf, php_stream_read(s, buf, chunk_size));
130 }
131 /* read & write left over */
132 if (len) {
133 http_send_response_data_plain(buffer, buf, php_stream_read(s, buf, chunk_size + len));
134 }
135
136 efree(buf);
137 }
138 }
139 break;
140
141 case SEND_DATA:
142 {
143 char *s = (char *) data + begin;
144
145 while (HTTP_CHUNK_AVAIL(len, chunk_size)) {
146 http_send_response_data_plain(buffer, s, chunk_size);
147 s += chunk_size;
148 }
149 /* write left over */
150 if (len) {
151 http_send_response_data_plain(buffer, s, chunk_size + len);
152 }
153 }
154 break;
155
156 EMPTY_SWITCH_DEFAULT_CASE();
157 }
158 }
159 /* }}} */
160
161 /* {{{ http_send_response_finish */
162 #define http_send_response_finish(b) _http_send_response_finish((b) TSRMLS_CC)
163 static inline void _http_send_response_finish(void **buffer TSRMLS_DC)
164 {
165 if (HTTP_G(send).gzip_encoding) {
166 #ifdef HTTP_HAVE_ZLIB
167 char *encoded = NULL;
168 size_t encoded_len = 0;
169 http_encoding_stream *s = *((http_encoding_stream **) buffer);
170
171 http_encoding_stream_finish(s, &encoded, &encoded_len);
172 phpstr_chunked_output((phpstr **) &s->storage, encoded, encoded_len, 0, _http_flush TSRMLS_CC);
173 STR_FREE(encoded);
174 efree(s);
175 #else
176 http_error(HE_ERROR, HTTP_E_RESPONSE, "Attempt to send GZIP response despite being able to do so; please report this bug");
177 #endif
178 } else {
179 phpstr_chunked_output((phpstr **) buffer, NULL, 0, 0, _http_flush TSRMLS_CC);
180 }
181 }
182 /* }}} */
183
184
185 /* {{{ STATUS http_send_header(char *, char *, zend_bool) */
186 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)
187 {
188 STATUS ret;
189 size_t header_len = sizeof(": ") + name_len + value_len + 1;
190 char *header = emalloc(header_len + 1);
191
192 header[header_len] = '\0';
193 header_len = snprintf(header, header_len, "%s: %s", name, value);
194 ret = http_send_header_string_ex(header, header_len, replace);
195 if (sent_header) {
196 *sent_header = header;
197 } else {
198 efree(header);
199 }
200 return ret;
201 }
202 /* }}} */
203
204 /* {{{ STATUS http_send_status_header(int, char *) */
205 PHP_HTTP_API STATUS _http_send_status_header_ex(int status, const char *header, size_t header_len, zend_bool replace TSRMLS_DC)
206 {
207 STATUS ret;
208 sapi_header_line h = {(char *) header, header_len, status};
209 if (SUCCESS != (ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, &h TSRMLS_CC))) {
210 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Could not send header: %s (%d)", header, status);
211 }
212 return ret;
213 }
214 /* }}} */
215
216 /* {{{ STATUS http_send_last_modified(int) */
217 PHP_HTTP_API STATUS _http_send_last_modified_ex(time_t t, char **sent_header TSRMLS_DC)
218 {
219 STATUS ret;
220 char *date = http_date(t);
221
222 if (!date) {
223 return FAILURE;
224 }
225
226 ret = http_send_header_ex("Last-Modified", lenof("Last-Modified"), date, strlen(date), 1, sent_header);
227 efree(date);
228
229 /* remember */
230 HTTP_G(send).last_modified = t;
231
232 return ret;
233 }
234 /* }}} */
235
236 /* {{{ STATUS http_send_etag(char *, size_t) */
237 PHP_HTTP_API STATUS _http_send_etag_ex(const char *etag, size_t etag_len, char **sent_header TSRMLS_DC)
238 {
239 STATUS status;
240 char *etag_header;
241
242 if (!etag_len){
243 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Attempt to send empty ETag (previous: %s)\n", HTTP_G(send).unquoted_etag);
244 return FAILURE;
245 }
246
247 /* remember */
248 STR_SET(HTTP_G(send).unquoted_etag, estrndup(etag, etag_len));
249
250 etag_len = spprintf(&etag_header, 0, "ETag: \"%s\"", etag);
251 status = http_send_header_string_ex(etag_header, etag_len, 1);
252
253 if (sent_header) {
254 *sent_header = etag_header;
255 } else {
256 efree(etag_header);
257 }
258
259 return status;
260 }
261 /* }}} */
262
263 /* {{{ STATUS http_send_content_type(char *, size_t) */
264 PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_len TSRMLS_DC)
265 {
266 HTTP_CHECK_CONTENT_TYPE(content_type, return FAILURE);
267
268 /* remember for multiple ranges */
269 STR_FREE(HTTP_G(send).content_type);
270 HTTP_G(send).content_type = estrndup(content_type, ct_len);
271
272 return http_send_header_ex("Content-Type", lenof("Content-Type"), content_type, ct_len, 1, NULL);
273 }
274 /* }}} */
275
276 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
277 PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename, size_t f_len, zend_bool send_inline TSRMLS_DC)
278 {
279 STATUS status;
280 char *cd_header;
281
282 if (send_inline) {
283 cd_header = ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len);
284 sprintf(cd_header, "Content-Disposition: inline; filename=\"%s\"", filename);
285 } else {
286 cd_header = ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len);
287 sprintf(cd_header, "Content-Disposition: attachment; filename=\"%s\"", filename);
288 }
289
290 status = http_send_header_string(cd_header);
291 efree(cd_header);
292 return status;
293 }
294 /* }}} */
295
296 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
297 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 {
299 void *s = NULL;
300 HashTable ranges;
301 http_range_status range_status;
302 int cache_etag = http_interrupt_ob_etaghandler();
303
304 if (!data_ptr) {
305 return FAILURE;
306 }
307 if (!data_size) {
308 return SUCCESS;
309 }
310
311 /* enable partial dl and resume */
312 http_send_header_string("Accept-Ranges: bytes");
313
314 zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0);
315 range_status = http_get_request_ranges(&ranges, data_size);
316
317 switch (range_status)
318 {
319 case RANGE_ERR:
320 {
321 zend_hash_destroy(&ranges);
322 http_send_status(416);
323 return FAILURE;
324 }
325 case RANGE_OK:
326 {
327 /* Range Request - only send ranges if entity hasn't changed */
328 if ( http_match_etag_ex("HTTP_IF_MATCH", HTTP_G(send).unquoted_etag, 0) &&
329 http_match_last_modified_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G(send).last_modified, 0) &&
330 http_match_last_modified_ex("HTTP_UNLESS_MODIFIED_SINCE", HTTP_G(send).last_modified, 0)) {
331
332 if (zend_hash_num_elements(&ranges) == 1) {
333 /* single range */
334 zval **range, **begin, **end;
335
336 if ( SUCCESS == zend_hash_index_find(&ranges, 0, (void **) &range) &&
337 SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void **) &begin) &&
338 SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void **) &end)) {
339 char range_header_str[256];
340 size_t range_header_len;
341
342 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);
343 http_send_status_header_ex(206, range_header_str, range_header_len, 1);
344 http_send_response_start(&s, Z_LVAL_PP(end)-Z_LVAL_PP(begin)+1);
345 http_send_response_data_fetch(&s, data_ptr, data_size, data_mode, Z_LVAL_PP(begin), Z_LVAL_PP(end) + 1);
346 http_send_response_finish(&s);
347 zend_hash_destroy(&ranges);
348 return SUCCESS;
349 }
350 } else {
351 /* multi range */
352 HashPosition pos;
353 zval **range, **begin, **end;
354 const char *content_type = HTTP_G(send).content_type;
355 char boundary_str[32], range_header_str[256];
356 size_t boundary_len, range_header_len;
357
358 boundary_len = snprintf(boundary_str, lenof(boundary_str), "%lu%0.9f", (ulong) time(NULL), (float) php_combined_lcg(TSRMLS_C));
359 range_header_len = snprintf(range_header_str, lenof(range_header_str), "Content-Type: multipart/byteranges; boundary=%s", boundary_str);
360
361 http_send_status_header_ex(206, range_header_str, range_header_len, 1);
362 http_send_response_start(&s, 0);
363
364 if (!content_type) {
365 content_type = "application/x-octetstream";
366 }
367
368 FOREACH_HASH_VAL(pos, &ranges, range) {
369 if ( SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void **) &begin) &&
370 SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void **) &end)) {
371 char preface_str[512];
372 size_t preface_len;
373
374 #define HTTP_RANGE_PREFACE \
375 HTTP_CRLF "--%s" \
376 HTTP_CRLF "Content-Type: %s" \
377 HTTP_CRLF "Content-Range: bytes %ld-%ld/%zu" \
378 HTTP_CRLF HTTP_CRLF
379
380 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);
381 http_send_response_data_plain(&s, preface_str, preface_len);
382 http_send_response_data_fetch(&s, data_ptr, data_size, data_mode, Z_LVAL_PP(begin), Z_LVAL_PP(end) + 1);
383 }
384 }
385
386 http_send_response_data_plain(&s, HTTP_CRLF "--", lenof(HTTP_CRLF "--"));
387 http_send_response_data_plain(&s, boundary_str, boundary_len);
388 http_send_response_data_plain(&s, "--", lenof("--"));
389
390 http_send_response_finish(&s);
391 zend_hash_destroy(&ranges);
392 return SUCCESS;
393 }
394 }
395 }
396 case RANGE_NO:
397 {
398 zend_hash_destroy(&ranges);
399
400 /* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */
401 if (!no_cache && cache_etag) {
402 char *etag = NULL;
403
404 if ((etag = http_etag(data_ptr, data_size, data_mode))) {
405 char *sent_header = NULL;
406
407 http_send_etag_ex(etag, strlen(etag), &sent_header);
408 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
409 return http_exit_ex(304, sent_header, NULL, 0);
410 } else {
411 STR_FREE(sent_header);
412 }
413 efree(etag);
414 }
415 }
416
417 /* send 304 Not Modified if last modified matches */
418 if (!no_cache && http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G(send).last_modified)) {
419 char *sent_header = NULL;
420 http_send_last_modified_ex(HTTP_G(send).last_modified, &sent_header);
421 return http_exit_ex(304, sent_header, NULL, 0);
422 }
423
424 /* send full response */
425 http_send_response_start(&s, data_size);
426 http_send_response_data_fetch(&s, data_ptr, data_size, data_mode, 0, data_size);
427 http_send_response_finish(&s);
428 return SUCCESS;
429 }
430 }
431 return FAILURE;
432 }
433 /* }}} */
434
435 /* {{{ STATUS http_send_stream(php_stream *) */
436 PHP_HTTP_API STATUS _http_send_stream_ex(php_stream *file, zend_bool close_stream, zend_bool no_cache TSRMLS_DC)
437 {
438 STATUS status;
439 php_stream_statbuf ssb;
440
441 if ((!file) || php_stream_stat(file, &ssb)) {
442 return FAILURE;
443 }
444
445 status = http_send_ex(file, ssb.sb.st_size, SEND_RSRC, no_cache);
446
447 if (close_stream) {
448 php_stream_close(file);
449 }
450
451 return status;
452 }
453 /* }}} */
454
455 /* {{{ char *http_guess_content_type(char *magic_file, long magic_mode, void *data, size_t size, http_send_mode mode) */
456 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 {
458 char *ct = NULL;
459
460 #ifdef HTTP_HAVE_MAGIC
461 struct magic_set *magic;
462
463 HTTP_CHECK_OPEN_BASEDIR(magicfile, return NULL);
464
465 /* magic_load() fails if MAGIC_MIME is set because it
466 cowardly adds .mime to the file name */
467 magic = magic_open(magicmode &~ MAGIC_MIME);
468
469 if (!magic) {
470 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid magic mode: %ld", magicmode);
471 } else if (-1 == magic_load(magic, magicfile)) {
472 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to load magic database '%s' (%s)", magicfile, magic_error(magic));
473 } else {
474 const char *ctype = NULL;
475
476 magic_setflags(magic, magicmode);
477
478 switch (data_mode)
479 {
480 case SEND_RSRC:
481 {
482 char *buffer;
483 size_t b_len;
484
485 b_len = php_stream_copy_to_mem(data_ptr, &buffer, 65536, 0);
486 ctype = magic_buffer(magic, buffer, b_len);
487 efree(buffer);
488 }
489 break;
490
491 case SEND_DATA:
492 ctype = magic_buffer(magic, data_ptr, data_len);
493 break;
494
495 default:
496 HTTP_CHECK_OPEN_BASEDIR(data_ptr, magic_close(magic); return NULL);
497 ctype = magic_file(magic, data_ptr);
498 break;
499 }
500
501 if (ctype) {
502 ct = estrdup(ctype);
503 } else {
504 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to guess Content-Type: %s", magic_error(magic));
505 }
506 }
507 if (magic) {
508 magic_close(magic);
509 }
510 #else
511 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available");
512 #endif
513
514 return ct;
515 }
516 /* }}} */
517
518 /*
519 * Local variables:
520 * tab-width: 4
521 * c-basic-offset: 4
522 * End:
523 * vim600: sw=4 ts=4 fdm=marker
524 * vim<600: sw=4 ts=4
525 */
526