From: Michael Wallner Date: Mon, 13 Jun 2016 12:54:41 +0000 (+0200) Subject: Merge branch 'v2.6.x' X-Git-Tag: RELEASE_3_1_0_BETA1~10 X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=commitdiff_plain;h=43a9a6f8cb56e25c3770d652ce77045f89f68ca6;hp=-c Merge branch 'v2.6.x' --- 43a9a6f8cb56e25c3770d652ce77045f89f68ca6 diff --combined src/php_http.c index f9bb4d0,abb8377..3b3338f --- a/src/php_http.c +++ b/src/php_http.c @@@ -106,15 -106,23 +106,15 @@@ static void php_http_globals_init_once( } #if 0 -static inline void php_http_globals_init(zend_php_http_globals *G TSRMLS_DC) +static inline void php_http_globals_init(zend_php_http_globals *G) { } -static inline void php_http_globals_free(zend_php_http_globals *G TSRMLS_DC) +static inline void php_http_globals_free(zend_php_http_globals *G) { } #endif -#if ZTS && PHP_DEBUG && !HAVE_GCOV -zend_php_http_globals *php_http_globals(void) -{ - TSRMLS_FETCH(); - return PHP_HTTP_G; -} -#endif - PHP_INI_BEGIN() STD_PHP_INI_ENTRY("http.etag.mode", "crc32b", PHP_INI_ALL, OnUpdateString, env.etag_mode, zend_php_http_globals, php_http_globals) PHP_INI_END() @@@ -126,7 -134,6 +126,7 @@@ PHP_MINIT_FUNCTION(http REGISTER_INI_ENTRIES(); if (0 + || SUCCESS != PHP_MINIT_CALL(http_object) || SUCCESS != PHP_MINIT_CALL(http_exception) || SUCCESS != PHP_MINIT_CALL(http_cookie) || SUCCESS != PHP_MINIT_CALL(http_encoding) @@@ -143,6 -150,7 +143,7 @@@ #if PHP_HTTP_HAVE_CURL || SUCCESS != PHP_MINIT_CALL(http_curl) || SUCCESS != PHP_MINIT_CALL(http_client_curl) + || SUCCESS != PHP_MINIT_CALL(http_client_curl_user) #endif || SUCCESS != PHP_MINIT_CALL(http_url) || SUCCESS != PHP_MINIT_CALL(http_env) diff --combined src/php_http_api.h index e8c175f,08b6ba8..b264694 --- a/src/php_http_api.h +++ b/src/php_http_api.h @@@ -23,8 -23,8 +23,8 @@@ #include #include -#include -#include +#include +#include #include #include #include @@@ -101,27 -101,20 +101,29 @@@ #include "php_http_client_request.h" #include "php_http_client_response.h" #include "php_http_client_curl.h" ++#include "php_http_client_curl_user.h" ++#include "php_http_client_curl_event.h" #include "php_http_url.h" #include "php_http_version.h" ZEND_BEGIN_MODULE_GLOBALS(php_http) struct php_http_env_globals env; +#ifdef PHP_HTTP_HAVE_CLIENT + struct { +#ifdef PHP_HTTP_HAVE_CURL + struct php_http_client_curl_globals curl; +#endif + } client; +#endif ZEND_END_MODULE_GLOBALS(php_http) ZEND_EXTERN_MODULE_GLOBALS(php_http); #ifdef ZTS # include "TSRM/TSRM.h" -# define PHP_HTTP_G ((zend_php_http_globals *) (*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(php_http_globals_id)]) +# define PHP_HTTP_G ((zend_php_http_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(php_http_globals_id)]) # undef TSRMLS_FETCH_FROM_CTX -# define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = ((ctx)?(ctx):ts_resource_ex(0, NULL)) +# define TSRMLS_FETCH_FROM_CTX(ctx) ERROR #else # define PHP_HTTP_G (&php_http_globals) #endif diff --combined src/php_http_client.c index 4b465c0,160e8bb..5d2aafe --- a/src/php_http_client.c +++ b/src/php_http_client.c @@@ -20,141 -20,131 +20,141 @@@ */ static HashTable php_http_client_drivers; +static void php_http_client_driver_hash_dtor(zval *pData) +{ + pefree(Z_PTR_P(pData), 1); +} + ZEND_RESULT_CODE 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); + return zend_hash_add_mem(&php_http_client_drivers, driver->driver_name, (void *) driver, sizeof(php_http_client_driver_t)) + ? SUCCESS : FAILURE; } -ZEND_RESULT_CODE php_http_client_driver_get(const char *name_str, size_t name_len, php_http_client_driver_t *driver) +php_http_client_driver_t *php_http_client_driver_get(zend_string *name) { + zval *ztmp; php_http_client_driver_t *tmp; - 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; + if (name && (tmp = zend_hash_find_ptr(&php_http_client_drivers, name))) { + return tmp; } - return FAILURE; + if ((ztmp = zend_hash_get_current_data(&php_http_client_drivers))) { + return Z_PTR_P(ztmp); + } + return NULL; } -static int apply_driver_list(void *p, void *arg TSRMLS_DC) +static int apply_driver_list(zval *p, void *arg) { - php_http_client_driver_t *d = p; - zval *zname; + php_http_client_driver_t *d = Z_PTR_P(p); + zval zname; - MAKE_STD_ZVAL(zname); - ZVAL_STRINGL(zname, d->name_str, d->name_len, 1); + ZVAL_STR_COPY(&zname, d->driver_name); - zend_hash_next_index_insert(arg, &zname, sizeof(zval *), NULL); + zend_hash_next_index_insert(arg, &zname); return ZEND_HASH_APPLY_KEEP; } -void php_http_client_driver_list(HashTable *ht TSRMLS_DC) +void php_http_client_driver_list(HashTable *ht) { - zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht TSRMLS_CC); + zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht); } -void php_http_client_options_set_subr(zval *this_ptr, char *key, size_t len, zval *opts, int overwrite TSRMLS_DC) +static zend_class_entry *php_http_client_class_entry; +zend_class_entry *php_http_client_get_class_entry(void) +{ + return php_http_client_class_entry; +} + +void php_http_client_options_set_subr(zval *instance, char *key, size_t len, zval *opts, int overwrite) { if (overwrite || (opts && zend_hash_num_elements(Z_ARRVAL_P(opts)))) { - zend_class_entry *this_ce = Z_OBJCE_P(getThis()); - zval *old_opts, *new_opts, **entry = NULL; + zend_class_entry *this_ce = Z_OBJCE_P(instance); + zval old_opts_tmp, *old_opts, new_opts, *entry = NULL; - MAKE_STD_ZVAL(new_opts); - array_init(new_opts); - old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + array_init(&new_opts); + old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp); if (Z_TYPE_P(old_opts) == IS_ARRAY) { - array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts)); + array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL(new_opts)); } if (overwrite) { if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) { Z_ADDREF_P(opts); - zend_symtable_update(Z_ARRVAL_P(new_opts), key, len, (void *) &opts, sizeof(zval *), NULL); + zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts); } else { - zend_symtable_del(Z_ARRVAL_P(new_opts), key, len); + zend_symtable_str_del(Z_ARRVAL(new_opts), key, len); } } else if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) { - if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) { - array_join(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry), 0, 0); + if ((entry = zend_symtable_str_find(Z_ARRVAL(new_opts), key, len))) { + array_join(Z_ARRVAL_P(opts), Z_ARRVAL_P(entry), 0, 0); } else { Z_ADDREF_P(opts); - zend_symtable_update(Z_ARRVAL_P(new_opts), key, len, (void *) &opts, sizeof(zval *), NULL); + zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts); } } - zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC); + zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts); zval_ptr_dtor(&new_opts); } } -void php_http_client_options_set(zval *this_ptr, zval *opts TSRMLS_DC) +void php_http_client_options_set(zval *instance, zval *opts) { - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - HashPosition pos; - zval *new_opts; - zend_class_entry *this_ce = Z_OBJCE_P(getThis()); - zend_bool is_client = instanceof_function(this_ce, php_http_client_class_entry TSRMLS_CC); + php_http_arrkey_t key; + zval new_opts; + zend_class_entry *this_ce = Z_OBJCE_P(instance); + zend_bool is_client = instanceof_function(this_ce, php_http_client_class_entry); - MAKE_STD_ZVAL(new_opts); - array_init(new_opts); + array_init(&new_opts); if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) { - zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC); + zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts); zval_ptr_dtor(&new_opts); } else { - zval *old_opts, *add_opts, **opt; + zval old_opts_tmp, *old_opts, add_opts, *opt; - MAKE_STD_ZVAL(add_opts); - array_init(add_opts); + array_init(&add_opts); /* some options need extra attention -- thus cannot use array_merge() directly */ - FOREACH_KEYVAL(pos, opts, key, opt) { - if (key.type == HASH_KEY_IS_STRING) { -#define KEYMATCH(k, s) ((sizeof(s)==k.len) && !strcasecmp(k.str, s)) - if (Z_TYPE_PP(opt) == IS_ARRAY && (KEYMATCH(key, "ssl") || KEYMATCH(key, "cookies"))) { - php_http_client_options_set_subr(getThis(), key.str, key.len, *opt, 0 TSRMLS_CC); - } else if (is_client && (KEYMATCH(key, "recordHistory") || KEYMATCH(key, "responseMessageClass"))) { - zend_update_property(this_ce, getThis(), key.str, key.len-1, *opt TSRMLS_CC); - } else if (Z_TYPE_PP(opt) == IS_NULL) { - old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(opts), key.h, key.key, opt) + { + if (key.key) { + if (Z_TYPE_P(opt) == IS_ARRAY && (zend_string_equals_literal(key.key, "ssl") || zend_string_equals_literal(key.key, "cookies"))) { + php_http_client_options_set_subr(instance, key.key->val, key.key->len, opt, 0); + } else if (is_client && (zend_string_equals_literal(key.key, "recordHistory") || zend_string_equals_literal(key.key, "responseMessageClass"))) { + zend_update_property(this_ce, instance, key.key->val, key.key->len, opt); + } else if (Z_TYPE_P(opt) == IS_NULL) { + old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp); if (Z_TYPE_P(old_opts) == IS_ARRAY) { - zend_symtable_del(Z_ARRVAL_P(old_opts), key.str, key.len); + zend_symtable_del(Z_ARRVAL_P(old_opts), key.key); } } else { - Z_ADDREF_P(*opt); - add_assoc_zval_ex(add_opts, key.str, key.len, *opt); + Z_TRY_ADDREF_P(opt); + add_assoc_zval_ex(&add_opts, key.key->val, key.key->len, opt); } } } + ZEND_HASH_FOREACH_END(); - old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp); if (Z_TYPE_P(old_opts) == IS_ARRAY) { - array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts)); + array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL(new_opts)); } - array_join(Z_ARRVAL_P(add_opts), Z_ARRVAL_P(new_opts), 0, 0); - zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC); + array_join(Z_ARRVAL(add_opts), Z_ARRVAL(new_opts), 0, 0); + zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts); zval_ptr_dtor(&new_opts); zval_ptr_dtor(&add_opts); } } -void php_http_client_options_get_subr(zval *this_ptr, char *key, size_t len, zval *return_value TSRMLS_DC) +void php_http_client_options_get_subr(zval *instance, char *key, size_t len, zval *return_value) { - zend_class_entry *this_ce = Z_OBJCE_P(getThis()); - zval **options, *opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + zend_class_entry *this_ce = Z_OBJCE_P(instance); + zval *options, opts_tmp, *opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &opts_tmp); - if ((Z_TYPE_P(opts) == IS_ARRAY) && (SUCCESS == zend_symtable_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) { - RETVAL_ZVAL(*options, 1, 0); + if ((Z_TYPE_P(opts) == IS_ARRAY) && (options = zend_symtable_str_find(Z_ARRVAL_P(opts), key, len))) { + RETVAL_ZVAL(options, 1, 0); } } @@@ -167,7 -157,7 +167,7 @@@ static void queue_dtor(void *enqueued } } -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) { php_http_client_t *free_h = NULL; @@@ -184,11 -174,14 +184,11 @@@ } zend_llist_init(&h->requests, sizeof(php_http_client_enqueue_t), queue_dtor, 0); zend_llist_init(&h->responses, sizeof(void *), NULL, 0); - TSRMLS_SET_CTX(h->ts); if (h->ops->init) { if (!(h = h->ops->init(h, init_arg))) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize client"); - if (free_h) { - efree(free_h); - } + php_error_docref(NULL, E_WARNING, "Could not initialize client"); + PTR_FREE(free_h); } } @@@ -225,9 -218,11 +225,9 @@@ void php_http_client_free(php_http_clie ZEND_RESULT_CODE 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_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to enqueue request; request already in queue"); + php_error_docref(NULL, E_WARNING, "Failed to enqueue request; request already in queue"); return FAILURE; } return h->ops->enqueue(h, enqueue); @@@ -238,11 -233,13 +238,11 @@@ ZEND_RESULT_CODE php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request) { - TSRMLS_FETCH_FROM_CTX(h->ts); - if (h->ops->dequeue) { php_http_client_enqueue_t *enqueue = php_http_client_enqueued(h, request, NULL); if (!enqueue) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to dequeue request; request not in queue"); + php_error_docref(NULL, E_WARNING, "Failed to dequeue request; request not in queue"); return FAILURE; } return h->ops->dequeue(h, enqueue); @@@ -324,86 -321,59 +324,98 @@@ ZEND_RESULT_CODE php_http_client_getopt return FAILURE; } -zend_class_entry *php_http_client_class_entry; static zend_object_handlers php_http_client_object_handlers; -void php_http_client_object_free(void *object TSRMLS_DC) +void php_http_client_object_free(zend_object *object) { - php_http_client_object_t *o = (php_http_client_object_t *) object; + php_http_client_object_t *o = PHP_HTTP_OBJ(object, NULL); + + PTR_FREE(o->gc); 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); + zend_object_std_dtor(object); } -zend_object_value php_http_client_object_new_ex(zend_class_entry *ce, php_http_client_t *client, php_http_client_object_t **ptr TSRMLS_DC) +php_http_client_object_t *php_http_client_object_new_ex(zend_class_entry *ce, php_http_client_t *client) { php_http_client_object_t *o; - o = ecalloc(1, sizeof(php_http_client_object_t)); - zend_object_std_init((zend_object *) o, ce TSRMLS_CC); - object_properties_init((zend_object *) o, ce); + o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); + zend_object_std_init(&o->zo, ce); + object_properties_init(&o->zo, ce); o->client = client; - if (ptr) { - *ptr = o; - } + o->zo.handlers = &php_http_client_object_handlers; - o->zv.handle = zend_objects_store_put(o, NULL, php_http_client_object_free, NULL TSRMLS_CC); - o->zv.handlers = &php_http_client_object_handlers; + return o; +} - return o->zv; +zend_object *php_http_client_object_new(zend_class_entry *ce) +{ + return &php_http_client_object_new_ex(ce, NULL)->zo; } -zend_object_value php_http_client_object_new(zend_class_entry *ce TSRMLS_DC) +static HashTable *php_http_client_object_get_gc(zval *object, zval **table, int *n) { - return php_http_client_object_new_ex(ce, NULL, NULL TSRMLS_CC); + php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, object); + zend_llist_element *el = NULL; + HashTable *props = Z_OBJPROP_P(object); - uint32_t count = zend_hash_num_elements(props) + zend_llist_count(&obj->client->responses) + zend_llist_count(&obj->client->requests); ++ uint32_t count = zend_hash_num_elements(props) + zend_llist_count(&obj->client->responses) + zend_llist_count(&obj->client->requests) + 1; + zval *val; + + *n = 0; + *table = obj->gc = erealloc(obj->gc, sizeof(zval) * count); + ++#if PHP_HTTP_HAVE_CURL ++ if (obj->client->ops == php_http_client_curl_get_ops()) { ++ php_http_client_curl_t *curl = obj->client->ctx; ++ ++ if (curl->ev_ops == php_http_client_curl_user_ops_get()) { ++ php_http_client_curl_user_context_t *ctx = curl->ev_ctx; ++ ++ ZVAL_COPY_VALUE(&obj->gc[(*n)++], &ctx->user); ++ } ++ } ++#endif ++ + for (el = obj->client->responses.head; el; el = el->next) { + php_http_message_object_t *response_obj = *(php_http_message_object_t **) el->data; + ZVAL_OBJ(&obj->gc[(*n)++], &response_obj->zo); + } + + for (el = obj->client->requests.head; el; el = el->next) { - php_http_client_enqueue_t *q = *(php_http_client_enqueue_t **) el->data; ++ php_http_client_enqueue_t *q = (php_http_client_enqueue_t *) el->data; + php_http_message_object_t *request_obj = q->opaque; /* FIXME */ + ZVAL_OBJ(&obj->gc[(*n)++], &request_obj->zo); + } + + ZEND_HASH_FOREACH_VAL(props, val) + { + ZVAL_COPY_VALUE(&obj->gc[(*n)++], val); + } + ZEND_HASH_FOREACH_END(); + + return NULL; } -static void handle_history(zval *zclient, php_http_message_t *request, php_http_message_t *response TSRMLS_DC) +static void handle_history(zval *zclient, php_http_message_t *request, php_http_message_t *response) { - 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_class_entry, zipped, NULL TSRMLS_CC); + zval new_hist, old_hist_tmp, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0, &old_hist_tmp); + php_http_message_t *req_copy = php_http_message_copy(request, NULL); + php_http_message_t *res_copy = php_http_message_copy(response, NULL); + php_http_message_t *zipped = php_http_message_zip(res_copy, req_copy); + php_http_message_object_t *obj = php_http_message_object_new_ex(php_http_message_get_class_entry(), zipped); - MAKE_STD_ZVAL(new_hist); - ZVAL_OBJVAL(new_hist, ov, 0); + ZVAL_OBJ(&new_hist, &obj->zo); if (Z_TYPE_P(old_hist) == IS_OBJECT) { - php_http_message_object_prepend(new_hist, old_hist, 1 TSRMLS_CC); + php_http_message_object_prepend(&new_hist, old_hist, 1); } - zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), new_hist TSRMLS_CC); + zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), &new_hist); zval_ptr_dtor(&new_hist); } @@@ -413,54 -383,61 +425,54 @@@ static ZEND_RESULT_CODE handle_response zval zclient; php_http_message_t *msg; php_http_client_progress_state_t *progress; - TSRMLS_FETCH_FROM_CTX(client->ts); - INIT_PZVAL(&zclient); - ZVAL_OBJVAL(&zclient, ((php_http_client_object_t*) arg)->zv, 0); + ZVAL_OBJ(&zclient, &((php_http_client_object_t*) arg)->zo); if ((msg = *response)) { php_http_message_object_t *msg_obj; - zval *info, *zresponse, *zrequest; + zval info, zresponse, zrequest, rec_hist_tmp; 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 (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); + if (zend_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0, &rec_hist_tmp))) { + handle_history(&zclient, e->request, *response); } /* hard detach, redirects etc. are in the history */ php_http_message_free(&msg->parent); *response = NULL; - MAKE_STD_ZVAL(zresponse); - 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); + msg_obj = php_http_message_object_new_ex(php_http_get_client_response_class_entry(), msg); + ZVAL_OBJECT(&zresponse, &msg_obj->zo, 1); + ZVAL_OBJECT(&zrequest, &((php_http_message_object_t *) e->opaque)->zo, 1); - php_http_message_object_prepend(zresponse, zrequest, 1 TSRMLS_CC); + php_http_message_object_prepend(&zresponse, &zrequest, 1); - MAKE_STD_ZVAL(info); - object_init(info); - info_ht = HASH_OF(info); + 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); + zend_update_property(php_http_get_client_response_class_entry(), &zresponse, ZEND_STRL("transferInfo"), &info); zval_ptr_dtor(&info); - zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC); zend_llist_add_element(&client->responses, &msg_obj); if (e->closure.fci.size) { - zval *retval = NULL; + zval retval; zend_error_handling zeh; - zend_fcall_info_argn(&e->closure.fci TSRMLS_CC, 1, &zresponse); - 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); + ZVAL_UNDEF(&retval); + zend_fcall_info_argn(&e->closure.fci, 1, &zresponse); + zend_replace_error_handling(EH_NORMAL, NULL, &zeh); + zend_fcall_info_call(&e->closure.fci, &e->closure.fcc, &retval, NULL); + zend_restore_error_handling(&zeh); + zend_fcall_info_argn(&e->closure.fci, 0); - if (retval) { - if (Z_TYPE_P(retval) == IS_BOOL) { - dequeue = Z_BVAL_P(retval); - } - zval_ptr_dtor(&retval); + if (Z_TYPE(retval) == IS_TRUE) { + dequeue = 1; } + zval_ptr_dtor(&retval); } zval_ptr_dtor(&zresponse); @@@ -482,35 -459,44 +494,35 @@@ 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, *zclient, **args[2]; + zval 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, 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); - 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); - 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_OBJECT(&zclient, &client_obj->zo, 1); + ZVAL_OBJECT(&args[0], &((php_http_message_object_t *) e->opaque)->zo, 1); + object_init(&args[1]); + add_property_bool(&args[1], "started", progress->started); + add_property_bool(&args[1], "finished", progress->finished); + add_property_string(&args[1], "info", STR_PTR(progress->info)); + add_property_double(&args[1], "dltotal", progress->dl.total); + add_property_double(&args[1], "dlnow", progress->dl.now); + add_property_double(&args[1], "ultotal", progress->ul.total); + add_property_double(&args[1], "ulnow", progress->ul.now); + + zend_replace_error_handling(EH_NORMAL, NULL, &zeh); + php_http_object_method_call(&client_obj->notify, &zclient, NULL, 2, args); + zend_restore_error_handling(&zeh); zval_ptr_dtor(&zclient); - zval_ptr_dtor(&zrequest); - zval_ptr_dtor(&zprogress); + zval_ptr_dtor(&args[0]); + zval_ptr_dtor(&args[1]); } static void response_dtor(void *data) { php_http_message_object_t *msg_obj = *(php_http_message_object_t **) data; - TSRMLS_FETCH_FROM_CTX(msg_obj->message->ts); - zend_objects_store_del_ref_by_handle_ex(msg_obj->zv.handle, msg_obj->zv.handlers TSRMLS_CC); + zend_object_release(&msg_obj->zo); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_construct, 0, 0, 0) @@@ -519,40 -505,45 +531,40 @@@ ZEND_END_ARG_INFO(); static PHP_METHOD(HttpClient, __construct) { - char *driver_str = NULL, *persistent_handle_str = NULL; - int driver_len = 0, persistent_handle_len = 0; - php_http_client_driver_t driver; + zend_string *driver_name = NULL, *persistent_handle_name = NULL; + php_http_client_driver_t *driver; php_resource_factory_t *rf = NULL; php_http_client_object_t *obj; - zval *os; + 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); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|S!S!", &driver_name, &persistent_handle_name), invalid_arg, return); - 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); + if (!zend_hash_num_elements(&php_http_client_drivers)) { + php_http_throw(unexpected_val, "No http\\Client drivers available", NULL); + return; + } + if (!(driver = php_http_client_driver_get(driver_name))) { + php_http_throw(unexpected_val, "Failed to locate \"%s\" client request handler", driver_name ? driver_name->val : "default"); 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); + object_init_ex(&os, spl_ce_SplObjectStorage); + zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), &os); zval_ptr_dtor(&os); - if (persistent_handle_len) { - char *name_str; - size_t name_len; + if (persistent_handle_name) { 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); - - if ((pf = php_persistent_handle_concede(NULL , name_str, name_len, persistent_handle_str, persistent_handle_len, NULL, NULL TSRMLS_CC))) { + if ((pf = php_persistent_handle_concede(NULL, driver->client_name, persistent_handle_name, NULL, NULL))) { rf = php_persistent_handle_resource_factory_init(NULL, pf); } - - efree(name_str); } - obj = zend_object_store_get_object(getThis() TSRMLS_CC); + obj = PHP_HTTP_OBJ(NULL, getThis()); - php_http_expect(obj->client = php_http_client_init(NULL, driver.client_ops, rf, NULL TSRMLS_CC), runtime, return); + php_http_expect(obj->client = php_http_client_init(NULL, driver->client_ops, rf, NULL), runtime, return); - php_http_object_method_init(&obj->notify, getThis(), ZEND_STRL("notify") TSRMLS_CC); + php_http_object_method_init(&obj->notify, getThis(), ZEND_STRL("notify")); obj->client->callback.response.func = handle_response; obj->client->callback.response.arg = obj; @@@ -569,7 -560,7 +581,7 @@@ static PHP_METHOD(HttpClient, reset 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 = PHP_HTTP_OBJ(NULL, getThis()); obj->iterator = 0; php_http_client_reset(obj->client); @@@ -577,19 -568,18 +589,19 @@@ RETVAL_ZVAL(getThis(), 1, 0); } -static HashTable *combined_options(zval *client, zval *request TSRMLS_DC) +static HashTable *combined_options(zval *client, zval *request) { HashTable *options; - int num_options = 0; - zval *z_roptions = NULL, *z_coptions = zend_read_property(php_http_client_class_entry, client, ZEND_STRL("options"), 0 TSRMLS_CC); + unsigned num_options = 0; + zval z_roptions, z_options_tmp, *z_coptions = zend_read_property(php_http_client_class_entry, client, ZEND_STRL("options"), 0, &z_options_tmp); if (Z_TYPE_P(z_coptions) == IS_ARRAY) { num_options = zend_hash_num_elements(Z_ARRVAL_P(z_coptions)); } - zend_call_method_with_0_params(&request, NULL, NULL, "getOptions", &z_roptions); - if (z_roptions && Z_TYPE_P(z_roptions) == IS_ARRAY) { - int num = zend_hash_num_elements(Z_ARRVAL_P(z_roptions)); + ZVAL_UNDEF(&z_roptions); + zend_call_method_with_0_params(request, NULL, NULL, "getOptions", &z_roptions); + if (Z_TYPE(z_roptions) == IS_ARRAY) { + unsigned num = zend_hash_num_elements(Z_ARRVAL(z_roptions)); if (num > num_options) { num_options = num; } @@@ -599,26 -589,28 +611,26 @@@ if (Z_TYPE_P(z_coptions) == IS_ARRAY) { array_copy(Z_ARRVAL_P(z_coptions), options); } - if (z_roptions) { - if (Z_TYPE_P(z_roptions) == IS_ARRAY) { - array_join(Z_ARRVAL_P(z_roptions), options, 0, 0); - } - zval_ptr_dtor(&z_roptions); + if (Z_TYPE(z_roptions) == IS_ARRAY) { + array_join(Z_ARRVAL(z_roptions), options, 0, 0); } + zval_ptr_dtor(&z_roptions); + return options; } static void msg_queue_dtor(php_http_client_enqueue_t *e) { php_http_message_object_t *msg_obj = e->opaque; - TSRMLS_FETCH_FROM_CTX(msg_obj->message->ts); - zend_objects_store_del_ref_by_handle_ex(msg_obj->zv.handle, msg_obj->zv.handlers TSRMLS_CC); + zend_object_release(&msg_obj->zo); zend_hash_destroy(e->options); FREE_HASHTABLE(e->options); if (e->closure.fci.size) { zval_ptr_dtor(&e->closure.fci.function_name); - if (e->closure.fci.object_ptr) { - zval_ptr_dtor(&e->closure.fci.object_ptr); + if (e->closure.fci.object) { + zend_object_release(e->closure.fci.object); } } } @@@ -636,10 -628,10 +648,10 @@@ static PHP_METHOD(HttpClient, enqueue php_http_message_object_t *msg_obj; php_http_client_enqueue_t 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); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O|f", &request, php_http_get_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); + obj = PHP_HTTP_OBJ(NULL, getThis()); + msg_obj = PHP_HTTP_OBJ(NULL, request); 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); @@@ -647,20 -639,20 +659,20 @@@ } q.request = msg_obj->message; - q.options = combined_options(getThis(), request TSRMLS_CC); + q.options = combined_options(getThis(), request); 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); + Z_TRY_ADDREF(fci.function_name); + if (fci.object) { + ++GC_REFCOUNT(fci.object); } } - zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC); + Z_ADDREF_P(request); php_http_expect(SUCCESS == php_http_client_enqueue(obj->client, &q), runtime, msg_queue_dtor(&q); @@@ -679,10 -671,10 +691,10 @@@ static PHP_METHOD(HttpClient, dequeue php_http_client_object_t *obj; php_http_message_object_t *msg_obj; - 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_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_get_client_request_class_entry()), invalid_arg, return); - obj = zend_object_store_get_object(getThis() TSRMLS_CC); - msg_obj = zend_object_store_get_object(request TSRMLS_CC); + obj = PHP_HTTP_OBJ(NULL, getThis()); + msg_obj = PHP_HTTP_OBJ(NULL, request); 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); @@@ -707,30 -699,30 +719,30 @@@ static PHP_METHOD(HttpClient, requeue php_http_message_object_t *msg_obj; php_http_client_enqueue_t 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); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O|f", &request, php_http_get_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); + obj = PHP_HTTP_OBJ(NULL, getThis()); + msg_obj = PHP_HTTP_OBJ(NULL, request); 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.options = combined_options(getThis(), request); 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); + Z_TRY_ADDREF(fci.function_name); + if (fci.object) { + ++GC_REFCOUNT(fci.object); } } - zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC); + Z_ADDREF_P(request); php_http_expect(SUCCESS == php_http_client_enqueue(obj->client, &q), runtime, msg_queue_dtor(&q); @@@ -744,10 -736,10 +756,10 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_co ZEND_END_ARG_INFO(); static PHP_METHOD(HttpClient, count) { - long count_mode = -1; + zend_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); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &count_mode)) { + php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); RETVAL_LONG(zend_llist_count(&obj->client->requests)); } @@@ -761,20 -753,20 +773,20 @@@ static PHP_METHOD(HttpClient, getRespon 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); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O", &zrequest, php_http_get_client_request_class_entry()), invalid_arg, return); - obj = zend_object_store_get_object(getThis() TSRMLS_CC); + obj = PHP_HTTP_OBJ(NULL, getThis()); 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); + php_http_message_object_t *req_obj = PHP_HTTP_OBJ(NULL, zrequest); 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) { - RETURN_OBJVAL(response_obj->zv, 1); + RETURN_OBJECT(&response_obj->zo, 1); } } @@@ -789,7 -781,7 +801,7 @@@ /* pop off and go */ if (response_obj) { - RETVAL_OBJVAL(response_obj->zv, 1); + RETVAL_OBJECT(&response_obj->zo, 1); zend_llist_remove_tail(&obj->client->responses); } } @@@ -799,11 -791,11 +811,11 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_ge ZEND_END_ARG_INFO(); static PHP_METHOD(HttpClient, getHistory) { - zval *zhistory; + zval zhistory_tmp, *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); + zhistory = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0, &zhistory_tmp); RETVAL_ZVAL(zhistory, 1, 0); } @@@ -815,7 -807,7 +827,7 @@@ static PHP_METHOD(HttpClient, send php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return); - obj = zend_object_store_get_object(getThis() TSRMLS_CC); + obj = PHP_HTTP_OBJ(NULL, getThis()); php_http_expect(SUCCESS == php_http_client_exec(obj->client), runtime, return); @@@ -827,7 -819,7 +839,7 @@@ ZEND_END_ARG_INFO() 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); + php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); RETURN_BOOL(0 < php_http_client_once(obj->client)); } @@@ -840,9 -832,9 +852,9 @@@ static PHP_METHOD(HttpClient, wait { double timeout = 0; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|d", &timeout)) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|d", &timeout)) { struct timeval timeout_val; - php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); timeout_val.tv_sec = (time_t) timeout; timeout_val.tv_usec = PHP_HTTP_USEC(timeout) % PHP_HTTP_MCROSEC; @@@ -859,8 -851,8 +871,8 @@@ 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 == zend_parse_parameters(ZEND_NUM_ARGS(), "|H!", &settings), invalid_arg, return); + obj = PHP_HTTP_OBJ(NULL, getThis()); php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_CONFIGURATION, settings), unexpected_val, return); @@@ -875,9 -867,9 +887,9 @@@ static PHP_METHOD(HttpClient, enablePip zend_bool enable = 1; php_http_client_object_t *obj; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enable), invalid_arg, return); - obj = zend_object_store_get_object(getThis() TSRMLS_CC); + obj = PHP_HTTP_OBJ(NULL, getThis()); php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING, &enable), unexpected_val, return); @@@ -892,9 -884,9 +904,9 @@@ static PHP_METHOD(HttpClient, enableEve zend_bool enable = 1; php_http_client_object_t *obj; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enable), invalid_arg, return); - obj = zend_object_store_get_object(getThis() TSRMLS_CC); + obj = PHP_HTTP_OBJ(NULL, getThis()); php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_USE_EVENTS, &enable), unexpected_val, return); @@@ -903,21 -895,20 +915,21 @@@ struct notify_arg { php_http_object_method_t *cb; - zval **args[3]; + zval args[3]; int argc; }; -static int notify(zend_object_iterator *iter, void *puser TSRMLS_DC) +static int notify(zend_object_iterator *iter, void *puser) { - zval **observer = NULL; + zval *observer; struct notify_arg *arg = puser; - iter->funcs->get_current_data(iter, &observer TSRMLS_CC); - if (observer) { - return php_http_object_method_call(arg->cb, *observer, NULL, arg->argc, arg->args TSRMLS_CC); + if ((observer = iter->funcs->get_current_data(iter))) { + if (SUCCESS == php_http_object_method_call(arg->cb, observer, NULL, arg->argc, arg->args)) { + return ZEND_HASH_APPLY_KEEP; + } } - return FAILURE; + return ZEND_HASH_APPLY_STOP; } ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_notify, 0, 0, 0) @@@ -925,14 -916,14 +937,14 @@@ ZEND_END_ARG_INFO(); static PHP_METHOD(HttpClient, notify) { - zval *request = NULL, *zprogress = NULL, *observers; + zval *request = NULL, *zprogress = NULL, observers_tmp, *observers; php_http_client_object_t *client_obj; struct notify_arg arg = {NULL}; - 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); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O!o!", &request, php_http_get_client_request_class_entry(), &zprogress), 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); + client_obj = PHP_HTTP_OBJ(NULL, getThis()); + observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp); if (Z_TYPE_P(observers) != IS_OBJECT) { php_http_throw(unexpected_val, "Observer storage is corrupted", NULL); @@@ -941,26 -932,31 +953,26 @@@ if (client_obj->update) { arg.cb = client_obj->update; - - Z_ADDREF_P(getThis()); - arg.args[0] = &getThis(); + ZVAL_COPY(&arg.args[0], getThis()); arg.argc = 1; if (request) { - Z_ADDREF_P(request); - arg.args[1] = &request; + ZVAL_COPY(&arg.args[1], request); arg.argc += 1; } - if (zprogress) { - Z_ADDREF_P(zprogress); - arg.args[2] = &zprogress; + ZVAL_COPY(&arg.args[2], zprogress); arg.argc += 1; } - spl_iterator_apply(observers, notify, &arg TSRMLS_CC); + spl_iterator_apply(observers, notify, &arg); - zval_ptr_dtor(&getThis()); + zval_ptr_dtor(getThis()); if (request) { - zval_ptr_dtor(&request); + zval_ptr_dtor(request); } if (zprogress) { - zval_ptr_dtor(&zprogress); + zval_ptr_dtor(zprogress); } } @@@ -972,13 -968,13 +984,13 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_at ZEND_END_ARG_INFO(); static PHP_METHOD(HttpClient, attach) { - zval *observers, *observer, *retval = NULL; + zval observers_tmp, *observers, *observer, retval; php_http_client_object_t *client_obj; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "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); + client_obj = PHP_HTTP_OBJ(NULL, getThis()); + observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp); if (Z_TYPE_P(observers) != IS_OBJECT) { php_http_throw(unexpected_val, "Observer storage is corrupted", NULL); @@@ -986,12 -982,13 +998,12 @@@ } if (!client_obj->update) { - client_obj->update = php_http_object_method_init(NULL, observer, ZEND_STRL("update") TSRMLS_CC); + client_obj->update = php_http_object_method_init(NULL, observer, ZEND_STRL("update")); } - zend_call_method_with_1_params(&observers, NULL, NULL, "attach", &retval, observer); - if (retval) { - zval_ptr_dtor(&retval); - } + ZVAL_UNDEF(&retval); + zend_call_method_with_1_params(observers, NULL, NULL, "attach", &retval, observer); + zval_ptr_dtor(&retval); RETVAL_ZVAL(getThis(), 1, 0); } @@@ -1001,20 -998,21 +1013,20 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_de ZEND_END_ARG_INFO(); static PHP_METHOD(HttpClient, detach) { - zval *observers, *observer, *retval = NULL; + zval observers_tmp, *observers, *observer, retval; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &observer, spl_ce_SplObserver), invalid_arg, return); - observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC); + observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp); 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); - } + ZVAL_UNDEF(&retval); + zend_call_method_with_1_params(observers, NULL, NULL, "detach", &retval, observer); + zval_ptr_dtor(&retval); RETVAL_ZVAL(getThis(), 1, 0); } @@@ -1023,11 -1021,11 +1035,11 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_ge ZEND_END_ARG_INFO(); static PHP_METHOD(HttpClient, getObservers) { - zval *observers; + zval observers_tmp, *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); + observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp); if (Z_TYPE_P(observers) != IS_OBJECT) { php_http_throw(unexpected_val, "Observer storage is corrupted", NULL); @@@ -1047,17 -1045,17 +1059,17 @@@ static PHP_METHOD(HttpClient, getProgre php_http_message_object_t *req_obj; php_http_client_progress_state_t *progress; - 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_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_get_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); + obj = PHP_HTTP_OBJ(NULL, getThis()); + req_obj = PHP_HTTP_OBJ(NULL, request); 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_string(return_value, "info", STR_PTR(progress->info)); 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); @@@ -1074,10 -1072,10 +1086,10 @@@ static PHP_METHOD(HttpClient, getTransf php_http_client_object_t *obj; php_http_message_object_t *req_obj; - 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_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_get_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); + obj = PHP_HTTP_OBJ(NULL, getThis()); + req_obj = PHP_HTTP_OBJ(NULL, request); object_init(return_value); info = HASH_OF(return_value); @@@ -1091,9 -1089,9 +1103,9 @@@ static PHP_METHOD(HttpClient, setOption { zval *opts = NULL; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return); - php_http_client_options_set(getThis(), opts TSRMLS_CC); + php_http_client_options_set(getThis(), opts); RETVAL_ZVAL(getThis(), 1, 0); } @@@ -1103,7 -1101,7 +1115,7 @@@ ZEND_END_ARG_INFO() static PHP_METHOD(HttpClient, getOptions) { if (SUCCESS == zend_parse_parameters_none()) { - zval *options = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC); + zval options_tmp, *options = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0, &options_tmp); RETVAL_ZVAL(options, 1, 0); } } @@@ -1115,9 -1113,9 +1127,9 @@@ static PHP_METHOD(HttpClient, setSslOpt { zval *opts = NULL; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return); - php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 1 TSRMLS_CC); + php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts, 1); RETVAL_ZVAL(getThis(), 1, 0); } @@@ -1129,9 -1127,9 +1141,9 @@@ static PHP_METHOD(HttpClient, addSslOpt { zval *opts = NULL; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return); - php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC); + php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts, 0); RETVAL_ZVAL(getThis(), 1, 0); } @@@ -1141,7 -1139,7 +1153,7 @@@ ZEND_END_ARG_INFO() static PHP_METHOD(HttpClient, getSslOptions) { if (SUCCESS == zend_parse_parameters_none()) { - php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC); + php_http_client_options_get_subr(getThis(), ZEND_STRL("ssl"), return_value); } } @@@ -1152,9 -1150,9 +1164,9 @@@ static PHP_METHOD(HttpClient, setCookie { zval *opts = NULL; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return); - php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 1 TSRMLS_CC); + php_http_client_options_set_subr(getThis(), ZEND_STRL("cookies"), opts, 1); RETVAL_ZVAL(getThis(), 1, 0); } @@@ -1166,9 -1164,9 +1178,9 @@@ static PHP_METHOD(HttpClient, addCookie { zval *opts = NULL; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return); - php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 0 TSRMLS_CC); + php_http_client_options_set_subr(getThis(), ZEND_STRL("cookies"), opts, 0); RETVAL_ZVAL(getThis(), 1, 0); } @@@ -1178,17 -1176,16 +1190,17 @@@ ZEND_END_ARG_INFO() static PHP_METHOD(HttpClient, getCookies) { if (SUCCESS == zend_parse_parameters_none()) { - php_http_client_options_get_subr(getThis(), ZEND_STRS("cookies"), return_value TSRMLS_CC); + php_http_client_options_get_subr(getThis(), ZEND_STRL("cookies"), return_value); } } ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableDrivers, 0, 0, 0) ZEND_END_ARG_INFO(); -static PHP_METHOD(HttpClient, getAvailableDrivers) { +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); + php_http_client_driver_list(Z_ARRVAL_P(return_value)); } } @@@ -1197,7 -1194,7 +1209,7 @@@ 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); + php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); array_init(return_value); php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS, NULL, &Z_ARRVAL_P(return_value)); @@@ -1209,7 -1206,7 +1221,7 @@@ 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); + php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); array_init(return_value); php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION, NULL, &Z_ARRVAL_P(return_value)); @@@ -1256,20 -1253,17 +1268,20 @@@ PHP_MINIT_FUNCTION(http_client zend_class_entry ce = {0}; 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 = zend_register_internal_class_ex(&ce, NULL); php_http_client_class_entry->create_object = php_http_client_object_new; - zend_class_implements(php_http_client_class_entry TSRMLS_CC, 2, spl_ce_SplSubject, spl_ce_Countable); + zend_class_implements(php_http_client_class_entry, 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.offset = XtOffsetOf(php_http_client_object_t, zo); + php_http_client_object_handlers.free_obj = php_http_client_object_free; 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); - zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED TSRMLS_CC); - zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("history"), ZEND_ACC_PROTECTED TSRMLS_CC); - zend_declare_property_bool(php_http_client_class_entry, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC TSRMLS_CC); + php_http_client_object_handlers.get_gc = php_http_client_object_get_gc; + zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE); + zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED); + zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("history"), ZEND_ACC_PROTECTED); + zend_declare_property_bool(php_http_client_class_entry, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC); - zend_hash_init(&php_http_client_drivers, 2, NULL, NULL, 1); + zend_hash_init(&php_http_client_drivers, 2, NULL, php_http_client_driver_hash_dtor, 1); return SUCCESS; } diff --combined src/php_http_client_curl.c index 423e12e,863b342..be17832 --- a/src/php_http_client_curl.c +++ b/src/php_http_client_curl.c @@@ -12,30 -12,11 +12,11 @@@ #include "php_http_api.h" #include "php_http_client.h" + #include "php_http_client_curl_event.h" + #include "php_http_client_curl_user.h" #if PHP_HTTP_HAVE_CURL - #if PHP_HTTP_HAVE_EVENT - # if !PHP_HTTP_HAVE_EVENT2 && /* just be really sure */ !(LIBEVENT_VERSION_NUMBER >= 0x02000000) - # include - # define event_base_new event_init - # define event_assign(e, b, s, a, cb, d) do {\ - event_set(e, s, a, cb, d); \ - event_base_set(b, e); \ - } while(0) - # else - # if PHP_HTTP_HAVE_EVENT2 - # include - # include - # else - # error "libevent presence is unknown" - # endif - # endif - # ifndef DBG_EVENTS - # define DBG_EVENTS 0 - # endif - #endif - #ifdef PHP_HTTP_HAVE_OPENSSL # include #endif @@@ -43,23 -24,6 +24,6 @@@ # include #endif - typedef struct php_http_client_curl_handle { - CURLM *multi; - CURLSH *share; - } php_http_client_curl_handle_t; - - typedef struct php_http_client_curl { - php_http_client_curl_handle_t *handle; - - int unfinished; /* int because of curl_multi_perform() */ - - #if PHP_HTTP_HAVE_EVENT - struct event_base *evbase; - struct event *timeout; - unsigned useevents:1; - #endif - } php_http_client_curl_t; - typedef struct php_http_client_curl_handler { CURL *handle; php_resource_factory_t *rf; @@@ -81,15 -45,15 +45,15 @@@ php_http_buffer_t cookies; php_http_buffer_t ranges; - long redirects; - unsigned range_request:1; - unsigned encode_cookies:1; - struct { uint count; double delay; } retry; + long redirects; + unsigned range_request:1; + unsigned encode_cookies:1; + } options; } php_http_client_curl_handler_t; @@@ -115,7 -79,7 +79,7 @@@ static inline php_http_curle_storage_t return st; } -static void *php_http_curle_ctor(void *opaque, void *init_arg TSRMLS_DC) +static void *php_http_curle_ctor(void *opaque, void *init_arg) { void *ch; @@@ -126,7 -90,7 +90,7 @@@ return NULL; } -static void *php_http_curle_copy(void *opaque, void *handle TSRMLS_DC) +static void *php_http_curle_copy(void *opaque, void *handle) { void *ch; @@@ -138,7 -102,7 +102,7 @@@ return NULL; } -static void php_http_curle_dtor(void *opaque, void *handle TSRMLS_DC) +static void php_http_curle_dtor(void *opaque, void *handle) { php_http_curle_storage_t *st = php_http_curle_get_storage(handle); @@@ -161,7 -125,7 +125,7 @@@ static php_resource_factory_ops_t php_h php_http_curle_dtor }; -static void *php_http_curlm_ctor(void *opaque, void *init_arg TSRMLS_DC) +static void *php_http_curlm_ctor(void *opaque, void *init_arg) { php_http_client_curl_handle_t *curl = calloc(1, sizeof(*curl)); @@@ -179,7 -143,7 +143,7 @@@ return curl; } -static void php_http_curlm_dtor(void *opaque, void *handle TSRMLS_DC) +static void php_http_curlm_dtor(void *opaque, void *handle) { php_http_client_curl_handle_t *curl = handle; @@@ -198,10 -162,15 +162,10 @@@ static php_resource_factory_ops_t php_h static size_t php_http_curle_read_callback(void *data, size_t len, size_t n, void *ctx) { - php_http_message_body_t *body = ctx; - - if (body && body->stream_id) { - php_stream *s = php_http_message_body_stream(body); + php_stream *s = php_http_message_body_stream(ctx); - if (s) { - TSRMLS_FETCH_FROM_CTX(body->ts); - return php_stream_read(s, data, len * n); - } else abort(); + if (s) { + return php_stream_read(s, data, len * n); } return 0; } @@@ -238,6 -207,7 +202,6 @@@ static int php_http_curle_progress_call static int php_http_curle_seek_callback(void *userdata, curl_off_t offset, int origin) { php_http_message_body_t *body = userdata; - TSRMLS_FETCH_FROM_CTX(body->ts); if (!body) { return 1; @@@ -332,161 -302,131 +296,161 @@@ static int php_http_curle_body_callback static ZEND_RESULT_CODE php_http_curle_get_info(CURL *ch, HashTable *info) { - char *c; - long l; - double d; - struct curl_slist *s, *p; - zval *subarray, array; - INIT_PZVAL_ARRAY(&array, info); + char *c = NULL; + long l = 0; + double d = 0; + struct curl_slist *s = NULL, *p = NULL; + zval tmp = {{0}}; /* BEGIN::CURLINFO */ if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_URL, &c)) { - add_assoc_string_ex(&array, "effective_url", sizeof("effective_url"), c ? c : "", 1); + ZVAL_STRING(&tmp, STR_PTR(c)); + zend_hash_str_update(info, "effective_url", lenof("effective_url"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) { - add_assoc_long_ex(&array, "response_code", sizeof("response_code"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "response_code", lenof("response_code"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) { - add_assoc_double_ex(&array, "total_time", sizeof("total_time"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "total_time", lenof("total_time"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) { - add_assoc_double_ex(&array, "namelookup_time", sizeof("namelookup_time"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "namelookup_time", lenof("namelookup_time"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) { - add_assoc_double_ex(&array, "connect_time", sizeof("connect_time"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "connect_time", lenof("connect_time"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) { - add_assoc_double_ex(&array, "pretransfer_time", sizeof("pretransfer_time"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "pretransfer_time", lenof("pretransfer_time"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) { - add_assoc_double_ex(&array, "size_upload", sizeof("size_upload"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "size_upload", lenof("size_upload"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) { - add_assoc_double_ex(&array, "size_download", sizeof("size_download"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "size_download", lenof("size_download"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) { - add_assoc_double_ex(&array, "speed_download", sizeof("speed_download"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "speed_download", lenof("speed_download"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) { - add_assoc_double_ex(&array, "speed_upload", sizeof("speed_upload"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "speed_upload", lenof("speed_upload"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) { - add_assoc_long_ex(&array, "header_size", sizeof("header_size"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "header_size", lenof("header_size"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) { - add_assoc_long_ex(&array, "request_size", sizeof("request_size"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "request_size", lenof("request_size"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) { - add_assoc_long_ex(&array, "ssl_verifyresult", sizeof("ssl_verifyresult"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "ssl_verifyresult", lenof("ssl_verifyresult"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) { - add_assoc_long_ex(&array, "filetime", sizeof("filetime"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "filetime", lenof("filetime"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) { - add_assoc_double_ex(&array, "content_length_download", sizeof("content_length_download"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "content_length_download", lenof("content_length_download"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_UPLOAD, &d)) { - add_assoc_double_ex(&array, "content_length_upload", sizeof("content_length_upload"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "content_length_upload", lenof("content_length_upload"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) { - add_assoc_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "starttransfer_time", lenof("starttransfer_time"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) { - add_assoc_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1); + ZVAL_STRING(&tmp, STR_PTR(c)); + zend_hash_str_update(info, "content_type", lenof("content_type"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) { - add_assoc_double_ex(&array, "redirect_time", sizeof("redirect_time"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "redirect_time", lenof("redirect_time"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) { - add_assoc_long_ex(&array, "redirect_count", sizeof("redirect_count"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "redirect_count", lenof("redirect_count"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) { - add_assoc_long_ex(&array, "connect_code", sizeof("connect_code"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "connect_code", lenof("connect_code"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) { - add_assoc_long_ex(&array, "httpauth_avail", sizeof("httpauth_avail"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "httpauth_avail", lenof("httpauth_avail"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) { - add_assoc_long_ex(&array, "proxyauth_avail", sizeof("proxyauth_avail"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "proxyauth_avail", lenof("proxyauth_avail"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) { - add_assoc_long_ex(&array, "os_errno", sizeof("os_errno"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "os_errno", lenof("os_errno"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) { - add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "num_connects", lenof("num_connects"), &tmp); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) { - MAKE_STD_ZVAL(subarray); - array_init(subarray); + array_init(&tmp); for (p = s; p; p = p->next) { if (p->data) { - add_next_index_string(subarray, p->data, 1); + add_next_index_string(&tmp, p->data); } } - add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray); + zend_hash_str_update(info, "ssl_engines", lenof("ssl_engines"), &tmp); curl_slist_free_all(s); } if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &c)) { - add_assoc_string_ex(&array, "redirect_url", sizeof("redirect_url"), c ? c : "", 1); + ZVAL_STRING(&tmp, STR_PTR(c)); + zend_hash_str_update(info, "redirect_url", lenof("redirect_url"), &tmp); } #if PHP_HTTP_CURL_VERSION(7,19,0) if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) { - add_assoc_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1); + ZVAL_STRING(&tmp, STR_PTR(c)); + zend_hash_str_update(info, "primary_ip", lenof("primary_ip"), &tmp); } #endif #if PHP_HTTP_CURL_VERSION(7,19,0) if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_APPCONNECT_TIME, &d)) { - add_assoc_double_ex(&array, "appconnect_time", sizeof("appconnect_time"), d); + ZVAL_DOUBLE(&tmp, d); + zend_hash_str_update(info, "appconnect_time", lenof("appconnect_time"), &tmp); } #endif #if PHP_HTTP_CURL_VERSION(7,19,4) if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONDITION_UNMET, &l)) { - add_assoc_long_ex(&array, "condition_unmet", sizeof("condition_unmet"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "condition_unmet", lenof("condition_unmet"), &tmp); } #endif #if PHP_HTTP_CURL_VERSION(7,21,0) if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_PORT, &l)) { - add_assoc_long_ex(&array, "primary_port", sizeof("primary_port"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "primary_port", lenof("primary_port"), &tmp); } #endif #if PHP_HTTP_CURL_VERSION(7,21,0) if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_IP, &c)) { - add_assoc_string_ex(&array, "local_ip", sizeof("local_ip"), c ? c : "", 1); + ZVAL_STRING(&tmp, STR_PTR(c)); + zend_hash_str_update(info, "local_ip", lenof("local_ip"), &tmp); } #endif #if PHP_HTTP_CURL_VERSION(7,21,0) if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_PORT, &l)) { - add_assoc_long_ex(&array, "local_port", sizeof("local_port"), l); + ZVAL_LONG(&tmp, l); + zend_hash_str_update(info, "local_port", lenof("local_port"), &tmp); } #endif @@@ -494,14 -434,16 +458,14 @@@ #if PHP_HTTP_CURL_VERSION(7,34,0) { - zval *ti_array; + zval ti_array, subarray; struct curl_tlssessioninfo *ti; if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TLS_SESSION, &ti)) { - const char *backend; + char *backend; - MAKE_STD_ZVAL(subarray); - ZVAL_NULL(subarray); - MAKE_STD_ZVAL(ti_array); - array_init(ti_array); + ZVAL_NULL(&subarray); + array_init(&ti_array); switch (ti->backend) { case CURLSSLBACKEND_NONE: @@@ -513,13 -455,13 +477,13 @@@ { SSL_CTX *ctx = ti->internals; - array_init(subarray); - add_assoc_long_ex(subarray, ZEND_STRS("number"), SSL_CTX_sess_number(ctx)); - add_assoc_long_ex(subarray, ZEND_STRS("connect"), SSL_CTX_sess_connect(ctx)); - add_assoc_long_ex(subarray, ZEND_STRS("connect_good"), SSL_CTX_sess_connect_good(ctx)); - add_assoc_long_ex(subarray, ZEND_STRS("connect_renegotiate"), SSL_CTX_sess_connect_renegotiate(ctx)); - add_assoc_long_ex(subarray, ZEND_STRS("hits"), SSL_CTX_sess_hits(ctx)); - add_assoc_long_ex(subarray, ZEND_STRS("cache_full"), SSL_CTX_sess_cache_full(ctx)); + array_init(&subarray); + add_assoc_long_ex(&subarray, ZEND_STRL("number"), SSL_CTX_sess_number(ctx)); + add_assoc_long_ex(&subarray, ZEND_STRL("connect"), SSL_CTX_sess_connect(ctx)); + add_assoc_long_ex(&subarray, ZEND_STRL("connect_good"), SSL_CTX_sess_connect_good(ctx)); + add_assoc_long_ex(&subarray, ZEND_STRL("connect_renegotiate"), SSL_CTX_sess_connect_renegotiate(ctx)); + add_assoc_long_ex(&subarray, ZEND_STRL("hits"), SSL_CTX_sess_hits(ctx)); + add_assoc_long_ex(&subarray, ZEND_STRL("cache_full"), SSL_CTX_sess_cache_full(ctx)); } #endif break; @@@ -530,12 -472,12 +494,12 @@@ gnutls_session_t sess = ti->internals; char *desc; - array_init(subarray); + array_init(&subarray); if ((desc = gnutls_session_get_desc(sess))) { - add_assoc_string_ex(subarray, ZEND_STRS("desc"), desc, 1); + add_assoc_string_ex(&subarray, ZEND_STRL("desc"), desc); gnutls_free(desc); } - add_assoc_bool_ex(subarray, ZEND_STRS("resumed"), gnutls_session_is_resumed(sess)); + add_assoc_bool_ex(&subarray, ZEND_STRL("resumed"), gnutls_session_is_resumed(sess)); } #endif break; @@@ -566,9 -508,9 +530,9 @@@ default: backend = "unknown"; } - add_assoc_string_ex(ti_array, ZEND_STRS("backend"), estrdup(backend), 0); - add_assoc_zval_ex(ti_array, ZEND_STRS("internals"), subarray); - add_assoc_zval_ex(&array, "tls_session", sizeof("tls_session"), ti_array); + add_assoc_string_ex(&ti_array, ZEND_STRL("backend"), backend); + add_assoc_zval_ex(&ti_array, ZEND_STRL("internals"), &subarray); + zend_hash_str_update(info, "tls_session", lenof("tls_session"), &ti_array); } } #endif @@@ -576,41 -518,41 +540,41 @@@ #if (PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)) || (PHP_HTTP_CURL_VERSION(7,34,0) && defined(PHP_HTTP_HAVE_NSS)) || (PHP_HTTP_CURL_VERSION(7,42,0) && defined(PHP_HTTP_HAVE_GNUTLS)) || (PHP_HTTP_CURL_VERSION(7,39,0) && defined(PHP_HTTP_HAVE_GSKIT)) { int i; - zval *ci_array; + zval ci_array, subarray; struct curl_certinfo *ci; char *colon, *keyname; if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) { - MAKE_STD_ZVAL(ci_array); - array_init(ci_array); + array_init(&ci_array); for (i = 0; i < ci->num_of_certs; ++i) { s = ci->certinfo[i]; - MAKE_STD_ZVAL(subarray); - array_init(subarray); + array_init(&subarray); for (p = s; p; p = p->next) { if (p->data) { if ((colon = strchr(p->data, ':'))) { keyname = estrndup(p->data, colon - p->data); - add_assoc_string_ex(subarray, keyname, colon - p->data + 1, colon + 1, 1); + add_assoc_string_ex(&subarray, keyname, colon - p->data, colon + 1); efree(keyname); } else { - add_next_index_string(subarray, p->data, 1); + add_next_index_string(&subarray, p->data); } } } - add_next_index_zval(ci_array, subarray); + add_next_index_zval(&ci_array, &subarray); } - add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array); + zend_hash_str_update(info, "certinfo", lenof("certinfo"), &ci_array); } } #endif { php_http_curle_storage_t *st = php_http_curle_get_storage(ch); - add_assoc_long_ex(&array, "curlcode", sizeof("curlcode"), st->errorcode); - add_assoc_string_ex(&array, "error", sizeof("error"), st->errorbuffer, 1); + ZVAL_LONG(&tmp, st->errorcode); + zend_hash_str_update(info, "curlcode", lenof("curlcode"), &tmp); + ZVAL_STRING(&tmp, st->errorbuffer); + zend_hash_str_update(info, "error", lenof("error"), &tmp); } return SUCCESS; @@@ -621,14 -563,14 +585,14 @@@ static int compare_queue(php_http_clien return handle == ((php_http_client_curl_handler_t *) e->opaque)->handle; } -static php_http_message_t *php_http_curlm_responseparser(php_http_client_curl_handler_t *h TSRMLS_DC) +static php_http_message_t *php_http_curlm_responseparser(php_http_client_curl_handler_t *h) { php_http_message_t *response; php_http_header_parser_t parser; - zval *zh; + zval *zh, tmp; - response = php_http_message_init(NULL, 0, h->response.body TSRMLS_CC); - php_http_header_parser_init(&parser TSRMLS_CC); + response = php_http_message_init(NULL, 0, h->response.body); + php_http_header_parser_init(&parser); while (h->response.headers.used) { php_http_header_parser_state_t st = php_http_header_parser_parse(&parser, &h->response.headers, PHP_HTTP_HEADER_PARSER_CLEANUP, &response->hdrs, @@@ -653,36 -595,33 +617,36 @@@ php_http_message_body_addref(h->response.body); /* let's update the response headers */ - if ((zh = php_http_message_header(response, ZEND_STRL("Content-Length"), 1))) { - zend_hash_update(&response->hdrs, "X-Original-Content-Length", sizeof("X-Original-Content-Length"), &zh, sizeof(zval *), NULL); + if ((zh = php_http_message_header(response, ZEND_STRL("Content-Length")))) { + ZVAL_COPY(&tmp, zh); + zend_hash_str_update(&response->hdrs, "X-Original-Content-Length", lenof("X-Original-Content-Length"), &tmp); } - if ((zh = php_http_message_header(response, ZEND_STRL("Transfer-Encoding"), 0))) { - zend_hash_update(&response->hdrs, "X-Original-Transfer-Encoding", sizeof("X-Original-Transfer-Encoding"), (void *) &zh, sizeof(zval *), NULL); - zend_hash_del(&response->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding")); + if ((zh = php_http_message_header(response, ZEND_STRL("Transfer-Encoding")))) { + ZVAL_COPY(&tmp, zh); + zend_hash_str_del(&response->hdrs, "Transfer-Encoding", lenof("Transfer-Encoding")); + zend_hash_str_update(&response->hdrs, "X-Original-Transfer-Encoding", lenof("X-Original-Transfer-Encoding"), &tmp); } - if ((zh = php_http_message_header(response, ZEND_STRL("Content-Range"), 0))) { - zend_hash_update(&response->hdrs, "X-Original-Content-Range", sizeof("X-Original-Content-Range"), &zh, sizeof(zval *), NULL); - zend_hash_del(&response->hdrs, "Content-Range", sizeof("Content-Range")); + if ((zh = php_http_message_header(response, ZEND_STRL("Content-Range")))) { + ZVAL_COPY(&tmp, zh); + zend_hash_str_del(&response->hdrs, "Content-Range", lenof("Content-Range")); + zend_hash_str_update(&response->hdrs, "X-Original-Content-Range", lenof("X-Original-Content-Range"), &tmp); } - if ((zh = php_http_message_header(response, ZEND_STRL("Content-Encoding"), 0))) { - zend_hash_update(&response->hdrs, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), &zh, sizeof(zval *), NULL); - zend_hash_del(&response->hdrs, "Content-Encoding", sizeof("Content-Encoding")); + if ((zh = php_http_message_header(response, ZEND_STRL("Content-Encoding")))) { + ZVAL_COPY(&tmp, zh); + zend_hash_str_del(&response->hdrs, "Content-Encoding", lenof("Content-Encoding")); + zend_hash_str_update(&response->hdrs, "X-Original-Content-Encoding", lenof("X-Original-Content-Encoding"), &tmp); } php_http_message_update_headers(response); return response; } - static void php_http_curlm_responsehandler(php_http_client_t *context) + void php_http_client_curl_responsehandler(php_http_client_t *context) { int err_count = 0, remaining = 0; php_http_curle_storage_t *st, *err = NULL; php_http_client_enqueue_t *enqueue; php_http_client_curl_t *curl = context->ctx; - TSRMLS_FETCH_FROM_CTX(context->ts); do { CURLMsg *msg = curl_multi_info_read(curl->handle->multi, &remaining); @@@ -705,7 -644,7 +669,7 @@@ if ((enqueue = php_http_client_enqueued(context, msg->easy_handle, compare_queue))) { php_http_client_curl_handler_t *handler = enqueue->opaque; - php_http_message_t *response = php_http_curlm_responseparser(handler TSRMLS_CC); + php_http_message_t *response = php_http_curlm_responseparser(handler); if (response) { context->callback.response.func(context->callback.response.arg, context, &handler->queue, &response); @@@ -719,7 -658,7 +683,7 @@@ int i = 0; do { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s; %s (%s)", curl_easy_strerror(err[i].errorcode), err[i].errorbuffer, STR_PTR(err[i].url)); + php_error_docref(NULL, E_WARNING, "%s; %s (%s)", curl_easy_strerror(err[i].errorcode), err[i].errorbuffer, STR_PTR(err[i].url)); if (err[i].url) { efree(err[i].url); } @@@ -729,158 -668,27 +693,26 @@@ } } - #if PHP_HTTP_HAVE_EVENT - - typedef struct php_http_curlm_event { - struct event evnt; - php_http_client_t *context; - } php_http_curlm_event_t; - - static inline int etoca(short action) { - switch (action & (EV_READ|EV_WRITE)) { - case EV_READ: - return CURL_CSELECT_IN; - break; - case EV_WRITE: - return CURL_CSELECT_OUT; - break; - case EV_READ|EV_WRITE: - return CURL_CSELECT_IN|CURL_CSELECT_OUT; - break; - default: - return 0; - } - } - - static void php_http_curlm_timeout_callback(int socket, short action, void *event_data) - { - php_http_client_t *context = event_data; - php_http_client_curl_t *curl = context->ctx; - - #if DBG_EVENTS - fprintf(stderr, "T"); - #endif - if (curl->useevents) { - CURLMcode rc; - - /* ignore and use -1,0 on timeout */ - (void) socket; - (void) action; - - while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle->multi, CURL_SOCKET_TIMEOUT, 0, &curl->unfinished))); - - if (CURLM_OK != rc) { - php_error_docref(NULL, E_WARNING, "%s", curl_multi_strerror(rc)); - } - - php_http_curlm_responsehandler(context); - } - } - - static void php_http_curlm_event_callback(int socket, short action, void *event_data) + void php_http_client_curl_loop(php_http_client_t *client, curl_socket_t s, int curl_action) { - php_http_client_t *context = event_data; - php_http_client_curl_t *curl = context->ctx; - - #if DBG_EVENTS - fprintf(stderr, "E"); - #endif - if (curl->useevents) { - CURLMcode rc = CURLM_OK; - - while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle->multi, socket, etoca(action), &curl->unfinished))); - - if (CURLM_OK != rc) { - php_error_docref(NULL, E_WARNING, "%s", curl_multi_strerror(rc)); - } - - php_http_curlm_responsehandler(context); - - /* remove timeout if there are no transfers left */ - if (!curl->unfinished && event_initialized(curl->timeout) && event_pending(curl->timeout, EV_TIMEOUT, NULL)) { - event_del(curl->timeout); - } - } - } - - static int php_http_curlm_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data) - { - php_http_client_t *context = socket_data; - php_http_client_curl_t *curl = context->ctx; + CURLMcode rc; + php_http_client_curl_t *curl = client->ctx; - TSRMLS_FETCH_FROM_CTX(client->ts); #if DBG_EVENTS - fprintf(stderr, "S"); + fprintf(stderr, "H"); #endif - if (curl->useevents) { - int events = EV_PERSIST; - php_http_curlm_event_t *ev = assign_data; - - if (!ev) { - ev = ecalloc(1, sizeof(php_http_curlm_event_t)); - ev->context = context; - curl_multi_assign(curl->handle->multi, sock, ev); - } else { - event_del(&ev->evnt); - } - switch (action) { - case CURL_POLL_IN: - events |= EV_READ; - break; - case CURL_POLL_OUT: - events |= EV_WRITE; - break; - case CURL_POLL_INOUT: - events |= EV_READ|EV_WRITE; - break; - - case CURL_POLL_REMOVE: - efree(ev); - /* no break */ - case CURL_POLL_NONE: - return 0; - - default: - php_error_docref(NULL, E_WARNING, "Unknown socket action %d", action); - return -1; - } + do { + rc = curl_multi_socket_action(curl->handle->multi, s, curl_action, &curl->unfinished); + } while (CURLM_CALL_MULTI_PERFORM == rc); - event_assign(&ev->evnt, curl->evbase, sock, events, php_http_curlm_event_callback, context); - event_add(&ev->evnt, NULL); + if (CURLM_OK != rc) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", curl_multi_strerror(rc)); ++ php_error_docref(NULL, E_WARNING, "%s", curl_multi_strerror(rc)); } - return 0; + php_http_client_curl_responsehandler(client); } - static void php_http_curlm_timer_callback(CURLM *multi, long timeout_ms, void *timer_data) - { - php_http_client_t *context = timer_data; - php_http_client_curl_t *curl = context->ctx; - - #if DBG_EVENTS - fprintf(stderr, "\ntimer <- timeout_ms: %ld\n", timeout_ms); - #endif - if (curl->useevents) { - - if (timeout_ms < 0) { - php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT, /*EV_READ|EV_WRITE*/0, context); - } else if (timeout_ms > 0 || !event_initialized(curl->timeout) || !event_pending(curl->timeout, EV_TIMEOUT, NULL)) { - struct timeval timeout; - - if (!event_initialized(curl->timeout)) { - event_assign(curl->timeout, curl->evbase, CURL_SOCKET_TIMEOUT, 0, php_http_curlm_timeout_callback, context); - } - - timeout.tv_sec = timeout_ms / 1000; - timeout.tv_usec = (timeout_ms % 1000) * 1000; - - event_add(curl->timeout, &timeout); - } - } - } - - #endif /* HAVE_EVENT */ - /* curl options */ static php_http_options_t php_http_curle_options, php_http_curlm_options; @@@ -894,7 -702,7 +726,7 @@@ static ZEND_RESULT_CODE php_http_curle_ php_http_client_curl_handler_t *curl = userdata; CURL *ch = curl->handle; - if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, Z_BVAL_P(val) ? 2 : 0)) { + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, Z_TYPE_P(val) == IS_TRUE ? 2 : 0)) { return FAILURE; } return SUCCESS; @@@ -905,10 -713,10 +737,10 @@@ static ZEND_RESULT_CODE php_http_curle_ php_http_client_curl_handler_t *curl = userdata; CURL *ch = curl->handle; - if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIESESSION, (long) Z_BVAL_P(val))) { + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIESESSION, (long) (Z_TYPE_P(val) == IS_TRUE))) { return FAILURE; } - if (Z_BVAL_P(val)) { + if (Z_TYPE_P(val) == IS_TRUE) { if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIELIST, "SESS")) { return FAILURE; } @@@ -926,7 -734,7 +758,7 @@@ static ZEND_RESULT_CODE php_http_curle_ if (storage->cookiestore) { pefree(storage->cookiestore, 1); } - if (val && Z_STRLEN_P(val)) { + if (val && Z_TYPE_P(val) == IS_STRING && Z_STRLEN_P(val)) { storage->cookiestore = pestrndup(Z_STRVAL_P(val), Z_STRLEN_P(val), 1); } else { storage->cookiestore = NULL; @@@ -944,12 -752,11 +776,12 @@@ static ZEND_RESULT_CODE php_http_curle_ { php_http_client_curl_handler_t *curl = userdata; CURL *ch = curl->handle; - TSRMLS_FETCH_FROM_CTX(curl->client->ts); if (val && Z_TYPE_P(val) != IS_NULL) { + HashTable *ht = HASH_OF(val); + if (curl->options.encode_cookies) { - if (SUCCESS == php_http_url_encode_hash_ex(HASH_OF(val), &curl->options.cookies, ZEND_STRL(";"), ZEND_STRL("="), NULL, 0 TSRMLS_CC)) { + if (SUCCESS == php_http_url_encode_hash_ex(ht, &curl->options.cookies, ZEND_STRL(";"), ZEND_STRL("="), NULL, 0)) { php_http_buffer_fix(&curl->options.cookies); if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data)) { return FAILURE; @@@ -958,20 -765,19 +790,20 @@@ return FAILURE; } } else { - HashPosition pos; - php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0); - zval **cookie_val; + php_http_arrkey_t cookie_key; + zval *cookie_val; - FOREACH_KEYVAL(pos, val, cookie_key, cookie_val) { - zval *zv = php_http_ztyp(IS_STRING, *cookie_val); + ZEND_HASH_FOREACH_KEY_VAL(ht, cookie_key.h, cookie_key.key, cookie_val) + { + zend_string *zs = zval_get_string(cookie_val); - php_http_array_hashkey_stringify(&cookie_key); - php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(zv)); - php_http_array_hashkey_stringfree(&cookie_key); + php_http_arrkey_stringify(&cookie_key, NULL); + php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.key->val, zs->val); + php_http_arrkey_dtor(&cookie_key); - zval_ptr_dtor(&zv); + zend_string_release(zs); } + ZEND_HASH_FOREACH_END(); php_http_buffer_fix(&curl->options.cookies); if (curl->options.cookies.used) { @@@ -993,7 -799,7 +825,7 @@@ static ZEND_RESULT_CODE php_http_curle_ { php_http_client_curl_handler_t *curl = userdata; - curl->options.encode_cookies = Z_BVAL_P(val); + curl->options.encode_cookies = Z_TYPE_P(val) == IS_TRUE; return SUCCESS; } @@@ -1001,6 -807,7 +833,6 @@@ static ZEND_RESULT_CODE php_http_curle_ { php_http_client_curl_handler_t *curl = userdata; CURL *ch = curl->handle; - TSRMLS_FETCH_FROM_CTX(curl->client->ts); if (Z_LVAL_P(val)) { if (Z_LVAL_P(val) > 0) { @@@ -1008,7 -815,7 +840,7 @@@ return FAILURE; } } else { - if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) sapi_get_request_time(TSRMLS_C) + Z_LVAL_P(val))) { + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) sapi_get_request_time() + Z_LVAL_P(val))) { return FAILURE; } } @@@ -1033,7 -840,7 +865,7 @@@ static ZEND_RESULT_CODE php_http_curle_ #if !PHP_HTTP_CURL_VERSION(7,21,6) # define CURLOPT_ACCEPT_ENCODING CURLOPT_ENCODING #endif - if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_ACCEPT_ENCODING, Z_BVAL_P(val) ? "" : NULL)) { + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_ACCEPT_ENCODING, Z_TYPE_P(val) == IS_TRUE ? "" : NULL)) { return FAILURE; } return SUCCESS; @@@ -1044,7 -851,7 +876,7 @@@ static ZEND_RESULT_CODE php_http_curle_ php_http_client_curl_handler_t *curl = userdata; php_http_buffer_t header; - if (Z_STRLEN_P(val)) { + if (val && Z_TYPE_P(val) == IS_STRING && Z_STRLEN_P(val)) { zend_bool is_quoted = !((Z_STRVAL_P(val)[0] != '"') || (Z_STRVAL_P(val)[Z_STRLEN_P(val)-1] != '"')); php_http_buffer_init(&header); php_http_buffer_appendf(&header, is_quoted?"%s: %s":"%s: \"%s\"", curl->options.range_request?"If-Match":"If-None-Match", Z_STRVAL_P(val)); @@@ -1059,29 -866,32 +891,29 @@@ static ZEND_RESULT_CODE php_http_curle_ { php_http_client_curl_handler_t *curl = userdata; CURL *ch = curl->handle; - TSRMLS_FETCH_FROM_CTX(curl->client->ts); php_http_buffer_reset(&curl->options.ranges); if (val && Z_TYPE_P(val) != IS_NULL) { - HashPosition pos; - zval **rr, **rb, **re; - - FOREACH_VAL(pos, val, rr) { - if (Z_TYPE_PP(rr) == IS_ARRAY) { - if (2 == php_http_array_list(Z_ARRVAL_PP(rr) TSRMLS_CC, 2, &rb, &re)) { - if ( ((Z_TYPE_PP(rb) == IS_LONG) || ((Z_TYPE_PP(rb) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(rb), Z_STRLEN_PP(rb), NULL, NULL, 1))) && - ((Z_TYPE_PP(re) == IS_LONG) || ((Z_TYPE_PP(re) == IS_STRING) && is_numeric_string(Z_STRVAL_PP(re), Z_STRLEN_PP(re), NULL, NULL, 1)))) { - zval *rbl = php_http_ztyp(IS_LONG, *rb); - zval *rel = php_http_ztyp(IS_LONG, *re); - - if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) { - php_http_buffer_appendf(&curl->options.ranges, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel)); + zval *rr, *rb, *re; + zend_long rbl, rel; + HashTable *ht = HASH_OF(val); + + ZEND_HASH_FOREACH_VAL(ht, rr) + { + if (Z_TYPE_P(rr) == IS_ARRAY) { + if (2 == php_http_array_list(Z_ARRVAL_P(rr), 2, &rb, &re)) { + if ( ((Z_TYPE_P(rb) == IS_LONG) || ((Z_TYPE_P(rb) == IS_STRING) && is_numeric_string(Z_STRVAL_P(rb), Z_STRLEN_P(rb), &rbl, NULL, 1))) && + ((Z_TYPE_P(re) == IS_LONG) || ((Z_TYPE_P(re) == IS_STRING) && is_numeric_string(Z_STRVAL_P(re), Z_STRLEN_P(re), &rel, NULL, 1)))) { + if ((rbl >= 0) && (rel >= 0)) { + php_http_buffer_appendf(&curl->options.ranges, "%ld-%ld,", rbl, rel); } - zval_ptr_dtor(&rbl); - zval_ptr_dtor(&rel); } } } } + ZEND_HASH_FOREACH_END(); if (curl->options.ranges.used) { curl->options.range_request = 1; @@@ -1144,16 -954,26 +976,16 @@@ static ZEND_RESULT_CODE php_http_curle_ php_http_client_curl_handler_t *curl = userdata; CURL *ch = curl->handle; long localport = 0, localportrange = 0; - TSRMLS_FETCH_FROM_CTX(curl->client->ts); if (val && Z_TYPE_P(val) != IS_NULL) { - zval **z_port_start, *zps_copy = NULL, **z_port_end, *zpe_copy = NULL; + zval *zps, *zpe; - switch (php_http_array_list(Z_ARRVAL_P(val) TSRMLS_CC, 2, &z_port_start, &z_port_end)) { + switch (php_http_array_list(Z_ARRVAL_P(val), 2, &zps, &zpe)) { case 2: - zps_copy = php_http_ztyp(IS_LONG, *z_port_start); - zpe_copy = php_http_ztyp(IS_LONG, *z_port_end); - localportrange = labs(Z_LVAL_P(zps_copy)-Z_LVAL_P(zpe_copy))+1L; + localportrange = labs(zval_get_long(zps)-zval_get_long(zpe))+1L; /* no break */ case 1: - if (!zps_copy) { - zps_copy = php_http_ztyp(IS_LONG, *z_port_start); - } - localport = (zpe_copy && Z_LVAL_P(zpe_copy) > 0) ? MIN(Z_LVAL_P(zps_copy), Z_LVAL_P(zpe_copy)) : Z_LVAL_P(zps_copy); - zval_ptr_dtor(&zps_copy); - if (zpe_copy) { - zval_ptr_dtor(&zpe_copy); - } + localport = (zval_get_long(zpe) > 0) ? MIN(zval_get_long(zps), zval_get_long(zpe)) : zval_get_long(zps); break; default: break; @@@ -1171,28 -991,26 +1003,28 @@@ static ZEND_RESULT_CODE php_http_curle_option_set_proxyheader(php_http_option_t *opt, zval *val, void *userdata) { php_http_client_curl_handler_t *curl = userdata; - TSRMLS_FETCH_FROM_CTX(curl->client->ts); if (val && Z_TYPE_P(val) != IS_NULL) { - php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0); - zval **header_val, *header_cpy; - HashPosition pos; + php_http_arrkey_t header_key; + zval *header_val; php_http_buffer_t header; php_http_buffer_init(&header); - FOREACH_KEYVAL(pos, val, header_key, header_val) { - if (header_key.type == HASH_KEY_IS_STRING) { - header_cpy = php_http_ztyp(IS_STRING, *header_val); - php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy)); + ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(val), header_key.h, header_key.key, header_val) + { + if (header_key.key) { + zend_string *zs = zval_get_string(header_val); + + php_http_buffer_appendf(&header, "%s: %s", header_key.key->val, zs->val); + zend_string_release(zs); + php_http_buffer_fix(&header); curl->options.proxyheaders = curl_slist_append(curl->options.proxyheaders, header.data); php_http_buffer_reset(&header); - zval_ptr_dtor(&header_cpy); } } + ZEND_HASH_FOREACH_END(); php_http_buffer_dtor(&header); } if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_PROXYHEADER, curl->options.proxyheaders)) { @@@ -1211,18 -1029,18 +1043,18 @@@ static ZEND_RESULT_CODE php_http_curle_ { php_http_client_curl_handler_t *curl = userdata; CURL *ch = curl->handle; - TSRMLS_FETCH_FROM_CTX(curl->client->ts); if (val && Z_TYPE_P(val) != IS_NULL) { - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - HashPosition pos; - zval **data; - - FOREACH_KEYVAL(pos, val, key, data) { - zval *cpy = php_http_ztyp(IS_STRING, *data); - curl->options.resolve = curl_slist_append(curl->options.resolve, Z_STRVAL_P(cpy)); - zval_ptr_dtor(&cpy); + HashTable *ht = HASH_OF(val); + zval *data; + + ZEND_HASH_FOREACH_VAL(ht, data) + { + zend_string *zs = zval_get_string(data); + curl->options.resolve = curl_slist_append(curl->options.resolve, zs->val); + zend_string_release(zs); } + ZEND_HASH_FOREACH_END(); if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESOLVE, curl->options.resolve)) { return FAILURE; @@@ -1260,13 -1078,13 +1092,13 @@@ static ZEND_RESULT_CODE php_http_curle_ } #endif -static void php_http_curle_options_init(php_http_options_t *registry TSRMLS_DC) +static void php_http_curle_options_init(php_http_options_t *registry) { php_http_option_t *opt; /* url options */ #if PHP_HTTP_CURL_VERSION(7,42,0) - php_http_option_register(registry, ZEND_STRL("path_as_is"), CURLOPT_PATH_AS_IS, IS_BOOL); + php_http_option_register(registry, ZEND_STRL("path_as_is"), CURLOPT_PATH_AS_IS, _IS_BOOL); #endif /* proxy */ @@@ -1279,7 -1097,7 +1111,7 @@@ if ((opt = php_http_option_register(registry, ZEND_STRL("proxyauthtype"), CURLOPT_PROXYAUTH, IS_LONG))) { Z_LVAL(opt->defval) = CURLAUTH_ANYSAFE; } - php_http_option_register(registry, ZEND_STRL("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL, IS_BOOL); + php_http_option_register(registry, ZEND_STRL("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL, _IS_BOOL); #if PHP_HTTP_CURL_VERSION(7,19,4) php_http_option_register(registry, ZEND_STRL("noproxy"), CURLOPT_NOPROXY, IS_STRING); #endif @@@ -1350,8 -1168,8 +1182,8 @@@ Z_LVAL(opt->defval) = 5; } */ - php_http_option_register(registry, ZEND_STRL("fresh_connect"), CURLOPT_FRESH_CONNECT, IS_BOOL); - php_http_option_register(registry, ZEND_STRL("forbid_reuse"), CURLOPT_FORBID_REUSE, IS_BOOL); + php_http_option_register(registry, ZEND_STRL("fresh_connect"), CURLOPT_FRESH_CONNECT, _IS_BOOL); + php_http_option_register(registry, ZEND_STRL("forbid_reuse"), CURLOPT_FORBID_REUSE, _IS_BOOL); /* outgoing interface */ php_http_option_register(registry, ZEND_STRL("interface"), CURLOPT_INTERFACE, IS_STRING); @@@ -1385,7 -1203,7 +1217,7 @@@ if ((opt = php_http_option_register(registry, ZEND_STRL("redirect"), CURLOPT_FOLLOWLOCATION, IS_LONG))) { opt->setter = php_http_curle_option_set_redirect; } - php_http_option_register(registry, ZEND_STRL("unrestricted_auth"), CURLOPT_UNRESTRICTED_AUTH, IS_BOOL); + php_http_option_register(registry, ZEND_STRL("unrestricted_auth"), CURLOPT_UNRESTRICTED_AUTH, _IS_BOOL); #if PHP_HTTP_CURL_VERSION(7,19,1) php_http_option_register(registry, ZEND_STRL("postredir"), CURLOPT_POSTREDIR, IS_LONG); #endif @@@ -1402,17 -1220,18 +1234,17 @@@ if ((opt = php_http_option_register(registry, ZEND_STRL("referer"), CURLOPT_REFERER, IS_STRING))) { opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN; } - if ((opt = php_http_option_register(registry, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER, IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER, _IS_BOOL))) { ZVAL_BOOL(&opt->defval, 1); } /* useragent */ if ((opt = php_http_option_register(registry, ZEND_STRL("useragent"), CURLOPT_USERAGENT, IS_STRING))) { /* don't check strlen, to allow sending no useragent at all */ - ZVAL_STRING(&opt->defval, + ZVAL_PSTRING(&opt->defval, "PECL_HTTP/" PHP_PECL_HTTP_VERSION " " "PHP/" PHP_VERSION " " - "libcurl/" LIBCURL_VERSION - , 0); + "libcurl/" LIBCURL_VERSION); } /* resume */ @@@ -1431,7 -1250,7 +1263,7 @@@ } /* compression */ - if ((opt = php_http_option_register(registry, ZEND_STRL("compress"), 0, IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("compress"), 0, _IS_BOOL))) { opt->setter = php_http_curle_option_set_compress; } @@@ -1441,7 -1260,7 +1273,7 @@@ } /* cookies */ - if ((opt = php_http_option_register(registry, ZEND_STRL("encodecookies"), 0, IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("encodecookies"), 0, _IS_BOOL))) { opt->setter = php_http_curle_option_set_encodecookies; ZVAL_BOOL(&opt->defval, 1); } @@@ -1450,7 -1269,7 +1282,7 @@@ } /* cookiesession, don't load session cookies from cookiestore */ - if ((opt = php_http_option_register(registry, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION, IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION, _IS_BOOL))) { opt->setter = php_http_curle_option_set_cookiesession; } /* cookiestore, read initial cookies from that file and store cookies back into that file */ @@@ -1482,9 -1301,9 +1314,9 @@@ #endif /* tcp */ - php_http_option_register(registry, ZEND_STRL("tcp_nodelay"), CURLOPT_TCP_NODELAY, IS_BOOL); + php_http_option_register(registry, ZEND_STRL("tcp_nodelay"), CURLOPT_TCP_NODELAY, _IS_BOOL); #if PHP_HTTP_CURL_VERSION(7,25,0) - php_http_option_register(registry, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE, IS_BOOL); + php_http_option_register(registry, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE, _IS_BOOL); if ((opt = php_http_option_register(registry, ZEND_STRL("tcp_keepidle"), CURLOPT_TCP_KEEPIDLE, IS_LONG))) { Z_LVAL(opt->defval) = 60; } @@@ -1504,7 -1323,7 +1336,7 @@@ } if ((opt = php_http_option_register(registry, ZEND_STRL("certtype"), CURLOPT_SSLCERTTYPE, IS_STRING))) { opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN; - ZVAL_STRING(&opt->defval, "PEM", 0); + ZVAL_PSTRING(&opt->defval, "PEM"); } if ((opt = php_http_option_register(registry, ZEND_STRL("key"), CURLOPT_SSLKEY, IS_STRING))) { opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN; @@@ -1512,36 -1331,36 +1344,36 @@@ } if ((opt = php_http_option_register(registry, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE, IS_STRING))) { opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN; - ZVAL_STRING(&opt->defval, "PEM", 0); + ZVAL_PSTRING(&opt->defval, "PEM"); } if ((opt = php_http_option_register(registry, ZEND_STRL("keypasswd"), CURLOPT_SSLKEYPASSWD, IS_STRING))) { opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN; } php_http_option_register(registry, ZEND_STRL("engine"), CURLOPT_SSLENGINE, IS_STRING); php_http_option_register(registry, ZEND_STRL("version"), CURLOPT_SSLVERSION, IS_LONG); - if ((opt = php_http_option_register(registry, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER, IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER, _IS_BOOL))) { ZVAL_BOOL(&opt->defval, 1); } - if ((opt = php_http_option_register(registry, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST, IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST, _IS_BOOL))) { ZVAL_BOOL(&opt->defval, 1); opt->setter = php_http_curle_option_set_ssl_verifyhost; } #if PHP_HTTP_CURL_VERSION(7,41,0) && (defined(PHP_HTTP_HAVE_OPENSSL) || defined(PHP_HTTP_HAVE_NSS) || defined(PHP_HTTP_HAVE_GNUTLS)) - php_http_option_register(registry, ZEND_STRL("verifystatus"), CURLOPT_SSL_VERIFYSTATUS, IS_BOOL); + php_http_option_register(registry, ZEND_STRL("verifystatus"), CURLOPT_SSL_VERIFYSTATUS, _IS_BOOL); #endif php_http_option_register(registry, ZEND_STRL("cipher_list"), CURLOPT_SSL_CIPHER_LIST, IS_STRING); if ((opt = php_http_option_register(registry, ZEND_STRL("cainfo"), CURLOPT_CAINFO, IS_STRING))) { opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN; opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR; #ifdef PHP_HTTP_CURL_CAINFO - ZVAL_STRING(&opt->defval, PHP_HTTP_CURL_CAINFO, 0); + ZVAL_PSTRING(&opt->defval, PHP_HTTP_CURL_CAINFO); #endif } if ((opt = php_http_option_register(registry, ZEND_STRL("capath"), CURLOPT_CAPATH, IS_STRING))) { opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN; opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR; #ifdef PHP_HTTP_CURL_CAPATH - ZVAL_STRING(&opt->defval, PHP_HTTP_CURL_CAPATH, 0); + ZVAL_PSTRING(&opt->defval, PHP_HTTP_CURL_CAPATH); #endif } if ((opt = php_http_option_register(registry, ZEND_STRL("random_file"), CURLOPT_RANDOM_FILE, IS_STRING))) { @@@ -1565,15 -1384,13 +1397,15 @@@ # endif #endif #if (PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)) || (PHP_HTTP_CURL_VERSION(7,34,0) && defined(PHP_HTTP_HAVE_NSS)) || (PHP_HTTP_CURL_VERSION(7,42,0) && defined(PHP_HTTP_HAVE_GNUTLS)) || (PHP_HTTP_CURL_VERSION(7,39,0) && defined(PHP_HTTP_HAVE_GSKIT)) - php_http_option_register(registry, ZEND_STRL("certinfo"), CURLOPT_CERTINFO, IS_BOOL); + if ((opt = php_http_option_register(registry, ZEND_STRL("certinfo"), CURLOPT_CERTINFO, _IS_BOOL))) { + ZVAL_FALSE(&opt->defval); + } #endif #if PHP_HTTP_CURL_VERSION(7,36,0) - if ((opt = php_http_option_register(registry, ZEND_STRL("enable_npn"), CURLOPT_SSL_ENABLE_NPN, IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("enable_npn"), CURLOPT_SSL_ENABLE_NPN, _IS_BOOL))) { ZVAL_BOOL(&opt->defval, 1); } - if ((opt = php_http_option_register(registry, ZEND_STRL("enable_alpn"), CURLOPT_SSL_ENABLE_ALPN, IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("enable_alpn"), CURLOPT_SSL_ENABLE_ALPN, _IS_BOOL))) { ZVAL_BOOL(&opt->defval, 1); } #endif @@@ -1596,7 -1413,7 +1428,7 @@@ } #endif #if PHP_HTTP_CURL_VERSION(7,42,0) && (defined(PHP_HTTP_HAVE_NSS) || defined(PHP_HTTP_HAVE_DARWINSSL)) - php_http_option_register(registry, ZEND_STRL("falsestart"), CURLOPT_SSL_FALSESTART, IS_BOOL); + php_http_option_register(registry, ZEND_STRL("falsestart"), CURLOPT_SSL_FALSESTART, _IS_BOOL); #endif } } @@@ -1608,12 -1425,8 +1440,12 @@@ static zval *php_http_curle_get_option( zval *option; if ((option = php_http_option_get(opt, options, NULL))) { - option = php_http_ztyp(opt->type, option); - zend_hash_quick_update(&curl->options.cache, opt->name.s, opt->name.l, opt->name.h, &option, sizeof(zval *), NULL); + zval zopt; + + ZVAL_DUP(&zopt, option); + convert_to_explicit_type(&zopt, opt->type); + zend_hash_update(&curl->options.cache, opt->name, &zopt); + return zend_hash_find(&curl->options.cache, opt->name); } return option; } @@@ -1625,16 -1438,17 +1457,16 @@@ static ZEND_RESULT_CODE php_http_curle_ zval tmp; CURLcode rc = CURLE_UNKNOWN_OPTION; ZEND_RESULT_CODE rv = SUCCESS; - TSRMLS_FETCH_FROM_CTX(curl->client->ts); if (!val) { val = &opt->defval; } switch (opt->type) { - case IS_BOOL: + case _IS_BOOL: if (opt->setter) { rv = opt->setter(opt, val, curl); - } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) Z_BVAL_P(val))) { + } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) (Z_TYPE_P(val) == IS_TRUE))) { rv = FAILURE; } break; @@@ -1650,15 -1464,11 +1482,15 @@@ case IS_STRING: if (opt->setter) { rv = opt->setter(opt, val, curl); + } else if (!val || Z_TYPE_P(val) == IS_NULL) { + if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, NULL))) { + rv = FAILURE; + } } else if ((opt->flags & PHP_HTTP_CURLE_OPTION_CHECK_STRLEN) && !Z_STRLEN_P(val)) { if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, NULL))) { rv = FAILURE; } - } else if ((opt->flags & PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR) && Z_STRVAL_P(val) && SUCCESS != php_check_open_basedir(Z_STRVAL_P(val) TSRMLS_CC)) { + } else if ((opt->flags & PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR) && Z_STRVAL_P(val) && SUCCESS != php_check_open_basedir(Z_STRVAL_P(val))) { if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, NULL))) { rv = FAILURE; } @@@ -1697,7 -1507,7 +1529,7 @@@ break; } if (rv != SUCCESS) { - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s (%s)", opt->name.s, curl_easy_strerror(rc)); + php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_easy_strerror(rc)); } return rv; } @@@ -1710,10 -1520,12 +1542,10 @@@ static ZEND_RESULT_CODE php_http_curlm_ CURLM *ch = curl->handle->multi; HashTable tmp_ht; char **bl = NULL; - TSRMLS_FETCH_FROM_CTX(client->ts); /* array of char *, ending with a NULL */ if (value && Z_TYPE_P(value) != IS_NULL) { - zval **entry; - HashPosition pos; + zval *entry; HashTable *ht = HASH_OF(value); int c = zend_hash_num_elements(ht); char **ptr = ecalloc(c + 1, sizeof(char *)); @@@ -1723,11 -1535,9 +1555,11 @@@ zend_hash_init(&tmp_ht, c, NULL, ZVAL_PTR_DTOR, 0); array_join(ht, &tmp_ht, 0, ARRAY_JOIN_STRINGIFY); - FOREACH_HASH_VAL(pos, &tmp_ht, entry) { - *ptr++ = Z_STRVAL_PP(entry); + ZEND_HASH_FOREACH_VAL(&tmp_ht, entry) + { + *ptr++ = Z_STRVAL_P(entry); } + ZEND_HASH_FOREACH_END(); } if (CURLM_OK != curl_multi_setopt(ch, opt->option, bl)) { @@@ -1746,27 -1556,24 +1578,24 @@@ } #endif - #if PHP_HTTP_HAVE_EVENT - static inline ZEND_RESULT_CODE php_http_curlm_use_eventloop(php_http_client_t *h, zend_bool enable) + static inline ZEND_RESULT_CODE php_http_curlm_use_eventloop(php_http_client_t *h, php_http_client_curl_ops_t *ev_ops, zval *init_data) { php_http_client_curl_t *curl = h->ctx; + void *ev_ctx; - if ((curl->useevents = enable)) { - if (!curl->evbase) { - curl->evbase = event_base_new(); - } - if (!curl->timeout) { - curl->timeout = ecalloc(1, sizeof(struct event)); + if (ev_ops) { + if (!(ev_ctx = ev_ops->init(h, init_data))) { + return FAILURE; } - curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETDATA, h); - curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETFUNCTION, php_http_curlm_socket_callback); - curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERDATA, h); - curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERFUNCTION, php_http_curlm_timer_callback); + curl->ev_ctx = ev_ctx; + curl->ev_ops = ev_ops; } else { - curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETDATA, NULL); - curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETFUNCTION, NULL); - curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERDATA, NULL); - curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERFUNCTION, NULL); + if (curl->ev_ops) { + if (curl->ev_ctx) { + curl->ev_ops->dtor(&curl->ev_ctx); + } + curl->ev_ops = NULL; + } } return SUCCESS; @@@ -1775,10 -1582,19 +1604,18 @@@ static ZEND_RESULT_CODE php_http_curlm_option_set_use_eventloop(php_http_option_t *opt, zval *value, void *userdata) { php_http_client_t *client = userdata; + php_http_client_curl_ops_t *ev_ops = NULL; - TSRMLS_FETCH_FROM_CTX(client->ts); - return php_http_curlm_use_eventloop(client, value && Z_TYPE_P(value) == IS_TRUE); - } - if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_client_curl_user_class_entry TSRMLS_CC)) { ++ if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_client_curl_user_get_class_entry())) { + ev_ops = php_http_client_curl_user_ops_get(); + #if PHP_HTTP_HAVE_EVENT - } else if (value && z_is_true(value)) { ++ } else if (value && zend_is_true(value)) { + ev_ops = php_http_client_curl_event_ops_get(); #endif + } + + return php_http_curlm_use_eventloop(client, ev_ops, value); + } static ZEND_RESULT_CODE php_http_curlm_option_set_share_cookies(php_http_option_t *opt, zval *value, void *userdata) { @@@ -1786,14 -1602,15 +1623,14 @@@ php_http_client_curl_t *curl = client->ctx; CURLSHcode rc; - if (Z_BVAL_P(value)) { + if (Z_TYPE_P(value) == IS_TRUE) { rc = curl_share_setopt(curl->handle->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); } else { rc = curl_share_setopt(curl->handle->share, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE); } if (CURLSHE_OK != rc) { - TSRMLS_FETCH_FROM_CTX(client->ts); - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s (%s)", opt->name.s, curl_share_strerror(rc)); + php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_share_strerror(rc)); return FAILURE; } return SUCCESS; @@@ -1806,21 -1623,22 +1643,21 @@@ static ZEND_RESULT_CODE php_http_curlm_ php_http_client_curl_t *curl = client->ctx; CURLSHcode rc; - if (Z_BVAL_P(value)) { + if (Z_TYPE_P(value) == IS_TRUE) { rc = curl_share_setopt(curl->handle->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION); } else { rc = curl_share_setopt(curl->handle->share, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_SSL_SESSION); } if (CURLSHE_OK != rc) { - TSRMLS_FETCH_FROM_CTX(client->ts); - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s (%s)", opt->name.s, curl_share_strerror(rc)); + php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_share_strerror(rc)); return FAILURE; } return SUCCESS; } #endif -static void php_http_curlm_options_init(php_http_options_t *registry TSRMLS_DC) +static void php_http_curlm_options_init(php_http_options_t *registry) { php_http_option_t *opt; @@@ -1844,7 -1662,7 +1681,7 @@@ php_http_option_register(registry, ZEND_STRL("max_total_connections"), CURLMOPT_MAX_TOTAL_CONNECTIONS, IS_LONG); #endif /* enable/disable HTTP pipelining */ - php_http_option_register(registry, ZEND_STRL("pipelining"), CURLMOPT_PIPELINING, IS_BOOL); + php_http_option_register(registry, ZEND_STRL("pipelining"), CURLMOPT_PIPELINING, _IS_BOOL); /* chunk length threshold for pipelining */ #if PHP_HTTP_CURL_VERSION(7,30,0) php_http_option_register(registry, ZEND_STRL("chunk_length_penalty_size"), CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, IS_LONG); @@@ -1866,20 -1684,18 +1703,18 @@@ } #endif /* events */ - #if PHP_HTTP_HAVE_EVENT - if ((opt = php_http_option_register(registry, ZEND_STRL("use_eventloop"), 0, _IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("use_eventloop"), 0, 0))) { opt->setter = php_http_curlm_option_set_use_eventloop; } - #endif /* share */ - if ((opt = php_http_option_register(registry, ZEND_STRL("share_cookies"), 0, IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("share_cookies"), 0, _IS_BOOL))) { opt->setter = php_http_curlm_option_set_share_cookies; - ZVAL_BOOL(&opt->defval, 1); + ZVAL_TRUE(&opt->defval); } #if PHP_HTTP_CURL_VERSION(7,23,0) - if ((opt = php_http_option_register(registry, ZEND_STRL("share_ssl"), 0, IS_BOOL))) { + if ((opt = php_http_option_register(registry, ZEND_STRL("share_ssl"), 0, _IS_BOOL))) { opt->setter = php_http_curlm_option_set_share_ssl; - ZVAL_BOOL(&opt->defval, 1); + ZVAL_TRUE(&opt->defval); } #endif } @@@ -1892,43 -1708,39 +1727,43 @@@ static ZEND_RESULT_CODE php_http_curlm_ zval *orig = val; CURLMcode rc = CURLM_UNKNOWN_OPTION; ZEND_RESULT_CODE rv = SUCCESS; - TSRMLS_FETCH_FROM_CTX(client->ts); if (!val) { val = &opt->defval; } else if (opt->type && Z_TYPE_P(val) != opt->type && !(Z_TYPE_P(val) == IS_NULL && opt->type == IS_ARRAY)) { - val = php_http_ztyp(opt->type, val); + zval zopt; + + ZVAL_DUP(&zopt, val); + convert_to_explicit_type(&zopt, opt->type); + + val = &zopt; } if (opt->setter) { rv = opt->setter(opt, val, client); } else { switch (opt->type) { - case IS_BOOL: - if (CURLM_OK != (rc = curl_multi_setopt(ch, opt->option, (long) Z_BVAL_P(val)))) { + case _IS_BOOL: + if (CURLM_OK != (rc = curl_multi_setopt(ch, opt->option, (long) zend_is_true(val)))) { rv = FAILURE; - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s (%s)", opt->name.s, curl_multi_strerror(rc)); + php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_multi_strerror(rc)); } break; case IS_LONG: if (CURLM_OK != (rc = curl_multi_setopt(ch, opt->option, Z_LVAL_P(val)))) { rv = FAILURE; - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s (%s)", opt->name.s, curl_multi_strerror(rc)); + php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_multi_strerror(rc)); } break; default: rv = FAILURE; - php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s", opt->name.s); + php_error_docref(NULL, E_NOTICE, "Could not set option %s", opt->name->val); break; } } if (val && val != orig && val != &opt->defval) { - zval_ptr_dtor(&val); + zval_ptr_dtor(val); } return rv; @@@ -1997,9 -1809,10 +1832,9 @@@ static php_http_client_curl_handler_t * void *handle; php_http_client_curl_t *curl = h->ctx; php_http_client_curl_handler_t *handler; - TSRMLS_FETCH_FROM_CTX(h->ts); - if (!(handle = php_resource_factory_handle_ctor(rf, NULL TSRMLS_CC))) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to initialize curl handle->multi"); + if (!(handle = php_resource_factory_handle_ctor(rf, NULL))) { + php_error_docref(NULL, E_WARNING, "Failed to initialize curl handle"); return NULL; } @@@ -2007,7 -1820,7 +1842,7 @@@ handler->rf = rf; handler->client = h; handler->handle = handle; - handler->response.body = php_http_message_body_init(NULL, NULL TSRMLS_CC); + handler->response.body = php_http_message_body_init(NULL, NULL); php_http_buffer_init(&handler->response.headers); php_http_buffer_init(&handler->options.cookies); php_http_buffer_init(&handler->options.ranges); @@@ -2049,10 -1862,11 +1884,10 @@@ static ZEND_RESULT_CODE php_http_client size_t body_size; php_http_message_t *msg = enqueue->request; php_http_curle_storage_t *storage = php_http_curle_get_storage(curl->handle); - TSRMLS_FETCH_FROM_CTX(curl->client->ts); /* request url */ if (!PHP_HTTP_INFO(msg).request.url) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot request empty URL"); + php_error_docref(NULL, E_WARNING, "Cannot request empty URL"); return FAILURE; } storage->errorbuffer[0] = '\0'; @@@ -2068,33 -1882,34 +1903,33 @@@ /* request headers */ php_http_message_update_headers(msg); if (zend_hash_num_elements(&msg->hdrs)) { - php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0); - zval **header_val, *header_cpy; - HashPosition pos; + php_http_arrkey_t header_key; + zval *header_val; + zend_string *header_str; php_http_buffer_t header; #if !PHP_HTTP_CURL_VERSION(7,23,0) - zval **ct = NULL; - - zend_hash_find(&msg->hdrs, ZEND_STRS("Content-Length"), (void *) &ct); + zval *ct = zend_hash_str_find(&msg->hdrs, ZEND_STRL("Content-Length")); #endif php_http_buffer_init(&header); - FOREACH_HASH_KEYVAL(pos, &msg->hdrs, header_key, header_val) { - if (header_key.type == HASH_KEY_IS_STRING) { + ZEND_HASH_FOREACH_KEY_VAL(&msg->hdrs, header_key.h, header_key.key, header_val) + { + if (header_key.key) { #if !PHP_HTTP_CURL_VERSION(7,23,0) /* avoid duplicate content-length header */ - if (ct && *ct == *header_val) { + if (ct && ct == header_val) { continue; } #endif - header_cpy = php_http_ztyp(IS_STRING, *header_val); - php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy)); + header_str = zval_get_string(header_val); + php_http_buffer_appendf(&header, "%s: %s", header_key.key->val, header_str->val); php_http_buffer_fix(&header); curl->options.headers = curl_slist_append(curl->options.headers, header.data); php_http_buffer_reset(&header); - - zval_ptr_dtor(&header_cpy); + zend_string_release(header_str); } } + ZEND_HASH_FOREACH_END(); php_http_buffer_dtor(&header); } curl_easy_setopt(curl->handle, CURLOPT_HTTPHEADER, curl->options.headers); @@@ -2144,7 -1959,7 +1979,7 @@@ curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, PHP_HTTP_INFO(msg).request.method); } } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot use empty request method"); + php_error_docref(NULL, E_WARNING, "Cannot use empty request method"); return FAILURE; } @@@ -2167,9 -1982,11 +2002,9 @@@ static void php_http_client_curl_handle static void php_http_client_curl_handler_dtor(php_http_client_curl_handler_t *handler) { - TSRMLS_FETCH_FROM_CTX(handler->client->ts); - php_http_client_curl_handler_clear(handler); - php_resource_factory_handle_dtor(handler->rf, handler->handle TSRMLS_CC); + php_resource_factory_handle_dtor(handler->rf, handler->handle); php_resource_factory_free(&handler->rf); php_http_message_body_free(&handler->response.body); @@@ -2178,32 -1995,21 +2013,32 @@@ php_http_buffer_dtor(&handler->options.cookies); zend_hash_destroy(&handler->options.cache); +#if PHP_HTTP_CURL_VERSION(7,21,3) + if (handler->options.resolve) { + curl_slist_free_all(handler->options.resolve); + handler->options.resolve = NULL; + } +#endif + if (handler->options.headers) { curl_slist_free_all(handler->options.headers); handler->options.headers = NULL; } + if (handler->options.proxyheaders) { + curl_slist_free_all(handler->options.proxyheaders); + handler->options.proxyheaders = NULL; + } + efree(handler); } static php_http_client_t *php_http_client_curl_init(php_http_client_t *h, void *handle) { php_http_client_curl_t *curl; - TSRMLS_FETCH_FROM_CTX(h->ts); - if (!handle && !(handle = php_resource_factory_handle_ctor(h->rf, NULL TSRMLS_CC))) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to initialize curl handle->multi"); + if (!handle && !(handle = php_resource_factory_handle_ctor(h->rf, NULL))) { + php_error_docref(NULL, E_WARNING, "Failed to initialize curl handle"); return NULL; } @@@ -2218,23 -2024,14 +2053,13 @@@ static void php_http_client_curl_dtor(php_http_client_t *h) { php_http_client_curl_t *curl = h->ctx; - TSRMLS_FETCH_FROM_CTX(h->ts); - #if PHP_HTTP_HAVE_EVENT - if (curl->timeout) { - if (event_initialized(curl->timeout) && event_pending(curl->timeout, EV_TIMEOUT, NULL)) { - event_del(curl->timeout); - } - efree(curl->timeout); - curl->timeout = NULL; - } - if (curl->evbase) { - event_base_free(curl->evbase); - curl->evbase = NULL; + if (curl->ev_ops) { + curl->ev_ops->dtor(&curl->ev_ctx); } - #endif curl->unfinished = 0; - php_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC); + php_resource_factory_handle_dtor(h->rf, curl->handle); efree(curl); h->ctx = NULL; @@@ -2251,38 -2048,37 +2076,38 @@@ static void queue_dtor(php_http_client_ php_http_client_curl_handler_dtor(handler); } -static php_resource_factory_t *create_rf(php_http_client_t *h, php_http_client_enqueue_t *enqueue TSRMLS_DC) +static php_resource_factory_t *create_rf(php_http_client_t *h, php_http_client_enqueue_t *enqueue) { php_persistent_handle_factory_t *pf = NULL; php_resource_factory_t *rf = NULL; php_http_url_t *url = enqueue->request->http.info.request.url; if (!url || (!url->host && !url->path)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot request empty URL"); + php_error_docref(NULL, E_WARNING, "Cannot request empty URL"); return NULL; } /* only if the client itself is setup for persistence */ if (php_resource_factory_is_persistent(h->rf)) { + zend_string *id; char *id_str = NULL; size_t id_len; int port = url->port ? url->port : 80; - zval **zport; + zval *zport; php_persistent_handle_factory_t *phf = h->rf->data; - if (SUCCESS == zend_hash_find(enqueue->options, ZEND_STRS("port"), (void *) &zport)) { - zval *zcpy = php_http_ztyp(IS_LONG, *zport); + if ((zport = zend_hash_str_find(enqueue->options, ZEND_STRL("port")))) { + zend_long lport = zval_get_long(zport); - if (Z_LVAL_P(zcpy)) { - port = Z_LVAL_P(zcpy); + if (lport > 0) { + port = lport; } - zval_ptr_dtor(&zcpy); } - id_len = spprintf(&id_str, 0, "%.*s:%s:%d", (int) phf->ident.len, phf->ident.str, STR_PTR(url->host), port); - pf = php_persistent_handle_concede(NULL, ZEND_STRL("http\\Client\\Curl\\Request"), id_str, id_len, NULL, NULL TSRMLS_CC); - efree(id_str); + id_len = spprintf(&id_str, 0, "%.*s:%s:%d", (int) phf->ident->len, phf->ident->val, STR_PTR(url->host), port); + id = php_http_cs2zs(id_str, id_len); + pf = php_persistent_handle_concede(NULL, PHP_HTTP_G->client.curl.driver.request_name, id, NULL, NULL); + zend_string_release(id); } if (pf) { @@@ -2301,8 -2097,9 +2126,8 @@@ static ZEND_RESULT_CODE php_http_client php_http_client_curl_handler_t *handler; php_http_client_progress_state_t *progress; php_resource_factory_t *rf; - TSRMLS_FETCH_FROM_CTX(h->ts); - rf = create_rf(h, enqueue TSRMLS_CC); + rf = create_rf(h, enqueue); if (!rf) { return FAILURE; } @@@ -2323,7 -2120,7 +2148,7 @@@ if (CURLM_OK != (rs = curl_multi_add_handle(curl->handle->multi, handler->handle))) { php_http_client_curl_handler_dtor(handler); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not enqueue request: %s", curl_multi_strerror(rs)); + php_error_docref(NULL, E_WARNING, "Could not enqueue request: %s", curl_multi_strerror(rs)); return FAILURE; } @@@ -2344,13 -2141,14 +2169,13 @@@ static ZEND_RESULT_CODE php_http_client CURLMcode rs; php_http_client_curl_t *curl = h->ctx; php_http_client_curl_handler_t *handler = enqueue->opaque; - TSRMLS_FETCH_FROM_CTX(h->ts); php_http_client_curl_handler_clear(handler); if (CURLM_OK == (rs = curl_multi_remove_handle(curl->handle->multi, handler->handle))) { zend_llist_del_element(&h->requests, handler->handle, (int (*)(void *, void *)) compare_queue); return SUCCESS; } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not dequeue request: %s", curl_multi_strerror(rs)); + php_error_docref(NULL, E_WARNING, "Could not dequeue request: %s", curl_multi_strerror(rs)); } return FAILURE; @@@ -2366,17 -2164,6 +2191,6 @@@ static void php_http_client_curl_reset( } } - static inline void php_http_client_curl_get_timeout(php_http_client_curl_t *curl, long max_tout, struct timeval *timeout) - { - if ((CURLM_OK == curl_multi_timeout(curl->handle->multi, &max_tout)) && (max_tout > 0)) { - timeout->tv_sec = max_tout / 1000; - timeout->tv_usec = (max_tout % 1000) * 1000; - } else { - timeout->tv_sec = 0; - timeout->tv_usec = 1000; - } - } - #ifdef PHP_WIN32 # define SELECT_ERROR SOCKET_ERROR #else @@@ -2390,22 -2177,9 +2204,9 @@@ static ZEND_RESULT_CODE php_http_client struct timeval timeout; php_http_client_curl_t *curl = h->ctx; - #if PHP_HTTP_HAVE_EVENT - if (curl->useevents) { - if (!event_initialized(curl->timeout)) { - event_assign(curl->timeout, curl->evbase, CURL_SOCKET_TIMEOUT, 0, php_http_curlm_timeout_callback, h); - } else if (custom_timeout && timerisset(custom_timeout)) { - event_add(curl->timeout, custom_timeout); - } else if (!event_pending(curl->timeout, EV_TIMEOUT, NULL)) { - php_http_client_curl_get_timeout(curl, 1000, &timeout); - event_add(curl->timeout, &timeout); - } - - event_base_loop(curl->evbase, EVLOOP_ONCE); - - return SUCCESS; + if (curl->ev_ops) { + return curl->ev_ops->wait(curl->ev_ctx, custom_timeout); } - #endif FD_ZERO(&R); FD_ZERO(&W); @@@ -2432,14 -2206,13 +2233,13 @@@ static int php_http_client_curl_once(ph { php_http_client_curl_t *curl = h->ctx; - #if PHP_HTTP_HAVE_EVENT - if (curl->useevents) { - event_base_loop(curl->evbase, EVLOOP_NONBLOCK); - } else - #endif - while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl->handle->multi, &curl->unfinished)); + if (curl->ev_ops) { + curl->ev_ops->once(curl->ev_ctx); + } else { + while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl->handle->multi, &curl->unfinished)); + } - php_http_curlm_responsehandler(h); + php_http_client_curl_responsehandler(h); return curl->unfinished; @@@ -2447,36 -2220,22 +2247,21 @@@ static ZEND_RESULT_CODE php_http_client_curl_exec(php_http_client_t *h) { - #if PHP_HTTP_HAVE_EVENT php_http_client_curl_t *curl = h->ctx; - TSRMLS_FETCH_FROM_CTX(h->ts); - if (curl->useevents) { - php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT, /*EV_READ|EV_WRITE*/0, h); - do { - int ev_rc = event_base_dispatch(curl->evbase); - - #if DBG_EVENTS - fprintf(stderr, "%c", "X.0"[ev_rc+1]); - #endif + if (curl->ev_ops) { + return curl->ev_ops->exec(curl->ev_ctx); + } - if (ev_rc < 0) { - php_error_docref(NULL, E_ERROR, "Error in event_base_dispatch()"); - return FAILURE; - } - } while (curl->unfinished && !EG(exception)); - } else - #endif - { - while (php_http_client_curl_once(h) && !EG(exception)) { - if (SUCCESS != php_http_client_curl_wait(h, NULL)) { + while (php_http_client_curl_once(h) && !EG(exception)) { + if (SUCCESS != php_http_client_curl_wait(h, NULL)) { #ifdef PHP_WIN32 - /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */ - php_error_docref(NULL, E_WARNING, "WinSock error: %d", WSAGetLastError()); + /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */ - php_error_docref(NULL TSRMLS_CC, E_WARNING, "WinSock error: %d", WSAGetLastError()); ++ php_error_docref(NULL, E_WARNING, "WinSock error: %d", WSAGetLastError()); #else - php_error_docref(NULL, E_WARNING, "%s", strerror(errno)); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno)); ++ php_error_docref(NULL, E_WARNING, "%s", strerror(errno)); #endif - return FAILURE; - } + return FAILURE; } } @@@ -2500,7 -2259,9 +2285,9 @@@ static ZEND_RESULT_CODE php_http_client case PHP_HTTP_CLIENT_OPT_USE_EVENTS: #if PHP_HTTP_HAVE_EVENT - return php_http_curlm_use_eventloop(h, *(zend_bool *) arg); + return php_http_curlm_use_eventloop(h, (*(zend_bool *) arg) + ? php_http_client_curl_event_ops_get() + : NULL, NULL); break; #endif @@@ -2510,31 -2271,34 +2297,31 @@@ return SUCCESS; } -static int apply_available_options(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) +static int apply_available_options(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key) { - php_http_option_t *opt = pDest; + php_http_option_t *opt = Z_PTR_P(pDest); HashTable *ht; - zval *entry; + zval entry; int c; ht = va_arg(args, HashTable*); - MAKE_STD_ZVAL(entry); - if ((c = zend_hash_num_elements(&opt->suboptions.options))) { - array_init_size(entry, c); - zend_hash_apply_with_arguments(&opt->suboptions.options TSRMLS_CC, apply_available_options, 1, Z_ARRVAL_P(entry)); + array_init_size(&entry, c); + zend_hash_apply_with_arguments(&opt->suboptions.options, apply_available_options, 1, Z_ARRVAL(entry)); } else { /* catch deliberate NULL options */ if (Z_TYPE(opt->defval) == IS_STRING && !Z_STRVAL(opt->defval)) { - ZVAL_NULL(entry); + ZVAL_NULL(&entry); } else { - ZVAL_COPY_VALUE(entry, &opt->defval); - zval_copy_ctor(entry); + ZVAL_ZVAL(&entry, &opt->defval, 1, 0); } } - if (hash_key->nKeyLength) { - zend_hash_quick_update(ht, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &entry, sizeof(zval *), NULL); + if (hash_key->key) { + zend_hash_update(ht, hash_key->key, &entry); } else { - zend_hash_index_update(ht, hash_key->h, (void *) &entry, sizeof(zval *), NULL); + zend_hash_index_update(ht, hash_key->h, &entry); } return ZEND_HASH_APPLY_KEEP; @@@ -2543,6 -2307,7 +2330,6 @@@ static ZEND_RESULT_CODE php_http_client_curl_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void **res) { php_http_client_enqueue_t *enqueue; - TSRMLS_FETCH_FROM_CTX(h->ts); switch (opt) { case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO: @@@ -2564,11 -2329,11 +2351,11 @@@ break; case PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS: - zend_hash_apply_with_arguments(&php_http_curle_options.options TSRMLS_CC, apply_available_options, 1, *(HashTable **) res); + zend_hash_apply_with_arguments(&php_http_curle_options.options, apply_available_options, 1, *(HashTable **) res); break; case PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION: - zend_hash_apply_with_arguments(&php_http_curlm_options.options TSRMLS_CC, apply_available_options, 1, *(HashTable **) res); + zend_hash_apply_with_arguments(&php_http_curlm_options.options, apply_available_options, 1, *(HashTable **) res); break; default: @@@ -2598,24 -2363,24 +2385,25 @@@ php_http_client_ops_t *php_http_client_ return &php_http_client_curl_ops; } + PHP_MINIT_FUNCTION(http_client_curl) { curl_version_info_data *info; php_http_options_t *options; - php_http_client_driver_t driver = { - ZEND_STRL("curl"), - &php_http_client_curl_ops - }; - if (SUCCESS != php_http_client_driver_add(&driver)) { + PHP_HTTP_G->client.curl.driver.driver_name = zend_string_init(ZEND_STRL("curl"), 1); + PHP_HTTP_G->client.curl.driver.client_name = zend_string_init(ZEND_STRL("http\\Client\\Curl"), 1); + PHP_HTTP_G->client.curl.driver.request_name = zend_string_init(ZEND_STRL("http\\Client\\Curl\\Request"), 1); + PHP_HTTP_G->client.curl.driver.client_ops = &php_http_client_curl_ops; + + if (SUCCESS != php_http_client_driver_add(&PHP_HTTP_G->client.curl.driver)) { return FAILURE; } - if (SUCCESS != php_persistent_handle_provide(ZEND_STRL("http\\Client\\Curl"), &php_http_curlm_resource_factory_ops, NULL, NULL TSRMLS_CC)) { + if (SUCCESS != php_persistent_handle_provide(PHP_HTTP_G->client.curl.driver.client_name, &php_http_curlm_resource_factory_ops, NULL, NULL)) { return FAILURE; } - if (SUCCESS != php_persistent_handle_provide(ZEND_STRL("http\\Client\\Curl\\Request"), &php_http_curle_resource_factory_ops, NULL, NULL TSRMLS_CC)) { + if (SUCCESS != php_persistent_handle_provide(PHP_HTTP_G->client.curl.driver.request_name, &php_http_curle_resource_factory_ops, NULL, NULL)) { return FAILURE; } @@@ -2623,13 -2388,13 +2411,13 @@@ options->getter = php_http_curle_get_option; options->setter = php_http_curle_set_option; - php_http_curle_options_init(options TSRMLS_CC); + php_http_curle_options_init(options); } if ((options = php_http_options_init(&php_http_curlm_options, 1))) { options->getter = php_http_option_get; options->setter = php_http_curlm_set_option; - php_http_curlm_options_init(options TSRMLS_CC); + php_http_curlm_options_init(options); } if ((info = curl_version_info(CURLVERSION_NOW))) { @@@ -2768,11 -2533,8 +2556,11 @@@ PHP_MSHUTDOWN_FUNCTION(http_client_curl) { - php_persistent_handle_cleanup(ZEND_STRL("http\\Client\\Curl"), NULL, 0 TSRMLS_CC); - php_persistent_handle_cleanup(ZEND_STRL("http\\Client\\Curl\\Request"), NULL, 0 TSRMLS_CC); + php_persistent_handle_cleanup(PHP_HTTP_G->client.curl.driver.client_name, NULL); + php_persistent_handle_cleanup(PHP_HTTP_G->client.curl.driver.request_name, NULL); + zend_string_release(PHP_HTTP_G->client.curl.driver.client_name); + zend_string_release(PHP_HTTP_G->client.curl.driver.request_name); + zend_string_release(PHP_HTTP_G->client.curl.driver.driver_name); php_http_options_dtor(&php_http_curle_options); php_http_options_dtor(&php_http_curlm_options); diff --combined src/php_http_client_curl.h index 9128647,beeb8df..abd8f99 --- a/src/php_http_client_curl.h +++ b/src/php_http_client_curl.h @@@ -15,12 -15,44 +15,50 @@@ #if PHP_HTTP_HAVE_CURL +struct php_http_client_curl_globals { + php_http_client_driver_t driver; +}; + + typedef struct php_http_client_curl_handle { + CURLM *multi; + CURLSH *share; + } php_http_client_curl_handle_t; + + typedef struct php_http_client_curl_ops { + void *(*init)(); + void (*dtor)(void **ctx_ptr); + ZEND_RESULT_CODE (*once)(void *ctx); + ZEND_RESULT_CODE (*wait)(void *ctx, struct timeval *custom_timeout); + ZEND_RESULT_CODE (*exec)(void *ctx); + } php_http_client_curl_ops_t; + + typedef struct php_http_client_curl { + php_http_client_curl_handle_t *handle; + + int unfinished; /* int because of curl_multi_perform() */ + + void *ev_ctx; + php_http_client_curl_ops_t *ev_ops; + } php_http_client_curl_t; + + static inline void php_http_client_curl_get_timeout(php_http_client_curl_t *curl, long max_tout, struct timeval *timeout) + { + if ((CURLM_OK == curl_multi_timeout(curl->handle->multi, &max_tout)) && (max_tout > 0)) { + timeout->tv_sec = max_tout / 1000; + timeout->tv_usec = (max_tout % 1000) * 1000; + } else { + timeout->tv_sec = 0; + timeout->tv_usec = 1000; + } + } + + PHP_HTTP_API void php_http_client_curl_responsehandler(php_http_client_t *client); + PHP_HTTP_API void php_http_client_curl_loop(php_http_client_t *client, curl_socket_t s, int curl_action); ++PHP_HTTP_API php_http_client_ops_t *php_http_client_curl_get_ops(void); + PHP_MINIT_FUNCTION(http_client_curl); PHP_MSHUTDOWN_FUNCTION(http_client_curl); ++ #endif /* PHP_HTTP_HAVE_CURL */ #endif /* PHP_HTTP_CLIENT_CURL_H */ diff --combined src/php_http_client_curl_event.c index 0000000,058c51f..e1ba505 mode 000000,100644..100644 --- a/src/php_http_client_curl_event.c +++ b/src/php_http_client_curl_event.c @@@ -1,0 -1,332 +1,327 @@@ + /* + +--------------------------------------------------------------------+ + | 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" -#include "php_http_client_curl.h" + + #if PHP_HTTP_HAVE_CURL + #if PHP_HTTP_HAVE_EVENT + # if !PHP_HTTP_HAVE_EVENT2 && /* just be really sure */ !(LIBEVENT_VERSION_NUMBER >= 0x02000000) + # include + # define event_base_new event_init + # define event_assign(e, b, s, a, cb, d) do {\ + event_set(e, s, a, cb, d); \ + event_base_set(b, e); \ + } while(0) + # else + # if PHP_HTTP_HAVE_EVENT2 + # include + # include + # else + # error "libevent presence is unknown" + # endif + # endif + # ifndef DBG_EVENTS + # define DBG_EVENTS 0 + # endif + + typedef struct php_http_client_curl_event_context { + php_http_client_t *client; + struct event_base *evbase; + struct event *timeout; + } php_http_client_curl_event_context_t; + + typedef struct php_http_client_curl_event_ev { + struct event evnt; + php_http_client_curl_event_context_t *context; + } php_http_client_curl_event_ev_t; + + static inline int etoca(short action) { + switch (action & (EV_READ|EV_WRITE)) { + case EV_READ: + return CURL_CSELECT_IN; + break; + case EV_WRITE: + return CURL_CSELECT_OUT; + break; + case EV_READ|EV_WRITE: + return CURL_CSELECT_IN|CURL_CSELECT_OUT; + break; + default: + return 0; + } + } + + static void php_http_client_curl_event_handler(void *context, curl_socket_t s, int curl_action) + { + CURLMcode rc; + php_http_client_curl_event_context_t *ctx = context; + php_http_client_curl_t *curl = ctx->client->ctx; - TSRMLS_FETCH_FROM_CTX(ctx->client->ts); + + #if DBG_EVENTS + fprintf(stderr, "H"); + #endif + + do { + rc = curl_multi_socket_action(curl->handle->multi, s, curl_action, &curl->unfinished); + } while (CURLM_CALL_MULTI_PERFORM == rc); + + if (CURLM_OK != rc) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", curl_multi_strerror(rc)); ++ php_error_docref(NULL, E_WARNING, "%s", curl_multi_strerror(rc)); + } + + php_http_client_curl_responsehandler(ctx->client); + } + + static void php_http_client_curl_event_timeout_callback(int socket, short action, void *event_data) + { + #if DBG_EVENTS + fprintf(stderr, "T"); + #endif + + /* ignore and use -1,0 on timeout */ + (void) socket; + (void) action; + + php_http_client_curl_event_handler(event_data, CURL_SOCKET_TIMEOUT, 0); + } + + static void php_http_client_curl_event_timer(CURLM *multi, long timeout_ms, void *timer_data) + { + php_http_client_curl_event_context_t *context = timer_data; + + #if DBG_EVENTS + fprintf(stderr, "\ntimer <- timeout_ms: %ld\n", timeout_ms); + #endif + + if (timeout_ms < 0) { + php_http_client_curl_event_handler(context, CURL_SOCKET_TIMEOUT, 0); + } else if (timeout_ms > 0 || !event_initialized(context->timeout) || !event_pending(context->timeout, EV_TIMEOUT, NULL)) { + struct timeval timeout; + + if (!event_initialized(context->timeout)) { + event_assign(context->timeout, context->evbase, CURL_SOCKET_TIMEOUT, 0, php_http_client_curl_event_timeout_callback, context); + } + + timeout.tv_sec = timeout_ms / 1000; + timeout.tv_usec = (timeout_ms % 1000) * 1000; + + event_add(context->timeout, &timeout); + } + } + + static void php_http_client_curl_event_callback(int socket, short action, void *event_data) + { + php_http_client_curl_event_context_t *ctx = event_data; + php_http_client_curl_t *curl = ctx->client->ctx; + + #if DBG_EVENTS + fprintf(stderr, "E"); + #endif + + php_http_client_curl_event_handler(event_data, socket, etoca(action)); + + /* remove timeout if there are no transfers left */ + if (!curl->unfinished && event_initialized(ctx->timeout) && event_pending(ctx->timeout, EV_TIMEOUT, NULL)) { + event_del(ctx->timeout); + } + } + + static int php_http_client_curl_event_socket(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data) + { + php_http_client_curl_event_context_t *ctx = socket_data; + php_http_client_curl_t *curl = ctx->client->ctx; + int events = EV_PERSIST; + php_http_client_curl_event_ev_t *ev = assign_data; - TSRMLS_FETCH_FROM_CTX(ctx->client->ts); + + #if DBG_EVENTS + fprintf(stderr, "S"); + #endif + + if (!ev) { + ev = ecalloc(1, sizeof(*ev)); + ev->context = ctx; + curl_multi_assign(curl->handle->multi, sock, ev); + } else { + event_del(&ev->evnt); + } + + switch (action) { + case CURL_POLL_IN: + events |= EV_READ; + break; + case CURL_POLL_OUT: + events |= EV_WRITE; + break; + case CURL_POLL_INOUT: + events |= EV_READ|EV_WRITE; + break; + + case CURL_POLL_REMOVE: + efree(ev); + /* no break */ + case CURL_POLL_NONE: + return 0; + + default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown socket action %d", action); ++ php_error_docref(NULL, E_WARNING, "Unknown socket action %d", action); + return -1; + } + + event_assign(&ev->evnt, ctx->evbase, sock, events, php_http_client_curl_event_callback, ctx); + event_add(&ev->evnt, NULL); + + return 0; + } + + static ZEND_RESULT_CODE php_http_client_curl_event_once(void *context) + { + php_http_client_curl_event_context_t *ctx = context; + + #if DBG_EVENTS + fprintf(stderr, "O"); + #endif + + if (0 > event_base_loop(ctx->evbase, EVLOOP_NONBLOCK)) { + return FAILURE; + } + return SUCCESS; + } + + static ZEND_RESULT_CODE php_http_client_curl_event_wait(void *context, struct timeval *custom_timeout) + { + php_http_client_curl_event_context_t *ctx = context; + struct timeval timeout; + + #if DBG_EVENTS + fprintf(stderr, "W"); + #endif + + if (!event_initialized(ctx->timeout)) { + if (0 > event_assign(ctx->timeout, ctx->evbase, CURL_SOCKET_TIMEOUT, 0, php_http_client_curl_event_timeout_callback, ctx)) { + return FAILURE; + } + } else if (custom_timeout && timerisset(custom_timeout)) { + if (0 > event_add(ctx->timeout, custom_timeout)) { + return FAILURE; + } + } else if (!event_pending(ctx->timeout, EV_TIMEOUT, NULL)) { + php_http_client_curl_get_timeout(ctx->client->ctx, 1000, &timeout); + if (0 > event_add(ctx->timeout, &timeout)) { + return FAILURE; + } + } + + if (0 > event_base_loop(ctx->evbase, EVLOOP_ONCE)) { + return FAILURE; + } + + return SUCCESS; + } + + static ZEND_RESULT_CODE php_http_client_curl_event_exec(void *context) + { + php_http_client_curl_event_context_t *ctx = context; + php_http_client_curl_t *curl = ctx->client->ctx; - TSRMLS_FETCH_FROM_CTX(ctx->client->ts); + + #if DBG_EVENTS + fprintf(stderr, "E"); + #endif + + /* kickstart */ + php_http_client_curl_event_handler(ctx, CURL_SOCKET_TIMEOUT, 0); + + do { + if (0 > event_base_dispatch(ctx->evbase)) { + return FAILURE; + } + } while (curl->unfinished && !EG(exception)); + + return SUCCESS; + } + + static void *php_http_client_curl_event_init(php_http_client_t *client) + { + php_http_client_curl_t *curl = client->ctx; + php_http_client_curl_event_context_t *ctx; + struct event_base *evb = event_base_new(); + + #if DBG_EVENTS + fprintf(stderr, "I"); + #endif + + if (!evb) { + return NULL; + } + + ctx = ecalloc(1, sizeof(*ctx)); + ctx->client = client; + ctx->evbase = evb; + ctx->timeout = ecalloc(1, sizeof(struct event)); + + curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETDATA, ctx); + curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETFUNCTION, php_http_client_curl_event_socket); + curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERDATA, ctx); + curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERFUNCTION, php_http_client_curl_event_timer); + + return ctx; + } + + static void php_http_client_curl_event_dtor(void **context) + { + php_http_client_curl_event_context_t *ctx = *context; + php_http_client_curl_t *curl; + + #if DBG_EVENTS + fprintf(stderr, "D"); + #endif + + curl = ctx->client->ctx; + + curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETDATA, NULL); + curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETFUNCTION, NULL); + curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERDATA, NULL); + curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERFUNCTION, NULL); + + if (event_initialized(ctx->timeout) && event_pending(ctx->timeout, EV_TIMEOUT, NULL)) { + event_del(ctx->timeout); + } + efree(ctx->timeout); + event_base_free(ctx->evbase); + + efree(ctx); + *context = NULL; + } + + static php_http_client_curl_ops_t php_http_client_curl_event_ops = { + &php_http_client_curl_event_init, + &php_http_client_curl_event_dtor, + &php_http_client_curl_event_once, + &php_http_client_curl_event_wait, + &php_http_client_curl_event_exec, + }; + + php_http_client_curl_ops_t *php_http_client_curl_event_ops_get() + { + return &php_http_client_curl_event_ops; + } + + #endif /* PHP_HTTP_HAVE_EVENT */ + #endif /* PHP_HTTP_HAVE_CURL */ + + /* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --combined src/php_http_client_curl_event.h index 0000000,d3b0928..4739f62 mode 000000,100644..100644 --- a/src/php_http_client_curl_event.h +++ b/src/php_http_client_curl_event.h @@@ -1,0 -1,28 +1,33 @@@ + /* + +--------------------------------------------------------------------+ + | 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 | + +--------------------------------------------------------------------+ + */ + ++#ifndef PHP_HTTP_CLIENT_CURL_EVENT_H ++#define PHP_HTTP_CLIENT_CURL_EVENT_H ++ + #if PHP_HTTP_HAVE_CURL + #if PHP_HTTP_HAVE_EVENT + + php_http_client_curl_ops_t *php_http_client_curl_event_ops_get(); + + #endif + #endif + ++#endif ++ + /* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --combined src/php_http_client_curl_user.c index 0000000,6fdfa36..a30d666 mode 000000,100644..100644 --- a/src/php_http_client_curl_user.c +++ b/src/php_http_client_curl_user.c @@@ -1,0 -1,346 +1,323 @@@ + /* + +--------------------------------------------------------------------+ + | 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" -#include "php_http_client_curl.h" -#include "php_http_client_curl_user.h" + + #include "php_network.h" + #include "zend_closures.h" + + #if PHP_HTTP_HAVE_CURL + -typedef struct php_http_client_curl_user_context { - php_http_client_t *client; - zval *user; - zend_function closure; - php_http_object_method_t timer; - php_http_object_method_t socket; - php_http_object_method_t once; - php_http_object_method_t wait; - php_http_object_method_t send; -} php_http_client_curl_user_context_t; - + typedef struct php_http_client_curl_user_ev { + php_stream *socket; + php_http_client_curl_user_context_t *context; + } php_http_client_curl_user_ev_t; + + static void php_http_client_curl_user_handler(INTERNAL_FUNCTION_PARAMETERS) + { + zval *zstream = NULL, *zclient = NULL; + php_stream *stream = NULL; + long action = 0; + php_socket_t fd = CURL_SOCKET_TIMEOUT; + php_http_client_object_t *client = NULL; + - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|rl", &zclient, php_http_client_class_entry, &zstream, &action)) { ++ if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "O|rl", &zclient, php_http_client_get_class_entry(), &zstream, &action)) { + return; + } + - client = zend_object_store_get_object(zclient TSRMLS_CC); ++ client = PHP_HTTP_OBJ(NULL, zclient); + if (zstream) { - php_stream_from_zval(stream, &zstream); ++ php_stream_from_zval(stream, zstream); + + if (SUCCESS != php_stream_cast(stream, PHP_STREAM_AS_SOCKETD, (void *) &fd, 1)) { + return; + } + } + php_http_client_curl_loop(client->client, fd, action); + } + + static void php_http_client_curl_user_timer(CURLM *multi, long timeout_ms, void *timer_data) + { + php_http_client_curl_user_context_t *context = timer_data; + + #if DBG_EVENTS + fprintf(stderr, "\ntimer <- timeout_ms: %ld\n", timeout_ms); + #endif + + if (timeout_ms <= 0) { + php_http_client_curl_loop(context->client, CURL_SOCKET_TIMEOUT, 0); + } else if (timeout_ms > 0) { - zval **args[1], *ztimeout; - TSRMLS_FETCH_FROM_CTX(context->client->ts); ++ zval args[1], *ztimeout = &args[0]; + - MAKE_STD_ZVAL(ztimeout); + ZVAL_LONG(ztimeout, timeout_ms); - args[0] = &ztimeout; - php_http_object_method_call(&context->timer, context->user, NULL, 1, args TSRMLS_CC); - zval_ptr_dtor(&ztimeout); ++ php_http_object_method_call(&context->timer, &context->user, NULL, 1, args); ++ zval_ptr_dtor(ztimeout); + } + } + + static int php_http_client_curl_user_socket(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data) + { + php_http_client_curl_user_context_t *ctx = socket_data; + php_http_client_curl_t *curl = ctx->client->ctx; + php_http_client_curl_user_ev_t *ev = assign_data; - zval **args[2], *zaction, *zsocket; - TSRMLS_FETCH_FROM_CTX(ctx->client->ts); ++ zval args[2], *zaction = &args[1], *zsocket = &args[0]; + + #if DBG_EVENTS + fprintf(stderr, "S"); + #endif + + if (!ev) { + ev = ecalloc(1, sizeof(*ev)); + ev->context = ctx; + ev->socket = php_stream_sock_open_from_socket(sock, NULL); + + curl_multi_assign(curl->handle->multi, sock, ev); + } + + switch (action) { + case CURL_POLL_IN: + case CURL_POLL_OUT: + case CURL_POLL_INOUT: + case CURL_POLL_REMOVE: + case CURL_POLL_NONE: - MAKE_STD_ZVAL(zsocket); + php_stream_to_zval(ev->socket, zsocket); - args[0] = &zsocket; - MAKE_STD_ZVAL(zaction); ++ Z_TRY_ADDREF_P(zsocket); + ZVAL_LONG(zaction, action); - args[1] = &zaction; - php_http_object_method_call(&ctx->socket, ctx->user, NULL, 2, args TSRMLS_CC); - zval_ptr_dtor(&zsocket); - zval_ptr_dtor(&zaction); ++ php_http_object_method_call(&ctx->socket, &ctx->user, NULL, 2, args); ++ zval_ptr_dtor(zsocket); ++ zval_ptr_dtor(zaction); + break; + + default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown socket action %d", action); ++ php_error_docref(NULL, E_WARNING, "Unknown socket action %d", action); + return -1; + } + + if (action == CURL_POLL_REMOVE && ev) { ++ php_stream_close(ev->socket); + efree(ev); ++ curl_multi_assign(curl->handle->multi, sock, NULL); + } + return 0; + } + + static ZEND_RESULT_CODE php_http_client_curl_user_once(void *context) + { + php_http_client_curl_user_context_t *ctx = context; - TSRMLS_FETCH_FROM_CTX(ctx->client->ts); + + #if DBG_EVENTS + fprintf(stderr, "O"); + #endif + - return php_http_object_method_call(&ctx->once, ctx->user, NULL, 0, NULL TSRMLS_CC); ++ return php_http_object_method_call(&ctx->once, &ctx->user, NULL, 0, NULL); + } + + static ZEND_RESULT_CODE php_http_client_curl_user_wait(void *context, struct timeval *custom_timeout) + { + php_http_client_curl_user_context_t *ctx = context; + struct timeval timeout; - zval **args[1], *ztimeout; ++ zval args[1], *ztimeout = &args[0]; + ZEND_RESULT_CODE rv; - TSRMLS_FETCH_FROM_CTX(ctx->client->ts); + + #if DBG_EVENTS + fprintf(stderr, "W"); + #endif + + if (!custom_timeout || !timerisset(custom_timeout)) { + php_http_client_curl_get_timeout(ctx->client->ctx, 1000, &timeout); + custom_timeout = &timeout; + } + - MAKE_STD_ZVAL(ztimeout); + ZVAL_LONG(ztimeout, custom_timeout->tv_sec * 1000 + custom_timeout->tv_usec / 1000); - args[0] = &ztimeout; - rv = php_http_object_method_call(&ctx->wait, ctx->user, NULL, 1, args TSRMLS_CC); - zval_ptr_dtor(&ztimeout); ++ rv = php_http_object_method_call(&ctx->wait, &ctx->user, NULL, 1, args); ++ zval_ptr_dtor(ztimeout); + + return rv; + } + + static ZEND_RESULT_CODE php_http_client_curl_user_exec(void *context) + { + php_http_client_curl_user_context_t *ctx = context; + php_http_client_curl_t *curl = ctx->client->ctx; - TSRMLS_FETCH_FROM_CTX(ctx->client->ts); + + #if DBG_EVENTS + fprintf(stderr, "E"); + #endif + + /* kickstart */ + php_http_client_curl_loop(ctx->client, CURL_SOCKET_TIMEOUT, 0); + + do { - if (SUCCESS != php_http_object_method_call(&ctx->send, ctx->user, NULL, 0, NULL TSRMLS_CC)) { ++ if (SUCCESS != php_http_object_method_call(&ctx->send, &ctx->user, NULL, 0, NULL)) { + return FAILURE; + } + } while (curl->unfinished && !EG(exception)); + + return SUCCESS; + } + + static void *php_http_client_curl_user_init(php_http_client_t *client, void *user_data) + { + php_http_client_curl_t *curl = client->ctx; + php_http_client_curl_user_context_t *ctx; + php_http_object_method_t init; - zval *zclosure, **args[1]; - TSRMLS_FETCH_FROM_CTX(client->ts); ++ zval args[1], *zclosure = &args[0]; + + #if DBG_EVENTS + fprintf(stderr, "I"); + #endif + + ctx = ecalloc(1, sizeof(*ctx)); + ctx->client = client; - ctx->user = user_data; - Z_ADDREF_P(ctx->user); ++ ZVAL_COPY(&ctx->user, user_data); + + memset(&ctx->closure, 0, sizeof(ctx->closure)); + ctx->closure.common.type = ZEND_INTERNAL_FUNCTION; - ctx->closure.common.function_name = "php_http_client_curl_user_handler"; ++ ctx->closure.common.function_name = zend_string_init(ZEND_STRL("php_http_client_curl_user_handler"), 0); + ctx->closure.internal_function.handler = php_http_client_curl_user_handler; + - MAKE_STD_ZVAL(zclosure); - zend_create_closure(zclosure, &ctx->closure, NULL, NULL TSRMLS_CC); - args[0] = &zclosure; ++ zend_create_closure(zclosure, &ctx->closure, NULL, NULL, NULL); + - php_http_object_method_init(&init, ctx->user, ZEND_STRL("init") TSRMLS_CC); - php_http_object_method_call(&init, ctx->user, NULL, 1, args TSRMLS_CC); ++ php_http_object_method_init(&init, &ctx->user, ZEND_STRL("init")); ++ php_http_object_method_call(&init, &ctx->user, NULL, 1, args); + php_http_object_method_dtor(&init); - zval_ptr_dtor(&zclosure); ++ zval_ptr_dtor(zclosure); + - php_http_object_method_init(&ctx->timer, ctx->user, ZEND_STRL("timer") TSRMLS_CC); - php_http_object_method_init(&ctx->socket, ctx->user, ZEND_STRL("socket") TSRMLS_CC); - php_http_object_method_init(&ctx->once, ctx->user, ZEND_STRL("once") TSRMLS_CC); - php_http_object_method_init(&ctx->wait, ctx->user, ZEND_STRL("wait") TSRMLS_CC); - php_http_object_method_init(&ctx->send, ctx->user, ZEND_STRL("send") TSRMLS_CC); ++ php_http_object_method_init(&ctx->timer, &ctx->user, ZEND_STRL("timer")); ++ php_http_object_method_init(&ctx->socket, &ctx->user, ZEND_STRL("socket")); ++ php_http_object_method_init(&ctx->once, &ctx->user, ZEND_STRL("once")); ++ php_http_object_method_init(&ctx->wait, &ctx->user, ZEND_STRL("wait")); ++ php_http_object_method_init(&ctx->send, &ctx->user, ZEND_STRL("send")); + + curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETDATA, ctx); + curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETFUNCTION, php_http_client_curl_user_socket); + curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERDATA, ctx); + curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERFUNCTION, php_http_client_curl_user_timer); + + return ctx; + } + + static void php_http_client_curl_user_dtor(void **context) + { + php_http_client_curl_user_context_t *ctx = *context; + php_http_client_curl_t *curl; + + #if DBG_EVENTS + fprintf(stderr, "D"); + #endif + + curl = ctx->client->ctx; + + curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETDATA, NULL); + curl_multi_setopt(curl->handle->multi, CURLMOPT_SOCKETFUNCTION, NULL); + curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERDATA, NULL); + curl_multi_setopt(curl->handle->multi, CURLMOPT_TIMERFUNCTION, NULL); + + php_http_object_method_dtor(&ctx->timer); + php_http_object_method_dtor(&ctx->socket); + php_http_object_method_dtor(&ctx->once); + php_http_object_method_dtor(&ctx->wait); + php_http_object_method_dtor(&ctx->send); + ++ zend_string_release(ctx->closure.common.function_name); + zval_ptr_dtor(&ctx->user); + + efree(ctx); + *context = NULL; + } + + static php_http_client_curl_ops_t php_http_client_curl_user_ops = { + &php_http_client_curl_user_init, + &php_http_client_curl_user_dtor, + &php_http_client_curl_user_once, + &php_http_client_curl_user_wait, + &php_http_client_curl_user_exec, + }; + + php_http_client_curl_ops_t *php_http_client_curl_user_ops_get() + { + return &php_http_client_curl_user_ops; + } + -zend_class_entry *php_http_client_curl_user_class_entry; ++static zend_class_entry *php_http_client_curl_user_class_entry; ++ ++zend_class_entry *php_http_client_curl_user_get_class_entry() ++{ ++ return php_http_client_curl_user_class_entry; ++} + + ZEND_BEGIN_ARG_INFO_EX(ai_init, 0, 0, 1) + ZEND_ARG_TYPE_INFO(0, run, IS_CALLABLE, 0) + ZEND_END_ARG_INFO(); + ZEND_BEGIN_ARG_INFO_EX(ai_timer, 0, 0, 1) + #if PHP_VERSION_ID >= 70000 + ZEND_ARG_TYPE_INFO(0, timeout_ms, IS_LONG, 0) + #else + ZEND_ARG_INFO(0, timeout_ms) + #endif + ZEND_END_ARG_INFO(); + ZEND_BEGIN_ARG_INFO_EX(ai_socket, 0, 0, 2) ++ ZEND_ARG_INFO(0, socket) + #if PHP_VERSION_ID >= 70000 - ZEND_ARG_TYPE_INFO(0, socket, IS_RESOURCE, 0) + ZEND_ARG_TYPE_INFO(0, action, IS_LONG, 0) + #else - ZEND_ARG_INFO(0, socket) + ZEND_ARG_INFO(0, action) + #endif + ZEND_END_ARG_INFO(); + ZEND_BEGIN_ARG_INFO_EX(ai_once, 0, 0, 0) + ZEND_END_ARG_INFO(); + ZEND_BEGIN_ARG_INFO_EX(ai_wait, 0, 0, 0) + #if PHP_VERSION_ID >= 70000 + ZEND_ARG_TYPE_INFO(0, timeout_ms, IS_LONG, 0) + #else + ZEND_ARG_INFO(0, timeout_ms) + #endif + ZEND_END_ARG_INFO(); + ZEND_BEGIN_ARG_INFO_EX(ai_send, 0, 0, 0) + ZEND_END_ARG_INFO(); + + static zend_function_entry php_http_client_curl_user_methods[] = { + PHP_ABSTRACT_ME(HttpClientCurlUser, init, ai_init) + PHP_ABSTRACT_ME(HttpClientCurlUser, timer, ai_timer) + PHP_ABSTRACT_ME(HttpClientCurlUser, socket, ai_socket) + PHP_ABSTRACT_ME(HttpClientCurlUser, once, ai_once) + PHP_ABSTRACT_ME(HttpClientCurlUser, wait, ai_wait) + PHP_ABSTRACT_ME(HttpClientCulrUser, send, ai_send) + {0} + }; + + PHP_MINIT_FUNCTION(http_client_curl_user) + { + zend_class_entry ce = {0}; + + INIT_NS_CLASS_ENTRY(ce, "http\\Client\\Curl", "User", php_http_client_curl_user_methods); - php_http_client_curl_user_class_entry = zend_register_internal_interface(&ce TSRMLS_CC); ++ php_http_client_curl_user_class_entry = zend_register_internal_interface(&ce); + - zend_declare_class_constant_long(php_http_client_curl_user_class_entry, ZEND_STRL("POLL_NONE"), CURL_POLL_NONE TSRMLS_CC); - zend_declare_class_constant_long(php_http_client_curl_user_class_entry, ZEND_STRL("POLL_IN"), CURL_POLL_IN TSRMLS_CC); - zend_declare_class_constant_long(php_http_client_curl_user_class_entry, ZEND_STRL("POLL_OUT"), CURL_POLL_OUT TSRMLS_CC); - zend_declare_class_constant_long(php_http_client_curl_user_class_entry, ZEND_STRL("POLL_INOUT"), CURL_POLL_INOUT TSRMLS_CC); - zend_declare_class_constant_long(php_http_client_curl_user_class_entry, ZEND_STRL("POLL_REMOVE"), CURL_POLL_REMOVE TSRMLS_CC); ++ zend_declare_class_constant_long(php_http_client_curl_user_class_entry, ZEND_STRL("POLL_NONE"), CURL_POLL_NONE); ++ zend_declare_class_constant_long(php_http_client_curl_user_class_entry, ZEND_STRL("POLL_IN"), CURL_POLL_IN); ++ zend_declare_class_constant_long(php_http_client_curl_user_class_entry, ZEND_STRL("POLL_OUT"), CURL_POLL_OUT); ++ zend_declare_class_constant_long(php_http_client_curl_user_class_entry, ZEND_STRL("POLL_INOUT"), CURL_POLL_INOUT); ++ zend_declare_class_constant_long(php_http_client_curl_user_class_entry, ZEND_STRL("POLL_REMOVE"), CURL_POLL_REMOVE); + + return SUCCESS; + } + + #endif /* PHP_HTTP_HAVE_CURL */ + + /* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim600: noet sw=4 ts=4 fdm=marker + * vim<600: noet sw=4 ts=4 + */ diff --combined src/php_http_client_curl_user.h index 0000000,8a6778f..35f5d6f mode 000000,100644..100644 --- a/src/php_http_client_curl_user.h +++ b/src/php_http_client_curl_user.h @@@ -1,0 -1,89 +1,105 @@@ + /* + +--------------------------------------------------------------------+ + | 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 | + +--------------------------------------------------------------------+ + */ + ++#ifndef PHP_HTTP_CLIENT_CURL_USER_H ++#define PHP_HTTP_CLIENT_CURL_USER_H ++ + #if PHP_HTTP_HAVE_CURL + -PHP_HTTP_API zend_class_entry *php_http_client_curl_user_class_entry; ++typedef struct php_http_client_curl_user_context { ++ php_http_client_t *client; ++ zval user; ++ zend_function closure; ++ php_http_object_method_t timer; ++ php_http_object_method_t socket; ++ php_http_object_method_t once; ++ php_http_object_method_t wait; ++ php_http_object_method_t send; ++} php_http_client_curl_user_context_t; ++ ++PHP_HTTP_API zend_class_entry *php_http_client_curl_user_get_class_entry(); + PHP_HTTP_API php_http_client_curl_ops_t *php_http_client_curl_user_ops_get(); + PHP_MINIT_FUNCTION(http_client_curl_user); + + #endif + + #if 0 + refcount; + php_http_message_body_addref(body); return body; } @@@ -45,11 -45,15 +45,11 @@@ body->refcount = 1; if (stream) { - php_stream_auto_cleanup(stream); - body->stream_id = php_stream_get_resource_id(stream); - zend_list_addref(body->stream_id); + body->res = stream->res; - ++GC_REFCOUNT(body->res); } else { - stream = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff); - php_stream_auto_cleanup(stream); - body->stream_id = php_stream_get_resource_id(stream); + body->res = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff)->res; } - TSRMLS_SET_CTX(body->ts); ++ ++GC_REFCOUNT(body->res); if (body_ptr) { *body_ptr = body; @@@ -66,10 -70,12 +66,10 @@@ unsigned php_http_message_body_addref(p php_http_message_body_t *php_http_message_body_copy(php_http_message_body_t *from, php_http_message_body_t *to) { if (from) { - TSRMLS_FETCH_FROM_CTX(from->ts); - if (to) { php_stream_truncate_set_size(php_http_message_body_stream(to), 0); } else { - to = php_http_message_body_init(NULL, NULL TSRMLS_CC); + to = php_http_message_body_init(NULL, NULL); } php_http_message_body_to_stream(from, php_http_message_body_stream(to), 0, 0); @@@ -91,6 -97,9 +91,6 @@@ void php_http_message_body_free(php_htt php_http_message_body_t *body = *body_ptr; if (!--body->refcount) { - TSRMLS_FETCH_FROM_CTX(body->ts); - /* NOFIXME: shows leakinfo in DEBUG mode */ - zend_list_delete(body->stream_id); PTR_FREE(body->boundary); efree(body); } @@@ -100,6 -109,7 +100,6 @@@ const php_stream_statbuf *php_http_message_body_stat(php_http_message_body_t *body) { - TSRMLS_FETCH_FROM_CTX(body->ts); php_stream_stat(php_http_message_body_stream(body), &body->ssb); return &body->ssb; } @@@ -108,8 -118,9 +108,8 @@@ const char *php_http_message_body_bound { if (!body->boundary) { union { double dbl; int num[2]; } data; - TSRMLS_FETCH_FROM_CTX(body->ts); - data.dbl = php_combined_lcg(TSRMLS_C); + data.dbl = php_combined_lcg(); spprintf(&body->boundary, 0, "%x.%x", data.num[0], data.num[1]); } return body->boundary; @@@ -119,6 -130,7 +119,6 @@@ char *php_http_message_body_etag(php_ht { php_http_etag_t *etag; php_stream *s = php_http_message_body_stream(body); - TSRMLS_FETCH_FROM_CTX(body->ts); /* real file or temp buffer ? */ if (s->ops != &php_stream_temp_ops && s->ops != &php_stream_memory_ops) { @@@ -133,7 -145,7 +133,7 @@@ } /* content based */ - if ((etag = php_http_etag_init(PHP_HTTP_G->env.etag_mode TSRMLS_CC))) { + if ((etag = php_http_etag_init(PHP_HTTP_G->env.etag_mode))) { php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_etag_update, etag, 0, 0); return php_http_etag_finish(etag); } @@@ -141,20 -153,22 +141,20 @@@ return NULL; } -void php_http_message_body_to_string(php_http_message_body_t *body, char **buf, size_t *len, off_t offset, size_t forlen) +zend_string *php_http_message_body_to_string(php_http_message_body_t *body, off_t offset, size_t forlen) { php_stream *s = php_http_message_body_stream(body); - TSRMLS_FETCH_FROM_CTX(body->ts); php_stream_seek(s, offset, SEEK_SET); if (!forlen) { forlen = -1; } - *len = php_stream_copy_to_mem(s, buf, forlen, 0); + return php_stream_copy_to_mem(s, forlen, 0); } ZEND_RESULT_CODE php_http_message_body_to_stream(php_http_message_body_t *body, php_stream *dst, off_t offset, size_t forlen) { php_stream *s = php_http_message_body_stream(body); - TSRMLS_FETCH_FROM_CTX(body->ts); php_stream_seek(s, offset, SEEK_SET); @@@ -168,6 -182,7 +168,6 @@@ ZEND_RESULT_CODE php_http_message_body_ { php_stream *s = php_http_message_body_stream(body); char *buf = emalloc(0x1000); - TSRMLS_FETCH_FROM_CTX(body->ts); php_stream_seek(s, offset, SEEK_SET); @@@ -200,6 -215,7 +200,6 @@@ size_t php_http_message_body_append(php { php_stream *s; size_t written; - TSRMLS_FETCH_FROM_CTX(body->ts); if (!(s = php_http_message_body_stream(body))) { return -1; @@@ -212,7 -228,7 +212,7 @@@ written = php_stream_write(s, buf, len); if (written != len) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to append %zu bytes to body; wrote %zu", len, written); + php_error_docref(NULL, E_WARNING, "Failed to append %zu bytes to body; wrote %zu", len, written); } return len; @@@ -236,13 -252,17 +236,13 @@@ size_t php_http_message_body_appendf(ph ZEND_RESULT_CODE php_http_message_body_add_form(php_http_message_body_t *body, HashTable *fields, HashTable *files) { - zval tmp; - if (fields) { - INIT_PZVAL_ARRAY(&tmp, fields); - if (SUCCESS != add_recursive_fields(body, NULL, &tmp)) { + if (SUCCESS != add_recursive_fields(body, NULL, fields)) { return FAILURE; } } if (files) { - INIT_PZVAL_ARRAY(&tmp, files); - if (SUCCESS != add_recursive_files(body, NULL, &tmp)) { + if (SUCCESS != add_recursive_files(body, NULL, files)) { return FAILURE; } } @@@ -252,6 -272,8 +252,6 @@@ void php_http_message_body_add_part(php_http_message_body_t *body, php_http_message_t *part) { - TSRMLS_FETCH_FROM_CTX(body->ts); - BOUNDARY_OPEN(body); php_http_message_to_callback(part, (php_http_pass_callback_t) php_http_message_body_append, body); BOUNDARY_CLOSE(body); @@@ -260,32 -282,34 +260,32 @@@ ZEND_RESULT_CODE php_http_message_body_add_form_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len) { - char *safe_name; - TSRMLS_FETCH_FROM_CTX(body->ts); + zend_string *safe_name = zend_string_init(name, strlen(name), 0); - safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC); + safe_name = php_addslashes(safe_name, 1); BOUNDARY_OPEN(body); php_http_message_body_appendf( body, "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF "" PHP_HTTP_CRLF, - safe_name + safe_name->val ); php_http_message_body_append(body, value_str, value_len); BOUNDARY_CLOSE(body); - efree(safe_name); + zend_string_release(safe_name); return SUCCESS; } ZEND_RESULT_CODE php_http_message_body_add_form_file(php_http_message_body_t *body, const char *name, const char *ctype, const char *path, php_stream *in) { - char *safe_name, *path_dup = estrdup(path), *bname; - size_t bname_len; - TSRMLS_FETCH_FROM_CTX(body->ts); + size_t path_len = strlen(path); + char *path_dup = estrndup(path, path_len); + zend_string *base_name, *safe_name = zend_string_init(name, strlen(name), 0); - safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC); - - php_basename(path_dup, strlen(path_dup), NULL, 0, &bname, &bname_len TSRMLS_CC); + safe_name = php_addslashes(safe_name, 1); + base_name = php_basename(path_dup, path_len, NULL, 0); BOUNDARY_OPEN(body); php_http_message_body_appendf( @@@ -294,29 -318,29 +294,29 @@@ "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF "Content-Type: %s" PHP_HTTP_CRLF PHP_HTTP_CRLF, - safe_name, bname, ctype + safe_name->val, base_name->val, ctype ); php_stream_copy_to_stream_ex(in, php_http_message_body_stream(body), PHP_STREAM_COPY_ALL, NULL); BOUNDARY_CLOSE(body); - efree(safe_name); + zend_string_release(safe_name); + zend_string_release(base_name); efree(path_dup); - efree(bname); return SUCCESS; } -static inline char *format_key(uint type, char *str, ulong num, const char *prefix) { +static inline char *format_key(php_http_arrkey_t *key, const char *prefix) { char *new_key = NULL; if (prefix && *prefix) { - if (type == HASH_KEY_IS_STRING) { - spprintf(&new_key, 0, "%s[%s]", prefix, str); + if (key->key) { + spprintf(&new_key, 0, "%s[%s]", prefix, key->key->val); } else { - spprintf(&new_key, 0, "%s[%lu]", prefix, num); + spprintf(&new_key, 0, "%s[%lu]", prefix, key->h); } - } else if (type == HASH_KEY_IS_STRING) { - new_key = estrdup(str); + } else if (key->key) { + new_key = estrdup(key->key->val); } else { new_key = estrdup(""); } @@@ -324,108 -348,106 +324,108 @@@ return new_key; } -static ZEND_RESULT_CODE add_recursive_fields(php_http_message_body_t *body, const char *name, zval *value) +static ZEND_RESULT_CODE add_recursive_field_value(php_http_message_body_t *body, const char *name, zval *value) +{ + zend_string *zs = zval_get_string(value); + ZEND_RESULT_CODE rc = php_http_message_body_add_form_field(body, name, zs->val, zs->len); + zend_string_release(zs); + return rc; +} + +static ZEND_RESULT_CODE add_recursive_fields(php_http_message_body_t *body, const char *name, HashTable *fields) { - if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) { - zval **val; - HashTable *ht; - HashPosition pos; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - TSRMLS_FETCH_FROM_CTX(body->ts); + zval *val; + php_http_arrkey_t key; - ht = HASH_OF(value); - if (!ht->nApplyCount) { - ++ht->nApplyCount; - FOREACH_KEYVAL(pos, value, key, val) { - char *str = format_key(key.type, key.str, key.num, name); - if (SUCCESS != add_recursive_fields(body, str, *val)) { + if (!ZEND_HASH_GET_APPLY_COUNT(fields)) { + ZEND_HASH_INC_APPLY_COUNT(fields); + ZEND_HASH_FOREACH_KEY_VAL_IND(fields, key.h, key.key, val) + { + char *str = format_key(&key, name); + + if (Z_TYPE_P(val) != IS_ARRAY && Z_TYPE_P(val) != IS_OBJECT) { + if (SUCCESS != add_recursive_field_value(body, str, val)) { efree(str); - ht->nApplyCount--; + ZEND_HASH_DEC_APPLY_COUNT(fields); return FAILURE; } + } else if (SUCCESS != add_recursive_fields(body, str, HASH_OF(val))) { efree(str); + ZEND_HASH_DEC_APPLY_COUNT(fields); + return FAILURE; } - --ht->nApplyCount; + efree(str); } - } else { - zval *cpy = php_http_ztyp(IS_STRING, value); - php_http_message_body_add_form_field(body, name, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)); - zval_ptr_dtor(&cpy); + ZEND_HASH_FOREACH_END(); + ZEND_HASH_DEC_APPLY_COUNT(fields); } return SUCCESS; } -static ZEND_RESULT_CODE add_recursive_files(php_http_message_body_t *body, const char *name, zval *value) +static ZEND_RESULT_CODE add_recursive_files(php_http_message_body_t *body, const char *name, HashTable *files) { - zval **zdata = NULL, **zfile, **zname, **ztype; - HashTable *ht; - TSRMLS_FETCH_FROM_CTX(body->ts); - - if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array or object (name, type, file) for message body file to add"); - return FAILURE; - } - - ht = HASH_OF(value); + zval *zdata = NULL, *zfile, *zname, *ztype; - if ((SUCCESS != zend_hash_find(ht, ZEND_STRS("name"), (void *) &zname)) - || (SUCCESS != zend_hash_find(ht, ZEND_STRS("type"), (void *) &ztype)) - || (SUCCESS != zend_hash_find(ht, ZEND_STRS("file"), (void *) &zfile)) + /* single entry */ + if (!(zname = zend_hash_str_find(files, ZEND_STRL("name"))) + || !(ztype = zend_hash_str_find(files, ZEND_STRL("type"))) + || !(zfile = zend_hash_str_find(files, ZEND_STRL("file"))) ) { - zval **val; - HashPosition pos; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + zval *val; + php_http_arrkey_t key; - if (!ht->nApplyCount) { - ++ht->nApplyCount; - FOREACH_HASH_KEYVAL(pos, ht, key, val) { - if (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT) { - char *str = format_key(key.type, key.str, key.num, name); + if (!ZEND_HASH_GET_APPLY_COUNT(files)) { + ZEND_HASH_INC_APPLY_COUNT(files); + ZEND_HASH_FOREACH_KEY_VAL_IND(files, key.h, key.key, val) + { + if (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT) { + char *str = format_key(&key, name); - if (SUCCESS != add_recursive_files(body, str, *val)) { + if (SUCCESS != add_recursive_files(body, str, HASH_OF(val))) { efree(str); - --ht->nApplyCount; + ZEND_HASH_DEC_APPLY_COUNT(files); return FAILURE; } efree(str); } } - --ht->nApplyCount; + ZEND_HASH_FOREACH_END(); + ZEND_HASH_DEC_APPLY_COUNT(files); } return SUCCESS; } else { + /* stream entry */ php_stream *stream; - zval *zfc = php_http_ztyp(IS_STRING, *zfile); + zend_string *zfc = zval_get_string(zfile); - if (SUCCESS == zend_hash_find(ht, ZEND_STRS("data"), (void *) &zdata)) { - if (Z_TYPE_PP(zdata) == IS_RESOURCE) { + if ((zdata = zend_hash_str_find(files, ZEND_STRL("data")))) { + if (Z_TYPE_P(zdata) == IS_RESOURCE) { php_stream_from_zval_no_verify(stream, zdata); } else { - zval *tmp = php_http_ztyp(IS_STRING, *zdata); + zend_string *tmp = zval_get_string(zdata); - stream = php_stream_memory_open(TEMP_STREAM_READONLY, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); - zval_ptr_dtor(&tmp); + stream = php_stream_memory_open(TEMP_STREAM_READONLY, tmp->val, tmp->len); + zend_string_release(tmp); } } else { - stream = php_stream_open_wrapper(Z_STRVAL_P(zfc), "r", REPORT_ERRORS|USE_PATH, NULL); + stream = php_stream_open_wrapper(zfc->val, "r", REPORT_ERRORS|USE_PATH, NULL); } if (!stream) { - zval_ptr_dtor(&zfc); + zend_string_release(zfc); return FAILURE; } else { - zval *znc = php_http_ztyp(IS_STRING, *zname), *ztc = php_http_ztyp(IS_STRING, *ztype); - char *key = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(znc), 0, name); - ZEND_RESULT_CODE ret = php_http_message_body_add_form_file(body, key, Z_STRVAL_P(ztc), Z_STRVAL_P(zfc), stream); + zend_string *znc = zval_get_string(zname), *ztc = zval_get_string(ztype); + php_http_arrkey_t arrkey = {0, znc}; + char *key = format_key(&arrkey, name); + ZEND_RESULT_CODE ret = php_http_message_body_add_form_file(body, key, ztc->val, zfc->val, stream); efree(key); - zval_ptr_dtor(&znc); - zval_ptr_dtor(&ztc); - zval_ptr_dtor(&zfc); - if (!zdata || Z_TYPE_PP(zdata) != IS_RESOURCE) { + zend_string_release(znc); + zend_string_release(ztc); + zend_string_release(zfc); + if (!zdata || Z_TYPE_P(zdata) != IS_RESOURCE) { php_stream_close(stream); } return ret; @@@ -442,7 -464,7 +442,7 @@@ struct splitbody_arg size_t consumed; }; -static size_t splitbody(void *opaque, char *buf, size_t len TSRMLS_DC) +static size_t splitbody(void *opaque, char *buf, size_t len) { struct splitbody_arg *arg = opaque; const char *boundary = NULL; @@@ -485,7 -507,7 +485,7 @@@ /* advance messages */ php_http_message_t *msg; - msg = php_http_message_init(NULL, 0, NULL TSRMLS_CC); + msg = php_http_message_init(NULL, 0, NULL); msg->parent = arg->parser->message; arg->parser->message = msg; } @@@ -497,7 -519,7 +497,7 @@@ len = 0; } else { /* let this be garbage */ - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Malformed multipart boundary at pos %zu", consumed); + php_error_docref(NULL, E_WARNING, "Malformed multipart boundary at pos %zu", consumed); return -1; } } @@@ -521,15 -543,16 +521,15 @@@ php_http_message_t *php_http_message_bo php_http_buffer_t *tmp = NULL; php_http_message_t *msg = NULL; struct splitbody_arg arg; - TSRMLS_FETCH_FROM_CTX(body->ts); php_http_buffer_init(&arg.buf); - arg.parser = php_http_message_parser_init(NULL TSRMLS_CC); + arg.parser = php_http_message_parser_init(NULL); arg.boundary_len = spprintf(&arg.boundary_str, 0, "\n--%s", boundary); arg.consumed = 0; php_stream_rewind(s); while (!php_stream_eof(s)) { - php_http_buffer_passthru(&tmp, 0x1000, (php_http_buffer_pass_func_t) _php_stream_read, s, splitbody, &arg TSRMLS_CC); + php_http_buffer_passthru(&tmp, 0x1000, (php_http_buffer_pass_func_t) _php_stream_read, s, splitbody, &arg); } msg = arg.parser->message; @@@ -543,89 -566,61 +543,89 @@@ return msg; } +static zend_class_entry *php_http_message_body_class_entry; +zend_class_entry *php_http_get_message_body_class_entry(void) +{ + return php_http_message_body_class_entry; +} + static zend_object_handlers php_http_message_body_object_handlers; -zend_object_value php_http_message_body_object_new(zend_class_entry *ce TSRMLS_DC) +zend_object *php_http_message_body_object_new(zend_class_entry *ce) { - return php_http_message_body_object_new_ex(ce, NULL, NULL TSRMLS_CC); + return &php_http_message_body_object_new_ex(ce, NULL)->zo; } -zend_object_value php_http_message_body_object_new_ex(zend_class_entry *ce, php_http_message_body_t *body, php_http_message_body_object_t **ptr TSRMLS_DC) +php_http_message_body_object_t *php_http_message_body_object_new_ex(zend_class_entry *ce, php_http_message_body_t *body) { php_http_message_body_object_t *o; - o = ecalloc(1, sizeof(php_http_message_body_object_t)); - zend_object_std_init((zend_object *) o, php_http_message_body_class_entry TSRMLS_CC); - object_properties_init((zend_object *) o, ce); + o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce)); + zend_object_std_init(&o->zo, php_http_message_body_class_entry); + object_properties_init(&o->zo, ce); - if (ptr) { - *ptr = o; - } + o->gc = emalloc(sizeof(zval)); if (body) { o->body = body; + php_stream_to_zval(php_http_message_body_stream(o->body), o->gc); + } - o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_body_object_free, NULL TSRMLS_CC); - o->zv.handlers = &php_http_message_body_object_handlers; + o->zo.handlers = &php_http_message_body_object_handlers; - return o->zv; + return o; } -zend_object_value php_http_message_body_object_clone(zval *object TSRMLS_DC) +zend_object *php_http_message_body_object_clone(zval *object) { - zend_object_value new_ov; php_http_message_body_object_t *new_obj = NULL; - php_http_message_body_object_t *old_obj = zend_object_store_get_object(object TSRMLS_CC); + php_http_message_body_object_t *old_obj = PHP_HTTP_OBJ(NULL, object); php_http_message_body_t *body = php_http_message_body_copy(old_obj->body, NULL); - new_ov = php_http_message_body_object_new_ex(old_obj->zo.ce, body, &new_obj TSRMLS_CC); - zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(object) TSRMLS_CC); + new_obj = php_http_message_body_object_new_ex(old_obj->zo.ce, body); + zend_objects_clone_members(&new_obj->zo, &old_obj->zo); + + return &new_obj->zo; +} + +static HashTable *php_http_message_body_object_get_gc(zval *object, zval **table, int *n) +{ + php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, object); + HashTable *props = Z_OBJPROP_P(object); + uint32_t count = zend_hash_num_elements(props); + + *n = 1; + if (count) { + zval *val; + + obj->gc = erealloc(obj->gc, (*n + count) * sizeof(zval)); + + ZEND_HASH_FOREACH_VAL(props, val) + { + ZVAL_COPY_VALUE(&obj->gc[(*n)++], val); + } + ZEND_HASH_FOREACH_END(); + } + *table = obj->gc; - return new_ov; + return NULL; } -void php_http_message_body_object_free(void *object TSRMLS_DC) +void php_http_message_body_object_free(zend_object *object) { - php_http_message_body_object_t *obj = object; + php_http_message_body_object_t *obj = PHP_HTTP_OBJ(object, NULL); + PTR_FREE(obj->gc); php_http_message_body_free(&obj->body); - zend_object_std_dtor((zend_object *) obj TSRMLS_CC); - efree(obj); + zend_object_std_dtor(object); } #define PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj) \ do { \ if (!obj->body) { \ - obj->body = php_http_message_body_init(NULL, NULL TSRMLS_CC); \ + obj->body = php_http_message_body_init(NULL, NULL); \ + php_stream_to_zval(php_http_message_body_stream(obj->body), obj->gc); \ } \ } while(0) @@@ -634,20 -629,19 +634,20 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBo ZEND_END_ARG_INFO(); PHP_METHOD(HttpMessageBody, __construct) { - php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); zval *zstream = NULL; php_stream *stream; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream), invalid_arg, return); if (zstream) { - php_http_expect(php_stream_from_zval_no_verify(stream, &zstream), unexpected_val, return); + php_http_expect(php_stream_from_zval_no_verify(stream, zstream), unexpected_val, return); if (obj->body) { php_http_message_body_free(&obj->body); } - obj->body = php_http_message_body_init(NULL, stream TSRMLS_CC); + obj->body = php_http_message_body_init(NULL, stream); + php_stream_to_zval(stream, obj->gc); } } @@@ -656,14 -650,15 +656,14 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpMessageBody, __toString) { if (SUCCESS == zend_parse_parameters_none()) { - php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); - char *str; - size_t len; + php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); + zend_string *zs; PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj); - php_http_message_body_to_string(obj->body, &str, &len, 0, 0); - if (str) { - RETURN_STRINGL(str, len, 0); + zs = php_http_message_body_to_string(obj->body, 0, 0); + if (zs) { + RETURN_STR(zs); } } RETURN_EMPTY_STRING(); @@@ -675,14 -670,13 +675,14 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpMessageBody, unserialize) { char *us_str; - int us_len; + size_t us_len; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &us_str, &us_len)) { - php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &us_str, &us_len)) { + php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); php_stream *s = php_stream_memory_open(0, us_str, us_len); - obj->body = php_http_message_body_init(NULL, s TSRMLS_CC); + obj->body = php_http_message_body_init(NULL, s); + php_stream_to_zval(s, obj->gc); } } @@@ -694,15 -688,15 +694,15 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpMessageBody, toStream) { zval *zstream; - long offset = 0, forlen = 0; + zend_long offset = 0, forlen = 0; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ll", &zstream, &offset, &forlen)) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &zstream, &offset, &forlen)) { php_stream *stream; - php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj); - php_stream_from_zval(stream, &zstream); + php_stream_from_zval(stream, zstream); php_http_message_body_to_stream(obj->body, stream, offset, forlen); RETURN_ZVAL(getThis(), 1, 0); } @@@ -716,16 -710,20 +716,16 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpMessageBody, toCallback) { php_http_pass_fcall_arg_t fcd; - long offset = 0, forlen = 0; + zend_long offset = 0, forlen = 0; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) { - php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) { + php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj); - fcd.fcz = getThis(); - Z_ADDREF_P(fcd.fcz); - TSRMLS_SET_CTX(fcd.ts); - + ZVAL_COPY(&fcd.fcz, getThis()); php_http_message_body_to_callback(obj->body, php_http_pass_fcall_callback, &fcd, offset, forlen); zend_fcall_info_args_clear(&fcd.fci, 1); - zval_ptr_dtor(&fcd.fcz); RETURN_ZVAL(getThis(), 1, 0); } @@@ -736,12 -734,12 +736,12 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpMessageBody, getResource) { if (SUCCESS == zend_parse_parameters_none()) { - php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj); - zend_list_addref(obj->body->stream_id); - RETVAL_RESOURCE(obj->body->stream_id); + php_stream_to_zval(php_http_message_body_stream(obj->body), return_value); + Z_ADDREF_P(return_value); } } @@@ -750,12 -748,12 +750,12 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpMessageBody, getBoundary) { if (SUCCESS == zend_parse_parameters_none()) { - php_http_message_body_object_t * obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_message_body_object_t * obj = PHP_HTTP_OBJ(NULL, getThis()); PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj); if (obj->body->boundary) { - RETURN_STRING(obj->body->boundary, 1); + RETURN_STRING(obj->body->boundary); } } } @@@ -766,12 -764,13 +766,12 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpMessageBody, append) { char *str; - int len; + size_t len; php_http_message_body_object_t *obj; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len), invalid_arg, return); - - obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &len), invalid_arg, return); + obj = PHP_HTTP_OBJ(NULL, getThis()); PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj); php_http_expect(len == php_http_message_body_append(obj->body, str, len), runtime, return); @@@ -788,9 -787,10 +788,9 @@@ PHP_METHOD(HttpMessageBody, addForm HashTable *fields = NULL, *files = NULL; php_http_message_body_object_t *obj; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h!h!", &fields, &files), invalid_arg, return); - - obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|h!h!", &fields, &files), invalid_arg, return); + obj = PHP_HTTP_OBJ(NULL, getThis()); PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj); php_http_expect(SUCCESS == php_http_message_body_add_form(obj->body, fields, files), runtime, return); @@@ -808,16 -808,16 +808,16 @@@ PHP_METHOD(HttpMessageBody, addPart php_http_message_object_t *mobj; zend_error_handling zeh; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zobj, php_http_message_class_entry), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zobj, php_http_message_get_class_entry()), invalid_arg, return); - obj = zend_object_store_get_object(getThis() TSRMLS_CC); - mobj = zend_object_store_get_object(zobj TSRMLS_CC); + obj = PHP_HTTP_OBJ(NULL, getThis()); + mobj = PHP_HTTP_OBJ(NULL, zobj); PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj); - zend_replace_error_handling(EH_THROW, php_http_exception_runtime_class_entry, &zeh TSRMLS_CC); + zend_replace_error_handling(EH_THROW, php_http_get_exception_runtime_class_entry(), &zeh); php_http_message_body_add_part(obj->body, mobj->message); - zend_restore_error_handling(&zeh TSRMLS_CC); + zend_restore_error_handling(&zeh); if (!EG(exception)) { RETURN_ZVAL(getThis(), 1, 0); @@@ -829,13 -829,13 +829,13 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpMessageBody, etag) { if (SUCCESS == zend_parse_parameters_none()) { - php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); char *etag; PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj); if ((etag = php_http_message_body_etag(obj->body))) { - RETURN_STRING(etag, 0); + RETURN_STR(php_http_cs2zs(etag, strlen(etag))); } else { RETURN_FALSE; } @@@ -848,10 -848,10 +848,10 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpMessageBody, stat) { char *field_str = NULL; - int field_len = 0; + size_t field_len = 0; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &field_str, &field_len)) { - php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &field_str, &field_len)) { + php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis()); const php_stream_statbuf *sb; PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj); @@@ -876,15 -876,15 +876,15 @@@ RETURN_LONG(sb->sb.st_ctime); break; default: - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown stat field: '%s' (should be one of [s]ize, [a]time, [m]time or [c]time)", field_str); + php_error_docref(NULL, E_WARNING, "Unknown stat field: '%s' (should be one of [s]ize, [a]time, [m]time or [c]time)", field_str); break; } } else { object_init(return_value); - add_property_long_ex(return_value, ZEND_STRS("size"), sb->sb.st_size TSRMLS_CC); - add_property_long_ex(return_value, ZEND_STRS("atime"), sb->sb.st_atime TSRMLS_CC); - add_property_long_ex(return_value, ZEND_STRS("mtime"), sb->sb.st_mtime TSRMLS_CC); - add_property_long_ex(return_value, ZEND_STRS("ctime"), sb->sb.st_ctime TSRMLS_CC); + add_property_long_ex(return_value, ZEND_STRL("size"), sb->sb.st_size); + add_property_long_ex(return_value, ZEND_STRL("atime"), sb->sb.st_atime); + add_property_long_ex(return_value, ZEND_STRL("mtime"), sb->sb.st_mtime); + add_property_long_ex(return_value, ZEND_STRL("ctime"), sb->sb.st_ctime); } } } @@@ -908,19 -908,18 +908,19 @@@ static zend_function_entry php_http_mes EMPTY_FUNCTION_ENTRY }; -zend_class_entry *php_http_message_body_class_entry; - PHP_MINIT_FUNCTION(http_message_body) { zend_class_entry ce = {0}; INIT_NS_CLASS_ENTRY(ce, "http\\Message", "Body", php_http_message_body_methods); - php_http_message_body_class_entry = zend_register_internal_class(&ce TSRMLS_CC); + php_http_message_body_class_entry = zend_register_internal_class(&ce); php_http_message_body_class_entry->create_object = php_http_message_body_object_new; memcpy(&php_http_message_body_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); + php_http_message_body_object_handlers.offset = XtOffsetOf(php_http_message_body_object_t, zo); php_http_message_body_object_handlers.clone_obj = php_http_message_body_object_clone; - zend_class_implements(php_http_message_body_class_entry TSRMLS_CC, 1, zend_ce_serializable); + php_http_message_body_object_handlers.free_obj = php_http_message_body_object_free; + php_http_message_body_object_handlers.get_gc = php_http_message_body_object_get_gc; + zend_class_implements(php_http_message_body_class_entry, 1, zend_ce_serializable); return SUCCESS; } diff --combined src/php_http_negotiate.c index 9fdad26,02b0a64..cd09d37 --- a/src/php_http_negotiate.c +++ b/src/php_http_negotiate.c @@@ -12,22 -12,31 +12,26 @@@ #include "php_http_api.h" + #ifndef PHP_HTTP_DEBUG_NEG + # define PHP_HTTP_DEBUG_NEG 0 + #endif + -static int php_http_negotiate_sort(const void *a, const void *b TSRMLS_DC) +static int php_http_negotiate_sort(const void *first, const void *second) { - zval result, *first, *second; + Bucket *b1 = (Bucket *) first, *b2 = (Bucket *) second; + int result = numeric_compare_function(&b1->val, &b2->val); - first = *((zval **) (*((Bucket **) a))->pData); - second= *((zval **) (*((Bucket **) b))->pData); - - if (numeric_compare_function(&result, first, second TSRMLS_CC) != SUCCESS) { - return 0; - } - return (Z_LVAL(result) > 0 ? -1 : (Z_LVAL(result) < 0 ? 1 : 0)); + return (result > 0 ? -1 : (result < 0 ? 1 : 0)); } #define M_PRI 5 #define M_SEC 2 - #define M_ANY 1 + #define M_ANY -1 #define M_NOT 0 #define M_ALL ~0 static inline unsigned php_http_negotiate_match(const char *param_str, size_t param_len, const char *supported_str, size_t supported_len, const char *sep_str, size_t sep_len) { - int match = M_NOT; + unsigned match = M_NOT; if (param_len == supported_len && !strncasecmp(param_str, supported_str, param_len)) { /* that was easy */ @@@ -46,7 -55,11 +50,11 @@@ match += M_PRI; } - if (param_sec && supported_sec && !strcasecmp(param_sec, supported_sec)) { + if (param_sec && supported_sec + && ((*(param_sec + sep_len) == '*' || *(supported_sec + sep_len) == '*') + || !strcasecmp(param_sec, supported_sec) + ) + ) { match += M_SEC; } @@@ -57,103 -70,107 +65,110 @@@ match += M_ANY; } } - #if 0 + #if PHP_HTTP_DEBUG_NEG fprintf(stderr, "match: %s == %s => %u\n", supported_str, param_str, match); #endif return match; } - -static int php_http_negotiate_reduce(void *p TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) +static int php_http_negotiate_reduce(zval *p, int num_args, va_list args, zend_hash_key *hash_key) { unsigned best_match = 0; + double q = 0; - HashPosition pos; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - zval **val, *supported = php_http_ztyp(IS_STRING, *(zval **)p); + php_http_arrkey_t key; - zval *value, *q = NULL; ++ zval *value; + zend_string *supported = zval_get_string(p); HashTable *params = va_arg(args, HashTable *); HashTable *result = va_arg(args, HashTable *); const char *sep_str = va_arg(args, const char *); size_t sep_len = va_arg(args, size_t); - FOREACH_HASH_KEYVAL(pos, params, key, val) { - if (key.type == HASH_KEY_IS_STRING) { - unsigned match = php_http_negotiate_match(key.str, key.len-1, Z_STRVAL_P(supported), Z_STRLEN_P(supported), sep_str, sep_len); + ZEND_HASH_FOREACH_KEY_VAL(params, key.h, key.key, value) + { + unsigned match; + + #if PHP_HTTP_DEBUG_NEG + fprintf(stderr, "match(%u) > best_match(%u) = %u (q=%f)\n", match, best_match, match>best_match, Z_DVAL_PP(val)); + #endif - if (match > best_match) { - best_match = match; - q = Z_DVAL_PP(val) - 0.1 / match; - } + php_http_arrkey_stringify(&key, NULL); + match = php_http_negotiate_match(key.key->val, key.key->len, supported->val, supported->len, sep_str, sep_len); + + if (match > best_match) { + best_match = match; - q = value; ++ q = Z_DVAL_P(value) - 0.1 / match; } + php_http_arrkey_dtor(&key); } + ZEND_HASH_FOREACH_END(); - if (q && Z_DVAL_P(q) > 0) { - Z_TRY_ADDREF_P(q); - zend_hash_update(result, supported, q); + if (q > 0) { - zval *tmp; ++ zval tmp; + - MAKE_STD_ZVAL(tmp); - ZVAL_DOUBLE(tmp, q); - zend_hash_update(result, Z_STRVAL_P(supported), Z_STRLEN_P(supported) + 1, (void *) &tmp, sizeof(zval *), NULL); ++ ZVAL_DOUBLE(&tmp, q); ++ zend_hash_update(result, supported, &tmp); } - zval_ptr_dtor(&supported); + zend_string_release(supported); return ZEND_HASH_APPLY_KEEP; } -HashTable *php_http_negotiate(const char *value_str, size_t value_len, HashTable *supported, const char *primary_sep_str, size_t primary_sep_len TSRMLS_DC) +HashTable *php_http_negotiate(const char *value_str, size_t value_len, HashTable *supported, const char *primary_sep_str, size_t primary_sep_len) { HashTable *result = NULL; if (value_str && value_len) { unsigned i = 0; - zval arr, **val, **arg, **zq; - HashPosition pos; + zval arr, *val, *arg, *zq; HashTable params; - php_http_array_hashkey_t key = php_http_array_hashkey_init(1); + php_http_arrkey_t key; php_http_params_opts_t opts; zend_hash_init(¶ms, 10, NULL, ZVAL_PTR_DTOR, 0); php_http_params_opts_default_get(&opts); opts.input.str = estrndup(value_str, value_len); opts.input.len = value_len; + opts.flags &= ~PHP_HTTP_PARAMS_RFC5987; - php_http_params_parse(¶ms, &opts TSRMLS_CC); + php_http_params_parse(¶ms, &opts); efree(opts.input.str); - INIT_PZVAL(&arr); array_init(&arr); - FOREACH_HASH_KEYVAL(pos, ¶ms, key, val) { + ZEND_HASH_FOREACH_KEY_VAL(¶ms, key.h, key.key, val) + { double q; - if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("arguments"), (void *) &arg) - && IS_ARRAY == Z_TYPE_PP(arg) - && SUCCESS == zend_hash_find(Z_ARRVAL_PP(arg), ZEND_STRS("q"), (void *) &zq)) { - zval *tmp = php_http_ztyp(IS_DOUBLE, *zq); - - q = Z_DVAL_P(tmp); - zval_ptr_dtor(&tmp); + if ((arg = zend_hash_str_find(Z_ARRVAL_P(val), ZEND_STRL("arguments"))) + && (IS_ARRAY == Z_TYPE_P(arg)) + && (zq = zend_hash_str_find(Z_ARRVAL_P(arg), ZEND_STRL("q")))) { + q = zval_get_double(zq); } else { - q = 1.0 - ++i / 100.0; + q = 1.0 - (((double) ++i) / 100.0); } - if (key.type == HASH_KEY_IS_STRING) { - add_assoc_double_ex(&arr, key.str, key.len, q); +#if 0 + fprintf(stderr, "Q: %s=%1.3f\n", key.key->val, q); +#endif + + if (key.key) { + add_assoc_double_ex(&arr, key.key->val, key.key->len, q); } else { - add_index_double(&arr, key.num, q); + add_index_double(&arr, key.h, q); } - PTR_FREE(key.str); } + ZEND_HASH_FOREACH_END(); - #if 0 + #if PHP_HTTP_DEBUG_NEG - zend_print_zval_r(&arr, 1 TSRMLS_CC); + zend_print_zval_r(&arr, 1); #endif ALLOC_HASHTABLE(result); zend_hash_init(result, zend_hash_num_elements(supported), NULL, ZVAL_PTR_DTOR, 0); - zend_hash_apply_with_arguments(supported TSRMLS_CC, php_http_negotiate_reduce, 4, Z_ARRVAL(arr), result, primary_sep_str, primary_sep_len); + zend_hash_apply_with_arguments(supported, php_http_negotiate_reduce, 4, Z_ARRVAL(arr), result, primary_sep_str, primary_sep_len); zend_hash_destroy(¶ms); zval_dtor(&arr); - zend_hash_sort(result, zend_qsort, php_http_negotiate_sort, 0 TSRMLS_CC); + zend_hash_sort(result, php_http_negotiate_sort, 0); } - + return result; } diff --combined src/php_http_params.c index 8db5c35,c722f06..c9feccb --- a/src/php_http_params.c +++ b/src/php_http_params.c @@@ -20,7 -20,7 +20,7 @@@ static php_http_params_opts_t def_opts def_param_sep_ptr, def_arg_sep_ptr, def_val_sep_ptr, - NULL, + {{0}}, PHP_HTTP_PARAMS_DEFAULT }; @@@ -41,101 -41,82 +41,101 @@@ typedef struct php_http_params_state php_http_params_token_t arg; php_http_params_token_t val; struct { - zval **param; - zval **args; - zval **val; + zval *param; + zval *args; + zval *val; } current; unsigned quotes:1; unsigned escape:1; unsigned rfc5987:1; } php_http_params_state_t; -static inline void sanitize_escaped(zval *zv TSRMLS_DC) +static inline void sanitize_escaped(zval *zv) { if (Z_STRVAL_P(zv)[0] == '"' && Z_STRVAL_P(zv)[Z_STRLEN_P(zv) - 1] == '"') { size_t deq_len = Z_STRLEN_P(zv) - 2; char *deq = estrndup(Z_STRVAL_P(zv) + 1, deq_len); zval_dtor(zv); - ZVAL_STRINGL(zv, deq, deq_len, 0); + ZVAL_STR(zv, php_http_cs2zs(deq, deq_len)); } - php_stripcslashes(Z_STRVAL_P(zv), &Z_STRLEN_P(zv)); + php_stripcslashes(Z_STR_P(zv)); } -static inline void quote_string(zval *zv, zend_bool force TSRMLS_DC) +static inline void quote_string(zend_string **zs, zend_bool force) { - int len = Z_STRLEN_P(zv); + int len = (*zs)->len; - Z_STRVAL_P(zv) = php_addcslashes(Z_STRVAL_P(zv), Z_STRLEN_P(zv), &Z_STRLEN_P(zv), 1, - ZEND_STRL("\0..\37\173\\\"") TSRMLS_CC); + *zs = php_addcslashes(*zs, 1, ZEND_STRL("\0..\37\173\\\"")); - if (force || len != Z_STRLEN_P(zv) || strpbrk(Z_STRVAL_P(zv), "()<>@,;:\"[]?={} ")) { - zval tmp = *zv; - int len = Z_STRLEN_P(zv) + 2; - char *str = emalloc(len + 1); + if (force || len != (*zs)->len || strpbrk((*zs)->val, "()<>@,;:\"[]?={} ")) { + int len = (*zs)->len + 2; - str[0] = '"'; - memcpy(&str[1], Z_STRVAL_P(zv), Z_STRLEN_P(zv)); - str[len-1] = '"'; - str[len] = '\0'; + *zs = zend_string_extend(*zs, len, 0); - zval_dtor(&tmp); - ZVAL_STRINGL(zv, str, len, 0); + memmove(&(*zs)->val[1], (*zs)->val, (*zs)->len); + (*zs)->val[0] = '"'; + (*zs)->val[len-1] = '"'; + (*zs)->val[len] = '\0'; + + zend_string_forget_hash_val(*zs); } } -static inline void prepare_escaped(zval *zv TSRMLS_DC) +/* if (Z_TYPE_P(zv) == IS_STRING) { + size_t len = Z_STRLEN_P(zv); + zend_string *stripped = php_addcslashes(Z_STR_P(zv), 0, + ZEND_STRL("\0..\37\173\\\"")); + + if (len != stripped->len || strpbrk(stripped->val, "()<>@,;:\"[]?={} ")) { + size_t len = stripped->len + 2; + char *str = emalloc(len + 1); + + str[0] = '"'; + memcpy(&str[1], stripped->val, stripped->len); + str[len-1] = '"'; + str[len] = '\0'; + + zval_dtor(zv); + zend_string_release(stripped); + ZVAL_STR(zv, php_http_cs2zs(str, len)); + } else { + zval_dtor(zv); + ZVAL_STR(zv, stripped); + } +*/ + +static inline void prepare_escaped(zval *zv) { if (Z_TYPE_P(zv) == IS_STRING) { - quote_string(zv, 0 TSRMLS_CC); + quote_string(&Z_STR_P(zv), 0); } else { zval_dtor(zv); ZVAL_EMPTY_STRING(zv); } } -static inline void sanitize_urlencoded(zval *zv TSRMLS_DC) +static inline void sanitize_urlencoded(zval *zv) { Z_STRLEN_P(zv) = php_raw_url_decode(Z_STRVAL_P(zv), Z_STRLEN_P(zv)); } -static inline void prepare_urlencoded(zval *zv TSRMLS_DC) +static inline void prepare_urlencoded(zval *zv) { - int len; - char *str = php_raw_url_encode(Z_STRVAL_P(zv), Z_STRLEN_P(zv), &len); + zend_string *str = php_raw_url_encode(Z_STRVAL_P(zv), Z_STRLEN_P(zv)); zval_dtor(zv); - ZVAL_STRINGL(zv, str, len, 0); + ZVAL_STR(zv, str); } -static void sanitize_dimension(zval *zv TSRMLS_DC) +static void sanitize_dimension(zval *zv) { - zval *arr = NULL, *tmp = NULL, **cur = NULL; + zval arr, tmp, *cur = NULL; char *var = NULL, *ptr = Z_STRVAL_P(zv), *end = Z_STRVAL_P(zv) + Z_STRLEN_P(zv); long level = 0; - MAKE_STD_ZVAL(arr); - array_init(arr); + array_init(&arr); cur = &arr; while (ptr < end) { @@@ -147,7 -128,7 +147,7 @@@ case '[': if (++level > PG(max_input_nesting_level)) { zval_ptr_dtor(&arr); - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Max input nesting level of %ld exceeded", (long) PG(max_input_nesting_level)); + php_error_docref(NULL, E_WARNING, "Max input nesting level of %ld exceeded", (long) PG(max_input_nesting_level)); return; } if (ptr - var == 0) { @@@ -158,16 -139,17 +158,16 @@@ case ']': - MAKE_STD_ZVAL(tmp); - ZVAL_NULL(tmp); - convert_to_array(*cur); + ZVAL_NULL(&tmp); + convert_to_array(cur); if (ptr - var) { char chr = *ptr; *ptr = '\0'; - zend_symtable_update(Z_ARRVAL_PP(cur), var, ptr - var + 1, (void *) &tmp, sizeof(zval *), (void *) &cur); + cur = zend_symtable_str_update(Z_ARRVAL_P(cur), var, ptr - var, &tmp); *ptr = chr; } else { - zend_hash_next_index_insert(Z_ARRVAL_PP(cur), (void *) &tmp, sizeof(zval *), (void *) &cur); + cur = zend_hash_next_index_insert(Z_ARRVAL_P(cur), &tmp); } var = NULL; @@@ -177,99 -159,100 +177,101 @@@ ++ptr; } - if (zend_hash_num_elements(Z_ARRVAL_P(arr))) { + if (zend_hash_num_elements(Z_ARRVAL(arr))) { zval_dtor(zv); -#if PHP_VERSION_ID >= 50400 - ZVAL_COPY_VALUE(zv, arr); -#else - zv->value = arr->value; - Z_TYPE_P(zv) = Z_TYPE_P(arr); -#endif - FREE_ZVAL(arr); + ZVAL_COPY_VALUE(zv, &arr); } else { zval_ptr_dtor(&arr); } } -static inline void shift_key(php_http_buffer_t *buf, char *key_str, size_t key_len, const char *ass, size_t asl, unsigned flags TSRMLS_DC); -static inline void shift_val(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags TSRMLS_DC); +static inline void shift_key(php_http_buffer_t *buf, char *key_str, size_t key_len, const char *ass, size_t asl, unsigned flags); +static inline void shift_val(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags); -static void prepare_dimension(php_http_buffer_t *buf, php_http_buffer_t *keybuf, zval *zvalue, const char *pss, size_t psl, const char *vss, size_t vsl, unsigned flags TSRMLS_DC) +static void prepare_dimension(php_http_buffer_t *buf, php_http_buffer_t *keybuf, zval *zvalue, const char *pss, size_t psl, const char *vss, size_t vsl, unsigned flags) { HashTable *ht = HASH_OF(zvalue); - HashPosition pos; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - zval **val; + php_http_arrkey_t key; + zval *val; php_http_buffer_t prefix; - if (!ht->nApplyCount++) { + if (!ZEND_HASH_GET_APPLY_COUNT(ht)) { + ZEND_HASH_INC_APPLY_COUNT(ht); php_http_buffer_init(&prefix); php_http_buffer_append(&prefix, keybuf->data, keybuf->used); - FOREACH_HASH_KEYVAL(pos, ht, key, val) { - if (key.type == HASH_KEY_IS_STRING && !*key.str) { + ZEND_HASH_FOREACH_KEY_VAL_IND(ht, key.h, key.key, val) + { + if (key.key && !*key.key->val) { /* only public properties */ continue; } php_http_buffer_appends(&prefix, "["); - if (key.type == HASH_KEY_IS_STRING) { - php_http_buffer_append(&prefix, key.str, key.len - 1); + if (key.key) { + php_http_buffer_append(&prefix, key.key->val, key.key->len); } else { - php_http_buffer_appendf(&prefix, "%lu", key.num); + php_http_buffer_appendf(&prefix, "%lu", key.h); } php_http_buffer_appends(&prefix, "]"); - if (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT) { - prepare_dimension(buf, &prefix, *val, pss, psl, vss, vsl, flags TSRMLS_CC); + if (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT) { + prepare_dimension(buf, &prefix, val, pss, psl, vss, vsl, flags); } else { - zval *cpy = php_http_ztyp(IS_STRING, *val); + zend_string *cpy = zval_get_string(val); + zval tmp; - shift_key(buf, prefix.data, prefix.used, pss, psl, flags TSRMLS_CC); - shift_val(buf, cpy, vss, vsl, flags TSRMLS_CC); - zval_ptr_dtor(&cpy); + ZVAL_STR(&tmp, cpy); + shift_key(buf, prefix.data, prefix.used, pss, psl, flags); + shift_val(buf, &tmp, vss, vsl, flags); + zend_string_release(cpy); } php_http_buffer_cut(&prefix, keybuf->used, prefix.used - keybuf->used); } + ZEND_HASH_FOREACH_END(); + ZEND_HASH_DEC_APPLY_COUNT(ht); + php_http_buffer_dtor(&prefix); } - --ht->nApplyCount; } -static inline void sanitize_key(unsigned flags, char *str, size_t len, zval *zv, zend_bool *rfc5987 TSRMLS_DC) +static inline void sanitize_key(unsigned flags, const char *str, size_t len, zval *zv, zend_bool *rfc5987) { char *eos; + zend_string *zs = zend_string_init(str, len, 0); zval_dtor(zv); - php_trim(str, len, NULL, 0, zv, 3 TSRMLS_CC); + ZVAL_STR(zv, php_trim(zs, NULL, 0, 3)); + zend_string_release(zs); if (flags & PHP_HTTP_PARAMS_ESCAPED) { - sanitize_escaped(zv TSRMLS_CC); + sanitize_escaped(zv); } if (!Z_STRLEN_P(zv)) { return; } - eos = &Z_STRVAL_P(zv)[Z_STRLEN_P(zv)-1]; - if (*eos == '*') { - *eos = '\0'; - *rfc5987 = 1; - Z_STRLEN_P(zv) -= 1; + if (flags & PHP_HTTP_PARAMS_RFC5987) { + eos = &Z_STRVAL_P(zv)[Z_STRLEN_P(zv)-1]; + if (*eos == '*') { + *eos = '\0'; + *rfc5987 = 1; + Z_STRLEN_P(zv) -= 1; + } } if (flags & PHP_HTTP_PARAMS_URLENCODED) { - sanitize_urlencoded(zv TSRMLS_CC); + sanitize_urlencoded(zv); } if (flags & PHP_HTTP_PARAMS_DIMENSION) { - sanitize_dimension(zv TSRMLS_CC); + sanitize_dimension(zv); } } -static inline void sanitize_rfc5987(zval *zv, char **language, zend_bool *latin1 TSRMLS_DC) +static inline void sanitize_rfc5987(zval *zv, char **language, zend_bool *latin1) { char *ptr; @@@ -311,20 -294,17 +313,20 @@@ /* remainder */ ptr = estrdup(++ptr); zval_dtor(zv); - ZVAL_STRING(zv, ptr, 0); + ZVAL_STR(zv, php_http_cs2zs(ptr, strlen(ptr))); } } -static inline void sanitize_rfc5988(char *str, size_t len, zval *zv TSRMLS_DC) +static inline void sanitize_rfc5988(char *str, size_t len, zval *zv) { + zend_string *zs = zend_string_init(str, len, 0); + zval_dtor(zv); - php_trim(str, len, " ><", 3, zv, 3 TSRMLS_CC); + ZVAL_STR(zv, php_trim(zs, " ><", 3, 3)); + zend_string_release(zs); } -static inline void prepare_rfc5988(zval *zv TSRMLS_DC) +static inline void prepare_rfc5988(zval *zv) { if (Z_TYPE_P(zv) != IS_STRING) { zval_dtor(zv); @@@ -354,129 -334,130 +356,129 @@@ static void utf8encode(zval *zv } } zval_dtor(zv); - ZVAL_STRINGL(zv, (char *) ptr, pos-1, 0); + ZVAL_STR(zv, php_http_cs2zs((char *) ptr, pos-1)); } -static inline void sanitize_value(unsigned flags, char *str, size_t len, zval *zv, zend_bool rfc5987 TSRMLS_DC) +static inline void sanitize_value(unsigned flags, const char *str, size_t len, zval *zv, zend_bool rfc5987) { char *language = NULL; zend_bool latin1 = 0; + zend_string *zs = zend_string_init(str, len, 0); zval_dtor(zv); - php_trim(str, len, NULL, 0, zv, 3 TSRMLS_CC); + ZVAL_STR(zv, php_trim(zs, NULL, 0, 3)); + zend_string_release(zs); if (rfc5987) { - sanitize_rfc5987(zv, &language, &latin1 TSRMLS_CC); + sanitize_rfc5987(zv, &language, &latin1); } if (flags & PHP_HTTP_PARAMS_ESCAPED) { - sanitize_escaped(zv TSRMLS_CC); + sanitize_escaped(zv); } if ((flags & PHP_HTTP_PARAMS_URLENCODED) || (rfc5987 && language)) { - sanitize_urlencoded(zv TSRMLS_CC); + sanitize_urlencoded(zv); } if (rfc5987 && language) { - zval *tmp; + zval tmp; if (latin1) { utf8encode(zv); } - MAKE_STD_ZVAL(tmp); - ZVAL_COPY_VALUE(tmp, zv); + ZVAL_COPY_VALUE(&tmp, zv); array_init(zv); - add_assoc_zval(zv, language, tmp); - PTR_FREE(language); + add_assoc_zval(zv, language, &tmp); + efree(language); } } -static inline void prepare_key(unsigned flags, char *old_key, size_t old_len, char **new_key, size_t *new_len TSRMLS_DC) +static inline void prepare_key(unsigned flags, char *old_key, size_t old_len, char **new_key, size_t *new_len) { zval zv; - INIT_PZVAL(&zv); - ZVAL_STRINGL(&zv, old_key, old_len, 1); + ZVAL_STRINGL(&zv, old_key, old_len); if (flags & PHP_HTTP_PARAMS_URLENCODED) { - prepare_urlencoded(&zv TSRMLS_CC); + prepare_urlencoded(&zv); } if (flags & PHP_HTTP_PARAMS_ESCAPED) { if (flags & PHP_HTTP_PARAMS_RFC5988) { - prepare_rfc5988(&zv TSRMLS_CC); + prepare_rfc5988(&zv); } else { - prepare_escaped(&zv TSRMLS_CC); + prepare_escaped(&zv); } } - *new_key = Z_STRVAL(zv); + *new_key = estrndup(Z_STRVAL(zv), Z_STRLEN(zv)); *new_len = Z_STRLEN(zv); + zval_ptr_dtor(&zv); } -static inline void prepare_value(unsigned flags, zval *zv TSRMLS_DC) +static inline void prepare_value(unsigned flags, zval *zv) { if (flags & PHP_HTTP_PARAMS_URLENCODED) { - prepare_urlencoded(zv TSRMLS_CC); + prepare_urlencoded(zv); } if (flags & PHP_HTTP_PARAMS_ESCAPED) { - prepare_escaped(zv TSRMLS_CC); + prepare_escaped(zv); } } -static void merge_param(HashTable *params, zval *zdata, zval ***current_param, zval ***current_args TSRMLS_DC) +static void merge_param(HashTable *params, zval *zdata, zval **current_param, zval **current_args) { - zval **ptr, **zdata_ptr; - php_http_array_hashkey_t hkey = php_http_array_hashkey_init(0); + zval *ptr, *zdata_ptr; + php_http_arrkey_t hkey = {0}; #if 0 { zval tmp; INIT_PZVAL_ARRAY(&tmp, params); fprintf(stderr, "params = "); - zend_print_zval_r(&tmp, 1 TSRMLS_CC); + zend_print_zval_r(&tmp, 1); fprintf(stderr, "\n"); } #endif - hkey.type = zend_hash_get_current_key_ex(Z_ARRVAL_P(zdata), &hkey.str, &hkey.len, &hkey.num, hkey.dup, NULL); + zend_hash_get_current_key(Z_ARRVAL_P(zdata), &hkey.key, &hkey.h); - if ((hkey.type == HASH_KEY_IS_STRING && !zend_hash_exists(params, hkey.str, hkey.len)) - || (hkey.type == HASH_KEY_IS_LONG && !zend_hash_index_exists(params, hkey.num)) + if ((hkey.key && !zend_hash_exists(params, hkey.key)) + || (!hkey.key && !zend_hash_index_exists(params, hkey.h)) ) { - zval *tmp, *arg, **args; + zval tmp, arg, *args; /* create the entry if it doesn't exist */ - zend_hash_get_current_data(Z_ARRVAL_P(zdata), (void *) &ptr); - Z_ADDREF_PP(ptr); - MAKE_STD_ZVAL(tmp); - array_init(tmp); - add_assoc_zval_ex(tmp, ZEND_STRS("value"), *ptr); - - MAKE_STD_ZVAL(arg); - array_init(arg); - zend_hash_update(Z_ARRVAL_P(tmp), "arguments", sizeof("arguments"), (void *) &arg, sizeof(zval *), (void *) &args); + ptr = zend_hash_get_current_data(Z_ARRVAL_P(zdata)); + Z_TRY_ADDREF_P(ptr); + array_init(&tmp); + add_assoc_zval_ex(&tmp, ZEND_STRL("value"), ptr); + + array_init(&arg); + args = zend_hash_str_update(Z_ARRVAL(tmp), "arguments", lenof("arguments"), &arg); *current_args = args; - if (hkey.type == HASH_KEY_IS_STRING) { - zend_hash_update(params, hkey.str, hkey.len, (void *) &tmp, sizeof(zval *), (void *) &ptr); + if (hkey.key) { + ptr = zend_hash_update(params, hkey.key, &tmp); } else { - zend_hash_index_update(params, hkey.num, (void *) &tmp, sizeof(zval *), (void *) &ptr); + ptr = zend_hash_index_update(params, hkey.h, &tmp); } } else { /* merge */ - if (hkey.type == HASH_KEY_IS_STRING) { - zend_hash_find(params, hkey.str, hkey.len, (void *) &ptr); + if (hkey.key) { + ptr = zend_hash_find(params, hkey.key); } else { - zend_hash_index_find(params, hkey.num, (void *) &ptr); + ptr = zend_hash_index_find(params, hkey.h); } - zdata_ptr = &zdata; + zdata_ptr = zdata; - if (Z_TYPE_PP(ptr) == IS_ARRAY - && SUCCESS == zend_hash_find(Z_ARRVAL_PP(ptr), "value", sizeof("value"), (void *) &ptr) - && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(zdata_ptr), (void *) &zdata_ptr) + if (Z_TYPE_P(ptr) == IS_ARRAY + && (ptr = zend_hash_str_find(Z_ARRVAL_P(ptr), "value", lenof("value"))) + && (zdata_ptr = zend_hash_get_current_data(Z_ARRVAL_P(zdata_ptr))) ) { /* * params = [arr => [value => [0 => 1]]] @@@ -484,50 -465,48 +486,50 @@@ * zdata = [arr => [0 => NULL]] * ^- zdata_ptr */ - zval **test_ptr; + zval *test_ptr; - while (Z_TYPE_PP(zdata_ptr) == IS_ARRAY - && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(zdata_ptr), (void *) &test_ptr) - ) { - if (Z_TYPE_PP(test_ptr) == IS_ARRAY) { + while (Z_TYPE_P(zdata_ptr) == IS_ARRAY && (test_ptr = zend_hash_get_current_data(Z_ARRVAL_P(zdata_ptr)))) { + if (Z_TYPE_P(test_ptr) == IS_ARRAY) { + zval *tmp_ptr = ptr; /* now find key in ptr */ - if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_PP(zdata_ptr), &hkey.str, &hkey.len, &hkey.num, hkey.dup, NULL)) { - if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(ptr), hkey.str, hkey.len, (void *) &ptr)) { + if (HASH_KEY_IS_STRING == zend_hash_get_current_key(Z_ARRVAL_P(zdata_ptr), &hkey.key, &hkey.h)) { + if ((ptr = zend_hash_find(Z_ARRVAL_P(ptr), hkey.key))) { zdata_ptr = test_ptr; } else { - Z_ADDREF_PP(test_ptr); - zend_hash_update(Z_ARRVAL_PP(ptr), hkey.str, hkey.len, (void *) test_ptr, sizeof(zval *), (void *) &ptr); + ptr = tmp_ptr; + Z_TRY_ADDREF_P(test_ptr); + ptr = zend_hash_update(Z_ARRVAL_P(ptr), hkey.key, test_ptr); break; } } else { - if (SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(ptr), hkey.num, (void *) &ptr)) { + if ((ptr = zend_hash_index_find(Z_ARRVAL_P(ptr), hkey.h))) { zdata_ptr = test_ptr; - } else if (hkey.num) { - Z_ADDREF_PP(test_ptr); - zend_hash_index_update(Z_ARRVAL_PP(ptr), hkey.num, (void *) test_ptr, sizeof(zval *), (void *) &ptr); + } else if (hkey.h) { + ptr = tmp_ptr; + Z_TRY_ADDREF_P(test_ptr); + ptr = zend_hash_index_update(Z_ARRVAL_P(ptr), hkey.h, test_ptr); break; } else { - Z_ADDREF_PP(test_ptr); - zend_hash_next_index_insert(Z_ARRVAL_PP(ptr), (void *) test_ptr, sizeof(zval *), (void *) &ptr); + ptr = tmp_ptr; + Z_TRY_ADDREF_P(test_ptr); + ptr = zend_hash_next_index_insert(Z_ARRVAL_P(ptr), test_ptr); break; } } } else { /* this is the leaf */ - Z_ADDREF_PP(test_ptr); - if (Z_TYPE_PP(ptr) != IS_ARRAY) { - zval_dtor(*ptr); - array_init(*ptr); + Z_TRY_ADDREF_P(test_ptr); + if (Z_TYPE_P(ptr) != IS_ARRAY) { + zval_dtor(ptr); + array_init(ptr); } - if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_PP(zdata_ptr), &hkey.str, &hkey.len, &hkey.num, hkey.dup, NULL)) { - zend_hash_update(Z_ARRVAL_PP(ptr), hkey.str, hkey.len, (void *) test_ptr, sizeof(zval *), (void *) &ptr); - } else if (hkey.num) { - zend_hash_index_update(Z_ARRVAL_PP(ptr), hkey.num, (void *) test_ptr, sizeof(zval *), (void *) &ptr); + if (HASH_KEY_IS_STRING == zend_hash_get_current_key(Z_ARRVAL_P(zdata_ptr), &hkey.key, &hkey.h)) { + ptr = zend_hash_update(Z_ARRVAL_P(ptr), hkey.key, test_ptr); + } else if (hkey.h) { + ptr = zend_hash_index_update(Z_ARRVAL_P(ptr), hkey.h, test_ptr); } else { - zend_hash_next_index_insert(Z_ARRVAL_PP(ptr), (void *) test_ptr, sizeof(zval *), (void *) &ptr); + ptr = zend_hash_next_index_insert(Z_ARRVAL_P(ptr), test_ptr); } break; } @@@ -537,87 -516,88 +539,89 @@@ } /* bubble up */ - while (Z_TYPE_PP(ptr) == IS_ARRAY && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(ptr), (void *) &ptr)); + while (Z_TYPE_P(ptr) == IS_ARRAY) { + zval *tmp = zend_hash_get_current_data(Z_ARRVAL_P(ptr)); + + if (tmp) { + ptr = tmp; + } else { + break; + } + } *current_param = ptr; } -static void push_param(HashTable *params, php_http_params_state_t *state, const php_http_params_opts_t *opts TSRMLS_DC) +static void push_param(HashTable *params, php_http_params_state_t *state, const php_http_params_opts_t *opts) { if (state->val.str) { if (0 < (state->val.len = state->input.str - state->val.str)) { - sanitize_value(opts->flags, state->val.str, state->val.len, *(state->current.val), state->rfc5987 TSRMLS_CC); + sanitize_value(opts->flags, state->val.str, state->val.len, state->current.val, state->rfc5987); + } else { - ZVAL_EMPTY_STRING(*(state->current.val)); ++ ZVAL_EMPTY_STRING(state->current.val); } state->rfc5987 = 0; } else if (state->arg.str) { if (0 < (state->arg.len = state->input.str - state->arg.str)) { - zval *val, key; + zval val, key; zend_bool rfc5987 = 0; - INIT_PZVAL(&key); ZVAL_NULL(&key); - sanitize_key(opts->flags, state->arg.str, state->arg.len, &key, &rfc5987 TSRMLS_CC); + sanitize_key(opts->flags, state->arg.str, state->arg.len, &key, &rfc5987); state->rfc5987 = rfc5987; if (Z_TYPE(key) == IS_STRING && Z_STRLEN(key)) { - MAKE_STD_ZVAL(val); - ZVAL_TRUE(val); + ZVAL_TRUE(&val); if (rfc5987) { - zval **rfc; + zval *rfc; - if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(state->current.args), ZEND_STRS("*rfc5987*"), (void *) &rfc)) { - zend_symtable_update(Z_ARRVAL_PP(rfc), Z_STRVAL(key), Z_STRLEN(key) + 1, (void *) &val, sizeof(zval *), (void *) &state->current.val); + if ((rfc = zend_hash_str_find(Z_ARRVAL_P(state->current.args), ZEND_STRL("*rfc5987*")))) { + state->current.val = zend_symtable_str_update(Z_ARRVAL_P(rfc), Z_STRVAL(key), Z_STRLEN(key), &val); } else { - zval *tmp; + zval tmp; - MAKE_STD_ZVAL(tmp); - array_init_size(tmp, 1); - zend_symtable_update(Z_ARRVAL_P(tmp), Z_STRVAL(key), Z_STRLEN(key) + 1, (void *) &val, sizeof(zval *), (void *) &state->current.val); - zend_symtable_update(Z_ARRVAL_PP(state->current.args), ZEND_STRS("*rfc5987*"), (void *) &tmp, sizeof(zval *), NULL); + array_init_size(&tmp, 1); + state->current.val = zend_symtable_str_update(Z_ARRVAL(tmp), Z_STRVAL(key), Z_STRLEN(key), &val); + zend_symtable_str_update(Z_ARRVAL_P(state->current.args), ZEND_STRL("*rfc5987*"), &tmp); } } else { - zend_symtable_update(Z_ARRVAL_PP(state->current.args), Z_STRVAL(key), Z_STRLEN(key) + 1, (void *) &val, sizeof(zval *), (void *) &state->current.val); + state->current.val = zend_symtable_str_update(Z_ARRVAL_P(state->current.args), Z_STRVAL(key), Z_STRLEN(key), &val); } } zval_dtor(&key); } } else if (state->param.str) { if (0 < (state->param.len = state->input.str - state->param.str)) { - zval *prm, *arg, *val, *key; + zval prm, arg, val, key; zend_bool rfc5987 = 0; - MAKE_STD_ZVAL(key); - ZVAL_NULL(key); + ZVAL_NULL(&key); if (opts->flags & PHP_HTTP_PARAMS_RFC5988) { - sanitize_rfc5988(state->param.str, state->param.len, key TSRMLS_CC); + sanitize_rfc5988(state->param.str, state->param.len, &key); } else { - sanitize_key(opts->flags, state->param.str, state->param.len, key, &rfc5987 TSRMLS_CC); + sanitize_key(opts->flags, state->param.str, state->param.len, &key, &rfc5987); state->rfc5987 = rfc5987; } - if (Z_TYPE_P(key) != IS_STRING) { - merge_param(params, key, &state->current.val, &state->current.args TSRMLS_CC); - } else if (Z_STRLEN_P(key)) { - MAKE_STD_ZVAL(prm); - array_init_size(prm, 2); - - MAKE_STD_ZVAL(val); - if (opts->defval) { - ZVAL_COPY_VALUE(val, opts->defval); - zval_copy_ctor(val); + if (Z_TYPE(key) == IS_ARRAY) { + merge_param(params, &key, &state->current.val, &state->current.args); + } else if (Z_TYPE(key) == IS_STRING && Z_STRLEN(key)) { + // FIXME: array_init_size(&prm, 2); + array_init(&prm); + + if (!Z_ISUNDEF(opts->defval)) { + ZVAL_COPY_VALUE(&val, &opts->defval); + zval_copy_ctor(&val); } else { - ZVAL_TRUE(val); + ZVAL_TRUE(&val); } if (rfc5987 && (opts->flags & PHP_HTTP_PARAMS_RFC5987)) { - zend_hash_update(Z_ARRVAL_P(prm), "*rfc5987*", sizeof("*rfc5987*"), (void *) &val, sizeof(zval *), (void *) &state->current.val); + state->current.val = zend_hash_str_update(Z_ARRVAL(prm), "*rfc5987*", lenof("*rfc5987*"), &val); } else { - zend_hash_update(Z_ARRVAL_P(prm), "value", sizeof("value"), (void *) &val, sizeof(zval *), (void *) &state->current.val); + state->current.val = zend_hash_str_update(Z_ARRVAL(prm), "value", lenof("value"), &val); } - - MAKE_STD_ZVAL(arg); - array_init_size(arg, 3); - zend_hash_update(Z_ARRVAL_P(prm), "arguments", sizeof("arguments"), (void *) &arg, sizeof(zval *), (void *) &state->current.args); - - zend_symtable_update(params, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, (void *) &prm, sizeof(zval *), (void *) &state->current.param); + // FIXME: array_init_size(&arg, 3); + array_init(&arg); + state->current.args = zend_hash_str_update(Z_ARRVAL(prm), "arguments", lenof("arguments"), &arg); + state->current.param = zend_symtable_str_update(params, Z_STRVAL(key), Z_STRLEN(key), &prm); } zval_ptr_dtor(&key); } @@@ -645,7 -625,7 +649,7 @@@ static size_t check_sep(php_http_params return 0; } -static void skip_sep(size_t skip, php_http_params_state_t *state, php_http_params_token_t **param, php_http_params_token_t **arg, php_http_params_token_t **val TSRMLS_DC) +static void skip_sep(size_t skip, php_http_params_state_t *state, php_http_params_token_t **param, php_http_params_token_t **arg, php_http_params_token_t **val) { size_t sep_len; @@@ -661,7 -641,7 +665,7 @@@ } } -HashTable *php_http_params_parse(HashTable *params, const php_http_params_opts_t *opts TSRMLS_DC) +HashTable *php_http_params_parse(HashTable *params, const php_http_params_opts_t *opts) { php_http_params_state_t state = {{NULL,0}, {NULL,0}, {NULL,0}, {NULL,0}, {NULL,NULL,NULL}, 0, 0}; @@@ -688,15 -668,15 +692,15 @@@ if (!state.param.str) { /* initialize */ - skip_sep(0, &state, opts->param, opts->arg, opts->val TSRMLS_CC); + skip_sep(0, &state, opts->param, opts->arg, opts->val); state.param.str = state.input.str; } else { size_t sep_len; /* are we at a param separator? */ if (0 < (sep_len = check_sep(&state, opts->param))) { - push_param(params, &state, opts TSRMLS_CC); + push_param(params, &state, opts); - skip_sep(sep_len, &state, opts->param, opts->arg, opts->val TSRMLS_CC); + skip_sep(sep_len, &state, opts->param, opts->arg, opts->val); /* start off with a new param */ state.param.str = state.input.str; @@@ -711,9 -691,9 +715,9 @@@ } else /* are we at an arg separator? */ if (0 < (sep_len = check_sep(&state, opts->arg))) { - push_param(params, &state, opts TSRMLS_CC); + push_param(params, &state, opts); - skip_sep(sep_len, &state, NULL, opts->arg, opts->val TSRMLS_CC); + skip_sep(sep_len, &state, NULL, opts->arg, opts->val); /* continue with a new arg */ state.arg.str = state.input.str; @@@ -728,9 -708,9 +732,9 @@@ if (0 < (sep_len = check_sep(&state, opts->val))) { /* only handle separator if we're not already reading in a val */ if (!state.val.str) { - push_param(params, &state, opts TSRMLS_CC); + push_param(params, &state, opts); - skip_sep(sep_len, &state, NULL, NULL, opts->val TSRMLS_CC); + skip_sep(sep_len, &state, NULL, NULL, opts->val); state.val.str = state.input.str; state.val.len = 0; @@@ -746,12 -726,12 +750,12 @@@ } } /* finalize */ - push_param(params, &state, opts TSRMLS_CC); + push_param(params, &state, opts); return params; } -static inline void shift_key(php_http_buffer_t *buf, char *key_str, size_t key_len, const char *ass, size_t asl, unsigned flags TSRMLS_DC) +static inline void shift_key(php_http_buffer_t *buf, char *key_str, size_t key_len, const char *ass, size_t asl, unsigned flags) { char *str; size_t len; @@@ -760,39 -740,34 +764,39 @@@ php_http_buffer_append(buf, ass, asl); } - prepare_key(flags, key_str, key_len, &str, &len TSRMLS_CC); + prepare_key(flags, key_str, key_len, &str, &len); php_http_buffer_append(buf, str, len); efree(str); } -static inline void shift_rfc5987(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags TSRMLS_DC) +static inline void shift_rfc5987(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags) { HashTable *ht = HASH_OF(zvalue); - zval **zdata, *tmp; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + zval *zdata, tmp; + zend_string *zs; + php_http_arrkey_t key = {0}; - if (SUCCESS == zend_hash_get_current_data(ht, (void *) &zdata) - && HASH_KEY_NON_EXISTENT != (key.type = zend_hash_get_current_key_ex(ht, &key.str, &key.len, &key.num, key.dup, NULL)) + if ((zdata = zend_hash_get_current_data(ht)) + && HASH_KEY_NON_EXISTENT != zend_hash_get_current_key(ht, &key.key, &key.h) ) { - php_http_array_hashkey_stringify(&key); + php_http_arrkey_stringify(&key, NULL); php_http_buffer_appendf(buf, "*%.*sutf-8'%.*s'", (int) (vsl > INT_MAX ? INT_MAX : vsl), vss, - (int) (key.len > INT_MAX ? INT_MAX : key.len), key.str); - php_http_array_hashkey_stringfree(&key); + (int) (key.key->len > INT_MAX ? INT_MAX : key.key->len), key.key->val); + php_http_arrkey_dtor(&key); - tmp = php_http_zsep(1, IS_STRING, *zdata); - prepare_value(flags | PHP_HTTP_PARAMS_URLENCODED, tmp TSRMLS_CC); - php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); + if (Z_TYPE_P(zdata) == IS_INDIRECT) { + zdata = Z_INDIRECT_P(zdata); + } + zs = zval_get_string(zdata); + ZVAL_STR(&tmp, zs); + prepare_value(flags | PHP_HTTP_PARAMS_URLENCODED, &tmp); + php_http_buffer_append(buf, Z_STRVAL(tmp), Z_STRLEN(tmp)); zval_ptr_dtor(&tmp); } } -static inline void shift_rfc5988(php_http_buffer_t *buf, char *key_str, size_t key_len, const char *ass, size_t asl, unsigned flags TSRMLS_DC) +static inline void shift_rfc5988(php_http_buffer_t *buf, char *key_str, size_t key_len, const char *ass, size_t asl, unsigned flags) { char *str; size_t len; @@@ -801,77 -776,64 +805,77 @@@ php_http_buffer_append(buf, ass, asl); } - prepare_key(flags, key_str, key_len, &str, &len TSRMLS_CC); + prepare_key(flags, key_str, key_len, &str, &len); php_http_buffer_appends(buf, "<"); php_http_buffer_append(buf, str, len); php_http_buffer_appends(buf, ">"); efree(str); } -static inline void shift_rfc5988_val(php_http_buffer_t *buf, zval *zv, const char *vss, size_t vsl, unsigned flags TSRMLS_DC) +static inline void shift_rfc5988_val(php_http_buffer_t *buf, zval *zv, const char *vss, size_t vsl, unsigned flags) { - zval *tmp = php_http_zsep(1, IS_STRING, zv); + zend_string *zs = zval_get_string(zv); - quote_string(tmp, 1 TSRMLS_CC); + quote_string(&zs, 1); php_http_buffer_append(buf, vss, vsl); - php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); + php_http_buffer_append(buf, zs->val, zs->len); - zval_ptr_dtor(&tmp); + zend_string_release(zs); } -static inline void shift_val(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags TSRMLS_DC) +static inline void shift_val(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags) { - if (Z_TYPE_P(zvalue) != IS_BOOL) { - zval *tmp = php_http_zsep(1, IS_STRING, zvalue); + zval tmp; + zend_string *zs; - prepare_value(flags, tmp TSRMLS_CC); - php_http_buffer_append(buf, vss, vsl); - php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); + switch (Z_TYPE_P(zvalue)) { + case IS_TRUE: + break; - zval_ptr_dtor(&tmp); - } else if (!Z_BVAL_P(zvalue)) { + case IS_FALSE: php_http_buffer_append(buf, vss, vsl); php_http_buffer_appends(buf, "0"); + break; + + default: + zs = zval_get_string(zvalue); + + ZVAL_STR(&tmp, zs); + prepare_value(flags, &tmp); + php_http_buffer_append(buf, vss, vsl); + php_http_buffer_append(buf, Z_STRVAL(tmp), Z_STRLEN(tmp)); + + zval_ptr_dtor(&tmp); + break; } } -static void shift_arg(php_http_buffer_t *buf, char *key_str, size_t key_len, zval *zvalue, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags TSRMLS_DC) +static void shift_arg(php_http_buffer_t *buf, char *key_str, size_t key_len, zval *zvalue, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags) { if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) { - HashPosition pos; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - zval **val; + php_http_arrkey_t key; + HashTable *ht = HASH_OF(zvalue); + zval *val; zend_bool rfc5987 = !strcmp(key_str, "*rfc5987*"); if (!rfc5987) { - shift_key(buf, key_str, key_len, ass, asl, flags TSRMLS_CC); + shift_key(buf, key_str, key_len, ass, asl, flags); } - FOREACH_KEYVAL(pos, zvalue, key, val) { + ZEND_HASH_FOREACH_KEY_VAL_IND(ht, key.h, key.key, val) + { /* did you mean recursion? */ - php_http_array_hashkey_stringify(&key); - if (rfc5987 && (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT)) { - shift_key(buf, key.str, key.len-1, ass, asl, flags TSRMLS_CC); - shift_rfc5987(buf, *val, vss, vsl, flags TSRMLS_CC); + php_http_arrkey_stringify(&key, NULL); + if (rfc5987 && (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT)) { + shift_key(buf, key.key->val, key.key->len, ass, asl, flags); + shift_rfc5987(buf, val, vss, vsl, flags); } else { - shift_arg(buf, key.str, key.len-1, *val, ass, asl, vss, vsl, flags TSRMLS_CC); + shift_arg(buf, key.key->val, key.key->len, val, ass, asl, vss, vsl, flags); } - php_http_array_hashkey_stringfree(&key); + php_http_arrkey_dtor(&key); } + ZEND_HASH_FOREACH_END(); } else { - shift_key(buf, key_str, key_len, ass, asl, flags TSRMLS_CC); + shift_key(buf, key_str, key_len, ass, asl, flags); if (flags & PHP_HTTP_PARAMS_RFC5988) { switch (key_len) { @@@ -880,60 -842,60 +884,60 @@@ case lenof("anchor"): /* some args must be quoted */ if (0 <= php_http_select_str(key_str, 3, "rel", "title", "anchor")) { - shift_rfc5988_val(buf, zvalue, vss, vsl, flags TSRMLS_CC); + shift_rfc5988_val(buf, zvalue, vss, vsl, flags); return; } break; } } - shift_val(buf, zvalue, vss, vsl, flags TSRMLS_CC); + shift_val(buf, zvalue, vss, vsl, flags); } } -static void shift_param(php_http_buffer_t *buf, char *key_str, size_t key_len, zval *zvalue, const char *pss, size_t psl, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags, zend_bool rfc5987 TSRMLS_DC) +static void shift_param(php_http_buffer_t *buf, char *key_str, size_t key_len, zval *zvalue, const char *pss, size_t psl, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags, zend_bool rfc5987) { if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) { /* treat as arguments, unless we care for dimensions or rfc5987 */ if (flags & PHP_HTTP_PARAMS_DIMENSION) { php_http_buffer_t *keybuf = php_http_buffer_from_string(key_str, key_len); - prepare_dimension(buf, keybuf, zvalue, pss, psl, vss, vsl, flags TSRMLS_CC); + prepare_dimension(buf, keybuf, zvalue, pss, psl, vss, vsl, flags); php_http_buffer_free(&keybuf); } else if (rfc5987) { - shift_key(buf, key_str, key_len, pss, psl, flags TSRMLS_CC); - shift_rfc5987(buf, zvalue, vss, vsl, flags TSRMLS_CC); + shift_key(buf, key_str, key_len, pss, psl, flags); + shift_rfc5987(buf, zvalue, vss, vsl, flags); } else { - shift_arg(buf, key_str, key_len, zvalue, ass, asl, vss, vsl, flags TSRMLS_CC); + shift_arg(buf, key_str, key_len, zvalue, ass, asl, vss, vsl, flags); } } else { if (flags & PHP_HTTP_PARAMS_RFC5988) { - shift_rfc5988(buf, key_str, key_len, pss, psl, flags TSRMLS_CC); + shift_rfc5988(buf, key_str, key_len, pss, psl, flags); } else { - shift_key(buf, key_str, key_len, pss, psl, flags TSRMLS_CC); + shift_key(buf, key_str, key_len, pss, psl, flags); } - shift_val(buf, zvalue, vss, vsl, flags TSRMLS_CC); + shift_val(buf, zvalue, vss, vsl, flags); } } -php_http_buffer_t *php_http_params_to_string(php_http_buffer_t *buf, HashTable *params, const char *pss, size_t psl, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags TSRMLS_DC) +php_http_buffer_t *php_http_params_to_string(php_http_buffer_t *buf, HashTable *params, const char *pss, size_t psl, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags) { - zval **zparam; - HashPosition pos, pos1; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0), key1 = php_http_array_hashkey_init(0); + zval *zparam; + php_http_arrkey_t key; zend_bool rfc5987 = 0; if (!buf) { buf = php_http_buffer_init(NULL); } - FOREACH_HASH_KEYVAL(pos, params, key, zparam) { - zval **zvalue, **zargs; + ZEND_HASH_FOREACH_KEY_VAL(params, key.h, key.key, zparam) + { + zval *zvalue, *zargs; - if (Z_TYPE_PP(zparam) != IS_ARRAY) { + if (Z_TYPE_P(zparam) != IS_ARRAY) { zvalue = zparam; } else { - if (SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("value"), (void *) &zvalue)) { - if (SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("*rfc5987*"), (void *) &zvalue)) { + if (!(zvalue = zend_hash_str_find(Z_ARRVAL_P(zparam), ZEND_STRL("value")))) { + if (!(zvalue = zend_hash_str_find(Z_ARRVAL_P(zparam), ZEND_STRL("*rfc5987*")))) { zvalue = zparam; } else { rfc5987 = 1; @@@ -941,37 -903,29 +945,37 @@@ } } - php_http_array_hashkey_stringify(&key); - shift_param(buf, key.str, key.len - 1, *zvalue, pss, psl, ass, asl, vss, vsl, flags, rfc5987 TSRMLS_CC); - php_http_array_hashkey_stringfree(&key); + php_http_arrkey_stringify(&key, NULL); + shift_param(buf, key.key->val, key.key->len, zvalue, pss, psl, ass, asl, vss, vsl, flags, rfc5987); + php_http_arrkey_dtor(&key); + + if (Z_TYPE_P(zparam) == IS_ARRAY) { + zval *tmp = zend_hash_str_find(Z_ARRVAL_P(zparam), ZEND_STRL("arguments")); - if (Z_TYPE_PP(zparam) == IS_ARRAY && SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("arguments"), (void *) &zvalue)) { - if (zvalue == zparam) { + if (tmp) { + zvalue = tmp; + } else if (zvalue == zparam) { continue; + } else { + zvalue = zparam; } - zvalue = zparam; } - if (Z_TYPE_PP(zvalue) == IS_ARRAY) { - FOREACH_KEYVAL(pos1, *zvalue, key1, zargs) { - if (zvalue == zparam && key1.type == HASH_KEY_IS_STRING && !strcmp(key1.str, "value")) { + if (Z_TYPE_P(zvalue) == IS_ARRAY) { + ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(zvalue), key.h, key.key, zargs) + { + if (zvalue == zparam && key.key && zend_string_equals_literal(key.key, "value")) { continue; } - php_http_array_hashkey_stringify(&key1); - shift_arg(buf, key1.str, key1.len - 1, *zargs, ass, asl, vss, vsl, flags TSRMLS_CC); - php_http_array_hashkey_stringfree(&key1); + php_http_arrkey_stringify(&key, NULL); + shift_arg(buf, key.key->val, key.key->len, zargs, ass, asl, vss, vsl, flags); + php_http_arrkey_dtor(&key); } + ZEND_HASH_FOREACH_END(); } } + ZEND_HASH_FOREACH_END(); php_http_buffer_shrink(buf); php_http_buffer_fix(buf); @@@ -979,36 -933,31 +983,36 @@@ return buf; } -php_http_params_token_t **php_http_params_separator_init(zval *zv TSRMLS_DC) +php_http_params_token_t **php_http_params_separator_init(zval *zv) { - zval **sep; - HashPosition pos; + zval *sep, ztmp; php_http_params_token_t **ret, **tmp; if (!zv) { return NULL; } - zv = php_http_ztyp(IS_ARRAY, zv); + ZVAL_DUP(&ztmp, zv); + zv = &ztmp; + convert_to_array(zv); + ret = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(zv)) + 1, sizeof(*ret)); tmp = ret; - FOREACH_VAL(pos, zv, sep) { - zval *zt = php_http_ztyp(IS_STRING, *sep); + ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zv), sep) + { + zend_string *zs = zval_get_string(sep); - if (Z_STRLEN_P(zt)) { + if (zs->len) { *tmp = emalloc(sizeof(**tmp)); - (*tmp)->str = estrndup(Z_STRVAL_P(zt), (*tmp)->len = Z_STRLEN_P(zt)); + (*tmp)->str = estrndup(zs->val, (*tmp)->len = zs->len); ++tmp; } - zval_ptr_dtor(&zt); + zend_string_release(zs); } - zval_ptr_dtor(&zv); + ZEND_HASH_FOREACH_END(); + + zval_ptr_dtor(&ztmp); *tmp = NULL; return ret; @@@ -1027,12 -976,6 +1031,12 @@@ void php_http_params_separator_free(php } } +static zend_class_entry *php_http_params_class_entry; +zend_class_entry *php_http_params_get_class_entry(void) +{ + return php_http_params_class_entry; +} + ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams___construct, 0, 0, 0) ZEND_ARG_INFO(0, params) ZEND_ARG_INFO(0, param_sep) @@@ -1042,27 -985,26 +1046,27 @@@ ZEND_END_ARG_INFO(); PHP_METHOD(HttpParams, __construct) { - zval *zcopy, *zparams = NULL, *param_sep = NULL, *arg_sep = NULL, *val_sep = NULL; - long flags = PHP_HTTP_PARAMS_DEFAULT; + zval *zparams = NULL, *param_sep = NULL, *arg_sep = NULL, *val_sep = NULL; + zend_long flags = PHP_HTTP_PARAMS_DEFAULT; zend_error_handling zeh; + zend_string *zs; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!/z/z/z/l", &zparams, ¶m_sep, &arg_sep, &val_sep, &flags), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!/z/z/z/l", &zparams, ¶m_sep, &arg_sep, &val_sep, &flags), invalid_arg, return); - zend_replace_error_handling(EH_THROW, php_http_exception_runtime_class_entry, &zeh TSRMLS_CC); + zend_replace_error_handling(EH_THROW, php_http_get_exception_runtime_class_entry(), &zeh); { switch (ZEND_NUM_ARGS()) { case 5: - zend_update_property_long(php_http_params_class_entry, getThis(), ZEND_STRL("flags"), flags TSRMLS_CC); + zend_update_property_long(php_http_params_class_entry, getThis(), ZEND_STRL("flags"), flags); /* no break */ case 4: - zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), val_sep TSRMLS_CC); + zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), val_sep); /* no break */ case 3: - zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), arg_sep TSRMLS_CC); + zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), arg_sep); /* no break */ case 2: - zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), param_sep TSRMLS_CC); + zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), param_sep); /* no break */ } @@@ -1070,55 -1012,54 +1074,55 @@@ switch (Z_TYPE_P(zparams)) { case IS_OBJECT: case IS_ARRAY: - zcopy = php_http_zsep(1, IS_ARRAY, zparams); - zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zcopy TSRMLS_CC); - zval_ptr_dtor(&zcopy); + convert_to_array(zparams); + zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zparams); break; default: - zcopy = php_http_ztyp(IS_STRING, zparams); - if (Z_STRLEN_P(zcopy)) { + zs = zval_get_string(zparams); + if (zs->len) { + zval tmp; + php_http_params_opts_t opts = { - {Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy)}, - php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC) TSRMLS_CC), - php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC) TSRMLS_CC), - php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC) TSRMLS_CC), - NULL, flags + {zs->val, zs->len}, + php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0, &tmp)), + php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0, &tmp)), + php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0, &tmp)), + {{0}}, flags }; - MAKE_STD_ZVAL(zparams); - array_init(zparams); - php_http_params_parse(Z_ARRVAL_P(zparams), &opts TSRMLS_CC); - zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC); - zval_ptr_dtor(&zparams); + array_init(&tmp); + php_http_params_parse(Z_ARRVAL(tmp), &opts); + zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), &tmp); + zval_ptr_dtor(&tmp); php_http_params_separator_free(opts.param); php_http_params_separator_free(opts.arg); php_http_params_separator_free(opts.val); } - zval_ptr_dtor(&zcopy); + zend_string_release(zs); break; } } else { - MAKE_STD_ZVAL(zparams); - array_init(zparams); - zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC); - zval_ptr_dtor(&zparams); + zval tmp; + + array_init(&tmp); + zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), &tmp); + zval_ptr_dtor(&tmp); } } - zend_restore_error_handling(&zeh TSRMLS_CC); + zend_restore_error_handling(&zeh); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_toArray, 0, 0, 0) ZEND_END_ARG_INFO(); PHP_METHOD(HttpParams, toArray) { - zval *zparams; + zval zparams_tmp, *zparams; if (SUCCESS != zend_parse_parameters_none()) { return; } - zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC); + zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp); RETURN_ZVAL(zparams, 1, 0); } @@@ -1126,43 -1067,41 +1130,43 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_to ZEND_END_ARG_INFO(); PHP_METHOD(HttpParams, toString) { - zval **tmp, *zparams, *zpsep, *zasep, *zvsep, *zflags; + zval *tmp, *zparams, *zpsep, *zasep, *zvsep; + zval zparams_tmp, flags_tmp, psep_tmp, asep_tmp, vsep_tmp; + zend_string *psep, *asep, *vsep; + long flags; php_http_buffer_t buf; - zparams = php_http_zsep(1, IS_ARRAY, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC)); - zflags = php_http_ztyp(IS_LONG, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("flags"), 0 TSRMLS_CC)); + zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp); + convert_to_array_ex(zparams); + flags = zval_get_long(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("flags"), 0, &flags_tmp)); - zpsep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC); - if (Z_TYPE_P(zpsep) == IS_ARRAY && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zpsep), (void *) &tmp)) { - zpsep = php_http_ztyp(IS_STRING, *tmp); + zpsep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0, &psep_tmp); + if (Z_TYPE_P(zpsep) == IS_ARRAY && (tmp = zend_hash_get_current_data(Z_ARRVAL_P(zpsep)))) { + psep = zval_get_string(tmp); } else { - zpsep = php_http_ztyp(IS_STRING, zpsep); + psep = zval_get_string(zpsep); } - zasep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC); - if (Z_TYPE_P(zasep) == IS_ARRAY && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zasep), (void *) &tmp)) { - zasep = php_http_ztyp(IS_STRING, *tmp); + zasep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0, &asep_tmp); + if (Z_TYPE_P(zasep) == IS_ARRAY && (tmp = zend_hash_get_current_data(Z_ARRVAL_P(zasep)))) { + asep = zval_get_string(tmp); } else { - zasep = php_http_ztyp(IS_STRING, zasep); + asep = zval_get_string(zasep); } - zvsep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC); - if (Z_TYPE_P(zvsep) == IS_ARRAY && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zvsep), (void *) &tmp)) { - zvsep = php_http_ztyp(IS_STRING, *tmp); + zvsep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0, &vsep_tmp); + if (Z_TYPE_P(zvsep) == IS_ARRAY && (tmp = zend_hash_get_current_data(Z_ARRVAL_P(zvsep)))) { + vsep = zval_get_string(tmp); } else { - zvsep = php_http_ztyp(IS_STRING, zvsep); + vsep = zval_get_string(zvsep); } php_http_buffer_init(&buf); - php_http_params_to_string(&buf, Z_ARRVAL_P(zparams), Z_STRVAL_P(zpsep), Z_STRLEN_P(zpsep), Z_STRVAL_P(zasep), Z_STRLEN_P(zasep), Z_STRVAL_P(zvsep), Z_STRLEN_P(zvsep), Z_LVAL_P(zflags) TSRMLS_CC); + php_http_params_to_string(&buf, Z_ARRVAL_P(zparams), psep->val, psep->len, asep->val, asep->len, vsep->val, vsep->len, flags); - zval_ptr_dtor(&zparams); - zval_ptr_dtor(&zpsep); - zval_ptr_dtor(&zasep); - zval_ptr_dtor(&zvsep); - zval_ptr_dtor(&zflags); + zend_string_release(psep); + zend_string_release(asep); + zend_string_release(vsep); - RETVAL_PHP_HTTP_BUFFER_VAL(&buf); + RETVAL_STR(php_http_cs2zs(buf.data, buf.used)); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetExists, 0, 0, 1) @@@ -1170,20 -1109,22 +1174,20 @@@ ZEND_END_ARG_INFO(); PHP_METHOD(HttpParams, offsetExists) { - char *name_str; - int name_len; - zval **zparam, *zparams; + zend_string *name; + zval zparams_tmp, *zparam, *zparams; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name_str, &name_len)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name)) { return; } - zparams = php_http_ztyp(IS_ARRAY, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC)); + zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp); - if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(zparams), name_str, name_len + 1, (void *) &zparam)) { - RETVAL_BOOL(Z_TYPE_PP(zparam) != IS_NULL); + if (Z_TYPE_P(zparams) == IS_ARRAY && (zparam = zend_symtable_find(Z_ARRVAL_P(zparams), name))) { + RETVAL_BOOL(Z_TYPE_P(zparam) != IS_NULL); } else { RETVAL_FALSE; } - zval_ptr_dtor(&zparams); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetGet, 0, 0, 1) @@@ -1191,18 -1132,21 +1195,18 @@@ ZEND_END_ARG_INFO(); PHP_METHOD(HttpParams, offsetGet) { - char *name_str; - int name_len; - zval **zparam, *zparams; + zend_string *name; + zval zparams_tmp, *zparam, *zparams; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name_str, &name_len)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name)) { return; } - zparams = php_http_ztyp(IS_ARRAY, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC)); + zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp); - if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(zparams), name_str, name_len + 1, (void *) &zparam)) { - RETVAL_ZVAL(*zparam, 1, 0); + if (Z_TYPE_P(zparams) == IS_ARRAY && (zparam = zend_symtable_find(Z_ARRVAL_P(zparams), name))) { + RETVAL_ZVAL(zparam, 1, 0); } - - zval_ptr_dtor(&zparams); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetUnset, 0, 0, 1) @@@ -1210,18 -1154,20 +1214,18 @@@ ZEND_END_ARG_INFO(); PHP_METHOD(HttpParams, offsetUnset) { - char *name_str; - int name_len; - zval *zparams; + zend_string *name; + zval zparams_tmp, *zparams; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name_str, &name_len)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &name)) { return; } - zparams = php_http_zsep(1, IS_ARRAY, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC)); + zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp); - zend_symtable_del(Z_ARRVAL_P(zparams), name_str, name_len + 1); - zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC); - - zval_ptr_dtor(&zparams); + if (Z_TYPE_P(zparams) == IS_ARRAY) { + zend_symtable_del(Z_ARRVAL_P(zparams), name); + } } ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetSet, 0, 0, 2) @@@ -1230,48 -1176,55 +1234,48 @@@ ZEND_END_ARG_INFO(); PHP_METHOD(HttpParams, offsetSet) { - zval *nvalue; - char *name_str; - int name_len; - zval **zparam, *zparams; + zend_string *name; + zval zparams_tmp, *zparam, *zparams, *nvalue; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name_str, &name_len, &nvalue)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &name, &nvalue)) { return; } - zparams = php_http_zsep(1, IS_ARRAY, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC)); + zparams = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0, &zparams_tmp); + convert_to_array(zparams); - if (name_len) { + if (name->len) { if (Z_TYPE_P(nvalue) == IS_ARRAY) { - zval *new_zparam; - - if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(zparams), name_str, name_len + 1, (void *) &zparam)) { - new_zparam = php_http_zsep(1, IS_ARRAY, *zparam); - array_join(Z_ARRVAL_P(nvalue), Z_ARRVAL_P(new_zparam), 0, 0); + if ((zparam = zend_symtable_find(Z_ARRVAL_P(zparams), name))) { + convert_to_array(zparam); + array_join(Z_ARRVAL_P(nvalue), Z_ARRVAL_P(zparam), 0, 0); } else { - new_zparam = nvalue; - Z_ADDREF_P(new_zparam); + Z_TRY_ADDREF_P(nvalue); + add_assoc_zval_ex(zparams, name->val, name->len, nvalue); } - add_assoc_zval_ex(zparams, name_str, name_len + 1, new_zparam); } else { - zval *tmp; + zval tmp; - if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(zparams), name_str, name_len + 1, (void *) &zparam)) { - tmp = php_http_zsep(1, IS_ARRAY, *zparam); + if ((zparam = zend_symtable_find(Z_ARRVAL_P(zparams), name))) { + ZVAL_DUP(&tmp, zparam); + convert_to_array(&tmp); } else { - MAKE_STD_ZVAL(tmp); - array_init(tmp); + array_init(&tmp); } - Z_ADDREF_P(nvalue); - add_assoc_zval_ex(tmp, ZEND_STRS("value"), nvalue); - add_assoc_zval_ex(zparams, name_str, name_len + 1, tmp); + Z_TRY_ADDREF_P(nvalue); + add_assoc_zval_ex(&tmp, ZEND_STRL("value"), nvalue); + add_assoc_zval_ex(zparams, name->val, name->len, &tmp); } } else { - zval *tmp = php_http_ztyp(IS_STRING, nvalue), *arr; + zval arr; + zend_string *zs = zval_get_string(nvalue); - MAKE_STD_ZVAL(arr); - array_init(arr); - add_assoc_bool_ex(arr, ZEND_STRS("value"), 1); - add_assoc_zval_ex(zparams, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp) + 1, arr); - zval_ptr_dtor(&tmp); + array_init(&arr); + add_assoc_bool_ex(&arr, ZEND_STRL("value"), 1); + add_assoc_zval_ex(zparams, zs->val, zs->len, &arr); + zend_string_release(zs); } - - zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC); - zval_ptr_dtor(&zparams); } static zend_function_entry php_http_params_methods[] = { @@@ -1289,34 -1242,36 +1293,34 @@@ EMPTY_FUNCTION_ENTRY }; -zend_class_entry *php_http_params_class_entry; - PHP_MINIT_FUNCTION(http_params) { zend_class_entry ce = {0}; INIT_NS_CLASS_ENTRY(ce, "http", "Params", php_http_params_methods); - php_http_params_class_entry = zend_register_internal_class(&ce TSRMLS_CC); + php_http_params_class_entry = zend_register_internal_class(&ce); php_http_params_class_entry->create_object = php_http_params_object_new; - zend_class_implements(php_http_params_class_entry TSRMLS_CC, 1, zend_ce_arrayaccess); - - zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_PARAM_SEP"), ZEND_STRL(",") TSRMLS_CC); - zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_ARG_SEP"), ZEND_STRL(";") TSRMLS_CC); - zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_VAL_SEP"), ZEND_STRL("=") TSRMLS_CC); - zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("COOKIE_PARAM_SEP"), ZEND_STRL("") TSRMLS_CC); - - zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_RAW"), PHP_HTTP_PARAMS_RAW TSRMLS_CC); - zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_ESCAPED"), PHP_HTTP_PARAMS_ESCAPED TSRMLS_CC); - zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_URLENCODED"), PHP_HTTP_PARAMS_URLENCODED TSRMLS_CC); - zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_DIMENSION"), PHP_HTTP_PARAMS_DIMENSION TSRMLS_CC); - zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_RFC5987"), PHP_HTTP_PARAMS_RFC5987 TSRMLS_CC); - zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_RFC5988"), PHP_HTTP_PARAMS_RFC5988 TSRMLS_CC); - zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_DEFAULT"), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC); - zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_QUERY"), PHP_HTTP_PARAMS_QUERY TSRMLS_CC); - - zend_declare_property_null(php_http_params_class_entry, ZEND_STRL("params"), ZEND_ACC_PUBLIC TSRMLS_CC); - zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("param_sep"), ZEND_STRL(","), ZEND_ACC_PUBLIC TSRMLS_CC); - zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("arg_sep"), ZEND_STRL(";"), ZEND_ACC_PUBLIC TSRMLS_CC); - zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("val_sep"), ZEND_STRL("="), ZEND_ACC_PUBLIC TSRMLS_CC); - zend_declare_property_long(php_http_params_class_entry, ZEND_STRL("flags"), PHP_HTTP_PARAMS_DEFAULT, ZEND_ACC_PUBLIC TSRMLS_CC); + zend_class_implements(php_http_params_class_entry, 1, zend_ce_arrayaccess); + + zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_PARAM_SEP"), ZEND_STRL(",")); + zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_ARG_SEP"), ZEND_STRL(";")); + zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_VAL_SEP"), ZEND_STRL("=")); + zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("COOKIE_PARAM_SEP"), ZEND_STRL("")); + + zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_RAW"), PHP_HTTP_PARAMS_RAW); + zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_ESCAPED"), PHP_HTTP_PARAMS_ESCAPED); + zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_URLENCODED"), PHP_HTTP_PARAMS_URLENCODED); + zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_DIMENSION"), PHP_HTTP_PARAMS_DIMENSION); + zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_RFC5987"), PHP_HTTP_PARAMS_RFC5987); + zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_RFC5988"), PHP_HTTP_PARAMS_RFC5988); + zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_DEFAULT"), PHP_HTTP_PARAMS_DEFAULT); + zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_QUERY"), PHP_HTTP_PARAMS_QUERY); + + zend_declare_property_null(php_http_params_class_entry, ZEND_STRL("params"), ZEND_ACC_PUBLIC); + zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("param_sep"), ZEND_STRL(","), ZEND_ACC_PUBLIC); + zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("arg_sep"), ZEND_STRL(";"), ZEND_ACC_PUBLIC); + zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("val_sep"), ZEND_STRL("="), ZEND_ACC_PUBLIC); + zend_declare_property_long(php_http_params_class_entry, ZEND_STRL("flags"), PHP_HTTP_PARAMS_DEFAULT, ZEND_ACC_PUBLIC); return SUCCESS; } diff --combined src/php_http_querystring.c index ea84d8d,322bd75..d45cd49 --- a/src/php_http_querystring.c +++ b/src/php_http_querystring.c @@@ -23,68 -23,54 +23,68 @@@ # include #endif +static zend_class_entry *php_http_querystring_class_entry; +zend_class_entry *php_http_querystring_get_class_entry(void) +{ + return php_http_querystring_class_entry; +} + #define QS_MERGE 1 -static inline void php_http_querystring_set(zval *instance, zval *params, int flags TSRMLS_DC) +static inline void php_http_querystring_set(zval *instance, zval *params, int flags) { - zval *qa; + zval qa; + + array_init(&qa); if (flags & QS_MERGE) { - qa = php_http_zsep(1, IS_ARRAY, zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0 TSRMLS_CC)); - } else { - MAKE_STD_ZVAL(qa); - array_init(qa); + zval old_tmp, *old = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &old_tmp); + + ZVAL_DEREF(old); + if (Z_TYPE_P(old) == IS_ARRAY) { + array_copy(Z_ARRVAL_P(old), Z_ARRVAL(qa)); + } } - php_http_querystring_update(qa, params, NULL TSRMLS_CC); - zend_update_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), qa TSRMLS_CC); + php_http_querystring_update(&qa, params, NULL); + zend_update_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), &qa); zval_ptr_dtor(&qa); } -static inline void php_http_querystring_str(zval *instance, zval *return_value TSRMLS_DC) +static inline void php_http_querystring_str(zval *instance, zval *return_value) { - zval *qa = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0 TSRMLS_CC); + zval qa_tmp, *qa = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &qa_tmp); + ZVAL_DEREF(qa); if (Z_TYPE_P(qa) == IS_ARRAY) { - php_http_querystring_update(qa, NULL, return_value TSRMLS_CC); + php_http_querystring_update(qa, NULL, return_value); } else { RETURN_EMPTY_STRING(); } } -static inline void php_http_querystring_get(zval *this_ptr, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value TSRMLS_DC) +static inline void php_http_querystring_get(zval *instance, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value) { - zval **arrval, *qarray = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC); + zval *arrval, qarray_tmp, *qarray = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &qarray_tmp); + + ZVAL_DEREF(qarray); + if ((Z_TYPE_P(qarray) == IS_ARRAY) && (arrval = zend_symtable_str_find(Z_ARRVAL_P(qarray), name, name_len))) { + if (type && type != Z_TYPE_P(arrval)) { + zval tmp; - if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_symtable_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void *) &arrval))) { - if (type) { - zval *value = php_http_ztyp(type, *arrval); - RETVAL_ZVAL(value, 1, 1); + ZVAL_DUP(&tmp, arrval); + convert_to_explicit_type(&tmp, type); + RETVAL_ZVAL(&tmp, 0, 0); } else { - RETVAL_ZVAL(*arrval, 1, 0); + RETVAL_ZVAL(arrval, 1, 0); } if (del) { - zval *delarr; + zval delarr; - MAKE_STD_ZVAL(delarr); - array_init(delarr); - add_assoc_null_ex(delarr, name, name_len + 1); - php_http_querystring_set(this_ptr, delarr, QS_MERGE TSRMLS_CC); + array_init(&delarr); + add_assoc_null_ex(&delarr, name, name_len); + php_http_querystring_set(instance, &delarr, QS_MERGE); zval_ptr_dtor(&delarr); } } else if(defval) { @@@ -93,85 -79,110 +93,104 @@@ } #ifdef PHP_HTTP_HAVE_ICONV -ZEND_RESULT_CODE php_http_querystring_xlate(zval *dst, zval *src, const char *ie, const char *oe TSRMLS_DC) +ZEND_RESULT_CODE php_http_querystring_xlate(zval *dst, zval *src, const char *ie, const char *oe) { - HashPosition pos; - zval **entry = NULL; - char *xlate_str = NULL, *xkey; - size_t xlate_len = 0, xlen; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); + zval *entry; + zend_string *xkey, *xstr; + php_http_arrkey_t key; - FOREACH_KEYVAL(pos, src, key, entry) { - if (key.type == HASH_KEY_IS_STRING) { - if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(key.str, key.len-1, &xkey, &xlen, oe, ie)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", key.len-1, key.str, ie, oe); + ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(src), key.h, key.key, entry) + { + if (key.key) { + if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(key.key->val, key.key->len, &xkey, oe, ie)) { + php_error_docref(NULL, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", key.key->len, key.key->val, ie, oe); return FAILURE; } } - if (Z_TYPE_PP(entry) == IS_STRING) { - if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry), &xlate_str, &xlate_len, oe, ie)) { - if (key.type == HASH_KEY_IS_STRING) { - efree(xkey); + if (Z_TYPE_P(entry) == IS_STRING) { + if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(Z_STRVAL_P(entry), Z_STRLEN_P(entry), &xstr, oe, ie)) { + if (key.key) { + zend_string_release(xkey); } - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", Z_STRLEN_PP(entry), Z_STRVAL_PP(entry), ie, oe); + php_error_docref(NULL, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", Z_STRLEN_P(entry), Z_STRVAL_P(entry), ie, oe); return FAILURE; } - if (key.type == HASH_KEY_IS_STRING) { - add_assoc_stringl_ex(dst, xkey, xlen+1, xlate_str, xlate_len, 0); + if (key.key) { + add_assoc_str_ex(dst, xkey->val, xkey->len, xstr); } else { - add_index_stringl(dst, key.num, xlate_str, xlate_len, 0); + add_index_str(dst, key.h, xstr); } - } else if (Z_TYPE_PP(entry) == IS_ARRAY) { - zval *subarray; + } else if (Z_TYPE_P(entry) == IS_ARRAY) { + zval subarray; - MAKE_STD_ZVAL(subarray); - array_init(subarray); - if (key.type == HASH_KEY_IS_STRING) { - add_assoc_zval_ex(dst, xkey, xlen+1, subarray); + array_init(&subarray); + if (key.key) { + add_assoc_zval_ex(dst, xkey->val, xkey->len, &subarray); } else { - add_index_zval(dst, key.num, subarray); + add_index_zval(dst, key.h, &subarray); } - if (SUCCESS != php_http_querystring_xlate(subarray, *entry, ie, oe TSRMLS_CC)) { - if (key.type == HASH_KEY_IS_STRING) { - efree(xkey); + if (SUCCESS != php_http_querystring_xlate(&subarray, entry, ie, oe)) { + if (key.key) { + zend_string_release(xkey); } return FAILURE; } } - if (key.type == HASH_KEY_IS_STRING) { - efree(xkey); + if (key.key) { + zend_string_release(xkey); } } + ZEND_HASH_FOREACH_END(); + return SUCCESS; } #endif /* HAVE_ICONV */ -ZEND_RESULT_CODE php_http_querystring_ctor(zval *instance, zval *params TSRMLS_DC) +ZEND_RESULT_CODE php_http_querystring_ctor(zval *instance, zval *params) { - php_http_querystring_set(instance, params, 0 TSRMLS_CC); + php_http_querystring_set(instance, params, 0); return SUCCESS; } -static int apply_querystring(void *pData TSRMLS_DC) +static int apply_querystring(zval *val) { - zval **val = pData; - - if (Z_TYPE_PP(val) == IS_ARRAY) { - zval **zvalue; + if (Z_TYPE_P(val) == IS_ARRAY) { + zval *zvalue; - if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(val), ZEND_STRS("value"), (void *) &zvalue)) { - zval *tmp = *val; + if ((zvalue = zend_hash_str_find(Z_ARRVAL_P(val), ZEND_STRL("value")))) { + zval tmp; - Z_ADDREF_PP(zvalue); - *val = *zvalue; - zval_dtor(tmp); - Z_TYPE_P(tmp) = IS_NULL; - zval_ptr_dtor(&tmp); + ZVAL_COPY(&tmp, zvalue); + zval_dtor(val); + ZVAL_COPY_VALUE(val, &tmp); } } return ZEND_HASH_APPLY_KEEP; } -static int apply_querystring_filter(void *pData TSRMLS_DC) ++static int apply_querystring_filter(zval *val) + { - zval **val = pData; - - switch (Z_TYPE_PP(val)) { ++ switch (Z_TYPE_P(val)) { + case IS_NULL: + return ZEND_HASH_APPLY_REMOVE; + case IS_ARRAY: + case IS_OBJECT: - zend_hash_apply(HASH_OF(*val), apply_querystring_filter TSRMLS_CC); - if (!zend_hash_num_elements(HASH_OF(*val))) { ++ zend_hash_apply(HASH_OF(val), apply_querystring_filter); ++ if (!zend_hash_num_elements(HASH_OF(val))) { + return ZEND_HASH_APPLY_REMOVE; + } + break; + default: + break; + } + + return ZEND_HASH_APPLY_KEEP; + } + -ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size_t len TSRMLS_DC) +ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size_t len) { ZEND_RESULT_CODE rv = FAILURE; php_http_params_opts_t opts; @@@ -187,23 -198,26 +206,23 @@@ opts.val = vsepp; opts.flags = PHP_HTTP_PARAMS_QUERY; - if (SUCCESS == php_http_ini_entry(ZEND_STRL("arg_separator.input"), &asi_str, &asi_len, 0 TSRMLS_CC) && asi_len) { - zval *arr; + if (SUCCESS == php_http_ini_entry(ZEND_STRL("arg_separator.input"), &asi_str, &asi_len, 0) && asi_len) { + zval arr; - MAKE_STD_ZVAL(arr); - array_init_size(arr, asi_len); + array_init_size(&arr, asi_len); do { - add_next_index_stringl(arr, asi_str++, 1, 1); + add_next_index_stringl(&arr, asi_str++, 1); } while (*asi_str); - opts.param = php_http_params_separator_init(arr TSRMLS_CC); - + opts.param = php_http_params_separator_init(&arr); zval_ptr_dtor(&arr); } - ZVAL_NULL(&opts.defval); - MAKE_STD_ZVAL(opts.defval); - ZVAL_TRUE(opts.defval); ++ ZVAL_TRUE(&opts.defval); - if (php_http_params_parse(ht, &opts TSRMLS_CC)) { - zend_hash_apply(ht, apply_querystring TSRMLS_CC); + if (php_http_params_parse(ht, &opts)) { + zend_hash_apply(ht, apply_querystring); rv = SUCCESS; } @@@ -216,7 -230,7 +235,7 @@@ return rv; } -ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *outstring TSRMLS_DC) +ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *outstring) { /* enforce proper type */ if (Z_TYPE_P(qarray) != IS_ARRAY) { @@@ -224,92 -238,93 +243,94 @@@ } /* modify qarray */ - if (params) { + if (!params) { - zend_hash_apply(Z_ARRVAL_P(qarray), apply_querystring_filter TSRMLS_CC); ++ zend_hash_apply(Z_ARRVAL_P(qarray), apply_querystring_filter); + } else { - HashPosition pos; - HashTable *ptr; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - zval **params_entry, **qarray_entry; - zval zv, *zv_ptr = NULL; + HashTable *ht; + php_http_arrkey_t key; + zval zv, *params_entry, *qarray_entry; - INIT_PZVAL(&zv); ZVAL_NULL(&zv); /* squeeze the hash out of the zval */ - if (Z_TYPE_P(params) == IS_OBJECT && instanceof_function(Z_OBJCE_P(params), php_http_querystring_class_entry TSRMLS_CC)) { - zv_ptr = php_http_ztyp(IS_ARRAY, zend_read_property(php_http_querystring_class_entry, params, ZEND_STRL("queryArray"), 0 TSRMLS_CC)); - ptr = Z_ARRVAL_P(zv_ptr); + if (Z_TYPE_P(params) == IS_OBJECT && instanceof_function(Z_OBJCE_P(params), php_http_querystring_class_entry)) { + zval qa_tmp, *qa = zend_read_property(php_http_querystring_class_entry, params, ZEND_STRL("queryArray"), 0, &qa_tmp); + + ZVAL_DEREF(qa); + convert_to_array(qa); + ht = Z_ARRVAL_P(qa); } else if (Z_TYPE_P(params) == IS_OBJECT || Z_TYPE_P(params) == IS_ARRAY) { - ptr = HASH_OF(params); + ht = HASH_OF(params); } else { - zv_ptr = php_http_ztyp(IS_STRING, params); + zend_string *zs = zval_get_string(params); + array_init(&zv); - php_http_querystring_parse(Z_ARRVAL(zv), Z_STRVAL_P(zv_ptr), Z_STRLEN_P(zv_ptr) TSRMLS_CC); - zval_ptr_dtor(&zv_ptr); - zv_ptr = NULL; - ptr = Z_ARRVAL(zv); + php_http_querystring_parse(Z_ARRVAL(zv), zs->val, zs->len); + zend_string_release(zs); + + ht = Z_ARRVAL(zv); } - FOREACH_HASH_KEYVAL(pos, ptr, key, params_entry) { + ZEND_HASH_FOREACH_KEY_VAL_IND(ht, key.h, key.key, params_entry) + { /* only public properties */ - if (key.type != HASH_KEY_IS_STRING || *key.str) { - if (Z_TYPE_PP(params_entry) == IS_NULL) { + if (!key.key || *key.key->val) { + if (Z_TYPE_P(params_entry) == IS_NULL) { /* * delete */ - if (key.type == HASH_KEY_IS_STRING) { - zend_hash_del(Z_ARRVAL_P(qarray), key.str, key.len); + if (key.key) { + zend_hash_del(Z_ARRVAL_P(qarray), key.key); } else { - zend_hash_index_del(Z_ARRVAL_P(qarray), key.num); + zend_hash_index_del(Z_ARRVAL_P(qarray), key.h); } - } else if ( ((key.type == HASH_KEY_IS_STRING) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), key.str, key.len, (void *) &qarray_entry))) - || ((key.type == HASH_KEY_IS_LONG) && (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(qarray), key.num, (void *) &qarray_entry)))) { + } else if ( ((key.key) && (qarray_entry = zend_hash_find(Z_ARRVAL_P(qarray), key.key))) + || ((!key.key) && (qarray_entry = zend_hash_index_find(Z_ARRVAL_P(qarray), key.h)))) { /* * update */ - zval equal, *entry = NULL; + zval equal, tmp, *entry = &tmp; + ZVAL_UNDEF(&tmp); /* recursive */ - if (Z_TYPE_PP(params_entry) == IS_ARRAY || Z_TYPE_PP(params_entry) == IS_OBJECT) { - entry = php_http_zsep(1, IS_ARRAY, *qarray_entry); - php_http_querystring_update(entry, *params_entry, NULL TSRMLS_CC); - } else if ((FAILURE == is_identical_function(&equal, *qarray_entry, *params_entry TSRMLS_CC)) || !Z_BVAL(equal)) { - Z_ADDREF_PP(params_entry); - entry = *params_entry; + if (Z_TYPE_P(params_entry) == IS_ARRAY || Z_TYPE_P(params_entry) == IS_OBJECT) { + ZVAL_DUP(entry, qarray_entry); + convert_to_array(entry); + php_http_querystring_update(entry, params_entry, NULL); - } else if ((FAILURE == is_equal_function(&equal, qarray_entry, params_entry)) || Z_TYPE(equal) != IS_TRUE) { ++ } else if ((FAILURE == is_identical_function(&equal, qarray_entry, params_entry)) || Z_TYPE(equal) != IS_TRUE) { + Z_TRY_ADDREF_P(params_entry); + entry = params_entry; } if (entry) { - if (key.type == HASH_KEY_IS_STRING) { - zend_hash_update(Z_ARRVAL_P(qarray), key.str, key.len, (void *) &entry, sizeof(zval *), NULL); + if (key.key) { + zend_hash_update(Z_ARRVAL_P(qarray), key.key, entry); } else { - zend_hash_index_update(Z_ARRVAL_P(qarray), key.num, (void *) &entry, sizeof(zval *), NULL); + zend_hash_index_update(Z_ARRVAL_P(qarray), key.h, entry); } } } else { - zval *entry; + zval entry, *entry_ptr = &entry; /* * add */ - if (Z_TYPE_PP(params_entry) == IS_OBJECT) { - MAKE_STD_ZVAL(entry); - array_init(entry); - php_http_querystring_update(entry, *params_entry, NULL TSRMLS_CC); + if (Z_TYPE_P(params_entry) == IS_OBJECT) { + array_init(&entry); + php_http_querystring_update(&entry, params_entry, NULL); } else { - Z_ADDREF_PP(params_entry); - entry = *params_entry; + Z_TRY_ADDREF_P(params_entry); + entry_ptr = params_entry; } - if (key.type == HASH_KEY_IS_STRING) { - add_assoc_zval_ex(qarray, key.str, key.len, entry); + if (key.key) { + add_assoc_zval_ex(qarray, key.key->val, key.key->len, entry_ptr); } else { - add_index_zval(qarray, key.num, entry); + add_index_zval(qarray, key.h, entry_ptr); } } } } - /* clean up */ - if (zv_ptr) { - zval_ptr_dtor(&zv_ptr); - } + ZEND_HASH_FOREACH_END(); + zval_dtor(&zv); } @@@ -318,11 -333,11 +339,11 @@@ char *s; size_t l; - if (SUCCESS == php_http_url_encode_hash(Z_ARRVAL_P(qarray), NULL, 0, &s, &l TSRMLS_CC)) { + if (SUCCESS == php_http_url_encode_hash(Z_ARRVAL_P(qarray), NULL, 0, &s, &l)) { zval_dtor(outstring); - ZVAL_STRINGL(outstring, s, l, 0); + ZVAL_STR(outstring, php_http_cs2zs(s, l)); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to encode query string"); + php_error_docref(NULL, E_WARNING, "Failed to encode query string"); return FAILURE; } } @@@ -338,53 -353,63 +359,53 @@@ PHP_METHOD(HttpQueryString, __construct zval *params = NULL; zend_error_handling zeh; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", ¶ms), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z", ¶ms), invalid_arg, return); - zend_replace_error_handling(EH_THROW, php_http_exception_bad_querystring_class_entry, &zeh TSRMLS_CC); - php_http_querystring_set(getThis(), params, 0 TSRMLS_CC); - zend_restore_error_handling(&zeh TSRMLS_CC); + zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_querystring_class_entry(), &zeh); + php_http_querystring_set(getThis(), params, 0); + zend_restore_error_handling(&zeh); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_getGlobalInstance, 0, 0, 0) ZEND_END_ARG_INFO(); PHP_METHOD(HttpQueryString, getGlobalInstance) { - zval *instance; + zval *instance, *_GET; + zend_string *zs; php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return); - instance = *zend_std_get_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), 0 PHP_HTTP_ZEND_LITERAL_CCN TSRMLS_CC); - - if (Z_TYPE_P(instance) != IS_OBJECT) { - zval **_GET = NULL; - - zend_is_auto_global("_GET", lenof("_GET") TSRMLS_CC); + zs = zend_string_init(ZEND_STRL("instance"), 0); + instance = zend_std_get_static_property(php_http_querystring_class_entry, zs, 0); + zend_string_release(zs); - if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void *) &_GET)) - && (Z_TYPE_PP(_GET) == IS_ARRAY) - ) { - MAKE_STD_ZVAL(instance); - ZVAL_OBJVAL(instance, php_http_querystring_object_new(php_http_querystring_class_entry TSRMLS_CC), 0); + if (Z_TYPE_P(instance) == IS_OBJECT) { + RETVAL_ZVAL(instance, 1, 0); + } else if ((_GET = php_http_env_get_superglobal(ZEND_STRL("_GET")))) { + ZVAL_OBJ(return_value, php_http_querystring_object_new(php_http_querystring_class_entry)); - SEPARATE_ZVAL_TO_MAKE_IS_REF(_GET); - convert_to_array(*_GET); - zend_update_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), *_GET TSRMLS_CC); + ZVAL_MAKE_REF(_GET); + zend_update_property(php_http_querystring_class_entry, return_value, ZEND_STRL("queryArray"), _GET); - zend_update_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), instance TSRMLS_CC); - zval_ptr_dtor(&instance); - } else { - php_http_throw(unexpected_val, "Could not acquire reference to superglobal GET array", NULL); - } + zend_update_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), return_value); + } else { + php_http_throw(unexpected_val, "Could not acquire reference to superglobal GET array", NULL); } - RETVAL_ZVAL(instance, 1, 0); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_getIterator, 0, 0, 0) ZEND_END_ARG_INFO(); PHP_METHOD(HttpQueryString, getIterator) { - zval *retval = NULL, *qa; + zval qa_tmp, *qa; php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return); - qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC); + qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp); object_init_ex(return_value, spl_ce_RecursiveArrayIterator); - zend_call_method_with_1_params(&return_value, spl_ce_RecursiveArrayIterator, NULL, "__construct", &retval, qa); - if (retval) { - zval_ptr_dtor(&retval); - } + zend_call_method_with_1_params(return_value, spl_ce_RecursiveArrayIterator, NULL, "__construct", NULL, qa); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_toString, 0, 0, 0) @@@ -394,20 -419,20 +415,20 @@@ PHP_METHOD(HttpQueryString, toString if (SUCCESS != zend_parse_parameters_none()) { return; } - php_http_querystring_str(getThis(), return_value TSRMLS_CC); + php_http_querystring_str(getThis(), return_value); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_toArray, 0, 0, 0) ZEND_END_ARG_INFO(); PHP_METHOD(HttpQueryString, toArray) { - zval *zqa; + zval zqa_tmp, *zqa; if (SUCCESS != zend_parse_parameters_none()) { return; } - zqa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC); + zqa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &zqa_tmp); RETURN_ZVAL(zqa, 1, 0); } @@@ -420,12 -445,12 +441,12 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpQueryString, get) { char *name_str = NULL; - int name_len = 0; - long type = 0; + size_t name_len = 0; + zend_long type = 0; zend_bool del = 0; zval *ztype = NULL, *defval = NULL; - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name_str, &name_len, &ztype, &defval, &del)) { + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|szzb", &name_str, &name_len, &ztype, &defval, &del)) { if (name_str && name_len) { if (ztype) { if (Z_TYPE_P(ztype) == IS_LONG) { @@@ -451,9 -476,9 +472,9 @@@ } } } - php_http_querystring_get(getThis(), type, name_str, name_len, defval, del, return_value TSRMLS_CC); + php_http_querystring_get(getThis(), type, name_str, name_len, defval, del, return_value); } else { - php_http_querystring_str(getThis(), return_value TSRMLS_CC); + php_http_querystring_str(getThis(), return_value); } } } @@@ -465,11 -490,11 +486,11 @@@ PHP_METHOD(HttpQueryString, set { zval *params; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", ¶ms)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "z", ¶ms)) { return; } - php_http_querystring_set(getThis(), params, QS_MERGE TSRMLS_CC); + php_http_querystring_set(getThis(), params, QS_MERGE); RETVAL_ZVAL(getThis(), 1, 0); } @@@ -478,17 -503,15 +499,17 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryStri ZEND_END_ARG_INFO(); PHP_METHOD(HttpQueryString, mod) { - zval *params; + zval qa_tmp, *params, *instance = getThis(); zend_error_handling zeh; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", ¶ms), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z", ¶ms), invalid_arg, return); - zend_replace_error_handling(EH_THROW, php_http_exception_bad_querystring_class_entry, &zeh TSRMLS_CC); - ZVAL_OBJVAL(return_value, Z_OBJ_HT_P(getThis())->clone_obj(getThis() TSRMLS_CC), 0); - php_http_querystring_set(return_value, params, QS_MERGE TSRMLS_CC); - zend_restore_error_handling(&zeh TSRMLS_CC); + zend_replace_error_handling(EH_THROW, php_http_get_exception_bad_querystring_class_entry(), &zeh); + ZVAL_OBJ(return_value, Z_OBJ_HT_P(instance)->clone_obj(instance)); + /* make sure we do not inherit the reference to _GET */ + SEPARATE_ZVAL(zend_read_property(Z_OBJCE_P(return_value), return_value, ZEND_STRL("queryArray"), 0, &qa_tmp)); + php_http_querystring_set(return_value, params, QS_MERGE); + zend_restore_error_handling(&zeh); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString___getter, 0, 0, 1) @@@ -500,14 -523,14 +521,14 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpQueryString, method) \ { \ char *name; \ - int name_len; \ + size_t name_len; \ zval *defval = NULL; \ zend_bool del = 0; \ - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \ - php_http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value TSRMLS_CC); \ + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|zb", &name, &name_len, &defval, &del)) { \ + php_http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \ } \ } -PHP_HTTP_QUERYSTRING_GETTER(getBool, IS_BOOL); +PHP_HTTP_QUERYSTRING_GETTER(getBool, _IS_BOOL); PHP_HTTP_QUERYSTRING_GETTER(getInt, IS_LONG); PHP_HTTP_QUERYSTRING_GETTER(getFloat, IS_DOUBLE); PHP_HTTP_QUERYSTRING_GETTER(getString, IS_STRING); @@@ -522,25 -545,26 +543,25 @@@ ZEND_END_ARG_INFO() PHP_METHOD(HttpQueryString, xlate) { char *ie, *oe; - int ie_len, oe_len; - zval *na, *qa; + size_t ie_len, oe_len; + zval na, qa_tmp, *qa; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ie, &ie_len, &oe, &oe_len), invalid_arg, return); + php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &ie, &ie_len, &oe, &oe_len), invalid_arg, return); - MAKE_STD_ZVAL(na); - array_init(na); - qa = php_http_ztyp(IS_ARRAY, zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC)); + array_init(&na); + qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp); + ZVAL_DEREF(qa); + convert_to_array(qa); - php_http_expect(SUCCESS == php_http_querystring_xlate(na, qa, ie, oe TSRMLS_CC), bad_conversion, + php_http_expect(SUCCESS == php_http_querystring_xlate(&na, qa, ie, oe), bad_conversion, zval_ptr_dtor(&na); - zval_ptr_dtor(&qa); return; ); - php_http_querystring_set(getThis(), na, 0 TSRMLS_CC); + php_http_querystring_set(getThis(), &na, 0); RETVAL_ZVAL(getThis(), 1, 0); zval_ptr_dtor(&na); - zval_ptr_dtor(&qa); } #endif /* HAVE_ICONV */ @@@ -551,7 -575,7 +572,7 @@@ PHP_METHOD(HttpQueryString, serialize if (SUCCESS != zend_parse_parameters_none()) { return; } - php_http_querystring_str(getThis(), return_value TSRMLS_CC); + php_http_querystring_str(getThis(), return_value); } ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_unserialize, 0, 0, 1) @@@ -561,14 -585,14 +582,14 @@@ PHP_METHOD(HttpQueryString, unserialize { zval *serialized; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &serialized)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "z", &serialized)) { return; } if (Z_TYPE_P(serialized) == IS_STRING) { - php_http_querystring_set(getThis(), serialized, 0 TSRMLS_CC); + php_http_querystring_set(getThis(), serialized, 0); } else { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected a string as parameter"); + php_error_docref(NULL, E_WARNING, "Expected a string as parameter"); } } @@@ -577,19 -601,19 +598,19 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryStri ZEND_END_ARG_INFO(); PHP_METHOD(HttpQueryString, offsetGet) { - char *offset_str; - int offset_len; - zval **value, *qa; + zend_string *offset; + zval *value, qa_tmp, *qa; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &offset_str, &offset_len)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) { return; } - qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC); + qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp); + ZVAL_DEREF(qa); if (Z_TYPE_P(qa) == IS_ARRAY) { - if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(qa), offset_str, offset_len + 1, (void *) &value)) { - RETVAL_ZVAL(*value, 1, 0); + if ((value = zend_symtable_find(Z_ARRVAL_P(qa), offset))) { + RETVAL_ZVAL(value, 1, 0); } } } @@@ -600,22 -624,27 +621,22 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryStri ZEND_END_ARG_INFO(); PHP_METHOD(HttpQueryString, offsetSet) { - char *offset_str; - int offset_len; - zval *value, *param; + zend_string *offset; + zval *value, param, znull; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &offset_str, &offset_len, &value)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &offset, &value)) { return; } - param = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC); - - if (Z_TYPE_P(param) == IS_ARRAY && zend_symtable_exists(Z_ARRVAL_P(param), offset_str, offset_len + 1)) { - Z_ADDREF_P(value); - zend_symtable_update(Z_ARRVAL_P(param), offset_str, offset_len + 1, (void *) &value, sizeof(zval *), NULL); - Z_ADDREF_P(param); - } else { - MAKE_STD_ZVAL(param); - array_init(param); - Z_ADDREF_P(value); - add_assoc_zval_ex(param, offset_str, offset_len + 1, value); - } - php_http_querystring_set(getThis(), param, QS_MERGE TSRMLS_CC); + array_init_size(¶m, 1); + /* unset first */ + ZVAL_NULL(&znull); + zend_symtable_update(Z_ARRVAL(param), offset, &znull); + php_http_querystring_set(getThis(), ¶m, QS_MERGE); + /* then update, else QS_MERGE would merge sub-arrrays */ + Z_TRY_ADDREF_P(value); + zend_symtable_update(Z_ARRVAL(param), offset, value); + php_http_querystring_set(getThis(), ¶m, QS_MERGE); zval_ptr_dtor(¶m); } @@@ -624,19 -653,19 +645,19 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryStri ZEND_END_ARG_INFO(); PHP_METHOD(HttpQueryString, offsetExists) { - char *offset_str; - int offset_len; - zval **value, *qa; + zend_string *offset; + zval *value, qa_tmp, *qa; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &offset_str, &offset_len)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) { return; } - qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0 TSRMLS_CC); + qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp); + ZVAL_DEREF(qa); if (Z_TYPE_P(qa) == IS_ARRAY) { - if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(qa), offset_str, offset_len + 1, (void *) &value)) { - RETURN_BOOL(Z_TYPE_PP(value) != IS_NULL); + if ((value = zend_symtable_find(Z_ARRVAL_P(qa), offset))) { + RETURN_BOOL(Z_TYPE_P(value) != IS_NULL); } } RETURN_FALSE; @@@ -647,20 -676,23 +668,20 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryStri ZEND_END_ARG_INFO(); PHP_METHOD(HttpQueryString, offsetUnset) { - char *offset_str; - int offset_len; - zval *param; + zend_string *offset; + zval param, znull; - if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &offset_str, &offset_len)) { + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) { return; } - MAKE_STD_ZVAL(param); - array_init(param); - add_assoc_null_ex(param, offset_str, offset_len + 1); - php_http_querystring_set(getThis(), param, QS_MERGE TSRMLS_CC); + array_init(¶m); + ZVAL_NULL(&znull); + zend_symtable_update(Z_ARRVAL(param), offset, &znull); + php_http_querystring_set(getThis(), ¶m, QS_MERGE); zval_ptr_dtor(¶m); } -zend_class_entry *php_http_querystring_class_entry; - static zend_function_entry php_http_querystring_methods[] = { PHP_ME(HttpQueryString, __construct, ai_HttpQueryString___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL) @@@ -704,19 -736,19 +725,19 @@@ PHP_MINIT_FUNCTION(http_querystring zend_class_entry ce = {0}; INIT_NS_CLASS_ENTRY(ce, "http", "QueryString", php_http_querystring_methods); - php_http_querystring_class_entry = zend_register_internal_class(&ce TSRMLS_CC); + php_http_querystring_class_entry = zend_register_internal_class(&ce); php_http_querystring_class_entry->create_object = php_http_querystring_object_new; - zend_class_implements(php_http_querystring_class_entry TSRMLS_CC, 3, zend_ce_serializable, zend_ce_arrayaccess, zend_ce_aggregate); + zend_class_implements(php_http_querystring_class_entry, 3, zend_ce_serializable, zend_ce_arrayaccess, zend_ce_aggregate); - zend_declare_property_null(php_http_querystring_class_entry, ZEND_STRL("instance"), (ZEND_ACC_STATIC|ZEND_ACC_PRIVATE) TSRMLS_CC); - zend_declare_property_null(php_http_querystring_class_entry, ZEND_STRL("queryArray"), ZEND_ACC_PRIVATE TSRMLS_CC); + zend_declare_property_null(php_http_querystring_class_entry, ZEND_STRL("instance"), (ZEND_ACC_STATIC|ZEND_ACC_PRIVATE)); + zend_declare_property_null(php_http_querystring_class_entry, ZEND_STRL("queryArray"), ZEND_ACC_PRIVATE); - zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_BOOL"), PHP_HTTP_QUERYSTRING_TYPE_BOOL TSRMLS_CC); - zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_INT"), PHP_HTTP_QUERYSTRING_TYPE_INT TSRMLS_CC); - zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_FLOAT"), PHP_HTTP_QUERYSTRING_TYPE_FLOAT TSRMLS_CC); - zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_STRING"), PHP_HTTP_QUERYSTRING_TYPE_STRING TSRMLS_CC); - zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_ARRAY"), PHP_HTTP_QUERYSTRING_TYPE_ARRAY TSRMLS_CC); - zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_OBJECT"), PHP_HTTP_QUERYSTRING_TYPE_OBJECT TSRMLS_CC); + zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_BOOL"), PHP_HTTP_QUERYSTRING_TYPE_BOOL); + zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_INT"), PHP_HTTP_QUERYSTRING_TYPE_INT); + zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_FLOAT"), PHP_HTTP_QUERYSTRING_TYPE_FLOAT); + zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_STRING"), PHP_HTTP_QUERYSTRING_TYPE_STRING); + zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_ARRAY"), PHP_HTTP_QUERYSTRING_TYPE_ARRAY); + zend_declare_class_constant_long(php_http_querystring_class_entry, ZEND_STRL("TYPE_OBJECT"), PHP_HTTP_QUERYSTRING_TYPE_OBJECT); return SUCCESS; } diff --combined tests/client028.phpt index 0000000,4678bcc..27ab2b6 mode 000000,100644..100644 --- a/tests/client028.phpt +++ b/tests/client028.phpt @@@ -1,0 -1,141 +1,141 @@@ + --TEST-- + client curl user handler + --SKIPIF-- + + --FILE-- + [], + "W" => [] + ]; + private $R = []; + private $W = []; + private $timeout = 1000; + + function __construct(http\Client $client) { + $this->client = $client; + } + + function init(callable $run) { + $this->run = $run; + } + - function timer($timeout_ms) { ++ function timer(int $timeout_ms) { + echo "T"; + $this->timeout = $timeout_ms; + } + - function socket($socket, $action) { ++ function socket($socket, int $action) { + echo "S"; + + switch ($action) { + case self::POLL_NONE: + break; + case self::POLL_REMOVE: + if (false !== ($r = array_search($socket, $this->fds["R"], true))) { + echo "U"; + unset($this->fds["R"][$r]); + } + + if (false !== ($w = array_search($socket, $this->fds["W"], true))) { + echo "U"; + unset($this->fds["W"][$w]); + } + + break; + + default: + if ($action & self::POLL_IN) { + if (!in_array($socket, $this->fds["R"], true)) { + $this->fds["R"][] = $socket; + } + } + if ($action & self::POLL_OUT) { + if (!in_array($socket, $this->fds["W"], true)) { + $this->fds["W"][] = $socket; + } + } + break; + } + } + + function once() { + echo "O"; + + foreach ($this->W as $w) { + call_user_func($this->run, $this->client, $w, self::POLL_OUT); + } + foreach ($this->R as $r) { + call_user_func($this->run, $this->client, $r, self::POLL_IN); + } + return count($this->client); + } + - function wait($timeout_ms = null) { ++ function wait(int $timeout_ms = null) { + echo "W"; + + if ($timeout_ms === null) { + $timeout_ms = $this->timeout; + } + $ts = floor($timeout_ms / 1000); + $tu = ($timeout_ms % 1000) * 1000; + + extract($this->fds); + + if (($wfds = count($R) + count($W))) { + $nfds = stream_select($R, $W, $E, $ts, $tu); + } else { + $nfds = 0; + } + $this->R = (array) $R; + $this->W = (array) $W; + + if ($nfds === false) { + return false; + } + if (!$nfds) { + if (!$wfds) { + echo "S"; + time_nanosleep($ts, $tu*1000); + } + call_user_func($this->run, $this->client); + } + + return true; + } + + function send() { + $this->wait(); + $this->once(); + } + } + + + include "helper/server.inc"; + + server("proxy.inc", function($port) { + $client = new http\Client; + $client->configure([ + "use_eventloop" => new UserHandler($client) + ]); + $client->enqueue(new http\Client\Request("GET", "http://localhost:$port/"), function($r) { + var_dump($r->getResponseCode()); + }); + $client->send(); + }); + + ?> + ===DONE=== + --EXPECTREGEX-- + Test + T[WST]*(O[WST]*)+U+int\(200\) + ===DONE=== diff --combined tests/negotiate001.phpt index 0e553f5,b4757c0..ff1644b --- a/tests/negotiate001.phpt +++ b/tests/negotiate001.phpt @@@ -11,43 -11,48 +11,48 @@@ HTTP_ACCEPT_LANGUAGE=de-DE,de-AT;q=0.9, CONTENT TYPE CHARSET ENCODING LANGUAGE CUSTOM @@@ -55,7 -60,7 +60,7 @@@ DONE --EXPECT-- @@@ -103,7 -108,7 +108,7 @@@ LANGUAG de: Array ( - [de] => 0.99 + [de] => 0.97 [en] => 0.8 ) de-DE: Array @@@ -122,7 -127,7 +127,7 @@@ CUSTO a.b: Array ( [a.b] => 0.9 - [a.x] => 0.1 - [c.e] => 0.1 - [c.e] => 0.08 + [a.x] => 0.08 ++ [c.e] => 0.08 ) DONE