back to dev
[m6w6/ext-http] / php_http_client.c
index f23322d12a457ca8a6bc116176f164e9773b8638..4f0de3cb6fc604334470937c0881b4efea3e56a2 100644 (file)
@@ -1,3 +1,15 @@
+/*
+    +--------------------------------------------------------------------+
+    | PECL :: http                                                       |
+    +--------------------------------------------------------------------+
+    | Redistribution and use in source and binary forms, with or without |
+    | modification, are permitted provided that the conditions mentioned |
+    | in the accompanying LICENSE file are met.                          |
+    +--------------------------------------------------------------------+
+    | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+    +--------------------------------------------------------------------+
+*/
+
 #include "php_http_api.h"
 #include "php_http_client.h"
 
  */
 static HashTable php_http_client_drivers;
 
-PHP_HTTP_API STATUS php_http_client_driver_add(php_http_client_driver_t *driver)
+STATUS php_http_client_driver_add(php_http_client_driver_t *driver)
 {
        return zend_hash_add(&php_http_client_drivers, driver->name_str, driver->name_len + 1, (void *) driver, sizeof(php_http_client_driver_t), NULL);
 }
 
-PHP_HTTP_API STATUS php_http_client_driver_get(const char *name_str, size_t name_len, php_http_client_driver_t *driver)
+STATUS php_http_client_driver_get(const char *name_str, size_t name_len, php_http_client_driver_t *driver)
 {
        php_http_client_driver_t *tmp;
 
@@ -37,7 +49,7 @@ static int apply_driver_list(void *p, void *arg TSRMLS_DC)
        return ZEND_HASH_APPLY_KEEP;
 }
 
-PHP_HTTP_API void php_http_client_driver_list(HashTable *ht TSRMLS_DC)
+void php_http_client_driver_list(HashTable *ht TSRMLS_DC)
 {
        zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht TSRMLS_CC);
 }
@@ -145,7 +157,7 @@ static void queue_dtor(void *enqueued)
        }
 }
 
