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