X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=php_http_client.c;h=f96164bbba08ad449733b745503149948412de03;hp=c307221f0f0bf98f82b5d4ebde43dd6f13d066f5;hb=refs%2Fheads%2Fv2.2.x;hpb=f753226f1f45e10ef3916256ec1bc0416c8644a8 diff --git a/php_http_client.c b/php_http_client.c index c307221..f96164b 100644 --- a/php_http_client.c +++ b/php_http_client.c @@ -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 | + +--------------------------------------------------------------------+ +*/ + #include "php_http_api.h" #include "php_http_client.h" @@ -8,27 +20,40 @@ */ static HashTable php_http_client_drivers; -PHP_HTTP_API STATUS php_http_client_driver_add(const char *name_str, uint name_len, 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, name_str, name_len + 1, (void *) driver, sizeof(php_http_client_driver_t), NULL); + 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(char **name_str, uint *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; - if (*name_str && SUCCESS == zend_hash_find(&php_http_client_drivers, *name_str, (*name_len) + 1, (void *) &tmp)) { - *driver = *tmp; - return SUCCESS; - } else if (SUCCESS == zend_hash_get_current_data(&php_http_client_drivers, (void *) &tmp)) { - zend_hash_get_current_key_ex(&php_http_client_drivers, name_str, name_len, NULL, 0, NULL); - --(*name_len); + if ((name_str && SUCCESS == zend_hash_find(&php_http_client_drivers, name_str, name_len + 1, (void *) &tmp)) + || (SUCCESS == zend_hash_get_current_data(&php_http_client_drivers, (void *) &tmp))) { *driver = *tmp; return SUCCESS; } return FAILURE; } +static int apply_driver_list(void *p, void *arg TSRMLS_DC) +{ + php_http_client_driver_t *d = p; + zval *zname; + + MAKE_STD_ZVAL(zname); + ZVAL_STRINGL(zname, d->name_str, d->name_len, 1); + + zend_hash_next_index_insert(arg, &zname, sizeof(zval *), NULL); + return ZEND_HASH_APPLY_KEEP; +} + +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); +} + void php_http_client_options_set_subr(zval *this_ptr, char *key, size_t len, zval *opts, int overwrite TSRMLS_DC) { if (overwrite || (opts && zend_hash_num_elements(Z_ARRVAL_P(opts)))) { @@ -132,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; @@ -153,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); } } } @@ -163,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); @@ -172,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); @@ -192,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); @@ -207,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); @@ -215,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); @@ -223,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; @@ -243,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); @@ -252,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); @@ -261,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); @@ -270,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); @@ -280,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); @@ -289,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); @@ -338,7 +362,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); @@ -353,6 +377,7 @@ static void handle_history(zval *zclient, php_http_message_t *request, php_http_ 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) { + zend_bool dequeue = 0; zval zclient; php_http_message_t *msg; php_http_client_progress_state_t *progress; @@ -364,8 +389,12 @@ 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; + + /* 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 (i_zend_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) { + if (z_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) { handle_history(&zclient, *request, *response TSRMLS_CC); } @@ -374,7 +403,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); @@ -382,9 +411,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); @@ -392,16 +422,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); } @@ -417,23 +448,38 @@ 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 *state) +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, *retval = NULL, *zclient; + zval *zrequest, *zprogress, *retval = NULL, *zclient; + 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); MAKE_STD_ZVAL(zrequest); ZVAL_OBJVAL(zrequest, ((php_http_message_object_t *) e->opaque)->zv, 1); - with_error_handling(EH_NORMAL, NULL) { - zend_call_method_with_1_params(&zclient, NULL, NULL, "notify", &retval, zrequest); - } end_error_handling(); + MAKE_STD_ZVAL(zprogress); + object_init(zprogress); + add_property_bool(zprogress, "started", progress->started); + add_property_bool(zprogress, "finished", progress->finished); + add_property_string(zprogress, "info", STR_PTR(progress->info), 1); + add_property_double(zprogress, "dltotal", progress->dl.total); + add_property_double(zprogress, "dlnow", progress->dl.now); + add_property_double(zprogress, "ultotal", progress->ul.total); + add_property_double(zprogress, "ulnow", progress->ul.now); + zend_replace_error_handling(EH_NORMAL, NULL, &zeh TSRMLS_CC); + zend_call_method_with_2_params(&zclient, NULL, NULL, "notify", &retval, zrequest, zprogress); + 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); } @@ -453,62 +499,64 @@ 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; + + 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 == 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; + 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; + } - if (SUCCESS == php_http_client_driver_get(&driver_str, (uint *) &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; + 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", php_http_pretty_key(driver_str, driver_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); - } + obj = zend_object_store_get_object(getThis() TSRMLS_CC); - 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; + php_http_expect(obj->client = php_http_client_init(NULL, driver.client_ops, rf, NULL TSRMLS_CC), runtime, return); - 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(); + 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); } @@ -535,7 +583,7 @@ static HashTable *combined_options(zval *client, zval *request TSRMLS_DC) } if (z_roptions) { if (Z_TYPE_P(z_roptions) == IS_ARRAY) { - array_join(Z_ARRVAL_P(z_roptions), options, 1, 0); + array_join(Z_ARRVAL_P(z_roptions), options, 0, 0); } zval_ptr_dtor(&z_roptions); } @@ -565,38 +613,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); } @@ -606,16 +659,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); } @@ -626,56 +684,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) { - if (SUCCESS == zend_parse_parameters_none()) { - php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + zval *zrequest = NULL; + php_http_client_object_t *obj; + + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &zrequest, php_http_client_request_class_entry), invalid_arg, return); + + obj = zend_object_store_get_object(getThis() 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); - if (obj->client->responses.tail) { - php_http_message_object_t *response_obj = *(php_http_message_object_t **) obj->client->responses.tail->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; - /* pop off and go */ - if (response_obj) { - RETVAL_OBJVAL(response_obj->zv, 1); - zend_llist_remove_tail(&obj->client->responses); + 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); + } } } @@ -683,27 +783,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); } @@ -715,11 +813,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) @@ -736,9 +831,8 @@ 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_enablePipelining, 0, 0, 0) @@ -747,12 +841,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); } @@ -762,29 +858,24 @@ 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); } static int notify(zend_object_iterator *iter, void *puser TSRMLS_DC) { - zval **observer = NULL, **args = puser; + zval **observer = NULL, ***args = puser; iter->funcs->get_current_data(iter, &observer TSRMLS_CC); if (observer) { - zval *retval = NULL; - - zend_call_method(observer, NULL, NULL, ZEND_STRL("update"), &retval, args[1]?2:1, args[0], args[1] TSRMLS_CC); - if (retval) { - zval_ptr_dtor(&retval); - } - - return SUCCESS; + return php_http_method_call(*observer, ZEND_STRL("update"), args[2]?3:args[1]?2:args[0]?1:0, args, NULL TSRMLS_CC); } return FAILURE; } @@ -794,24 +885,34 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_notify, 0, 0, 0) ZEND_END_ARG_INFO(); static PHP_METHOD(HttpClient, notify) { - zval *request = NULL; + zval *request = NULL, *zprogress = NULL, *observers, **args[3]; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O!", &request, php_http_client_request_get_class_entry())) { - zval *args[2], *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()); - if (request) { - Z_ADDREF_P(request); - } - args[0] = getThis(); - args[1] = request; - spl_iterator_apply(observers, notify, &args TSRMLS_CC); - zval_ptr_dtor(&getThis()); - if (request) { - zval_ptr_dtor(&request); - } - } + 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; + } + + 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); } RETVAL_ZVAL(getThis(), 1, 0); @@ -822,14 +923,20 @@ 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; - 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); + + 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; + } + + zend_call_method_with_1_params(&observers, NULL, NULL, "attach", &retval, observer); + if (retval) { + zval_ptr_dtor(&retval); } RETVAL_ZVAL(getThis(), 1, 0); @@ -840,11 +947,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 (Z_TYPE_P(observers) != IS_OBJECT) { + php_http_throw(unexpected_val, "Observer storage is corrupted", NULL); + return; + } - 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); + zend_call_method_with_1_params(&observers, NULL, NULL, "detach", &retval, observer); + if (retval) { zval_ptr_dtor(&retval); } @@ -855,12 +970,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) @@ -869,25 +990,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) @@ -896,16 +1017,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) @@ -915,11 +1038,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) @@ -939,11 +1062,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) @@ -953,11 +1076,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) @@ -976,11 +1099,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) @@ -990,11 +1113,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) @@ -1006,33 +1129,44 @@ static PHP_METHOD(HttpClient, getCookies) } } +ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableDrivers, 0, 0, 0) +ZEND_END_ARG_INFO(); +static PHP_METHOD(HttpClient, getAvailableDrivers) { + if (SUCCESS == zend_parse_parameters_none()) { + array_init(return_value); + php_http_client_driver_list(Z_ARRVAL_P(return_value) TSRMLS_CC); + } +} + 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, __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, 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) EMPTY_FUNCTION_ENTRY }; @@ -1043,7 +1177,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); @@ -1061,3 +1195,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 + */