-PHP_HTTP_API php_http_client_t *php_http_client_init(php_http_client_t *h, php_http_client_ops_t *ops, php_resource_factory_t *rf, void *init_arg TSRMLS_DC)
+php_http_client_t *php_http_client_init(php_http_client_t *h, php_http_client_ops_t *ops, php_resource_factory_t *rf, void *init_arg TSRMLS_DC)
 {
        php_http_client_t *free_h = NULL;
 
@@ -166,9 +178,9 @@ PHP_HTTP_API php_http_client_t *php_http_client_init(php_http_client_t *h, php_h
 
        if (h->ops->init) {
                if (!(h = h->ops->init(h, init_arg))) {
-                       php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Could not initialize client");
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize client");
                        if (free_h) {
-                               efree(h);
+                               efree(free_h);
                        }
                }
        }
@@ -176,7 +188,7 @@ PHP_HTTP_API php_http_client_t *php_http_client_init(php_http_client_t *h, php_h
        return h;
 }
 
-PHP_HTTP_API php_http_client_t *php_http_client_copy(php_http_client_t *from, php_http_client_t *to)
+php_http_client_t *php_http_client_copy(php_http_client_t *from, php_http_client_t *to)
 {
        if (from->ops->copy) {
                return from->ops->copy(from, to);
@@ -185,19 +197,18 @@ PHP_HTTP_API php_http_client_t *php_http_client_copy(php_http_client_t *from, ph
        return NULL;
 }
 
-PHP_HTTP_API void php_http_client_dtor(php_http_client_t *h)
+void php_http_client_dtor(php_http_client_t *h)
 {
+       php_http_client_reset(h);
+
        if (h->ops->dtor) {
                h->ops->dtor(h);
        }
 
-       zend_llist_clean(&h->requests);
-       zend_llist_clean(&h->responses);
-
        php_resource_factory_free(&h->rf);
 }
 
-PHP_HTTP_API void php_http_client_free(php_http_client_t **h) {
+void php_http_client_free(php_http_client_t **h) {
        if (*h) {
                php_http_client_dtor(*h);
                efree(*h);
@@ -205,13 +216,13 @@ PHP_HTTP_API void php_http_client_free(php_http_client_t **h) {
        }
 }
 
-PHP_HTTP_API STATUS php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
+STATUS php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
 {
        TSRMLS_FETCH_FROM_CTX(h->ts);
 
        if (h->ops->enqueue) {
                if (php_http_client_enqueued(h, enqueue->request, NULL)) {
-                       php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Failed to enqueue request; request already in queue");
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to enqueue request; request already in queue");
                        return FAILURE;
                }
                return h->ops->enqueue(h, enqueue);
@@ -220,7 +231,7 @@ PHP_HTTP_API STATUS php_http_client_enqueue(php_http_client_t *h, php_http_clien
        return FAILURE;
 }
 
-PHP_HTTP_API STATUS php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request)
+STATUS php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request)
 {
        TSRMLS_FETCH_FROM_CTX(h->ts);
 
@@ -228,7 +239,7 @@ PHP_HTTP_API STATUS php_http_client_dequeue(php_http_client_t *h, php_http_messa
                php_http_client_enqueue_t *enqueue = php_http_client_enqueued(h, request, NULL);
 
                if (!enqueue) {
-                       php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Failed to dequeue request; request not in queue");
+                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to dequeue request; request not in queue");
                        return FAILURE;
                }
                return h->ops->dequeue(h, enqueue);
@@ -236,7 +247,7 @@ PHP_HTTP_API STATUS php_http_client_dequeue(php_http_client_t *h, php_http_messa
        return FAILURE;
 }
 
-PHP_HTTP_API php_http_client_enqueue_t *php_http_client_enqueued(php_http_client_t *h, void *compare_arg, php_http_client_enqueue_cmp_func_t compare_func)
+php_http_client_enqueue_t *php_http_client_enqueued(php_http_client_t *h, void *compare_arg, php_http_client_enqueue_cmp_func_t compare_func)
 {
        zend_llist_element *el = NULL;
 
@@ -256,7 +267,7 @@ PHP_HTTP_API php_http_client_enqueue_t *php_http_client_enqueued(php_http_client
        return el ? (php_http_client_enqueue_t *) el->data : NULL;
 }
 
-PHP_HTTP_API STATUS php_http_client_wait(php_http_client_t *h, struct timeval *custom_timeout)
+STATUS php_http_client_wait(php_http_client_t *h, struct timeval *custom_timeout)
 {
        if (h->ops->wait) {
                return h->ops->wait(h, custom_timeout);
@@ -265,7 +276,7 @@ PHP_HTTP_API STATUS php_http_client_wait(php_http_client_t *h, struct timeval *c
        return FAILURE;
 }
 
-PHP_HTTP_API int php_http_client_once(php_http_client_t *h)
+int php_http_client_once(php_http_client_t *h)
 {
        if (h->ops->once) {
                return h->ops->once(h);
@@ -274,7 +285,7 @@ PHP_HTTP_API int php_http_client_once(php_http_client_t *h)
        return FAILURE;
 }
 
-PHP_HTTP_API STATUS php_http_client_exec(php_http_client_t *h)
+STATUS php_http_client_exec(php_http_client_t *h)
 {
        if (h->ops->exec) {
                return h->ops->exec(h);
@@ -283,7 +294,7 @@ PHP_HTTP_API STATUS php_http_client_exec(php_http_client_t *h)
        return FAILURE;
 }
 
-PHP_HTTP_API void php_http_client_reset(php_http_client_t *h)
+void php_http_client_reset(php_http_client_t *h)
 {
        if (h->ops->reset) {
                h->ops->reset(h);
@@ -293,7 +304,7 @@ PHP_HTTP_API void php_http_client_reset(php_http_client_t *h)
        zend_llist_clean(&h->responses);
 }
 
-PHP_HTTP_API STATUS php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
+STATUS php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
 {
        if (h->ops->setopt) {
                return h->ops->setopt(h, opt, arg);
@@ -302,7 +313,7 @@ PHP_HTTP_API STATUS php_http_client_setopt(php_http_client_t *h, php_http_client
        return FAILURE;
 }
 
-PHP_HTTP_API STATUS php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void *res_ptr)
+STATUS php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void *res_ptr)
 {
        if (h->ops->getopt) {
                return h->ops->getopt(h, opt, arg, res_ptr);
@@ -318,6 +329,8 @@ void php_http_client_object_free(void *object TSRMLS_DC)
        php_http_client_object_t *o = (php_http_client_object_t *) object;
 
        php_http_client_free(&o->client);
+       php_http_object_method_dtor(&o->notify);
+       php_http_object_method_free(&o->update);
        zend_object_std_dtor((zend_object *) o TSRMLS_CC);
        efree(o);
 }
@@ -351,7 +364,7 @@ static void handle_history(zval *zclient, php_http_message_t *request, php_http_
 {
        zval *new_hist, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0 TSRMLS_CC);
        php_http_message_t *zipped = php_http_message_zip(response, request);
-       zend_object_value ov = php_http_message_object_new_ex(php_http_message_get_class_entry(), zipped, NULL TSRMLS_CC);
+       zend_object_value ov = php_http_message_object_new_ex(php_http_message_class_entry, zipped, NULL TSRMLS_CC);
 
        MAKE_STD_ZVAL(new_hist);
        ZVAL_OBJVAL(new_hist, ov, 0);
@@ -364,8 +377,9 @@ static void handle_history(zval *zclient, php_http_message_t *request, php_http_
        zval_ptr_dtor(&new_hist);
 }
 
-static STATUS handle_response(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_message_t **request, php_http_message_t **response)
+static STATUS handle_response(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_message_t **response)
 {
+       zend_bool dequeue = 0;
        zval zclient;
        php_http_message_t *msg;
        php_http_client_progress_state_t *progress;
@@ -377,9 +391,13 @@ static STATUS handle_response(void *arg, php_http_client_t *client, php_http_cli
        if ((msg = *response)) {
                php_http_message_object_t *msg_obj;
                zval *info, *zresponse, *zrequest;
+               HashTable *info_ht;
 
-               if (i_zend_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) {
-                       handle_history(&zclient, *request, *response TSRMLS_CC);
+               /* ensure the message is of type response (could be uninitialized in case of early error, like DNS) */
+               php_http_message_set_type(msg, PHP_HTTP_RESPONSE);
+
+               if (z_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) {
+                       handle_history(&zclient, e->request, *response TSRMLS_CC);
                }
 
                /* hard detach, redirects etc. are in the history */
@@ -387,7 +405,7 @@ static STATUS handle_response(void *arg, php_http_client_t *client, php_http_cli
                *response = NULL;
 
                MAKE_STD_ZVAL(zresponse);
-               ZVAL_OBJVAL(zresponse, php_http_message_object_new_ex(php_http_client_response_get_class_entry(), msg, &msg_obj TSRMLS_CC), 0);
+               ZVAL_OBJVAL(zresponse, php_http_message_object_new_ex(php_http_client_response_class_entry, msg, &msg_obj TSRMLS_CC), 0);
 
                MAKE_STD_ZVAL(zrequest);
                ZVAL_OBJVAL(zrequest, ((php_http_message_object_t *) e->opaque)->zv, 1);
@@ -395,9 +413,10 @@ static STATUS handle_response(void *arg, php_http_client_t *client, php_http_cli
                php_http_message_object_prepend(zresponse, zrequest, 1 TSRMLS_CC);
 
                MAKE_STD_ZVAL(info);
-               array_init(info);
-               php_http_client_getopt(client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, e->request, &Z_ARRVAL_P(info));
-               zend_update_property(php_http_client_response_get_class_entry(), zresponse, ZEND_STRL("transferInfo"), info TSRMLS_CC);
+               object_init(info);
+               info_ht = HASH_OF(info);
+               php_http_client_getopt(client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, e->request, &info_ht);
+               zend_update_property(php_http_client_response_class_entry, zresponse, ZEND_STRL("transferInfo"), info TSRMLS_CC);
                zval_ptr_dtor(&info);
 
                zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
@@ -405,16 +424,17 @@ static STATUS handle_response(void *arg, php_http_client_t *client, php_http_cli
 
                if (e->closure.fci.size) {
                        zval *retval = NULL;
+                       zend_error_handling zeh;
 
                        zend_fcall_info_argn(&e->closure.fci TSRMLS_CC, 1, &zresponse);
-                       with_error_handling(EH_NORMAL, NULL) {
-                               zend_fcall_info_call(&e->closure.fci, &e->closure.fcc, &retval, NULL TSRMLS_CC);
-                       } end_error_handling();
+                       zend_replace_error_handling(EH_NORMAL, NULL, &zeh TSRMLS_CC);
+                       zend_fcall_info_call(&e->closure.fci, &e->closure.fcc, &retval, NULL TSRMLS_CC);
+                       zend_restore_error_handling(&zeh TSRMLS_CC);
                        zend_fcall_info_argn(&e->closure.fci TSRMLS_CC, 0);
 
                        if (retval) {
-                               if (Z_TYPE_P(retval) == IS_BOOL && Z_BVAL_P(retval)) {
-                                       php_http_client_dequeue(client, e->request);
+                               if (Z_TYPE_P(retval) == IS_BOOL) {
+                                       dequeue = Z_BVAL_P(retval);
                                }
                                zval_ptr_dtor(&retval);
                        }
@@ -430,18 +450,27 @@ static STATUS handle_response(void *arg, php_http_client_t *client, php_http_cli
                client->callback.progress.func(client->callback.progress.arg, client, e, progress);
        }
 
+       if (dequeue) {
+               php_http_client_dequeue(client, e->request);
+       }
+
        return SUCCESS;
 }
 
 static void handle_progress(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_client_progress_state_t *progress)
 {
-       zval *zrequest, *zprogress, *retval = NULL, *zclient;
+       zval *zrequest, *zprogress, *zclient, **args[2];
+       php_http_client_object_t *client_obj = arg;
+       zend_error_handling zeh;
        TSRMLS_FETCH_FROM_CTX(client->ts);
 
        MAKE_STD_ZVAL(zclient);
-       ZVAL_OBJVAL(zclient, ((php_http_client_object_t *) arg)->zv, 1);
+       ZVAL_OBJVAL(zclient, client_obj->zv, 1);
+
        MAKE_STD_ZVAL(zrequest);
        ZVAL_OBJVAL(zrequest, ((php_http_message_object_t *) e->opaque)->zv, 1);
+       args[0] = &zrequest;
+
        MAKE_STD_ZVAL(zprogress);
        object_init(zprogress);
        add_property_bool(zprogress, "started", progress->started);
@@ -451,15 +480,15 @@ static void handle_progress(void *arg, php_http_client_t *client, php_http_clien
        add_property_double(zprogress, "dlnow", progress->dl.now);
        add_property_double(zprogress, "ultotal", progress->ul.total);
        add_property_double(zprogress, "ulnow", progress->ul.now);
-       with_error_handling(EH_NORMAL, NULL) {
-               zend_call_method_with_2_params(&zclient, NULL, NULL, "notify", &retval, zrequest, zprogress);
-       } end_error_handling();
+       args[1] = &zprogress;
+
+       zend_replace_error_handling(EH_NORMAL, NULL, &zeh TSRMLS_CC);
+       php_http_object_method_call(&client_obj->notify, zclient, NULL, 2, args TSRMLS_CC);
+       zend_restore_error_handling(&zeh TSRMLS_CC);
+
        zval_ptr_dtor(&zclient);
        zval_ptr_dtor(&zrequest);
        zval_ptr_dtor(&zprogress);
-       if (retval) {
-               zval_ptr_dtor(&retval);
-       }
 }
 
 static void response_dtor(void *data)
@@ -476,63 +505,66 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_construct, 0, 0, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, __construct)
 {
-       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                char *driver_str = NULL, *persistent_handle_str = NULL;
                int driver_len = 0, persistent_handle_len = 0;
+               php_http_client_driver_t driver;
+               php_resource_factory_t *rf = NULL;
+               php_http_client_object_t *obj;
+               zval *os;
 
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &driver_str, &driver_len, &persistent_handle_str, &persistent_handle_len)) {
-                       php_http_client_driver_t driver;
+               php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &driver_str, &driver_len, &persistent_handle_str, &persistent_handle_len), invalid_arg, return);
 
-                       if (SUCCESS == php_http_client_driver_get(driver_str, driver_len, &driver)) {
-                               php_resource_factory_t *rf = NULL;
-                               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
-                               zval *os;
+               if (SUCCESS != php_http_client_driver_get(driver_str, driver_len, &driver)) {
+                       php_http_throw(unexpected_val, "Failed to locate \"%s\" client request handler", driver_str);
+                       return;
+               }
 
-                               MAKE_STD_ZVAL(os);
-                               object_init_ex(os, spl_ce_SplObjectStorage);
-                               zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), os TSRMLS_CC);
-                               zval_ptr_dtor(&os);
+               MAKE_STD_ZVAL(os);
+               object_init_ex(os, spl_ce_SplObjectStorage);
+               zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), os TSRMLS_CC);
+               zval_ptr_dtor(&os);
 
-                               if (persistent_handle_len) {
-                                       char *name_str;
-                                       size_t name_len;
-                                       php_persistent_handle_factory_t *pf;
+               if (persistent_handle_len) {
+                       char *name_str;
+                       size_t name_len;
+                       php_persistent_handle_factory_t *pf;
 
-                                       name_len = spprintf(&name_str, 0, "http\\Client\\%s", driver.name_str);
-                                       php_http_pretty_key(name_str + sizeof("http\\Client"), driver.name_len, 1, 1);
+                       name_len = spprintf(&name_str, 0, "http\\Client\\%s", driver.name_str);
+                       php_http_pretty_key(name_str + sizeof("http\\Client"), driver.name_len, 1, 1);
 
-                                       if ((pf = php_persistent_handle_concede(NULL , name_str, name_len, persistent_handle_str, persistent_handle_len, NULL, NULL TSRMLS_CC))) {
-                                               rf = php_resource_factory_init(NULL, php_persistent_handle_get_resource_factory_ops(), pf, (void (*)(void *)) php_persistent_handle_abandon);
-                                       }
+                       if ((pf = php_persistent_handle_concede(NULL , name_str, name_len, persistent_handle_str, persistent_handle_len, NULL, NULL TSRMLS_CC))) {
+                               rf = php_resource_factory_init(NULL, php_persistent_handle_get_resource_factory_ops(), pf, (void (*)(void *)) php_persistent_handle_abandon);
+                       }
 
-                                       efree(name_str);
-                               }
+                       efree(name_str);
+               }
 
-                               if ((obj->client = php_http_client_init(NULL, driver.client_ops, rf, NULL TSRMLS_CC))) {
-                                       obj->client->callback.response.func = handle_response;
-                                       obj->client->callback.response.arg = obj;
-                                       obj->client->callback.progress.func = handle_progress;
-                                       obj->client->callback.progress.arg = obj;
+               obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
-                                       obj->client->responses.dtor = response_dtor;
-                               }
-                       } else {
-                               php_http_error(HE_WARNING, PHP_HTTP_E_REQUEST_FACTORY, "Failed to locate \"%s\" client request handler", driver_str);
-                       }
-               }
-       } end_error_handling();
+               php_http_expect(obj->client = php_http_client_init(NULL, driver.client_ops, rf, NULL TSRMLS_CC), runtime, return);
+
+               php_http_object_method_init(&obj->notify, getThis(), ZEND_STRL("notify") TSRMLS_CC);
+
+               obj->client->callback.response.func = handle_response;
+               obj->client->callback.response.arg = obj;
+               obj->client->callback.progress.func = handle_progress;
+               obj->client->callback.progress.arg = obj;
+
+               obj->client->responses.dtor = response_dtor;
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_reset, 0, 0, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, reset)
 {
-       if (SUCCESS == zend_parse_parameters_none()) {
-               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       php_http_client_object_t *obj;
+       php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
+
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+       obj->iterator = 0;
+       php_http_client_reset(obj->client);
 
-               obj->iterator = 0;
-               php_http_client_reset(obj->client);
-       }
        RETVAL_ZVAL(getThis(), 1, 0);
 }
 
@@ -589,38 +621,43 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enqueue, 0, 0, 1)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, enqueue)
 {
-       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
-               zval *request;
-               zend_fcall_info fci = empty_fcall_info;
-               zend_fcall_info_cache fcc = empty_fcall_info_cache;
+       zval *request;
+       zend_fcall_info fci = empty_fcall_info;
+       zend_fcall_info_cache fcc = empty_fcall_info_cache;
+       php_http_client_object_t *obj;
+       php_http_message_object_t *msg_obj;
+       php_http_client_enqueue_t q;
 
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|f", &request, php_http_client_request_get_class_entry(), &fci, &fcc)) {
-                       php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
-                       php_http_message_object_t *msg_obj = zend_object_store_get_object(request TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|f", &request, php_http_client_request_class_entry, &fci, &fcc), invalid_arg, return);
 
-                       if (php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
-                               php_http_error(HE_WARNING, PHP_HTTP_E_CLIENT, "Failed to enqueue request; request already in queue");
-                       } else {
-                               php_http_client_enqueue_t q = {
-                                       msg_obj->message,
-                                       combined_options(getThis(), request TSRMLS_CC),
-                                       msg_queue_dtor,
-                                       msg_obj,
-                                       {fci, fcc}
-                               };
-
-                               if (fci.size) {
-                                       Z_ADDREF_P(fci.function_name);
-                                       if (fci.object_ptr) {
-                                               Z_ADDREF_P(fci.object_ptr);
-                                       }
-                               }
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       msg_obj = zend_object_store_get_object(request TSRMLS_CC);
 
-                               zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
-                               php_http_client_enqueue(obj->client, &q);
-                       }
+       if (php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
+               php_http_throw(bad_method_call, "Failed to enqueue request; request already in queue", NULL);
+               return;
+       }
+
+       q.request = msg_obj->message;
+       q.options = combined_options(getThis(), request TSRMLS_CC);
+       q.dtor = msg_queue_dtor;
+       q.opaque = msg_obj;
+       q.closure.fci = fci;
+       q.closure.fcc = fcc;
+
+       if (fci.size) {
+               Z_ADDREF_P(fci.function_name);
+               if (fci.object_ptr) {
+                       Z_ADDREF_P(fci.object_ptr);
                }
-       } end_error_handling();
+       }
+
+       zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
+
+       php_http_expect(SUCCESS == php_http_client_enqueue(obj->client, &q), runtime,
+                       msg_queue_dtor(&q);
+                       return;
+       );
 
        RETVAL_ZVAL(getThis(), 1, 0);
 }
@@ -630,16 +667,21 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_dequeue, 0, 0, 1)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, dequeue)
 {
-       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
-               zval *request;
+       zval *request;
+       php_http_client_object_t *obj;
+       php_http_message_object_t *msg_obj;
 
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_get_class_entry())) {
-                       php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
-                       php_http_message_object_t *msg_obj = zend_object_store_get_object(request TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_class_entry), invalid_arg, return);
 
-                       php_http_client_dequeue(obj->client, msg_obj->message);
-               }
-       } end_error_handling();
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       msg_obj = zend_object_store_get_object(request TSRMLS_CC);
+
+       if (!php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
+               php_http_throw(bad_method_call, "Failed to dequeue request; request not in queue", NULL);
+               return;
+       }
+
+       php_http_expect(SUCCESS == php_http_client_dequeue(obj->client, msg_obj->message), runtime, return);
 
        RETVAL_ZVAL(getThis(), 1, 0);
 }
@@ -650,75 +692,98 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_requeue, 0, 0, 1)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, requeue)
 {
-       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
-               zval *request;
-               zend_fcall_info fci = empty_fcall_info;
-               zend_fcall_info_cache fcc = empty_fcall_info_cache;
-
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|f", &request, php_http_client_request_get_class_entry(), &fci, &fcc)) {
-                       php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
-                       php_http_message_object_t *msg_obj = zend_object_store_get_object(request TSRMLS_CC);
-                       php_http_client_enqueue_t q = {
-                               msg_obj->message,
-                               combined_options(getThis(), request TSRMLS_CC),
-                               msg_queue_dtor,
-                               msg_obj,
-                               {fci, fcc}
-                       };
-
-                       if (fci.size) {
-                               Z_ADDREF_P(fci.function_name);
-                               if (fci.object_ptr) {
-                                       Z_ADDREF_P(fci.object_ptr);
-                               }
-                       }
+       zval *request;
+       zend_fcall_info fci = empty_fcall_info;
+       zend_fcall_info_cache fcc = empty_fcall_info_cache;
+       php_http_client_object_t *obj;
+       php_http_message_object_t *msg_obj;
+       php_http_client_enqueue_t q;
 
-                       zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
-                       if (php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
-                               php_http_client_dequeue(obj->client, msg_obj->message);
-                       }
-                       php_http_client_enqueue(obj->client, &q);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|f", &request, php_http_client_request_class_entry, &fci, &fcc), invalid_arg, return);
+
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       msg_obj = zend_object_store_get_object(request TSRMLS_CC);
+
+       if (php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
+               php_http_expect(SUCCESS == php_http_client_dequeue(obj->client, msg_obj->message), runtime, return);
+       }
+
+       q.request = msg_obj->message;
+       q.options = combined_options(getThis(), request TSRMLS_CC);
+       q.dtor = msg_queue_dtor;
+       q.opaque = msg_obj;
+       q.closure.fci = fci;
+       q.closure.fcc = fcc;
+
+       if (fci.size) {
+               Z_ADDREF_P(fci.function_name);
+               if (fci.object_ptr) {
+                       Z_ADDREF_P(fci.object_ptr);
                }
-       } end_error_handling();
+       }
+
+       zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
+
+       php_http_expect(SUCCESS == php_http_client_enqueue(obj->client, &q), runtime,
+                       msg_queue_dtor(&q);
+                       return;
+       );
 
        RETVAL_ZVAL(getThis(), 1, 0);
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_count, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(HttpClient, count)
+{
+       long count_mode = -1;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &count_mode)) {
+               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               RETVAL_LONG(zend_llist_count(&obj->client->requests));
+       }
+}
+
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getResponse, 0, 0, 0)
        ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 1)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, getResponse)
 {
        zval *zrequest = NULL;
+       php_http_client_object_t *obj;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &zrequest, php_http_client_request_get_class_entry())) {
-               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &zrequest, php_http_client_request_class_entry), invalid_arg, return);
 
