- wrap request exceptions into one request pool exception
authorMichael Wallner <mike@php.net>
Wed, 30 Nov 2005 18:46:53 +0000 (18:46 +0000)
committerMichael Wallner <mike@php.net>
Wed, 30 Nov 2005 18:46:53 +0000 (18:46 +0000)
# I don't know if this is a good idea and it definitly needs some more testing

http_exception_object.c
http_request_pool_api.c
php_http_exception_object.h

index b6594bac3f98a952b1d2f5b21ac0342dfe2be951..60966a9bbaf8e9ae03fe5c8036792fcbb736daec 100644 (file)
@@ -23,9 +23,7 @@
 
 #include "php_http_exception_object.h"
 
-#define HTTP_EX_DEF_CE http_exception_object_ce
 zend_class_entry *http_exception_object_ce;
-#define HTTP_EX_CE(name) http_ ##name## _exception_object_ce
 zend_class_entry *HTTP_EX_CE(runtime);
 zend_class_entry *HTTP_EX_CE(header);
 zend_class_entry *HTTP_EX_CE(malformed_headers);
@@ -54,6 +52,8 @@ PHP_MINIT_FUNCTION(http_exception_object)
        HTTP_REGISTER_EXCEPTION(HttpSocketException, HTTP_EX_CE(socket), HTTP_EX_DEF_CE);
        HTTP_REGISTER_EXCEPTION(HttpResponseException, HTTP_EX_CE(response), HTTP_EX_DEF_CE);
        HTTP_REGISTER_EXCEPTION(HttpUrlException, HTTP_EX_CE(url), HTTP_EX_DEF_CE);
+       
+       zend_declare_property_null(HTTP_EX_CE(request_pool), "requestExceptions", sizeof("requestExceptions")-1, ZEND_ACC_PUBLIC TSRMLS_CC);
 
        HTTP_LONG_CONSTANT("HTTP_E_RUNTIME", HTTP_E_RUNTIME);
        HTTP_LONG_CONSTANT("HTTP_E_INVALID_PARAM", HTTP_E_INVALID_PARAM);
index 48c1cde8a2995bb1496e9192e8373549cce4c350..6bbde400d1b929ef4888b6526b57df5d10a2098e 100644 (file)
@@ -22,6 +22,7 @@
 #if defined(ZEND_ENGINE_2) && defined(HTTP_HAVE_CURL)
 
 #include "php_http_api.h"
+#include "php_http_exception_object.h"
 #include "php_http_request_api.h"
 #include "php_http_request_object.h"
 #include "php_http_request_pool_api.h"
@@ -37,6 +38,8 @@ ZEND_EXTERN_MODULE_GLOBALS(http);
 #      define curl_multi_strerror(dummy) "unknown error"
 #endif
 
+#define http_request_pool_wrap_exception(o, n) _http_request_pool_wrap_exception((o), (n) TSRMLS_CC)
+static inline void _http_request_pool_wrap_exception(zval *old_exception, zval *new_exception TSRMLS_DC);
 static void http_request_pool_freebody(http_request_callback_ctx **body);
 static int http_request_pool_compare_handles(void *h1, void *h2);
 
@@ -272,11 +275,25 @@ PHP_HTTP_API int _http_request_pool_perform(http_request_pool *pool TSRMLS_DC)
        while ((msg = curl_multi_info_read(pool->ch, &remaining))) {
                if (CURLMSG_DONE == msg->msg) {
                        if (CURLE_OK != msg->data.result) {
+                               zval *old_exception = EG(exception);
+                               
+                               if (old_exception) {
+                                       EG(exception) = NULL;
+                               }
                                http_error(HE_WARNING, HTTP_E_REQUEST, curl_easy_strerror(msg->data.result));
+                               if (old_exception) {
+                                       http_request_pool_wrap_exception(old_exception, EG(exception));
+                               }
                        }
                        zend_llist_apply_with_argument(&pool->handles, (llist_apply_with_arg_func_t) http_request_pool_responsehandler, msg->easy_handle TSRMLS_CC);
                }
        }
+       if (EG(exception)) {
+               zval *exception = EG(exception);
+               
+               EG(exception) = NULL;
+               zend_throw_exception_object(exception TSRMLS_CC);
+       }
        
        return pool->unfinished;
 }
@@ -314,6 +331,43 @@ void _http_request_pool_responsehandler(zval **req, CURL *ch TSRMLS_DC)
 
 /*#*/
 
+/* {{{ static void http_request_pool_wrap_exception(zval *, zval *) */
+static inline void _http_request_pool_wrap_exception(zval *old_exception, zval *new_exception TSRMLS_DC)
+{
+       zend_class_entry *ce = HTTP_EX_CE(request_pool);
+       
+       /*      if old_exception is already an HttpRequestPoolException append the new one, 
+               else create a new HttpRequestPoolException and append the old and new exceptions */
+       if (Z_OBJCE_P(old_exception) == ce) {
+               zval *exprop;
+               
+               exprop = zend_read_property(ce, old_exception, "requestExceptions", sizeof("requestExceptions")-1, 0 TSRMLS_CC);
+               SEP_PROP(&exprop);
+               convert_to_array(exprop);
+               
+               add_next_index_zval(exprop, new_exception);
+               zend_update_property(ce, old_exception, "requestExceptions", sizeof("requestExceptions")-1, exprop TSRMLS_CC);
+               zval_ptr_dtor(&exprop);
+               
+               EG(exception) = old_exception;
+       } else {
+               zval *exval, *exprop;
+               
+               MAKE_STD_ZVAL(exval);
+               object_init_ex(exval, ce);
+               MAKE_STD_ZVAL(exprop);
+               array_init(exprop);
+               
+               add_next_index_zval(exprop, old_exception);
+               add_next_index_zval(exprop, new_exception);
+               zend_update_property(ce, exval, "requestExceptions", sizeof("requestExceptions")-1, exprop TSRMLS_CC);
+               zval_ptr_dtor(&exprop);
+               
+               EG(exception) = exval;
+       }
+}
+/* }}} */
+
 /* {{{ static void http_request_pool_freebody(http_request_ctx **) */
 static void http_request_pool_freebody(http_request_callback_ctx **body)
 {
index 478aa25686df8d3eb54f83f74e8abf24525531d6..68c462f6e5510c816cd2e8d6fa8f7db561b64b37 100644 (file)
 
 PHP_MINIT_FUNCTION(http_exception_object);
 
+#define HTTP_EX_DEF_CE http_exception_object_ce
+#define HTTP_EX_CE(name) http_ ##name## _exception_object_ce
+
 extern zend_class_entry *http_exception_object_ce;
 extern zend_function_entry http_exception_object_fe[];
+extern zend_class_entry *HTTP_EX_CE(runtime);
+extern zend_class_entry *HTTP_EX_CE(header);
+extern zend_class_entry *HTTP_EX_CE(malformed_headers);
+extern zend_class_entry *HTTP_EX_CE(request_method);
+extern zend_class_entry *HTTP_EX_CE(message_type);
+extern zend_class_entry *HTTP_EX_CE(invalid_param);
+extern zend_class_entry *HTTP_EX_CE(encoding);
+extern zend_class_entry *HTTP_EX_CE(request);
+extern zend_class_entry *HTTP_EX_CE(request_pool);
+extern zend_class_entry *HTTP_EX_CE(socket);
+extern zend_class_entry *HTTP_EX_CE(response);
+extern zend_class_entry *HTTP_EX_CE(url);
 
 #define http_exception_get_default _http_exception_get_default
 extern zend_class_entry *_http_exception_get_default();