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