-               if (!zrequest) {
-                       /* pop off the last respone */
-                       if (obj->client->responses.tail) {
-                               php_http_message_object_t *response_obj = *(php_http_message_object_t **) obj->client->responses.tail->data;
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
-                               /* pop off and go */
-                               if (response_obj) {
-                                       RETVAL_OBJVAL(response_obj->zv, 1);
-                                       zend_llist_remove_tail(&obj->client->responses);
-                               }
-                       }
-               } else {
-                       /* lookup the response with the request */
-                       zend_llist_element *el = NULL;
-                       php_http_message_object_t *req_obj = zend_object_store_get_object(zrequest TSRMLS_CC);
+       if (zrequest) {
+               /* lookup the response with the request */
+               zend_llist_element *el = NULL;
+               php_http_message_object_t *req_obj = zend_object_store_get_object(zrequest TSRMLS_CC);
 
-                       for (el = obj->client->responses.head; el; el = el->next) {
-                               php_http_message_object_t *response_obj = *(php_http_message_object_t **) el->data;
+               for (el = obj->client->responses.head; el; el = el->next) {
+                       php_http_message_object_t *response_obj = *(php_http_message_object_t **) el->data;
 
-                               if (response_obj->message->parent == req_obj->message) {
-                                       RETVAL_OBJVAL(response_obj->zv, 1);
-                                       break;
-                               }
+                       if (response_obj->message->parent == req_obj->message) {
+                               RETURN_OBJVAL(response_obj->zv, 1);
                        }
                }
+
+               /* not found for the request! */
+               php_http_throw(unexpected_val, "Could not find response for the request", NULL);
+               return;
+       }
+
+       /* pop off the last response */
+       if (obj->client->responses.tail) {
+               php_http_message_object_t *response_obj = *(php_http_message_object_t **) obj->client->responses.tail->data;
+
+               /* pop off and go */
+               if (response_obj) {
+                       RETVAL_OBJVAL(response_obj->zv, 1);
+                       zend_llist_remove_tail(&obj->client->responses);
+               }
        }
 }
 
@@ -726,27 +791,25 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getHistory, 0, 0, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, getHistory)
 {
-       if (SUCCESS == zend_parse_parameters_none()) {
-               zval *zhistory = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC);
-               RETVAL_ZVAL(zhistory, 1, 0);
-       }
+       zval *zhistory;
+
+       php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
+
+       zhistory = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC);
+       RETVAL_ZVAL(zhistory, 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_send, 0, 0, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, send)
 {
-       RETVAL_FALSE;
+       php_http_client_object_t *obj;
 
-       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
-               if (SUCCESS == zend_parse_parameters_none()) {
-                       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
-                               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 
-                               php_http_client_exec(obj->client);
-                       } end_error_handling();
-               }
-       } end_error_handling();
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+       php_http_expect(SUCCESS == php_http_client_exec(obj->client), runtime, return);
 
        RETVAL_ZVAL(getThis(), 1, 0);
 }
