From e22fc5f78e8cfc1101c4548de0568add46a8694a Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 27 Jan 2015 05:19:47 +0100 Subject: [PATCH] zend_string updates --- config9.m4 | 3 +++ php_http_api.h | 7 +++++++ php_http_client.c | 28 ++++++++++------------------ php_http_client.h | 7 ++++--- php_http_client_curl.c | 35 ++++++++++++++++++++++------------- php_http_client_curl.h | 4 ++++ 6 files changed, 50 insertions(+), 34 deletions(-) diff --git a/config9.m4 b/config9.m4 index 3b1f701..9b88242 100644 --- a/config9.m4 +++ b/config9.m4 @@ -567,4 +567,7 @@ dnl ---- PHP_INSTALL_HEADERS(ext/http, $PHP_HTTP_HEADERS) AC_DEFINE([HAVE_HTTP], [1], [Have extended HTTP support]) + if $HTTP_HAVE_A_REQUEST_LIB; then + AC_DEFINE([PHP_HTTP_HAVE_CLIENT], [1], [Have HTTP client support]) + fi fi diff --git a/php_http_api.h b/php_http_api.h index 1a848b2..5aa96b7 100644 --- a/php_http_api.h +++ b/php_http_api.h @@ -109,6 +109,13 @@ 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); diff --git a/php_http_client.c b/php_http_client.c index f69a56f..66afb7c 100644 --- a/php_http_client.c +++ b/php_http_client.c @@ -27,16 +27,16 @@ static void php_http_client_driver_hash_dtor(zval *pData) ZEND_RESULT_CODE php_http_client_driver_add(php_http_client_driver_t *driver) { - return zend_hash_str_add_mem(&php_http_client_drivers, driver->name_str, driver->name_len, (void *) driver, sizeof(php_http_client_driver_t)) + return zend_hash_add_mem(&php_http_client_drivers, driver->driver_name, (void *) driver, sizeof(php_http_client_driver_t)) ? SUCCESS : FAILURE; } -php_http_client_driver_t *php_http_client_driver_get(const char *name_str, size_t name_len) +php_http_client_driver_t *php_http_client_driver_get(zend_string *name) { zval *ztmp; php_http_client_driver_t *tmp; - if (name_str && (tmp = zend_hash_str_find_ptr(&php_http_client_drivers, name_str, name_len))) { + if (name && (tmp = zend_hash_find_ptr(&php_http_client_drivers, name))) { return tmp; } if ((ztmp = zend_hash_get_current_data(&php_http_client_drivers))) { @@ -50,7 +50,7 @@ static int apply_driver_list(zval *p, void *arg) php_http_client_driver_t *d = Z_PTR_P(p); zval zname; - ZVAL_STRINGL(&zname, d->name_str, d->name_len); + ZVAL_STR(&zname, d->driver_name); zend_hash_next_index_insert(arg, &zname); return ZEND_HASH_APPLY_KEEP; @@ -479,21 +479,20 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_construct, 0, 0, 0) ZEND_END_ARG_INFO(); static PHP_METHOD(HttpClient, __construct) { - char *driver_str = NULL, *persistent_handle_str = NULL; - size_t driver_len = 0, persistent_handle_len = 0; + 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; - php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!", &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 (!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_str, driver_len))) { - php_http_throw(unexpected_val, "Failed to locate \"%s\" client request handler", driver_len ? driver_str : "default"); + 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; } @@ -501,19 +500,12 @@ static PHP_METHOD(HttpClient, __construct) 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 + lenof("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))) { + if ((pf = php_persistent_handle_concede(NULL, driver->client_name, persistent_handle_name, NULL, NULL))) { rf = php_resource_factory_init(NULL, php_persistent_handle_get_resource_factory_ops(), pf, (void (*)(void *)) php_persistent_handle_abandon); } - - efree(name_str); } obj = PHP_HTTP_OBJ(NULL, getThis()); diff --git a/php_http_client.h b/php_http_client.h index 308361f..5067f6c 100644 --- a/php_http_client.h +++ b/php_http_client.h @@ -62,13 +62,14 @@ typedef struct php_http_client_ops { } php_http_client_ops_t; typedef struct php_http_client_driver { - const char *name_str; - size_t name_len; + zend_string *driver_name; + zend_string *client_name; + zend_string *request_name; php_http_client_ops_t *client_ops; } php_http_client_driver_t; PHP_HTTP_API ZEND_RESULT_CODE php_http_client_driver_add(php_http_client_driver_t *driver); -PHP_HTTP_API php_http_client_driver_t *php_http_client_driver_get(const char *name_str, size_t name_len); +PHP_HTTP_API php_http_client_driver_t *php_http_client_driver_get(zend_string *name); typedef struct php_http_client_progress_state { struct { diff --git a/php_http_client_curl.c b/php_http_client_curl.c index 2e2f87a..fc06b38 100644 --- a/php_http_client_curl.c +++ b/php_http_client_curl.c @@ -1789,6 +1789,7 @@ static php_resource_factory_t *create_rf(php_http_url_t *url) { php_persistent_handle_factory_t *pf; php_resource_factory_t *rf = NULL; + zend_string *id; char *id_str = NULL; size_t id_len; @@ -1798,15 +1799,18 @@ static php_resource_factory_t *create_rf(php_http_url_t *url) } id_len = spprintf(&id_str, 0, "%s:%d", STR_PTR(url->host), url->port ? url->port : 80); + 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); - pf = php_persistent_handle_concede(NULL, ZEND_STRL("http\\Client\\Curl\\Request"), id_str, id_len, NULL, NULL); if (pf) { rf = php_resource_factory_init(NULL, php_persistent_handle_get_resource_factory_ops(), pf, (void (*)(void*)) php_persistent_handle_abandon); } else { rf = php_resource_factory_init(NULL, &php_http_curle_resource_factory_ops, NULL, NULL); } - efree(id_str); + zend_string_release(id); return rf; } @@ -2091,19 +2095,21 @@ php_http_client_ops_t *php_http_client_curl_get_ops(void) PHP_MINIT_FUNCTION(http_client_curl) { php_http_options_t *options; - php_http_client_driver_t driver = { - ZEND_STRL("curl"), - &php_http_client_curl_ops - }; + php_http_client_driver_t driver; - if (SUCCESS != php_http_client_driver_add(&driver)) { - return FAILURE; - } + 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)) { + 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)) { + if (SUCCESS != php_persistent_handle_provide(PHP_HTTP_G->client.curl.driver.request_name, &php_http_curle_resource_factory_ops, NULL, NULL)) { return FAILURE; } @@ -2185,8 +2191,11 @@ PHP_MINIT_FUNCTION(http_client_curl) PHP_MSHUTDOWN_FUNCTION(http_client_curl) { - php_persistent_handle_cleanup(ZEND_STRL("http\\Client\\Curl"), NULL, 0); - php_persistent_handle_cleanup(ZEND_STRL("http\\Client\\Curl\\Request"), NULL, 0); + 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); diff --git a/php_http_client_curl.h b/php_http_client_curl.h index c82a09c..9128647 100644 --- a/php_http_client_curl.h +++ b/php_http_client_curl.h @@ -15,6 +15,10 @@ #if PHP_HTTP_HAVE_CURL +struct php_http_client_curl_globals { + php_http_client_driver_t driver; +}; + PHP_MINIT_FUNCTION(http_client_curl); PHP_MSHUTDOWN_FUNCTION(http_client_curl); #endif /* PHP_HTTP_HAVE_CURL */ -- 2.30.2