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