From 1374172d6c717de67faf91519b100b5348329aee Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Thu, 8 Jun 2006 22:07:39 +0000 Subject: [PATCH] - add HttpMessage::guessContentType() - release 1.0.0 --- docs/functions.html | 10 ++++++++- http_message_object.c | 42 ++++++++++++++++++++++++++++++++++++++ http_send_api.c | 10 ++++----- http_util_object.c | 2 ++ package.xml | 28 +++++++++++++------------ package2.xml | 9 +++++--- php_http_message_object.h | 1 + tests/HttpRequest_010.phpt | 1 + 8 files changed, 80 insertions(+), 23 deletions(-) diff --git a/docs/functions.html b/docs/functions.html index 25b14cc..b939e06 100644 --- a/docs/functions.html +++ b/docs/functions.html @@ -576,6 +576,13 @@ HttpMessage::TYPE_REQUEST or supplied URL was empty.

Set the HTTP Protocol version of the Message.

Expects a string parameter containing the HTTP protocol version.

Returns TRUE on success, or FALSE if supplied version is out of range (1.0/1.1).

+

string HttpMessage::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME])

+

Attempts to guess the content type of supplied payload through libmagic.

+

Expects a string parameter specifying the magic.mime database to use.
+Additionally accepts an optional int parameter, being flags for libmagic.

+

Returns the guessed content type on success, or FALSE on failure.

+

Throws HttpRuntimeException, HttpInvalidParamException
+if http.only_exceptions is TRUE.

HttpMessage HttpMessage::getParentMessage()

Get parent Message.

Returns the parent HttpMessage on success, or NULL if there's none.

