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