- Fixed build on php-trunk
[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-2010, 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
22 #include "php_http_api.h"
23 #include "php_http_cache_api.h"
24 #include "php_http_date_api.h"
25 #include "php_http_encoding_api.h"
26 #include "php_http_headers_api.h"
27 #include "php_http_send_api.h"
28
29 /* {{{ http_flush() */
30 #define http_flush(d, l) _http_flush(NULL, (d), (l) TSRMLS_CC)
31 static inline void _http_flush(void *nothing, const char *data, size_t data_len TSRMLS_DC)
32 {
33 PHPWRITE(data, data_len);
34 /* we really only need to flush when throttling is enabled,
35 because we push the data as fast as possible anyway if not */
36 if (HTTP_G->send.throttle_delay >= HTTP_DIFFSEC) {
37 #if PHP_VERSION_ID < 50399
38 if (OG(ob_nesting_level)) {
39 php_end_ob_buffer(1, 1 TSRMLS_CC);
40 }
41 if (!OG(implicit_flush)) {
42 sapi_flush(TSRMLS_C);
43 }
44 #else
45 int ob_level;
46
47 if ((php_output_handler_hook(PHP_OUTPUT_HANDLER_HOOK_GET_LEVEL, &ob_level TSRMLS_CC) == SUCCESS) && ob_level > 0) {
48 php_output_discard(TSRMLS_C);
49 }
50
51 if (php_output_get_status(TSRMLS_C) & PHP_OUTPUT_IMPLICITFLUSH) {
52 sapi_flush(TSRMLS_C);
53 }
54 #endif
55 http_sleep(HTTP_G->send.throttle_delay);
56 }
57 }
58 /* }}} */
59
60 /* {{{ http_send_response_start */
61 #define http_send_response_start(b, cl) _http_send_response_start((b), (cl) TSRMLS_CC)
62 static inline void _http_send_response_start(void **buffer, size_t content_length TSRMLS_DC)
63 {
64 int encoding;
65
66 if ((encoding = http_encoding_response_start(content_length, 0))) {
67 #ifdef HTTP_HAVE_ZLIB
68 *((http_encoding_stream **) buffer) = http_encoding_deflate_stream_init(NULL,
69 (encoding == HTTP_ENCODING_GZIP) ?
70 HTTP_DEFLATE_TYPE_GZIP : HTTP_DEFLATE_TYPE_ZLIB);
71 #endif
72 }
73 /* flush headers */
74 sapi_flush(TSRMLS_C);
75 }
76 /* }}} */
77
78 /* {{{ http_send_response_data_plain */
79 #define http_send_response_data_plain(b, d, dl) _http_send_response_data_plain((b), (d), (dl) TSRMLS_CC)
80 static inline void _http_send_response_data_plain(void **buffer, const char *data, size_t data_len TSRMLS_DC)
81 {
82 if (HTTP_G->send.deflate.response && HTTP_G->send.deflate.encoding) {
83 #ifdef HTTP_HAVE_ZLIB
84 char *encoded;
85 size_t encoded_len;
86 http_encoding_stream *s = *((http_encoding_stream **) buffer);
87
88 http_encoding_deflate_stream_update(s, data, data_len, &encoded, &encoded_len);
89 if (HTTP_G->send.buffer_size) {
90 phpstr_chunked_output((phpstr **) &s->storage, encoded, encoded_len, HTTP_G->send.buffer_size, _http_flush, NULL TSRMLS_CC);
91 } else {
92 http_flush(encoded, encoded_len);
93 }
94 efree(encoded);
95 #else
96 http_error(HE_ERROR, HTTP_E_RESPONSE, "Attempt to send GZIP response despite being able to do so; please report this bug");
97 #endif
98 } else if (HTTP_G->send.buffer_size) {
99 phpstr_chunked_output((phpstr **) buffer, data, data_len, HTTP_G->send.buffer_size, _http_flush, NULL TSRMLS_CC);
100 } else {
101 http_flush(data, data_len);
102 }
103 }
104 /* }}} */
105
106 /* {{{ http_send_response_data_fetch */
107 #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)
108 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)
109 {
110 long bsz, got, len = end - begin;
111
112 if (!(bsz = HTTP_G->send.buffer_size)) {
113 bsz = HTTP_SENDBUF_SIZE;
114 }
115
116 switch (mode) {
117 case SEND_RSRC: {
118 php_stream *s = (php_stream *) data;
119 if (SUCCESS == php_stream_seek(s, begin, SEEK_SET)) {
120 char *buf = emalloc(bsz);
121
122 while (len > 0) {
123 got = php_stream_read(s, buf, MIN(len, bsz));
124 http_send_response_data_plain(buffer, buf, got);
125 len -= got;
126 }
127
128 efree(buf);
129 }
130 break;
131 }
132 case SEND_DATA: {
133 const char *buf = ((const char *) data) + begin;
134 while (len > 0) {
135 got = MIN(len, bsz);
136 http_send_response_data_plain(buffer, buf, got);
137 len -= got;
138 buf += got;
139 }
140 break;
141 }
142 EMPTY_SWITCH_DEFAULT_CASE();
143 }
144 }
145 /* }}} */
146
147 /* {{{ http_send_response_finish */
148 #define http_send_response_finish(b) _http_send_response_finish((b) TSRMLS_CC)
149 static inline void _http_send_response_finish(void **buffer TSRMLS_DC)
150 {
151 if (HTTP_G->send.deflate.response && HTTP_G->send.deflate.encoding) {
152 #ifdef HTTP_HAVE_ZLIB
153 char *encoded = NULL;
154 size_t encoded_len = 0;
155 http_encoding_stream *s = *((http_encoding_stream **) buffer);
156
157 http_encoding_deflate_stream_finish(s, &encoded, &encoded_len);
158 if (HTTP_G->send.buffer_size) {
159 phpstr_chunked_output((phpstr **) &s->storage, encoded, encoded_len, 0, _http_flush, NULL TSRMLS_CC);
160 } else {
161 http_flush(encoded, encoded_len);
162 }
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 if (HTTP_G->send.buffer_size) {
169 phpstr_chunked_output((phpstr **) buffer, NULL, 0, 0, _http_flush, NULL 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 /* {{{ http_find_header */
189 typedef struct {
190 const char *h;
191 size_t l;
192 } http_response_header_t;
193
194 static int http_find_header(void *data, void *arg)
195 {
196 http_response_header_t *h = arg;
197 sapi_header_struct *s = data;
198
199 return (!strncasecmp(s->header, h->h, h->l)) && s->header[h->l] == ':';
200 }
201 /* }}} */
202
203 /* {{{ void http_hide_header(char *) */
204 PHP_HTTP_API void _http_hide_header_ex(const char *name, size_t name_len TSRMLS_DC)
205 {
206 http_response_header_t h = {name, name_len};
207 zend_llist_del_element(&SG(sapi_headers).headers, (void *) &h, http_find_header);
208 }
209 /* }}} */
210
211 /* {{{ void http_send_header_zval(char*, zval **, zend_bool) */
212 PHP_HTTP_API void _http_send_header_zval_ex(const char *name, size_t name_len, zval **val, zend_bool replace TSRMLS_DC)
213 {
214 if (!val || !*val || Z_TYPE_PP(val) == IS_NULL || (Z_TYPE_PP(val) == IS_STRING && !Z_STRLEN_PP(val))) {
215 http_hide_header_ex(name, name_len);
216 } else if (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT) {
217 zend_bool first = replace;
218 zval **data_ptr;
219 HashPosition pos;
220
221 FOREACH_HASH_VAL(pos, HASH_OF(*val), data_ptr) {
222 zval *data = http_zsep(IS_STRING, *data_ptr);
223
224 http_send_header_ex(name, name_len, Z_STRVAL_P(data), Z_STRLEN_P(data), first, NULL);
225 zval_ptr_dtor(&data);
226 first = 0;
227 }
228 } else {
229 zval *data = http_zsep(IS_STRING, *val);
230
231 http_send_header_ex(name, name_len, Z_STRVAL_P(data), Z_STRLEN_P(data), replace, NULL);
232 zval_ptr_dtor(&data);
233 }
234 }
235 /* }}} */
236
237 /* {{{ STATUS http_send_header(char *, char *, zend_bool) */
238 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)
239 {
240 STATUS ret;
241
242 if (value && value_len) {
243 size_t header_len = sizeof(": ") + name_len + value_len + 1;
244 char *header = emalloc(header_len + 1);
245
246 header[header_len] = '\0';
247 header_len = snprintf(header, header_len, "%s: %s", name, value);
248 ret = http_send_header_string_ex(header, header_len, replace);
249 if (sent_header) {
250 *sent_header = header;
251 } else {
252 efree(header);
253 }
254 } else {
255 http_hide_header_ex(name, name_len);
256 ret = SUCCESS;
257 }
258 return ret;
259 }
260 /* }}} */
261
262 /* {{{ STATUS http_send_status_header(int, char *) */
263 PHP_HTTP_API STATUS _http_send_status_header_ex(int status, const char *header, size_t header_len, zend_bool replace TSRMLS_DC)
264 {
265 STATUS ret;
266 sapi_header_line h = {(char *) header, header_len, status};
267 if (SUCCESS != (ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, &h TSRMLS_CC))) {
268 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Could not send header: %s (%d)", header, status);
269 }
270 return ret;
271 }
272 /* }}} */
273
274 /* {{{ STATUS http_send_last_modified(int) */
275 PHP_HTTP_API STATUS _http_send_last_modified_ex(time_t t, char **sent_header TSRMLS_DC)
276 {
277 STATUS ret;
278 char *date = http_date(t);
279
280 if (!date) {
281 return FAILURE;
282 }
283
284 ret = http_send_header_ex("Last-Modified", lenof("Last-Modified"), date, strlen(date), 1, sent_header);
285 efree(date);
286
287 /* remember */
288 HTTP_G->send.last_modified = t;
289
290 return ret;
291 }
292 /* }}} */
293
294 /* {{{ STATUS http_send_etag(char *, size_t) */
295 PHP_HTTP_API STATUS _http_send_etag_ex(const char *etag, size_t etag_len, char **sent_header TSRMLS_DC)
296 {
297 STATUS status;
298 char *etag_header;
299 size_t etag_header_len;
300
301 if (!etag_len){
302 http_error_ex(HE_WARNING, HTTP_E_HEADER, "Attempt to send empty ETag (previous: %s)\n", HTTP_G->send.unquoted_etag);
303 return FAILURE;
304 }
305
306 etag_header_len = spprintf(&etag_header, 0, "ETag: \"%s\"", etag);
307 status = http_send_header_string_ex(etag_header, etag_header_len, 1);
308
309 /* remember */
310 STR_SET(HTTP_G->send.unquoted_etag, estrndup(etag, etag_len));
311
312 if (sent_header) {
313 *sent_header = etag_header;
314 } else {
315 efree(etag_header);
316 }
317
318 return status;
319 }
320 /* }}} */
321
322 /* {{{ STATUS http_send_content_type(char *, size_t) */
323 PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_len TSRMLS_DC)
324 {
325 HTTP_CHECK_CONTENT_TYPE(content_type, return FAILURE);
326
327 /* remember for multiple ranges */
328 STR_FREE(HTTP_G->send.content_type);
329 HTTP_G->send.content_type = estrndup(content_type, ct_len);
330
331 return http_send_header_ex("Content-Type", lenof("Content-Type"), content_type, ct_len, 1, NULL);
332 }
333 /* }}} */
334
335 /* {{{ STATUS http_send_content_disposition(char *, size_t, zend_bool) */
336 PHP_HTTP_API STATUS _http_send_content_disposition(const char *filename, size_t f_len, zend_bool send_inline TSRMLS_DC)
337 {
338 STATUS status;
339 char *cd_header;
340
341 if (send_inline) {
342 cd_header = ecalloc(1, sizeof("Content-Disposition: inline; filename=\"\"") + f_len);
343 sprintf(cd_header, "Content-Disposition: inline; filename=\"%s\"", filename);
344 } else {
345 cd_header = ecalloc(1, sizeof("Content-Disposition: attachment; filename=\"\"") + f_len);
346 sprintf(cd_header, "Content-Disposition: attachment; filename=\"%s\"", filename);
347 }
348
349 status = http_send_header_string(cd_header);
350 efree(cd_header);
351 return status;
352 }
353 /* }}} */
354
355 /* {{{ STATUS http_send(void *, size_t, http_send_mode) */
356 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)
357 {
358 void *s = NULL;
359 HashTable ranges;
360 http_range_status range_status;
361
362 if (!data_ptr) {
363 return FAILURE;
364 }
365 if (!data_size) {
366 return SUCCESS;
367 }
368
369 /* enable partial dl and resume */
370 http_send_header_string("Accept-Ranges: bytes");
371
372 zend_hash_init(&ranges, 0, NULL, ZVAL_PTR_DTOR, 0);
373 range_status = http_get_request_ranges(&ranges, data_size);
374
375 switch (range_status) {
376 case RANGE_ERR:
377 {
378 zend_hash_destroy(&ranges);
379 http_send_status(416);
380 return FAILURE;
381 }
382 case RANGE_OK:
383 {
384 /* Range Request - only send ranges if entity hasn't changed */
385 if ( http_got_server_var("HTTP_IF_RANGE") &&
386 !http_match_etag("HTTP_IF_RANGE", HTTP_G->send.unquoted_etag) &&
387 !http_match_last_modified("HTTP_IF_RANGE", HTTP_G->send.last_modified)) {
388 /* fallthrough to send full entity with 200 Ok */
389 no_cache = 1;
390 } else if ( !http_match_etag_ex("HTTP_IF_MATCH", HTTP_G->send.unquoted_etag, 0) ||
391 !http_match_last_modified_ex("HTTP_IF_UNMODIFIED_SINCE", HTTP_G->send.last_modified, 0) ||
392 !http_match_last_modified_ex("HTTP_UNLESS_MODIFIED_SINCE", HTTP_G->send.last_modified, 0)) {
393 /* 412 Precondition failed */
394 zend_hash_destroy(&ranges);
395 http_send_status(412);
396 return FAILURE;
397 } else if (zend_hash_num_elements(&ranges) == 1) {
398 /* single range */
399 zval **range, **begin, **end;
400
401 if ( SUCCESS != zend_hash_index_find(&ranges, 0, (void *) &range) ||
402 SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void *) &begin) ||
403 SUCCESS != zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void *) &end)) {
404 /* this should never happen */
405 zend_hash_destroy(&ranges);
406 http_send_status(500);
407 return FAILURE;
408 } else {
409 phpstr header;
410
411 phpstr_init(&header);
412 phpstr_appendf(&header, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin), Z_LVAL_PP(end), data_size);
413 phpstr_fix(&header);
414 http_send_status_header_ex(206, PHPSTR_VAL(&header), PHPSTR_LEN(&header), 1);
415 phpstr_dtor(&header);
416 http_send_response_start(&s, Z_LVAL_PP(end)-Z_LVAL_PP(begin)+1);
417 http_send_response_data_fetch(&s, data_ptr, data_size, data_mode, Z_LVAL_PP(begin), Z_LVAL_PP(end) + 1);
418 http_send_response_finish(&s);
419 zend_hash_destroy(&ranges);
420 return SUCCESS;
421 }
422 } else {
423 /* multi range */
424 HashPosition pos;
425 zval **range, **begin, **end;
426 const char *content_type = HTTP_G->send.content_type;
427 char boundary_str[32];
428 size_t boundary_len;
429 phpstr header, preface;
430
431 boundary_len = http_boundary(boundary_str, sizeof(boundary_str));
432 phpstr_init(&header);
433 phpstr_appendf(&header, "Content-Type: multipart/byteranges; boundary=%s", boundary_str);
434 phpstr_fix(&header);
435 http_send_status_header_ex(206, PHPSTR_VAL(&header), PHPSTR_LEN(&header), 1);
436 phpstr_dtor(&header);
437 http_send_response_start(&s, 0);
438
439 if (!content_type) {
440 content_type = "application/x-octetstream";
441 }
442
443 phpstr_init(&preface);
444 FOREACH_HASH_VAL(pos, &ranges, range) {
445 if ( SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 0, (void *) &begin) &&
446 SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(range), 1, (void *) &end)) {
447
448 #define HTTP_RANGE_PREFACE \
449 HTTP_CRLF "--%s" \
450 HTTP_CRLF "Content-Type: %s" \
451 HTTP_CRLF "Content-Range: bytes %ld-%ld/%zu" \
452 HTTP_CRLF HTTP_CRLF
453
454 phpstr_appendf(&preface, HTTP_RANGE_PREFACE, boundary_str, content_type, Z_LVAL_PP(begin), Z_LVAL_PP(end), data_size);
455 phpstr_fix(&preface);
456 http_send_response_data_plain(&s, PHPSTR_VAL(&preface), PHPSTR_LEN(&preface));
457 phpstr_reset(&preface);
458 http_send_response_data_fetch(&s, data_ptr, data_size, data_mode, Z_LVAL_PP(begin), Z_LVAL_PP(end) + 1);
459 }
460 }
461 phpstr_dtor(&preface);
462
463 http_send_response_data_plain(&s, HTTP_CRLF "--", lenof(HTTP_CRLF "--"));
464 http_send_response_data_plain(&s, boundary_str, boundary_len);
465 http_send_response_data_plain(&s, "--", lenof("--"));
466
467 http_send_response_finish(&s);
468 zend_hash_destroy(&ranges);
469 return SUCCESS;
470 }
471 }
472 case RANGE_NO:
473 {
474 zend_hash_destroy(&ranges);
475
476 /* send 304 Not Modified if etag matches - DON'T return on ETag generation failure */
477 if (!no_cache && (http_interrupt_ob_etaghandler() || (HTTP_G->send.unquoted_etag != NULL))) {
478 char *etag = NULL;
479
480 if (HTTP_G->send.unquoted_etag) {
481 etag = estrdup(HTTP_G->send.unquoted_etag);
482 }
483
484 if (etag || (etag = http_etag(data_ptr, data_size, data_mode))) {
485 char *sent_header = NULL;
486
487 http_send_etag_ex(etag, strlen(etag), &sent_header);
488 if (http_match_etag("HTTP_IF_NONE_MATCH", etag)) {
489 return http_exit_ex(304, sent_header, NULL, 0);
490 } else {
491 STR_FREE(sent_header);
492 /* no caching for Last-Modified if ETags really don't match */
493 no_cache = http_got_server_var("HTTP_IF_NONE_MATCH");
494 }
495 efree(etag);
496 }
497 }
498
499 /* send 304 Not Modified if last modified matches */
500 if (!no_cache && HTTP_G->send.last_modified && http_match_last_modified("HTTP_IF_MODIFIED_SINCE", HTTP_G->send.last_modified)) {
501 char *sent_header = NULL;
502 http_send_last_modified_ex(HTTP_G->send.last_modified, &sent_header);
503 return http_exit_ex(304, sent_header, NULL, 0);
504 }
505
506 /* send full response */
507 http_send_response_start(&s, data_size);
508 http_send_response_data_fetch(&s, data_ptr, data_size, data_mode, 0, data_size);
509 http_send_response_finish(&s);
510 return SUCCESS;
511 }
512 }
513 return FAILURE;
514 }
515 /* }}} */
516
517 /* {{{ STATUS http_send_stream(php_stream *) */
518 PHP_HTTP_API STATUS _http_send_stream_ex(php_stream *file, zend_bool close_stream, zend_bool no_cache TSRMLS_DC)
519 {
520 STATUS status;
521 php_stream_statbuf ssb;
522 int orig_flags;
523
524 if ((!file) || php_stream_stat(file, &ssb)) {
525 char *defct = sapi_get_default_content_type(TSRMLS_C);
526
527 http_hide_header("Content-Disposition");
528 http_send_content_type(defct, strlen(defct));
529 http_error(HE_WARNING, HTTP_E_RESPONSE, "File not found; stat failed");
530 STR_FREE(defct);
531
532 if (HTTP_G->send.not_found_404) {
533 http_exit_ex(404, NULL, estrdup("File not found\n"), 0);
534 }
535 return FAILURE;
536 }
537
538 orig_flags = file->flags;
539 file->flags |= PHP_STREAM_FLAG_NO_BUFFER;
540 status = http_send_ex(file, ssb.sb.st_size, SEND_RSRC, no_cache);
541 file->flags = orig_flags;
542
543 if (close_stream) {
544 php_stream_close(file);
545 }
546
547 return status;
548 }
549 /* }}} */
550
551 /* {{{ char *http_guess_content_type(char *magic_file, long magic_mode, void *data, size_t size, http_send_mode mode) */
552 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)
553 {
554 char *ct = NULL;
555
556 #ifdef HTTP_HAVE_MAGIC
557 struct magic_set *magic = NULL;
558
559 HTTP_CHECK_OPEN_BASEDIR(magicfile, return NULL);
560
561 if (!data_ptr) {
562 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Supplied payload is empty");
563 } else if (!(magic = magic_open(magicmode &~ MAGIC_MIME))) {
564 http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid magic mode: %ld", magicmode);
565 } else if (-1 == magic_load(magic, magicfile)) {
566 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to load magic database '%s' (%s)", magicfile, magic_error(magic));
567 } else {
568 const char *ctype = NULL;
569
570 magic_setflags(magic, magicmode);
571
572 switch (data_mode) {
573 case SEND_RSRC:
574 {
575 char *buffer;
576 size_t b_len;
577
578 b_len = php_stream_copy_to_mem(data_ptr, &buffer, 65536, 0);
579 ctype = magic_buffer(magic, buffer, b_len);
580 efree(buffer);
581 break;
582 }
583
584 case SEND_DATA:
585 ctype = magic_buffer(magic, data_ptr, data_len);
586 break;
587
588 default:
589 HTTP_CHECK_OPEN_BASEDIR(data_ptr, magic_close(magic); return NULL);
590 ctype = magic_file(magic, data_ptr);
591 break;
592 }
593
594 if (ctype) {
595 ct = estrdup(ctype);
596 } else {
597 http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to guess Content-Type: %s", magic_error(magic));
598 }
599 }
600 if (magic) {
601 magic_close(magic);
602 }
603 #else
604 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available");
605 #endif
606
607 return ct;
608 }
609 /* }}} */
610
611 /*
612 * Local variables:
613 * tab-width: 4
614 * c-basic-offset: 4
615 * End:
616 * vim600: sw=4 ts=4 fdm=marker
617 * vim<600: sw=4 ts=4
618 */
619