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