From fcebff9b60cdeede7970f5b4bb7b01318b4415c3 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 12 Apr 2005 17:45:34 +0000 Subject: [PATCH] - use exceptions in constructors and HttpRequest::send() --- config.m4 | 3 +- config.w32 | 2 +- http.c | 2 ++ http_exception_object.c | 71 +++++++++++++++++++++++++++++++++++++ http_methods.c | 58 +++++++++++++++--------------- package.xml | 2 ++ php_http_exception_object.h | 50 ++++++++++++++++++++++++++ php_http_std_defs.h | 5 +++ 8 files changed, 163 insertions(+), 30 deletions(-) create mode 100644 http_exception_object.c create mode 100644 php_http_exception_object.h diff --git a/config.m4 b/config.m4 index 9fb219f..8b3a49f 100644 --- a/config.m4 +++ b/config.m4 @@ -72,7 +72,8 @@ dnl ---- dnl DONE dnl ---- PHP_HTTP_SOURCES="http.c http_functions.c http_methods.c phpstr/phpstr.c \ - http_util_object.c http_message_object.c http_request_object.c http_response_object.c \ + http_util_object.c http_message_object.c http_request_object.c \ + http_response_object.c http_exception_object.c \ http_api.c http_auth_api.c http_cache_api.c http_curl_api.c http_date_api.c \ http_headers_api.c http_message_api.c http_send_api.c http_url_api.c" PHP_NEW_EXTENSION([http], $PHP_HTTP_SOURCES, [$ext_shared]) diff --git a/config.w32 b/config.w32 index 93e5330..fcb66b6 100644 --- a/config.w32 +++ b/config.w32 @@ -5,7 +5,7 @@ ARG_ENABLE("http", "whether to enable extended HTTP support", "no"); if (PHP_HTTP != "no") { EXTENSION("http", - "http.c http_functions.c http_methods.c "+ + "http.c http_functions.c http_methods.c http_exception_object.c "+ "http_util_object.c http_message_object.c "+ "http_request_object.c http_response_object.c "+ "http_api.c http_auth_api.c http_cache_api.c "+ diff --git a/http.c b/http.c index 50f387b..3902443 100644 --- a/http.c +++ b/http.c @@ -44,6 +44,7 @@ # ifdef HTTP_HAVE_CURL # include "php_http_request_object.h" # endif +# include "php_http_exception_object.h" #endif #include "phpstr/phpstr.h" @@ -233,6 +234,7 @@ PHP_MINIT_FUNCTION(http) # ifdef HTTP_HAVE_CURL http_request_object_init(INIT_FUNC_ARGS_PASSTHRU); # endif /* HTTP_HAVE_CURL */ + http_exception_object_init(INIT_FUNC_ARGS_PASSTHRU); #endif /* ZEND_ENGINE_2 */ return SUCCESS; diff --git a/http_exception_object.c b/http_exception_object.c new file mode 100644 index 0000000..5af0dc5 --- /dev/null +++ b/http_exception_object.c @@ -0,0 +1,71 @@ +/* + +----------------------------------------------------------------------+ + | PECL :: http | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.0 of the PHP license, that | + | is bundled with this package in the file LICENSE, and is available | + | through the world-wide-web at http://www.php.net/license/3_0.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Copyright (c) 2004-2005 Michael Wallner | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "php.h" + +#include "php_http.h" +#include "php_http_std_defs.h" + +#ifdef ZEND_ENGINE_2 + +#include "php_http_exception_object.h" +#include "zend_exceptions.h" + +zend_class_entry *http_exception_object_ce; +zend_function_entry http_exception_object_fe[] = { + {NULL, NULL, NULL} +}; + +void _http_exception_object_init(INIT_FUNC_ARGS) +{ + HTTP_REGISTER_CLASS(HttpException, http_exception_object, zend_exception_get_default(), 0); +} + +zend_class_entry *_http_exception_get_default() +{ + return http_exception_object_ce; +} + +void _http_exception_throw_ce_ex(zend_class_entry *ce, int code TSRMLS_DC) +{ + static char * const errors[] = { + "Unkown Error" + }; + + if (!ce) { + ce = http_exception_get_default(); + } + + zend_throw_exception(ce, errors[code], code TSRMLS_CC); +} + +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/http_methods.c b/http_methods.c index a6e2f20..6b77947 100644 --- a/http_methods.c +++ b/http_methods.c @@ -33,6 +33,7 @@ #include "php_http_message_object.h" #include "php_http_response_object.h" #include "php_http_request_object.h" +#include "php_http_exception_object.h" #ifdef ZEND_ENGINE_2 @@ -51,13 +52,12 @@ PHP_METHOD(HttpResponse, __construct) zend_bool do_cache = 0, do_gzip = 0; getObject(http_response_object, obj); - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) { - // throw exception - return; + SET_EH_THROW_HTTP(); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bb", &do_cache, &do_gzip)) { + UPD_PROP(obj, long, cache, do_cache); + UPD_PROP(obj, long, gzip, do_gzip); } - - UPD_PROP(obj, long, cache, do_cache); - UPD_PROP(obj, long, gzip, do_gzip); + SET_EH_NORMAL(); } /* }}} */ @@ -553,7 +553,7 @@ PHP_METHOD(HttpMessage, setRaw) { zval *message; getObject(http_message_object, obj); - + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/", &message)) { return; } @@ -571,7 +571,7 @@ PHP_METHOD(HttpMessage, getBody) { zval *body; getObject(http_message_object, obj); - + NO_ARGS; body = GET_PROP(obj, body); @@ -587,9 +587,9 @@ PHP_METHOD(HttpMessage, getHeaders) { zval *headers; getObject(http_message_object, obj); - + NO_ARGS; - + headers = GET_PROP(obj, headers); array_init(return_value); array_copy(headers, return_value); @@ -613,22 +613,22 @@ PHP_METHOD(HttpRequest, __construct) long meth = -1; getObject(http_request_object, obj); - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) { - return; - } - - INIT_PARR(obj, options); - INIT_PARR(obj, responseInfo); - INIT_PARR(obj, responseData); - INIT_PARR(obj, postData); - INIT_PARR(obj, postFiles); + SET_EH_THROW_HTTP(); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &URL, &URL_len, &meth)) { + INIT_PARR(obj, options); + INIT_PARR(obj, responseInfo); + INIT_PARR(obj, responseData); + INIT_PARR(obj, postData); + INIT_PARR(obj, postFiles); - if (URL) { - UPD_PROP(obj, string, url, URL); - } - if (meth > -1) { - UPD_PROP(obj, long, method, meth); + if (URL) { + UPD_PROP(obj, string, url, URL); + } + if (meth > -1) { + UPD_PROP(obj, long, method, meth); + } } + SET_EH_NORMAL(); } /* }}} */ @@ -1518,6 +1518,8 @@ PHP_METHOD(HttpRequest, send) getObject(http_request_object, obj); NO_ARGS; + + SET_EH_THROW_HTTP(); if ((!obj->ch) && (!(obj->ch = curl_easy_init()))) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initilaize cURL"); @@ -1621,9 +1623,7 @@ PHP_METHOD(HttpRequest, send) efree(request_uri); /* final data handling */ - if (status != SUCCESS) { - RETURN_FALSE; - } else { + if (status == SUCCESS) { char *body = NULL; size_t body_len = 0; zval *zheaders; @@ -1645,7 +1645,9 @@ PHP_METHOD(HttpRequest, send) RETURN_TRUE; } - /* */ + + SET_EH_NORMAL(); + RETURN_SUCCESS(status); } /* }}} */ /* }}} */ diff --git a/package.xml b/package.xml index 85516db..53a26c6 100644 --- a/package.xml +++ b/package.xml @@ -66,6 +66,7 @@ php_http_message_object.h php_http_request_object.h php_http_response_object.h + php_http_exception_object.h http.c http_functions.c @@ -83,6 +84,7 @@ http_message_object.c http_request_object.c http_response_object.c + http_exception_object.c diff --git a/php_http_exception_object.h b/php_http_exception_object.h new file mode 100644 index 0000000..423704a --- /dev/null +++ b/php_http_exception_object.h @@ -0,0 +1,50 @@ +/* + +----------------------------------------------------------------------+ + | PECL :: http | + +----------------------------------------------------------------------+ + | This source file is subject to version 3.0 of the PHP license, that | + | is bundled with this package in the file LICENSE, and is available | + | through the world-wide-web at http://www.php.net/license/3_0.txt. | + | If you did not receive a copy of the PHP license and are unable to | + | obtain it through the world-wide-web, please send a note to | + | license@php.net so we can mail you a copy immediately. | + +----------------------------------------------------------------------+ + | Copyright (c) 2004-2005 Michael Wallner | + +----------------------------------------------------------------------+ +*/ + +/* $Id$ */ + +#ifndef PHP_HTTP_EXCEPTION_OBJECT_H +#define PHP_HTTP_EXCEPTION_OBJECT_H +#ifdef ZEND_ENGINE_2 + +extern zend_class_entry *http_exception_object_ce; +extern zend_function_entry http_exception_object_fe[]; + +#define http_exception_object_init _http_exception_object_init +extern void _http_exception_object_init(INIT_FUNC_ARGS); + +#define http_exception_get_default _http_exception_get_default +extern zend_class_entry *_http_exception_get_default(); + +#define http_exception_throw() http_exception_throw_ex(0) +#define http_exception_throw_ex(code) http_exception_throw_ce_ex(NULL, code) +#define http_exception_throw_ce(ce) http_exception_throw_ce_ex(ce, 0) +#define http_exception_throw_ce_ex(ce, code) _http_exception_throw_ce_ex(ce, code TSRMLS_CC) +extern void _http_exception_throw_ce_ex(zend_class_entry *ce, int code TSRMLS_DC); + +#define HTTP_E_UNKOWN 0 + +#endif +#endif + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ + diff --git a/php_http_std_defs.h b/php_http_std_defs.h index e79a9b0..49591cd 100644 --- a/php_http_std_defs.h +++ b/php_http_std_defs.h @@ -137,6 +137,11 @@ typedef int STATUS; } \ } +# define SET_EH_THROW() SET_EH_THROW_EX(zend_exception_get_default()) +# define SET_EH_THROW_HTTP() SET_EH_THROW_EX(http_exception_get_default()) +# define SET_EH_THROW_EX(ex) php_set_error_handling(EH_THROW, ex TSRMLS_CC) +# define SET_EH_NORMAL() php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC) + #endif /* ZEND_ENGINE_2 */ /* }}} */ -- 2.30.2