- commonize some error messages
authorMichael Wallner <mike@php.net>
Fri, 7 Oct 2005 15:19:37 +0000 (15:19 +0000)
committerMichael Wallner <mike@php.net>
Fri, 7 Oct 2005 15:19:37 +0000 (15:19 +0000)
http_message_object.c
http_request_api.c
http_request_object.c
http_request_pool_api.c
http_response_object.c
http_send_api.c
php_http_api.h

index 759c75a94f8cfee7ea7b06487d766f1a62fd0d6a..d588192e2fc0e8e523eba35c1297f32f6a2090ff 100644 (file)
@@ -702,12 +702,7 @@ PHP_METHOD(HttpMessage, getResponseCode)
 
        IF_RETVAL_USED {
                getObject(http_message_object, obj);
-
-               if (!HTTP_MSG_TYPE(RESPONSE, obj->message)) {
-                       http_error(HE_NOTICE, HTTP_E_MESSAGE_TYPE, "HttpMessage is not of type HTTP_MSG_RESPONSE");
-                       RETURN_FALSE;
-               }
-
+               HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
                RETURN_LONG(obj->message->http.info.response.code);
        }
 }
@@ -727,10 +722,7 @@ PHP_METHOD(HttpMessage, setResponseCode)
        long code;
        getObject(http_message_object, obj);
 
-       if (!HTTP_MSG_TYPE(RESPONSE, obj->message)) {
-               http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is not of type HTTP_MSG_RESPONSE");
-               RETURN_FALSE;
-       }
+       HTTP_CHECK_MESSAGE_TYPE_RESPONSE(obj->message, RETURN_FALSE);
 
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
                RETURN_FALSE;
@@ -758,12 +750,7 @@ PHP_METHOD(HttpMessage, getRequestMethod)
 
        IF_RETVAL_USED {
                getObject(http_message_object, obj);
-
-               if (!HTTP_MSG_TYPE(REQUEST, obj->message)) {
-                       http_error(HE_NOTICE, HTTP_E_MESSAGE_TYPE, "HttpMessage is not of type HTTP_MSG_REQUEST");
-                       RETURN_FALSE;
-               }
-
+               HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
                RETURN_STRING(obj->message->http.info.request.method, 1);
        }
 }
@@ -784,10 +771,7 @@ PHP_METHOD(HttpMessage, setRequestMethod)
        int method_len;
        getObject(http_message_object, obj);
 
-       if (!HTTP_MSG_TYPE(REQUEST, obj->message)) {
-               http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is not of type HTTP_MSG_REQUEST");
-               RETURN_FALSE;
-       }
+       HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
 
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len)) {
                RETURN_FALSE;
@@ -819,12 +803,7 @@ PHP_METHOD(HttpMessage, getRequestUri)
 
        IF_RETVAL_USED {
                getObject(http_message_object, obj);
-
-               if (!HTTP_MSG_TYPE(REQUEST, obj->message)) {
-                       http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is not of type HTTP_MSG_REQUEST");
-                       RETURN_FALSE;
-               }
-
+               HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
                RETURN_STRING(obj->message->http.info.request.URI, 1);
        }
 }
@@ -845,13 +824,10 @@ PHP_METHOD(HttpMessage, setRequestUri)
        int URIlen;
        getObject(http_message_object, obj);
 
-       if (!HTTP_MSG_TYPE(REQUEST, obj->message)) {
-               http_error(HE_WARNING, HTTP_E_MESSAGE_TYPE, "HttpMessage is not of type HTTP_MSG_REQUEST");
-               RETURN_FALSE;
-       }
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &URI, &URIlen)) {
                RETURN_FALSE;
        }
+       HTTP_CHECK_MESSAGE_TYPE_REQUEST(obj->message, RETURN_FALSE);
        if (URIlen < 1) {
                http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Cannot set HttpMessage::requestUri to an empty string");
                RETURN_FALSE;
index d9b1a2198a06067823881907a09bbad592407856..2e3118114f9e282944613788493e41012d634346 100644 (file)
@@ -739,14 +739,9 @@ PHP_HTTP_API void _http_request_info(CURL *ch, HashTable *info TSRMLS_DC)
 PHP_HTTP_API STATUS _http_request_ex(CURL *ch, http_request_method meth, char *url, http_request_body *body, HashTable *options, HashTable *info, phpstr *response TSRMLS_DC)
 {
        STATUS status;
-       zend_bool clean_curl;
+       zend_bool clean_curl = !ch;
 
-       if ((clean_curl = (!ch))) {
-               if (!(ch = curl_easy_init())) {
-                       http_error(HE_WARNING, HTTP_E_REQUEST, "Could not initialize curl.");
-                       return FAILURE;
-               }
-       }
+       HTTP_CHECK_CURL_INIT(ch, curl_easy_init(), return FAILURE);
 
        status =        ((SUCCESS == http_request_init(ch, meth, url, body, options)) &&
                                (SUCCESS == http_request_exec(ch, info, response, NULL))) ? SUCCESS : FAILURE;
index cbb8b2be93b0810b521c174a54b9c597021b8898..7b1d82bec99ac92b1c4d3a909bea9d8f87a041af 100644 (file)
@@ -424,10 +424,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
        if (!body) {
                return FAILURE;
        }
-       if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) {
-               http_error(HE_WARNING, HTTP_E_REQUEST, "Could not initilaize curl");
-               return FAILURE;
-       }
+       HTTP_CHECK_CURL_INIT(obj->ch, curl_easy_init(), return FAILURE);
 
        URL = convert_to_type_ex(IS_STRING, GET_PROP(obj, url));
        // HTTP_URI_MAXLEN+1 long char *
@@ -1033,11 +1030,7 @@ PHP_METHOD(HttpRequest, setContentType)
                RETURN_FALSE;
        }
 