@@ -758,11 +821,8 @@ static PHP_METHOD(HttpClient, once)
        if (SUCCESS == zend_parse_parameters_none()) {
                php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
-               if (0 < php_http_client_once(obj->client)) {
-                       RETURN_TRUE;
-               }
+               RETURN_BOOL(0 < php_http_client_once(obj->client));
        }
-       RETURN_FALSE;
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_wait, 0, 0, 0)
@@ -779,9 +839,24 @@ static PHP_METHOD(HttpClient, wait)
                timeout_val.tv_sec = (time_t) timeout;
                timeout_val.tv_usec = PHP_HTTP_USEC(timeout) % PHP_HTTP_MCROSEC;
 
-               RETURN_SUCCESS(php_http_client_wait(obj->client, timeout > 0 ? &timeout_val : NULL));
+               RETURN_BOOL(SUCCESS == php_http_client_wait(obj->client, timeout > 0 ? &timeout_val : NULL));
        }
-       RETURN_FALSE;
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_configure, 0, 0, 1)
+       ZEND_ARG_ARRAY_INFO(0, settings, 1)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(HttpClient, configure)
+{
+       HashTable *settings = NULL;
+       php_http_client_object_t *obj;
+
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|H!", &settings), invalid_arg, return);
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+       php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_CONFIGURATION, settings), unexpected_val, return);
+
+       RETVAL_ZVAL(getThis(), 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enablePipelining, 0, 0, 0)
@@ -790,12 +865,14 @@ ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, enablePipelining)
 {
        zend_bool enable = 1;
+       php_http_client_object_t *obj;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) {
-               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable), invalid_arg, return);
+
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+       php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING, &enable), unexpected_val, return);
 