@@ -1289,6 +1296,7 @@ http.cache_log is set.

  • HttpMessage::setRequestUrl()
  • HttpMessage::getHttpVersion()
  • HttpMessage::setHttpVersion()
  • +
  • HttpMessage::guessContentType()
  • HttpMessage::getParentMessage()
  • HttpMessage::send()
  • HttpMessage::toString()
  • @@ -1434,7 +1442,7 @@ http.cache_log is set.

    -

    Generated at: Sun, 28 May 2006 17:55:39 +0200

    +

    Generated at: Thu, 08 Jun 2006 23:59:56 +0200

    diff --git a/http_message_object.c b/http_message_object.c index 3d736cd..56b1a1e 100644 --- a/http_message_object.c +++ b/http_message_object.c @@ -14,6 +14,7 @@ #define HTTP_WANT_SAPI #define HTTP_WANT_CURL +#define HTTP_WANT_MAGIC #include "php_http.h" #ifdef ZEND_ENGINE_2 @@ -94,6 +95,11 @@ HTTP_BEGIN_ARGS(setHttpVersion, 1) HTTP_ARG_VAL(http_version, 0) HTTP_END_ARGS; +HTTP_BEGIN_ARGS(guessContentType, 1) + HTTP_ARG_VAL(magic_file, 0) + HTTP_ARG_VAL(magic_mode, 0) +HTTP_END_ARGS; + HTTP_EMPTY_ARGS(getParentMessage); HTTP_EMPTY_ARGS(send); HTTP_BEGIN_ARGS(toString, 0) @@ -149,6 +155,7 @@ zend_function_entry http_message_object_fe[] = { HTTP_MESSAGE_ME(setRequestUrl, ZEND_ACC_PUBLIC) HTTP_MESSAGE_ME(getHttpVersion, ZEND_ACC_PUBLIC) HTTP_MESSAGE_ME(setHttpVersion, ZEND_ACC_PUBLIC) + HTTP_MESSAGE_ME(guessContentType, ZEND_ACC_PUBLIC) HTTP_MESSAGE_ME(getParentMessage, ZEND_ACC_PUBLIC) HTTP_MESSAGE_ME(send, ZEND_ACC_PUBLIC) HTTP_MESSAGE_ME(toString, ZEND_ACC_PUBLIC) @@ -1126,6 +1133,41 @@ PHP_METHOD(HttpMessage, setHttpVersion) } /* }}} */ +/* {{{ proto string HttpMessage::guessContentType(string magic_file[, int magic_mode = MAGIC_MIME]) + * + * Attempts to guess the content type of supplied payload through libmagic. + * + * Expects a string parameter specifying the magic.mime database to use. + * Additionally accepts an optional int parameter, being flags for libmagic. + * + * Returns the guessed content type on success, or FALSE on failure. + * + * Throws HttpRuntimeException, HttpInvalidParamException + * if http.only_exceptions is TRUE. + */ +PHP_METHOD(HttpMessage, guessContentType) +{ +#ifdef HTTP_HAVE_MAGIC + char *magic_file, *ct = NULL; + int magic_file_len; + long magic_mode = MAGIC_MIME; + + RETVAL_FALSE; + SET_EH_THROW_HTTP(); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &magic_file, &magic_file_len, &magic_mode)) { + getObject(http_message_object, obj); + if ((ct = http_guess_content_type(magic_file, magic_mode, PHPSTR_VAL(&obj->message->body), PHPSTR_LEN(&obj->message->body), SEND_DATA))) { + RETVAL_STRING(ct, 0); + } + } + SET_EH_NORMAL(); +#else + http_error(HE_THROW, HTTP_E_RUNTIME, "Cannot guess Content-Type; libmagic not available"); + RETURN_FALSE; +#endif +} +/* }}} */ + /* {{{ proto HttpMessage HttpMessage::getParentMessage() * * Get parent Message. diff --git a/http_send_api.c b/http_send_api.c index 88a27e1..587870f 100644 --- a/http_send_api.c +++ b/http_send_api.c @@ -484,15 +484,13 @@ PHP_HTTP_API char *_http_guess_content_type(const char *magicfile, long magicmod char *ct = NULL; #ifdef HTTP_HAVE_MAGIC - struct magic_set *magic; + struct magic_set *magic = NULL; HTTP_CHECK_OPEN_BASEDIR(magicfile, return NULL); - /* magic_load() fails if MAGIC_MIME is set because it - cowardly adds .mime to the file name */ - magic = magic_open(magicmode &~ MAGIC_MIME); - - if (!magic) { + if (!data_ptr) { + http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Supplied payload is empty"); + } else if (!(magic = magic_open(magicmode &~ MAGIC_MIME))) { 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' (%s)", magicfile, magic_error(magic)); diff --git a/http_util_object.c b/http_util_object.c index 2d46aa8..ad75f7a 100644 --- a/http_util_object.c +++ b/http_util_object.c @@ -94,6 +94,7 @@ HTTP_BEGIN_ARGS(chunkedDecode, 1) HTTP_ARG_VAL(encoded_string, 0) HTTP_END_ARGS; +#ifdef HTTP_HAVE_ZLIB HTTP_BEGIN_ARGS(deflate, 1) HTTP_ARG_VAL(plain, 0) HTTP_ARG_VAL(flags, 0) @@ -102,6 +103,7 @@ HTTP_END_ARGS; HTTP_BEGIN_ARGS(inflate, 1) HTTP_ARG_VAL(encoded, 0) HTTP_END_ARGS; +#endif HTTP_BEGIN_ARGS(support, 0) HTTP_ARG_VAL(feature, 0) diff --git a/package.xml b/package.xml index 775bedc..45aa0d9 100644 --- a/package.xml +++ b/package.xml @@ -23,19 +23,16 @@ support. Parallel requests are available for PHP 5 and greater. - 1.0.0RC5 - 2006-05-28 + 1.0.0 + 2006-06-09 BSD, revised - beta - + Added HttpRequest::enableCookies() and HttpRequest::resetCookies([bool session_only=FALSE]) -+ Added optional flags argument to http_parse_params() -+ Added HTTP_PARAMS_ALLOW_COMMA, HTTP_PARAMS_ALLOW_FAILURE, HTTP_PARAMS_RAISE_ERROR constants -* Fixed http_build_url("./path") if REQUEST_URI is empty -* Fixed http_parse_params("foo;bar") returning "foo" and "ar" -* Fixed return value of http_parse_params() Object{"params"=>Array("value", Array("name"=>"value"), ...)} -* Fixed HttpMessage::setRequestMethod() errenously issuing a warning about an unknown request method -* Fixed bugs introduced by using the new REQUEST_TIME server variable -! NOTE: Many INI settings have been renamed to comply with the internal structure + stable + + Added --with[out]-http-shared-deps configure option (dependencies on shared extensions) ++ Added INI entries: http.log.not_found, http.send.not_found_404 ++ Added HttpMessage::guessContentType() +* Fixed build on Debian systems where access to Curl_* functions is prohibited +* Fixed empty Cache-Control header if not customly set with HttpResponse +* Reset Content-Disposition and Content-Type if file is not found by http_send_file() etc @@ -43,8 +40,9 @@ support. Parallel requests are available for PHP 5 and greater. - + + @@ -59,6 +57,7 @@ support. Parallel requests are available for PHP 5 and greater. + @@ -131,6 +130,7 @@ support. Parallel requests are available for PHP 5 and greater. + @@ -196,6 +196,7 @@ support. Parallel requests are available for PHP 5 and greater. + @@ -250,6 +251,7 @@ support. Parallel requests are available for PHP 5 and greater. + diff --git a/package2.xml b/package2.xml index f3552fa..be45841 100644 --- a/package2.xml +++ b/package2.xml @@ -30,17 +30,18 @@ support. Parallel requests are available for PHP 5 and greater. 2006-00-00 - 1.0.0dev + 1.0.0 1.0.0 - beta - beta + stable + stable BSD, revised + @@ -314,6 +316,7 @@ support. Parallel requests are available for PHP 5 and greater. + diff --git a/php_http_message_object.h b/php_http_message_object.h index 1004ac6..38030ad 100644 --- a/php_http_message_object.h +++ b/php_http_message_object.h @@ -95,6 +95,7 @@ PHP_METHOD(HttpMessage, getRequestUrl); PHP_METHOD(HttpMessage, setRequestUrl); PHP_METHOD(HttpMessage, getHttpVersion); PHP_METHOD(HttpMessage, setHttpVersion); +PHP_METHOD(HttpMessage, guessContentType); PHP_METHOD(HttpMessage, getParentMessage); PHP_METHOD(HttpMessage, send); PHP_METHOD(HttpMessage, toString); diff --git a/tests/HttpRequest_010.phpt b/tests/HttpRequest_010.phpt index b8bcaa5..4559be2 100644 --- a/tests/HttpRequest_010.phpt +++ b/tests/HttpRequest_010.phpt @@ -4,6 +4,7 @@ HttpRequest cookie API --FILE--