-       if (!strchr(ctype, '/')) {
-               http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", ctype);
-               RETURN_FALSE;
-       }
-
+       HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
        UPD_STRL(obj, contentType, ctype, ct_len);
        RETURN_TRUE;
 }
@@ -1341,10 +1334,7 @@ PHP_METHOD(HttpRequest, addPostFile)
        }
 
        if (type_len) {
-               if (!strchr(type, '/')) {
-                       http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to contain a primary and a secondary part", type);
-                       RETURN_FALSE;
-               }
+               HTTP_CHECK_CONTENT_TYPE(type, RETURN_FALSE);
        } else {
                type = "application/x-octetstream";
                type_len = sizeof("application/x-octetstream") - 1;
index 76bd181f0764a023b2b9b99a19c5dfbcbdcd2397..cca4fbaf0241b5d2a661b99711b6ba0c41f322d6 100644 (file)
@@ -55,14 +55,12 @@ PHP_HTTP_API http_request_pool *_http_request_pool_init(http_request_pool *pool
                pool->ch = NULL;
        }
 
+       HTTP_CHECK_CURL_INIT(pool->ch, curl_multi_init(), ;);
        if (!pool->ch) {
-               if (!(pool->ch = curl_multi_init())) {
-                       http_error(HE_WARNING, HTTP_E_REQUEST, "Could not initialize curl");
-                       if (free_pool) {
-                               efree(pool);
-                       }
-                       return NULL;
+               if (free_pool) {
+                       efree(pool);
                }
+               return NULL;
        }
 
        pool->unfinished = 0;
index 0e1eb29abd7455fdb3a26c9756722cc1498a3a30..8be3efd7e988f1dc9308576d7b00df8e5bc2255c 100644 (file)
@@ -533,11 +533,7 @@ PHP_METHOD(HttpResponse, setContentType)
                RETURN_FALSE;
        }
 
-       if (!strchr(ctype, '/')) {
-               http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content type '%s' doesn't seem to contain a primary and a secondary part", ctype);
-               RETURN_FALSE;
-       }
-
+       HTTP_CHECK_CONTENT_TYPE(ctype, RETURN_FALSE);
        RETURN_SUCCESS(UPD_STATIC_STRL(contentType, ctype, ctype_len));
 }
 /* }}} */
index f22a3d1fd398fb2a3e75ee46ff926accb5523659..6fc9cc00916f3bfbc23ebf30b817bf1a780d0623 100644 (file)
@@ -231,10 +231,7 @@ PHP_HTTP_API STATUS _http_send_etag_ex(const char *etag, size_t etag_len, char *
 /* {{{ STATUS http_send_content_type(char *, size_t) */
 PHP_HTTP_API STATUS _http_send_content_type(const char *content_type, size_t ct_len TSRMLS_DC)
 {
-       if (!strchr(content_type, '/')) {
-               http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, "Content-Type '%s' doesn't seem to consist of a primary and a secondary part", content_type);
-               return FAILURE;
-       }
+       HTTP_CHECK_CONTENT_TYPE(content_type, return FAILURE);
 
        /* remember for multiple ranges */
        STR_FREE(HTTP_G(send).content_type);
index 826f6cd198572b66b0192f8c26f46e431513f0ea..4f40fa520ab035a07c67c46edd22adaa31e2f069 100644 (file)
@@ -36,6 +36,29 @@ extern STATUS _http_parse_key_list(const char *list, HashTable *items, char sepa
 #define http_error_ex _http_error_ex
 extern void _http_error_ex(long type TSRMLS_DC, long code, const char *format, ...);
 
+#define HTTP_CHECK_CURL_INIT(ch, init, action) \
+       if ((!(ch)) && (!((ch) = init))) { \
+               http_error(HE_WARNING, HTTP_E_REQUEST, "Could not initialize curl"); \
+               action; \
+       }
+#define HTTP_CHECK_CONTENT_TYPE(ct, action) \
+       if (!strchr((ct), '/')) { \
+               http_error_ex(HE_WARNING, HTTP_E_INVALID_PARAM, \
+                       "Content type \"%s\" does not seem to contain a primary and a secondary part", (ct)); \
+               action; \
+       }
+#define HTTP_CHECK_MESSAGE_TYPE_RESPONSE(msg, action) \
+               if (!HTTP_MSG_TYPE(RESPONSE, (msg))) { \
+                       http_error(HE_NOTICE, HTTP_E_MESSAGE_TYPE, "HttpMessage is not of type HTTP_MSG_RESPONSE"); \
+                       action; \
+               }
+#define HTTP_CHECK_MESSAGE_TYPE_REQUEST(msg, action) \
+               if (!HTTP_MSG_TYPE(REQUEST, (msg))) { \
+                       http_error(HE_NOTICE, HTTP_E_MESSAGE_TYPE, "HttpMessage is not of type HTTP_MSG_REQUEST"); \
+                       action; \
+               }
+
+
 #define http_log(f, i, m) _http_log_ex((f), (i), (m) TSRMLS_CC)
 extern void http_log_ex(char *file, const char *ident, const char *message TSRMLS_DC);