-               php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING, &enable);
-       }
        RETVAL_ZVAL(getThis(), 1, 0);
 }
 
@@ -805,22 +882,31 @@ ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, enableEvents)
 {
        zend_bool enable = 1;
+       php_http_client_object_t *obj;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable)) {
-               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable), invalid_arg, return);
+
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+       php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_USE_EVENTS, &enable), unexpected_val, return);
 
-               php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_USE_EVENTS, &enable);
-       }
        RETVAL_ZVAL(getThis(), 1, 0);
 }
 
+struct notify_arg {
+       php_http_object_method_t *cb;
+       zval **args[3];
+       int argc;
+};
+
 static int notify(zend_object_iterator *iter, void *puser TSRMLS_DC)
 {
-       zval **observer = NULL, ***args = puser;
+       zval **observer = NULL;
+       struct notify_arg *arg = puser;
 
        iter->funcs->get_current_data(iter, &observer TSRMLS_CC);
        if (observer) {
-               return php_http_method_call(*observer, ZEND_STRL("update"), args[2]?3:args[1]?2:args[0]?1:0, args, NULL TSRMLS_CC);
+               return php_http_object_method_call(arg->cb, *observer, NULL, arg->argc, arg->args TSRMLS_CC);
        }
        return FAILURE;
 }
