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