From dd579c42c06ebfe26ea2ff8ee6106a22d27e3340 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 7 Oct 2005 15:19:37 +0000 Subject: [PATCH] - commonize some error messages --- http_message_object.c | 36 ++++++------------------------------ http_request_api.c | 9 ++------- http_request_object.c | 16 +++------------- http_request_pool_api.c | 10 ++++------ http_response_object.c | 6 +----- http_send_api.c | 5 +---- php_http_api.h | 23 +++++++++++++++++++++++ 7 files changed, 40 insertions(+), 65 deletions(-) diff --git a/http_message_object.c b/http_message_object.c index 759c75a..d588192 100644 --- a/http_message_object.c +++ b/http_message_object.c @@ -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; diff --git a/http_request_api.c b/http_request_api.c index d9b1a21..2e31181 100644 --- a/http_request_api.c +++ b/http_request_api.c @@ -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; diff --git a/http_request_object.c b/http_request_object.c index cbb8b2b..7b1d82b 100644 --- a/http_request_object.c +++ b/http_request_object.c @@ -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; diff --git a/http_request_pool_api.c b/http_request_pool_api.c index 76bd181..cca4fba 100644 --- a/http_request_pool_api.c +++ b/http_request_pool_api.c @@ -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; diff --git a/http_response_object.c b/http_response_object.c index 0e1eb29..8be3efd 100644 --- a/http_response_object.c +++ b/http_response_object.c @@ -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)); } /* }}} */ diff --git a/http_send_api.c b/http_send_api.c index f22a3d1..6fc9cc0 100644 --- a/http_send_api.c +++ b/http_send_api.c @@ -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); diff --git a/php_http_api.h b/php_http_api.h index 826f6cd..4f40fa5 100644 --- a/php_http_api.h +++ b/php_http_api.h @@ -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); -- 2.30.2