@@ -830,30 +916,47 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_notify, 0, 0, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, notify)
 {
-       zval *request = NULL, *zprogress = NULL;
+       zval *request = NULL, *zprogress = NULL, *observers;
+       php_http_client_object_t *client_obj;
+       struct notify_arg arg = {NULL};
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O!o!", &request, php_http_client_request_get_class_entry(), &zprogress)) {
-               zval **args[3], *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O!o!", &request, php_http_client_request_class_entry, &zprogress), invalid_arg, return);
 
-               if (Z_TYPE_P(observers) == IS_OBJECT) {
-                       Z_ADDREF_P(getThis());
-                       args[0] = &getThis();
-                       if (request) {
-                               Z_ADDREF_P(request);
-                       }
-                       args[1] = &request;
-                       if (zprogress) {
-                               Z_ADDREF_P(zprogress);
-                       }
-                       args[2] = &zprogress;
-                       spl_iterator_apply(observers, notify, args TSRMLS_CC);
-                       zval_ptr_dtor(&getThis());
-                       if (request) {
-                               zval_ptr_dtor(&request);
-                       }
-                       if (zprogress) {
-                               zval_ptr_dtor(&zprogress);
-                       }
+       client_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
+
+       if (Z_TYPE_P(observers) != IS_OBJECT) {
+               php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
+               return;
+       }
+
+       if (client_obj->update) {
+               arg.cb = client_obj->update;
+
+               Z_ADDREF_P(getThis());
+               arg.args[0] = &getThis();
+               arg.argc = 1;
+
+               if (request) {
+                       Z_ADDREF_P(request);
+                       arg.args[1] = &request;
+                       arg.argc += 1;
+               }
+
+               if (zprogress) {
+                       Z_ADDREF_P(zprogress);
+                       arg.args[2] = &zprogress;
+                       arg.argc += 1;
+               }
+
+               spl_iterator_apply(observers, notify, &arg TSRMLS_CC);
+
+               zval_ptr_dtor(&getThis());
+               if (request) {
+                       zval_ptr_dtor(&request);
+               }
+               if (zprogress) {
+                       zval_ptr_dtor(&zprogress);
                }
        }
 
@@ -865,14 +968,26 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_attach, 0, 0, 1)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, attach)
 {
-       zval *observer;
+       zval *observers, *observer, *retval = NULL;
+       php_http_client_object_t *client_obj;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver)) {
-               zval *retval = NULL, *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
-               zend_call_method_with_1_params(&observers, NULL, NULL, "attach", &retval, observer);
-               if (retval) {
-                       zval_ptr_dtor(&retval);
-               }
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver), invalid_arg, return);
+
+       client_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
+
+       if (Z_TYPE_P(observers) != IS_OBJECT) {
+               php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
+               return;
+       }
+
+       if (!client_obj->update) {
+               client_obj->update = php_http_object_method_init(NULL, observer, ZEND_STRL("update") TSRMLS_CC);
+       }
+
+       zend_call_method_with_1_params(&observers, NULL, NULL, "attach", &retval, observer);
+       if (retval) {
+               zval_ptr_dtor(&retval);
        }
 
        RETVAL_ZVAL(getThis(), 1, 0);
