From: Michael Wallner Date: Wed, 21 Sep 2005 12:03:44 +0000 (+0000) Subject: - fix very silly behaviour of libmagic if we want to get the mime type intead X-Git-Tag: RELEASE_0_14_0~8 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=commitdiff_plain;h=c04184b2188a60efed4753d7eef7f3952aed9cef;ds=sidebyside - fix very silly behaviour of libmagic if we want to get the mime type intead of a human readable string, which is usually the default use case # background: libmagic appends ".mime" to the magic file name if MAGIC_MIME is set (sic!) --- diff --git a/http_api.c b/http_api.c index abc2c9b..eec41da 100644 --- a/http_api.c +++ b/http_api.c @@ -358,15 +358,19 @@ PHP_HTTP_API char *_http_guess_content_type(const char *magicfile, long magicmod char *ct = NULL; #ifdef HTTP_HAVE_MAGIC - struct magic_set *magic = magic_open(magicmode); + /* magic_load() fails if MAGIC_MIME is set because it + cowardly adds .mime to the file name */ + struct magic_set *magic = magic_open(magicmode &~ MAGIC_MIME); if (!magic) { http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid magic mode: %ld", magicmode); } else if (-1 == magic_load(magic, magicfile)) { - http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to load magic database '%s'", magicfile); + http_error_ex(HE_WARNING, HTTP_E_RUNTIME, "Failed to load magic database '%s' (%s)", magicfile, magic_error(magic)); } else { const char *ctype = NULL; + magic_setflags(magic, magicmode); + switch (data_mode) { case SEND_RSRC: diff --git a/http_response_object.c b/http_response_object.c index dfc2476..c0c66af 100644 --- a/http_response_object.c +++ b/http_response_object.c @@ -500,7 +500,7 @@ PHP_METHOD(HttpResponse, getContentType) } /* }}} */ -/* {{{ proto static string HttpResponse::guessContentType(string magic_file[, long magic_mode]) +/* {{{ proto static string HttpResponse::guessContentType(string magic_file[, long magic_mode = MAGIC_MIME]) * * Attempts to guess the content type of supplied payload through libmagic. */ @@ -512,6 +512,9 @@ PHP_METHOD(HttpResponse, guessContentType) RETVAL_NULL(); +#ifdef HTTP_HAVE_MAGIC + magic_mode = MAGIC_MIME; + SET_EH_THROW_HTTP(); if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &magic_file, &magic_file_len, &magic_mode)) { switch (Z_LVAL_P(GET_STATIC_PROP(mode))) { @@ -542,6 +545,7 @@ PHP_METHOD(HttpResponse, guessContentType) } } SET_EH_NORMAL(); +#endif } /* }}} */