zend_string updates
authorMichael Wallner <mike@php.net>
Tue, 27 Jan 2015 04:19:47 +0000 (05:19 +0100)
committerMichael Wallner <mike@php.net>
Tue, 27 Jan 2015 04:19:47 +0000 (05:19 +0100)
config9.m4
php_http_api.h
php_http_client.c
php_http_client.h
php_http_client_curl.c
php_http_client_curl.h

index 3b1f7015554d1683469836e9548efd85b2eb0a08..9b88242153e9c47969731984ce7717a59c79ff79 100644 (file)
@@ -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
index 1a848b299486c555ae51aa70d5d1fddceaaab9ec..5aa96b7efcdb821bfdd46e9a5451a0c125e73cbd 100644 (file)
 
 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);
index f69a56fdde9c6842477e86d154b5abef8f5ed04c..66afb7cd7957fc6ca0ec9f760da1f9e7bd6eb9f1 100644 (file)
@@ -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());
index 308361f50193e5e4f7c6ea738bdbf507a73bba0b..5067f6c5832415cf905d704c6f99e5ef1308220f 100644 (file)
@@ -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 {
index 2e2f87aeaebe61bd8f2706731c23d143ec5b9868..fc06b38d25093f24cbec4595ff1ecbdeeb546e43 100644 (file)
@@ -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);
 
index c82a09cb94609c6a719acd6ca7437d2293b981fc..91286477d76514b4511ecd37d1fb9844a3aadf4a 100644 (file)
 
 #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 */