@@ -883,11 +998,19 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_detach, 0, 0, 1)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, detach)
 {
-       zval *observer;
+       zval *observers, *observer, *retval = NULL;
+
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver), invalid_arg, return);
+
+       observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver)) {
-               zval *retval, *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
-               zend_call_method_with_1_params(&observers, NULL, NULL, "detach", &retval, observer);
+       if (Z_TYPE_P(observers) != IS_OBJECT) {
+               php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
+               return;
+       }
+
+       zend_call_method_with_1_params(&observers, NULL, NULL, "detach", &retval, observer);
+       if (retval) {
                zval_ptr_dtor(&retval);
        }
 
@@ -898,12 +1021,18 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getObservers, 0, 0, 0)
 ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, getObservers)
 {
-       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
-               if (SUCCESS == zend_parse_parameters_none()) {
-                       zval *observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
-                       RETVAL_ZVAL(observers, 1, 0);
-               }
-       } end_error_handling();
+       zval *observers;
+
+       php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
+
+       observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
+
+       if (Z_TYPE_P(observers) != IS_OBJECT) {
+               php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
+               return;
+       }
+
+       RETVAL_ZVAL(observers, 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getProgressInfo, 0, 0, 1)
@@ -912,25 +1041,25 @@ ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, getProgressInfo)
 {
        zval *request;
+       php_http_client_object_t *obj;
+       php_http_message_object_t *req_obj;
+       php_http_client_progress_state_t *progress;
 
-       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_get_class_entry())) {
-                       php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
-                       php_http_message_object_t *req_obj = zend_object_store_get_object(request TSRMLS_CC);
-                       php_http_client_progress_state_t *progress;
-
-                       if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, req_obj->message, &progress)) {
-                               object_init(return_value);
-                               add_property_bool(return_value, "started", progress->started);
-                               add_property_bool(return_value, "finished", progress->finished);
-                               add_property_string(return_value, "info", STR_PTR(progress->info), 1);
-                               add_property_double(return_value, "dltotal", progress->dl.total);
-                               add_property_double(return_value, "dlnow", progress->dl.now);
-                               add_property_double(return_value, "ultotal", progress->ul.total);
-                               add_property_double(return_value, "ulnow", progress->ul.now);
-                       }
-               }
-       } end_error_handling();
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_class_entry), invalid_arg, return);
+
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       req_obj = zend_object_store_get_object(request TSRMLS_CC);
+
+       php_http_expect(SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, req_obj->message, &progress), unexpected_val, return);
+
+       object_init(return_value);
+       add_property_bool(return_value, "started", progress->started);
+       add_property_bool(return_value, "finished", progress->finished);
+       add_property_string(return_value, "info", STR_PTR(progress->info), 1);
+       add_property_double(return_value, "dltotal", progress->dl.total);
+       add_property_double(return_value, "dlnow", progress->dl.now);
+       add_property_double(return_value, "ultotal", progress->ul.total);
+       add_property_double(return_value, "ulnow", progress->ul.now);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getTransferInfo, 0, 0, 1)
@@ -939,16 +1068,18 @@ ZEND_END_ARG_INFO();
 static PHP_METHOD(HttpClient, getTransferInfo)
 {
        zval *request;
+       HashTable *info;
+       php_http_client_object_t *obj;
+       php_http_message_object_t *req_obj;
 
-       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_get_class_entry())) {
-                       php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
-                       php_http_message_object_t *req_obj = zend_object_store_get_object(request TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_class_entry), invalid_arg, return);
 
-                       array_init(return_value);
-                       php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, req_obj->message, &Z_ARRVAL_P(return_value));
-               }
-       } end_error_handling();
+       obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       req_obj = zend_object_store_get_object(request TSRMLS_CC);
+
+       object_init(return_value);
+       info = HASH_OF(return_value);
+       php_http_expect(SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, req_obj->message, &info), unexpected_val, return);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setOptions, 0, 0, 0)
@@ -958,11 +1089,11 @@ static PHP_METHOD(HttpClient, setOptions)
 {
        zval *opts = NULL;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
-               php_http_client_options_set(getThis(), opts TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
 
-               RETVAL_ZVAL(getThis(), 1, 0);
-       }
+       php_http_client_options_set(getThis(), opts TSRMLS_CC);
+
+       RETVAL_ZVAL(getThis(), 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getOptions, 0, 0, 0)
@@ -982,11 +1113,11 @@ static PHP_METHOD(HttpClient, setSslOptions)
 {
        zval *opts = NULL;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
-               php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 1 TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
 
-               RETVAL_ZVAL(getThis(), 1, 0);
-       }
+       php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 1 TSRMLS_CC);
+
+       RETVAL_ZVAL(getThis(), 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addSslOptions, 0, 0, 0)
@@ -996,11 +1127,11 @@ static PHP_METHOD(HttpClient, addSslOptions)
 {
        zval *opts = NULL;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
-               php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
 
-               RETVAL_ZVAL(getThis(), 1, 0);
-       }
+       php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC);
+
+       RETVAL_ZVAL(getThis(), 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getSslOptions, 0, 0, 0)
@@ -1019,11 +1150,11 @@ static PHP_METHOD(HttpClient, setCookies)
 {
        zval *opts = NULL;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
-               php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 1 TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
 
-               RETVAL_ZVAL(getThis(), 1, 0);
-       }
+       php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 1 TSRMLS_CC);
+
+       RETVAL_ZVAL(getThis(), 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addCookies, 0, 0, 0)
@@ -1033,11 +1164,11 @@ static PHP_METHOD(HttpClient, addCookies)
 {
        zval *opts = NULL;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts)) {
-               php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 0 TSRMLS_CC);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
 
