- use exceptions in constructors and HttpRequest::send()
authorMichael Wallner <mike@php.net>
Tue, 12 Apr 2005 17:45:34 +0000 (17:45 +0000)
committerMichael Wallner <mike@php.net>
Tue, 12 Apr 2005 17:45:34 +0000 (17:45 +0000)
config.m4
config.w32
http.c
http_exception_object.c [new file with mode: 0644]
http_methods.c
package.xml
php_http_exception_object.h [new file with mode: 0644]
php_http_std_defs.h

index 9fb219feb5792458a78f50d31cceb13bc2c47e76..8b3a49f107c0932d3a29ed7a4a49c31011602c88 100644 (file)
--- 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])
index 93e5330f13faab6867a23fb76301ee2cee1e7a63..fcb66b60c3b3393866f1a6876da9199c865b52e7 100644 (file)
@@ -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 50f387b81363d3b98eeaf69d2669483e4b742631..3902443bde951d6a5d8e861079c6947cdf55e90f 100644 (file)
--- 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 (file)
index 0000000..5af0dc5
--- /dev/null
@@ -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 <mike@php.net>               |
+   +----------------------------------------------------------------------+
+*/
+
+/* $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
+ */
+
index a6e2f2007ff3fc0453f9fc6b455c8161c2661d57..6b7794780f59df7999770b32f9ceb06265c0104d 100644 (file)
@@ -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);
 }
 /* }}} */
 /* }}} */
index 85516db8493b71f22cf36297c04d59acea5886c5..53a26c6238672cfcf2214e525228451a6987b872 100644 (file)
@@ -66,6 +66,7 @@
       <file role="src">php_http_message_object.h</file>
       <file role="src">php_http_request_object.h</file>
       <file role="src">php_http_response_object.h</file>
+      <file role="src">php_http_exception_object.h</file>
       
       <file role="src">http.c</file>
       <file role="src">http_functions.c</file>
@@ -83,6 +84,7 @@
       <file role="src">http_message_object.c</file>
       <file role="src">http_request_object.c</file>
       <file role="src">http_response_object.c</file>
+      <file role="src">http_exception_object.c</file>
       
     </dir>
   </filelist>
diff --git a/php_http_exception_object.h b/php_http_exception_object.h
new file mode 100644 (file)
index 0000000..423704a
--- /dev/null
@@ -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 <mike@php.net>               |
+   +----------------------------------------------------------------------+
+*/
+
+/* $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
+ */
+
index e79a9b0be29dd9d6dfef22aec7c8aa3e867692eb..49591cd578476612499a4f51e7394147ce0a6e92 100644 (file)
@@ -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 */
 /* }}} */