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