-               RETVAL_ZVAL(getThis(), 1, 0);
-       }
+       php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 0 TSRMLS_CC);
+
+       RETVAL_ZVAL(getThis(), 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getCookies, 0, 0, 0)
@@ -1058,34 +1189,62 @@ static PHP_METHOD(HttpClient, getAvailableDrivers) {
        }
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableOptions, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(HttpClient, getAvailableOptions)
+{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               array_init(return_value);
+               php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS, NULL, &Z_ARRVAL_P(return_value));
+       }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableConfiguration, 0, 0, 0)
+ZEND_END_ARG_INFO();
+static PHP_METHOD(HttpClient, getAvailableConfiguration)
+{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
+               array_init(return_value);
+               php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION, NULL, &Z_ARRVAL_P(return_value));
+       }
+}
+
 static zend_function_entry php_http_client_methods[] = {
-       PHP_ME(HttpClient, __construct, ai_HttpClient_construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
-       PHP_ME(HttpClient, reset, ai_HttpClient_reset, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, enqueue, ai_HttpClient_enqueue, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, dequeue, ai_HttpClient_dequeue, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, requeue, ai_HttpClient_requeue, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, send, ai_HttpClient_send, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, once, ai_HttpClient_once, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, wait, ai_HttpClient_wait, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, getResponse, ai_HttpClient_getResponse, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, getHistory, ai_HttpClient_getHistory, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, enablePipelining, ai_HttpClient_enablePipelining, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, enableEvents, ai_HttpClient_enableEvents, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, notify, ai_HttpClient_notify, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, attach, ai_HttpClient_attach, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, detach, ai_HttpClient_detach, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, getObservers, ai_HttpClient_getObservers, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, getProgressInfo, ai_HttpClient_getProgressInfo, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, getTransferInfo, ai_HttpClient_getTransferInfo, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, setOptions, ai_HttpClient_setOptions, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, getOptions, ai_HttpClient_getOptions, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, setSslOptions, ai_HttpClient_setSslOptions, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, addSslOptions, ai_HttpClient_addSslOptions, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, getSslOptions, ai_HttpClient_getSslOptions, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, setCookies, ai_HttpClient_setCookies, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, addCookies, ai_HttpClient_addCookies, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, getCookies, ai_HttpClient_getCookies, ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, getAvailableDrivers, ai_HttpClient_getAvailableDrivers, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+       PHP_ME(HttpClient, __construct,          ai_HttpClient_construct,            ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+       PHP_ME(HttpClient, reset,                ai_HttpClient_reset,                ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, enqueue,              ai_HttpClient_enqueue,              ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, dequeue,              ai_HttpClient_dequeue,              ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, requeue,              ai_HttpClient_requeue,              ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, count,                ai_HttpClient_count,                ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, send,                 ai_HttpClient_send,                 ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, once,                 ai_HttpClient_once,                 ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, wait,                 ai_HttpClient_wait,                 ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getResponse,          ai_HttpClient_getResponse,          ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getHistory,           ai_HttpClient_getHistory,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, configure,            ai_HttpClient_configure,            ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, enablePipelining,     ai_HttpClient_enablePipelining,     ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
+       PHP_ME(HttpClient, enableEvents,         ai_HttpClient_enableEvents,         ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
+       PHP_ME(HttpClient, notify,               ai_HttpClient_notify,               ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, attach,               ai_HttpClient_attach,               ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, detach,               ai_HttpClient_detach,               ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getObservers,         ai_HttpClient_getObservers,         ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getProgressInfo,      ai_HttpClient_getProgressInfo,      ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getTransferInfo,      ai_HttpClient_getTransferInfo,      ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, setOptions,           ai_HttpClient_setOptions,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getOptions,           ai_HttpClient_getOptions,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, setSslOptions,        ai_HttpClient_setSslOptions,        ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, addSslOptions,        ai_HttpClient_addSslOptions,        ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getSslOptions,        ai_HttpClient_getSslOptions,        ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, setCookies,           ai_HttpClient_setCookies,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, addCookies,           ai_HttpClient_addCookies,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getCookies,           ai_HttpClient_getCookies,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getAvailableDrivers,  ai_HttpClient_getAvailableDrivers,  ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+       PHP_ME(HttpClient, getAvailableOptions,  ai_HttpClient_getAvailableOptions,  ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getAvailableConfiguration, ai_HttpClient_getAvailableConfiguration, ZEND_ACC_PUBLIC)
        EMPTY_FUNCTION_ENTRY
 };
 
@@ -1096,7 +1255,7 @@ PHP_MINIT_FUNCTION(http_client)
        INIT_NS_CLASS_ENTRY(ce, "http", "Client", php_http_client_methods);
        php_http_client_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
        php_http_client_class_entry->create_object = php_http_client_object_new;
-       zend_class_implements(php_http_client_class_entry TSRMLS_CC, 1, spl_ce_SplSubject);
+       zend_class_implements(php_http_client_class_entry TSRMLS_CC, 2, spl_ce_SplSubject, spl_ce_Countable);
        memcpy(&php_http_client_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
        php_http_client_object_handlers.clone_obj = NULL;
        zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE TSRMLS_CC);
@@ -1114,3 +1273,12 @@ PHP_MSHUTDOWN_FUNCTION(http_client)
        zend_hash_destroy(&php_http_client_drivers);
        return SUCCESS;
 }
+
+/*
+ * 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
+ */