From 7ba90c6d9d6843549fb9227d1a7aa05033e60772 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Fri, 7 Dec 2012 17:08:36 +0000 Subject: [PATCH] refactored client options --- config9.m4 | 2 + php_http.c | 4 + php_http_api.h | 3 +- php_http_client.c | 15 +- php_http_curl.c | 1 + php_http_curl_client.c | 1320 +++++++++++++++++++---------------- php_http_curl_client.h | 3 + php_http_curl_client_pool.c | 11 +- php_http_curl_client_pool.h | 1 + php_http_misc.c | 24 + php_http_misc.h | 2 + php_http_options.c | 126 ++++ php_http_options.h | 65 ++ 13 files changed, 963 insertions(+), 614 deletions(-) create mode 100644 php_http_options.c create mode 100644 php_http_options.h diff --git a/config9.m4 b/config9.m4 index f9f92c6..e6924ac 100644 --- a/config9.m4 +++ b/config9.m4 @@ -436,6 +436,7 @@ dnl ---- php_http_misc.c \ php_http_negotiate.c \ php_http_object.c \ + php_http_options.c \ php_http_params.c \ php_http_persistent_handle.c \ php_http_property_proxy.c \ @@ -486,6 +487,7 @@ dnl ---- php_http_misc.h \ php_http_negotiate.h \ php_http_object.h \ + php_http_options.h \ php_http_params.h \ php_http_persistent_handle.h \ php_http_property_proxy.h \ diff --git a/php_http.c b/php_http.c index 4eb0f5e..06ec915 100644 --- a/php_http.c +++ b/php_http.c @@ -177,6 +177,7 @@ PHP_MSHUTDOWN_FUNCTION(http) if (0 || SUCCESS != PHP_MSHUTDOWN_CALL(http_message) #if PHP_HTTP_HAVE_CURL + || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl_client) || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl) #endif || SUCCESS != PHP_MSHUTDOWN_CALL(http_persistent_handle) @@ -205,6 +206,9 @@ PHP_RINIT_FUNCTION(http) PHP_RSHUTDOWN_FUNCTION(http) { if (0 +#if PHP_HTTP_HAVE_CURL && PHP_HTTP_HAVE_EVENT + || SUCCESS != PHP_RSHUTDOWN_CALL(http_curl_client_pool) +#endif || SUCCESS != PHP_RSHUTDOWN_CALL(http_env) ) { return FAILURE; diff --git a/php_http_api.h b/php_http_api.h index fbfea28..ca1e2f7 100644 --- a/php_http_api.h +++ b/php_http_api.h @@ -74,10 +74,10 @@ typedef int STATUS; #include "php_http_strlist.h" #include "php_http_misc.h" #include "php_http_resource_factory.h" +#include "php_http_options.h" #include "php_http.h" #include "php_http_cookie.h" -#include "php_http_curl.h" #include "php_http_encoding.h" #include "php_http_env.h" #include "php_http_env_request.h" @@ -99,6 +99,7 @@ typedef int STATUS; #include "php_http_querystring.h" #include "php_http_client_interface.h" #include "php_http_client.h" +#include "php_http_curl.h" #include "php_http_client_request.h" #include "php_http_client_response.h" #include "php_http_curl_client.h" diff --git a/php_http_client.c b/php_http_client.c index 0de62c9..80de977 100644 --- a/php_http_client.c +++ b/php_http_client.c @@ -13,6 +13,7 @@ #include "php_http_api.h" #include +#include PHP_HTTP_API php_http_client_t *php_http_client_init(php_http_client_t *h, php_http_client_ops_t *ops, php_http_resource_factory_t *rf, void *init_arg TSRMLS_DC) { @@ -352,6 +353,7 @@ STATUS php_http_client_object_handle_request(zval *zclient, zval **zreq TSRMLS_D php_http_client_object_t *obj = zend_object_store_get_object(zclient TSRMLS_CC); php_http_client_progress_t *progress; zval *zoptions; + HashTable options; /* do we have a valid request? */ if (*zreq) { @@ -373,12 +375,19 @@ STATUS php_http_client_object_handle_request(zval *zclient, zval **zreq TSRMLS_D /* reset transfer info */ zend_update_property_null(php_http_client_class_entry, zclient, ZEND_STRL("transferInfo") TSRMLS_CC); + /* set client options */ + zend_hash_init(&options, 0, NULL, ZVAL_PTR_DTOR, 0); zoptions = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("options"), 0 TSRMLS_CC); - php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_SETTINGS, Z_ARRVAL_P(zoptions)); - /* set request options */ + if (Z_TYPE_P(zoptions) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(zoptions))) { + php_array_merge(&options, Z_ARRVAL_P(zoptions), 1 TSRMLS_CC); + } zoptions = zend_read_property(php_http_client_request_get_class_entry(), *zreq, ZEND_STRL("options"), 0 TSRMLS_CC); - php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_SETTINGS, Z_ARRVAL_P(zoptions)); + if (Z_TYPE_P(zoptions) == IS_ARRAY && zend_hash_num_elements(Z_ARRVAL_P(zoptions))) { + php_array_merge(&options, Z_ARRVAL_P(zoptions), 1 TSRMLS_CC); + } + php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_SETTINGS, &options); + zend_hash_destroy(&options); /* set progress callback */ if (SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, &progress)) { diff --git a/php_http_curl.c b/php_http_curl.c index f6641dc..e9153b1 100644 --- a/php_http_curl.c +++ b/php_http_curl.c @@ -88,6 +88,7 @@ static struct gcry_thread_cbs php_http_gnutls_tsl = { }; #endif + PHP_MINIT_FUNCTION(http_curl) { php_http_client_factory_driver_t driver = { diff --git a/php_http_curl_client.c b/php_http_curl_client.c index bf04651..b2beba6 100644 --- a/php_http_curl_client.c +++ b/php_http_curl_client.c @@ -10,38 +10,10 @@ +--------------------------------------------------------------------+ */ -#include "php_http_api.h" - -#define PHP_HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \ - { \ - char *K = #OPTION; \ - PHP_HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \ - } -#define PHP_HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \ - if (!strcasecmp(key.str, keyname)) { \ - zval *copy = cache_option(&curl->options.cache, keyname, strlen(keyname)+1, 0, php_http_ztyp(IS_STRING, *param)); \ - if (obdc) { \ - if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(copy) TSRMLS_CC)) { \ - return FAILURE; \ - } \ - } \ - curl_easy_setopt(ch, optname, Z_STRVAL_P(copy)); \ - zval_ptr_dtor(©); \ - continue; \ - } -#define PHP_HTTP_CURL_OPT_LONG(OPTION, ldiff) \ - { \ - char *K = #OPTION; \ - PHP_HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \ - } -#define PHP_HTTP_CURL_OPT_LONG_EX(keyname, optname) \ - if (!strcasecmp(key.str, keyname)) { \ - zval *copy = php_http_ztyp(IS_LONG, *param); \ - curl_easy_setopt(ch, optname, Z_LVAL_P(copy)); \ - zval_ptr_dtor(©); \ - continue; \ - } +#include +#include +#include "php_http_api.h" /* resource_factory ops */ @@ -211,608 +183,827 @@ static int php_http_curl_client_dummy_callback(char *data, size_t n, size_t l, v return n*l; } -static inline zval *cache_option(HashTable *cache, char *key, size_t keylen, ulong h, zval *opt) +static STATUS get_info(CURL *ch, HashTable *info) { - Z_ADDREF_P(opt); + char *c; + long l; + double d; + struct curl_slist *s, *p; + zval *subarray, array; + INIT_PZVAL_ARRAY(&array, info); - if (h) { - zend_hash_quick_update(cache, key, keylen, h, &opt, sizeof(zval *), NULL); - } else { - zend_hash_update(cache, key, keylen, &opt, sizeof(zval *), NULL); + /* 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); } - - return opt; -} - -static inline zval *get_option(HashTable *cache, HashTable *options, char *key, size_t keylen, int type) -{ - if (options) { - zval **zoption; - ulong h = zend_hash_func(key, keylen); - - if (SUCCESS == zend_hash_quick_find(options, key, keylen, h, (void *) &zoption)) { - zval *option = php_http_ztyp(type, *zoption); - - if (cache) { - zval *cached = cache_option(cache, key, keylen, h, option); - - zval_ptr_dtor(&option); - return cached; - } - return option; - } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) { + add_assoc_long_ex(&array, "response_code", sizeof("response_code"), l); } - - return NULL; -} - -static STATUS set_options(php_http_client_t *h, HashTable *options) -{ - zval *zoption; - php_http_curl_client_t *curl = h->ctx; - CURL *ch = curl->handle; - TSRMLS_FETCH_FROM_CTX(h->ts); - - /* proxy */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyhost"), IS_STRING))) { - curl_easy_setopt(ch, CURLOPT_PROXY, Z_STRVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) { + add_assoc_double_ex(&array, "total_time", sizeof("total_time"), d); } - /* type */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytype"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_PROXYTYPE, Z_LVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) { + add_assoc_double_ex(&array, "namelookup_time", sizeof("namelookup_time"), d); } - /* port */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyport"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_PROXYPORT, Z_LVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) { + add_assoc_double_ex(&array, "connect_time", sizeof("connect_time"), d); } - /* user:pass */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauth"), IS_STRING)) && Z_STRLEN_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) { + add_assoc_double_ex(&array, "pretransfer_time", sizeof("pretransfer_time"), d); } - /* auth method */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxyauthtype"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_PROXYAUTH, Z_LVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) { + add_assoc_double_ex(&array, "size_upload", sizeof("size_upload"), d); } - /* tunnel */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("proxytunnel"), IS_BOOL)) && Z_BVAL_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 1L); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) { + add_assoc_double_ex(&array, "size_download", sizeof("size_download"), d); } -#if PHP_HTTP_CURL_VERSION(7,19,4) - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("noproxy"), IS_STRING))) { - curl_easy_setopt(ch, CURLOPT_NOPROXY, Z_STRVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) { + add_assoc_double_ex(&array, "speed_download", sizeof("speed_download"), d); } -#endif - - /* dns */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_cache_timeout"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, Z_LVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) { + add_assoc_double_ex(&array, "speed_upload", sizeof("speed_upload"), d); } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ipresolve"), IS_LONG)) && Z_LVAL_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_IPRESOLVE, Z_LVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) { + add_assoc_long_ex(&array, "header_size", sizeof("header_size"), l); } -#if PHP_HTTP_CURL_VERSION(7,21,3) - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resolve"), IS_ARRAY))) { - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - HashPosition pos; - zval **data; - - FOREACH_KEYVAL(pos, zoption, 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); - } - - curl_easy_setopt(ch, CURLOPT_RESOLVE, curl->options.resolve); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) { + add_assoc_long_ex(&array, "request_size", sizeof("request_size"), l); } -#endif -#if PHP_HTTP_CURL_VERSION(7,24,0) - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("dns_servers"), IS_STRING)) && Z_STRLEN_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, Z_STRVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) { + add_assoc_long_ex(&array, "ssl_verifyresult", sizeof("ssl_verifyresult"), l); } -#endif - - /* limits */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_limit"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, Z_LVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) { + add_assoc_long_ex(&array, "filetime", sizeof("filetime"), l); } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("low_speed_time"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, Z_LVAL_P(zoption)); + 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); } - /* LSF weirdance - if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_send_speed"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption)); + 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); } - if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_recv_speed"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) { + add_assoc_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d); } - */ - /* crashes - if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("maxconnects"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption)); - } */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("fresh_connect"), IS_BOOL)) && Z_BVAL_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 1L); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) { + add_assoc_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) { + add_assoc_double_ex(&array, "redirect_time", sizeof("redirect_time"), d); } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("forbid_reuse"), IS_BOOL)) && Z_BVAL_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 1L); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) { + add_assoc_long_ex(&array, "redirect_count", sizeof("redirect_count"), l); } - - /* outgoing interface */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("interface"), IS_STRING))) { - curl_easy_setopt(ch, CURLOPT_INTERFACE, Z_STRVAL_P(zoption)); - } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("portrange"), IS_ARRAY))) { - zval **prs, **pre; - - zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption)); - if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) { - zend_hash_move_forward(Z_ARRVAL_P(zoption)); - if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) { - zval *prs_cpy = php_http_ztyp(IS_LONG, *prs); - zval *pre_cpy = php_http_ztyp(IS_LONG, *pre); - - if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) { - curl_easy_setopt(ch, CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy))); - curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L); - } - zval_ptr_dtor(&prs_cpy); - zval_ptr_dtor(&pre_cpy); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) { + add_assoc_long_ex(&array, "connect_code", sizeof("connect_code"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) { + add_assoc_long_ex(&array, "httpauth_avail", sizeof("httpauth_avail"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) { + add_assoc_long_ex(&array, "proxyauth_avail", sizeof("proxyauth_avail"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) { + add_assoc_long_ex(&array, "os_errno", sizeof("os_errno"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) { + add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l); + } + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) { + MAKE_STD_ZVAL(subarray); + array_init(subarray); + for (p = s; p; p = p->next) { + if (p->data) { + add_next_index_string(subarray, p->data, 1); } } + add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray); + curl_slist_free_all(s); } - - /* another port */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("port"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_PORT, Z_LVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_COOKIELIST, &s)) { + MAKE_STD_ZVAL(subarray); + array_init(subarray); + for (p = s; p; p = p->next) { + if (p->data) { + add_next_index_string(subarray, p->data, 1); + } + } + add_assoc_zval_ex(&array, "cookies", sizeof("cookies"), subarray); + 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); } - - /* RFC4007 zone_id */ #if PHP_HTTP_CURL_VERSION(7,19,0) - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("address_scope"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, Z_LVAL_P(zoption)); + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) { + add_assoc_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1); } #endif - - /* auth */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauth"), IS_STRING)) && Z_STRLEN_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_USERPWD, Z_STRVAL_P(zoption)); +#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); + } +#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); } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("httpauthtype"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_HTTPAUTH, Z_LVAL_P(zoption)); +#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); } - - /* redirects, defaults to 0 */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("redirect"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1L : 0L); - curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(zoption)); - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("unrestrictedauth"), IS_BOOL))) { - curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption)); - } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("postredir"), IS_BOOL))) { -#if PHP_HTTP_CURL_VERSION(7,19,1) - curl_easy_setopt(ch, CURLOPT_POSTREDIR, Z_BVAL_P(zoption) ? 1L : 0L); -#else - curl_easy_setopt(ch, CURLOPT_POST301, Z_BVAL_P(zoption) ? 1L : 0L); #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); } - - /* retries, defaults to 0 */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrycount"), IS_LONG))) { - curl->options.retry.count = Z_LVAL_P(zoption); - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("retrydelay"), IS_DOUBLE))) { - curl->options.retry.delay = Z_DVAL_P(zoption); - } +#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); } +#endif - /* referer */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("referer"), IS_STRING)) && Z_STRLEN_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_REFERER, Z_STRVAL_P(zoption)); - } + /* END::CURLINFO */ - /* useragent, default "PECL::HTTP/version (PHP/version)" */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("useragent"), IS_STRING))) { - /* allow to send no user agent, not even default one */ - if (Z_STRLEN_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_USERAGENT, Z_STRVAL_P(zoption)); - } else { - curl_easy_setopt(ch, CURLOPT_USERAGENT, NULL); - } - } +#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) + { + int i; + zval *ci_array; + struct curl_certinfo *ci; + char *colon, *keyname; - /* resume */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("resume"), IS_LONG)) && (Z_LVAL_P(zoption) > 0)) { - curl->options.range_request = 1; - curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(zoption)); - } - /* or range of kind array(array(0,499), array(100,1499)) */ - else if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("range"), IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) { - HashPosition pos1, pos2; - zval **rr, **rb, **re; - php_http_buffer_t rs; + if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) { + MAKE_STD_ZVAL(ci_array); + array_init(ci_array); - php_http_buffer_init(&rs); - FOREACH_VAL(pos1, zoption, rr) { - if (Z_TYPE_PP(rr) == IS_ARRAY) { - zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2); - if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) { - zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2); - if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) { - 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(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel)); - } - zval_ptr_dtor(&rbl); - zval_ptr_dtor(&rel); + for (i = 0; i < ci->num_of_certs; ++i) { + s = ci->certinfo[i]; + + MAKE_STD_ZVAL(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); + efree(keyname); + } else { + add_next_index_string(subarray, p->data, 1); } } } + add_next_index_zval(ci_array, subarray); } + add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array); } + } +#endif + add_assoc_string_ex(&array, "error", sizeof("error"), get_storage(ch)->errorbuffer, 1); - if (PHP_HTTP_BUFFER_LEN(&rs)) { - zval *cached_range; + return SUCCESS; +} - curl->options.range_request = 1; - /* ditch last comma */ - PHP_HTTP_BUFFER_VAL(&rs)[PHP_HTTP_BUFFER_LEN(&rs)-- -1] = '\0'; - /* cache string */ - MAKE_STD_ZVAL(cached_range); - ZVAL_STRINGL(cached_range, PHP_HTTP_BUFFER_VAL(&rs), PHP_HTTP_BUFFER_LEN(&rs), 0); - curl_easy_setopt(ch, CURLOPT_RANGE, Z_STRVAL_P(cache_option(&curl->options.cache, ZEND_STRS("range"), 0, cached_range))); - zval_ptr_dtor(&cached_range); - } - } +/* curl client options */ - /* etag */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("etag"), IS_STRING)) && Z_STRLEN_P(zoption)) { - zend_bool is_quoted = !((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"')); - php_http_buffer_t header; +static php_http_options_t php_http_curl_client_options; - 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(zoption)); - php_http_buffer_fix(&header); - curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header)); - php_http_buffer_dtor(&header); - } - /* compression */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("compress"), IS_BOOL)) && Z_LVAL_P(zoption)) { - curl->options.headers = curl_slist_append(curl->options.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5"); +#define PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN 0x0001 +#define PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR 0x0002 +#define PHP_HTTP_CURL_CLIENT_OPTION_TRANSFORM_MS 0x0004 + +static STATUS php_http_curl_client_option_set_ssl_verifyhost(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + CURL *ch = curl->handle; + + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, Z_BVAL_P(val) ? 2 : 0)) { + return FAILURE; } - curl_easy_setopt(ch, CURLOPT_HTTPHEADER, curl->options.headers); + return SUCCESS; +} - /* lastmodified */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("lastmodified"), IS_LONG))) { - if (Z_LVAL_P(zoption)) { - if (Z_LVAL_P(zoption) > 0) { - curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(zoption)); - } else { - curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) PHP_HTTP_G->env.request.time + Z_LVAL_P(zoption)); - } - curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (curl->options.range_request ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE)); - } else { - curl_easy_setopt(ch, CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE); +static STATUS php_http_curl_client_option_set_cookiestore(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + CURL *ch = curl->handle; + + if (val) { + php_http_curl_client_storage_t *storage = get_storage(curl->handle); + + if (storage->cookiestore) { + pefree(storage->cookiestore, 1); + } + storage->cookiestore = pestrndup(Z_STRVAL_P(val), Z_STRLEN_P(val), 1); + if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore) + || CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore) + ) { + return FAILURE; } } + return SUCCESS; +} + +static STATUS php_http_curl_client_option_set_cookies(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + CURL *ch = curl->handle; + TSRMLS_FETCH_FROM_CTX(h->ts); - /* cookies, array('name' => 'value') */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookies"), IS_ARRAY))) { - if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) { - zval *urlenc_cookies = NULL; - /* check whether cookies should not be urlencoded; default is to urlencode them */ - if ((!(urlenc_cookies = get_option(&curl->options.cache, options, ZEND_STRS("encodecookies"), IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) { - if (SUCCESS == php_http_url_encode_hash_ex(HASH_OF(zoption), &curl->options.cookies, ZEND_STRL(";"), ZEND_STRL("="), NULL, 0 TSRMLS_CC)) { - php_http_buffer_fix(&curl->options.cookies); - curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data); + if (val && Z_TYPE_P(val) != IS_NULL) { + 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)) { + php_http_buffer_fix(&curl->options.cookies); + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data)) { + return FAILURE; } } else { - HashPosition pos; - php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0); - zval **cookie_val; + return FAILURE; + } + } else { + HashPosition pos; + php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0); + zval **cookie_val; - FOREACH_KEYVAL(pos, zoption, cookie_key, cookie_val) { - zval *val = php_http_ztyp(IS_STRING, *cookie_val); + FOREACH_KEYVAL(pos, val, cookie_key, cookie_val) { + zval *zv = php_http_ztyp(IS_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(val)); - php_http_array_hashkey_stringfree(&cookie_key); + 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); - zval_ptr_dtor(&val); - } + zval_ptr_dtor(&zv); + } - php_http_buffer_fix(&curl->options.cookies); - if (PHP_HTTP_BUFFER_LEN(&curl->options.cookies)) { - curl_easy_setopt(ch, CURLOPT_COOKIE, PHP_HTTP_BUFFER_VAL(&curl->options.cookies)); + php_http_buffer_fix(&curl->options.cookies); + if (PHP_HTTP_BUFFER_LEN(&curl->options.cookies)) { + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, PHP_HTTP_BUFFER_VAL(&curl->options.cookies))) { + return FAILURE; } } } } + return SUCCESS; +} - /* don't load session cookies from cookiestore */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiesession"), IS_BOOL)) && Z_BVAL_P(zoption)) { - curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 1L); - } +static STATUS php_http_curl_client_option_set_encodecookies(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; - /* cookiestore, read initial cookies from that file and store cookies back into that file */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("cookiestore"), IS_STRING))) { - php_http_curl_client_storage_t *storage = get_storage(curl->handle); + curl->options.encode_cookies = Z_BVAL_P(val); + return SUCCESS; +} + +static STATUS php_http_curl_client_option_set_lastmodified(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + CURL *ch = curl->handle; + TSRMLS_FETCH_FROM_CTX(h->ts); - if (Z_STRLEN_P(zoption)) { - if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(zoption) TSRMLS_CC)) { + if (Z_LVAL_P(val)) { + if (Z_LVAL_P(val) > 0) { + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(val))) { + return FAILURE; + } + } else { + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) PHP_HTTP_G->env.request.time + Z_LVAL_P(val))) { return FAILURE; } } - if (storage->cookiestore) { - pefree(storage->cookiestore, 1); + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (curl->options.range_request ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE))) { + return FAILURE; + } + } else { + if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0) + || CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0) + ) { + return FAILURE; } - storage->cookiestore = pestrndup(Z_STRVAL_P(zoption), Z_STRLEN_P(zoption), 1); - curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore); - curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore); } + return SUCCESS; +} - /* maxfilesize */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("maxfilesize"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption)); - } +static STATUS php_http_curl_client_option_set_compress(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; - /* http protocol */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("protocol"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption)); + if (Z_BVAL_P(val)) { + curl->options.headers = curl_slist_append(curl->options.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5"); } + return SUCCESS; +} - /* timeout, defaults to 0 */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("timeout"), IS_DOUBLE))) { - curl_easy_setopt(ch, CURLOPT_TIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000)); - } - /* connecttimeout, defaults to 0 */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("connecttimeout"), IS_DOUBLE))) { - curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000)); +static STATUS php_http_curl_client_option_set_etag(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + php_http_buffer_t header; + 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)); + php_http_buffer_fix(&header); + curl->options.headers = curl_slist_append(curl->options.headers, PHP_HTTP_BUFFER_VAL(&header)); + php_http_buffer_dtor(&header); + return SUCCESS; +} + +static STATUS php_http_curl_client_option_set_range(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + CURL *ch = curl->handle; + TSRMLS_FETCH_FROM_CTX(h->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(*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_ptr_dtor(&rbl); + zval_ptr_dtor(&rel); + } + + } + } + } + + if (PHP_HTTP_BUFFER_LEN(&curl->options.ranges)) { + curl->options.range_request = 1; + /* ditch last comma */ + PHP_HTTP_BUFFER_VAL(&curl->options.ranges)[PHP_HTTP_BUFFER_LEN(&curl->options.ranges)-- -1] = '\0'; + } } - -#if PHP_HTTP_CURL_VERSION(7,25,0) - /* tcp */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("tcp_keepalive"), IS_BOOL))) { - curl_easy_setopt(ch, CURLOPT_TCP_KEEPALIVE, (long)Z_BVAL_P(zoption)); + + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RANGE, PHP_HTTP_BUFFER_VAL(&curl->options.ranges))) { + return FAILURE; } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("tcp_keepidle"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_TCP_KEEPIDLE, Z_LVAL_P(zoption)); + return SUCCESS; +} + +static STATUS php_http_curl_client_option_set_resume(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + CURL *ch = curl->handle; + + if (Z_LVAL_P(val) > 0) { + curl->options.range_request = 1; } - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("tcp_keepintvl"), IS_LONG))) { - curl_easy_setopt(ch, CURLOPT_TCP_KEEPINTVL, Z_LVAL_P(zoption)); + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(val))) { + return FAILURE; } -#endif + return SUCCESS; +} - /* ssl */ - if ((zoption = get_option(&curl->options.cache, options, ZEND_STRS("ssl"), IS_ARRAY))) { - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - zval **param; - HashPosition pos; +static STATUS php_http_curl_client_option_set_retrydelay(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; - FOREACH_KEYVAL(pos, zoption, key, param) { - if (key.type == HASH_KEY_IS_STRING) { - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0); + curl->options.retry.delay = Z_DVAL_P(val); + return SUCCESS; +} - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0); +static STATUS php_http_curl_client_option_set_retrycount(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0); - PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0); + curl->options.retry.count = Z_LVAL_P(val); + return SUCCESS; +} - PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1); - PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0); +static STATUS php_http_curl_client_option_set_redirect(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + CURL *ch = curl->handle; - PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1); - PHP_HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1); -#if PHP_HTTP_CURL_VERSION(7,19,0) - PHP_HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT, -3, 1); - #if defined(PHP_HTTP_HAVE_OPENSSL) - PHP_HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE, -3, 1); - #endif -#endif -#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) - PHP_HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO, -3); -#endif + if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(val) ? 1L : 0L) + || CURLE_OK != curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(val)) + ) { + return FAILURE; + } + return SUCCESS; +} + +static STATUS php_http_curl_client_option_set_portrange(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + CURL *ch = curl->handle; + long localport = 0, localportrange = 0; + TSRMLS_FETCH_FROM_CTX(h->ts); + + if (val && Z_TYPE_P(val) != IS_NULL) { + zval **z_port_start, *zps_copy = NULL, **z_port_end, *zpe_copy = NULL; + + switch (php_http_array_list(val TSRMLS_CC, 2, &z_port_start, &z_port_end)) { + 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; + /* 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); } + break; + default: + break; } } + if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_LOCALPORT, localport) + || CURLE_OK != curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, localportrange) + ) { + return FAILURE; + } return SUCCESS; } -static STATUS get_info(CURL *ch, HashTable *info) +static STATUS php_http_curl_client_option_set_resolve(php_http_option_t *opt, zval *val, void *userdata) { - char *c; - long l; - double d; - struct curl_slist *s, *p; - zval *subarray, array; - INIT_PZVAL_ARRAY(&array, info); + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + CURL *ch = curl->handle; + TSRMLS_FETCH_FROM_CTX(h->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); + } + + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESOLVE, curl->options.resolve)) { + return FAILURE; + } + } else { + if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESOLVE, NULL)) { + return FAILURE; + } + } + return SUCCESS; +} + +static void php_http_curl_client_options_init(php_http_options_t *registry TSRMLS_DC) +{ + php_http_option_t *opt; - /* 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); + /* proxy */ + if ((opt = php_http_option_register(registry, ZEND_STRL("proxyhost"), CURLOPT_PROXY, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) { - add_assoc_long_ex(&array, "response_code", sizeof("response_code"), l); + php_http_option_register(registry, ZEND_STRL("proxytype"), CURLOPT_PROXYTYPE, IS_LONG); + php_http_option_register(registry, ZEND_STRL("proxyport"), CURLOPT_PROXYPORT, IS_LONG); + if ((opt = php_http_option_register(registry, ZEND_STRL("proxyauth"), CURLOPT_PROXYUSERPWD, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) { - add_assoc_double_ex(&array, "total_time", sizeof("total_time"), d); + php_http_option_register(registry, ZEND_STRL("proxyauthtype"), CURLOPT_PROXYAUTH, IS_LONG); + 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 + + /* dns */ + if ((opt = php_http_option_register(registry, ZEND_STRL("dns_cache_timeout"), CURLOPT_DNS_CACHE_TIMEOUT, IS_LONG))) { + Z_LVAL(opt->defval) = 60; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) { - add_assoc_double_ex(&array, "namelookup_time", sizeof("namelookup_time"), d); + php_http_option_register(registry, ZEND_STRL("ipresolve"), CURLOPT_IPRESOLVE, IS_LONG); +#if PHP_HTTP_CURL_VERSION(7,21,3) + if ((opt = php_http_option_register(registry, ZEND_STRL("resolve"), CURLOPT_RESOLVE, IS_ARRAY))) { + opt->setter = php_http_curl_client_option_set_resolve; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) { - add_assoc_double_ex(&array, "connect_time", sizeof("connect_time"), d); +#endif +#if PHP_HTTP_CURL_VERSION(7,24,0) + if ((opt = php_http_option_register(registry, ZEND_STRL("dns_servers"), CURLOPT_DNS_SERVERS, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) { - add_assoc_double_ex(&array, "pretransfer_time", sizeof("pretransfer_time"), d); +#endif + + /* limits */ + php_http_option_register(registry, ZEND_STRL("low_speed_limit"), CURLOPT_LOW_SPEED_LIMIT, IS_LONG); + php_http_option_register(registry, ZEND_STRL("low_speed_time"), CURLOPT_LOW_SPEED_TIME, IS_LONG); + + /* LSF weirdance + php_http_option_register(registry, ZEND_STRL("max_send_speed"), CURLOPT_MAX_SEND_SPEED_LARGE, IS_LONG); + php_http_option_register(registry, ZEND_STRL("max_recv_speed"), CURLOPT_MAX_RECV_SPEED_LARGE, IS_LONG); + */ + + /* connection handling */ + /* crashes + if ((opt = php_http_option_register(registry, ZEND_STRL("maxconnects"), CURLOPT_MAXCONNECTS, IS_LONG))) { + Z_LVAL(opt->defval) = 5; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) { - add_assoc_double_ex(&array, "size_upload", sizeof("size_upload"), d); + */ + 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); + if ((opt = php_http_option_register(registry, ZEND_STRL("portrange"), CURLOPT_LOCALPORT, IS_ARRAY))) { + opt->setter = php_http_curl_client_option_set_portrange; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) { - add_assoc_double_ex(&array, "size_download", sizeof("size_download"), d); + + /* another endpoint port */ + php_http_option_register(registry, ZEND_STRL("port"), CURLOPT_PORT, IS_LONG); + + /* RFC4007 zone_id */ +#if PHP_HTTP_CURL_VERSION(7,19,0) + php_http_option_register(registry, ZEND_STRL("address_scope"), CURLOPT_ADDRESS_SCOPE, IS_LONG); +#endif + + /* auth */ + if ((opt = php_http_option_register(registry, ZEND_STRL("httpauth"), CURLOPT_USERPWD, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) { - add_assoc_double_ex(&array, "speed_download", sizeof("speed_download"), d); + php_http_option_register(registry, ZEND_STRL("httpauthtype"), CURLOPT_HTTPAUTH, IS_LONG); + + /* redirects */ + if ((opt = php_http_option_register(registry, ZEND_STRL("redirect"), CURLOPT_FOLLOWLOCATION, IS_LONG))) { + opt->setter = php_http_curl_client_option_set_redirect; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) { - add_assoc_double_ex(&array, "speed_upload", sizeof("speed_upload"), d); + php_http_option_register(registry, ZEND_STRL("unrestrictedauth"), CURLOPT_UNRESTRICTED_AUTH, IS_BOOL); +#if PHP_HTTP_CURL_VERSION(7,19,1) + php_http_option_register(registry, ZEND_STRL("postredir"), CURLOPT_POSTREDIR, IS_BOOL); +#else + php_http_option_register(registry, ZEND_STRL("postredir"), CURLOPT_POST301, IS_BOOL); +#endif + + /* retries */ + if ((opt = php_http_option_register(registry, ZEND_STRL("retrycount"), 0, IS_LONG))) { + opt->setter = php_http_curl_client_option_set_retrycount; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) { - add_assoc_long_ex(&array, "header_size", sizeof("header_size"), l); + if ((opt = php_http_option_register(registry, ZEND_STRL("retrydelay"), 0, IS_DOUBLE))) { + opt->setter = php_http_curl_client_option_set_retrydelay; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) { - add_assoc_long_ex(&array, "request_size", sizeof("request_size"), l); + + /* referer */ + if ((opt = php_http_option_register(registry, ZEND_STRL("referer"), CURLOPT_REFERER, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) { - add_assoc_long_ex(&array, "ssl_verifyresult", sizeof("ssl_verifyresult"), l); + if ((opt = php_http_option_register(registry, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER, IS_BOOL))) { + ZVAL_BOOL(&opt->defval, 1); } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) { - add_assoc_long_ex(&array, "filetime", sizeof("filetime"), l); + + /* 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, "PECL::HTTP/" PHP_HTTP_EXT_VERSION " (PHP/" PHP_VERSION ")", 0); } - 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); + + /* resume */ + if ((opt = php_http_option_register(registry, ZEND_STRL("resume"), CURLOPT_RESUME_FROM, IS_LONG))) { + opt->setter = php_http_curl_client_option_set_resume; } - 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); + /* ranges */ + if ((opt = php_http_option_register(registry, ZEND_STRL("range"), CURLOPT_RANGE, IS_ARRAY))) { + opt->setter = php_http_curl_client_option_set_range; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) { - add_assoc_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d); + + /* etag */ + if ((opt = php_http_option_register(registry, ZEND_STRL("etag"), 0, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; + opt->setter = php_http_curl_client_option_set_etag; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) { - add_assoc_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1); + + /* compression */ + if ((opt = php_http_option_register(registry, ZEND_STRL("compress"), 0, IS_BOOL))) { + opt->setter = php_http_curl_client_option_set_compress; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) { - add_assoc_double_ex(&array, "redirect_time", sizeof("redirect_time"), d); + + /* lastmodified */ + if ((opt = php_http_option_register(registry, ZEND_STRL("lastmodified"), 0, IS_LONG))) { + opt->setter = php_http_curl_client_option_set_lastmodified; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) { - add_assoc_long_ex(&array, "redirect_count", sizeof("redirect_count"), l); + + /* cookies */ + if ((opt = php_http_option_register(registry, ZEND_STRL("encodecookies"), 0, IS_BOOL))) { + opt->setter = php_http_curl_client_option_set_encodecookies; + ZVAL_BOOL(&opt->defval, 1); } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) { - add_assoc_long_ex(&array, "connect_code", sizeof("connect_code"), l); + if ((opt = php_http_option_register(registry, ZEND_STRL("cookies"), 0, IS_ARRAY))) { + opt->setter = php_http_curl_client_option_set_cookies; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) { - add_assoc_long_ex(&array, "httpauth_avail", sizeof("httpauth_avail"), l); + + /* cookiesession, don't load session cookies from cookiestore */ + php_http_option_register(registry, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION, IS_BOOL); + /* cookiestore, read initial cookies from that file and store cookies back into that file */ + if ((opt = php_http_option_register(registry, ZEND_STRL("cookiestore"), 0, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR; + opt->setter = php_http_curl_client_option_set_cookiestore; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) { - add_assoc_long_ex(&array, "proxyauth_avail", sizeof("proxyauth_avail"), l); + + /* maxfilesize */ + php_http_option_register(registry, ZEND_STRL("maxfilesize"), CURLOPT_MAXFILESIZE, IS_LONG); + + /* http protocol version */ + php_http_option_register(registry, ZEND_STRL("protocol"), CURLOPT_HTTP_VERSION, IS_LONG); + + /* timeouts */ + if ((opt = php_http_option_register(registry, ZEND_STRL("timeout"), CURLOPT_TIMEOUT_MS, IS_DOUBLE))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_TRANSFORM_MS; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) { - add_assoc_long_ex(&array, "os_errno", sizeof("os_errno"), l); + if ((opt = php_http_option_register(registry, ZEND_STRL("connecttimeout"), CURLOPT_CONNECTTIMEOUT_MS, IS_DOUBLE))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_TRANSFORM_MS; + Z_DVAL(opt->defval) = 3; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) { - add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l); + + /* tcp */ +#if PHP_HTTP_CURL_VERSION(7,25,0) + 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; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) { - MAKE_STD_ZVAL(subarray); - array_init(subarray); - for (p = s; p; p = p->next) { - if (p->data) { - add_next_index_string(subarray, p->data, 1); - } - } - add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray); - curl_slist_free_all(s); + if ((opt = php_http_option_register(registry, ZEND_STRL("tcp_keepintvl"), CURLOPT_TCP_KEEPINTVL, IS_LONG))) { + Z_LVAL(opt->defval) = 60; } - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_COOKIELIST, &s)) { - MAKE_STD_ZVAL(subarray); - array_init(subarray); - for (p = s; p; p = p->next) { - if (p->data) { - add_next_index_string(subarray, p->data, 1); - } +#endif + + /* ssl */ + if ((opt = php_http_option_register(registry, ZEND_STRL("ssl"), 0, IS_ARRAY))) { + registry = &opt->suboptions; + + if ((opt = php_http_option_register(registry, ZEND_STRL("cert"), CURLOPT_SSLCERT, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR; } - add_assoc_zval_ex(&array, "cookies", sizeof("cookies"), subarray); - 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); - } -#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); - } + php_http_option_register(registry, ZEND_STRL("certtype"), CURLOPT_SSLCERTTYPE, IS_STRING); + php_http_option_register(registry, ZEND_STRL("certpasswd"), CURLOPT_SSLCERTPASSWD, IS_STRING); + + if ((opt = php_http_option_register(registry, ZEND_STRL("key"), CURLOPT_SSLKEY, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR; + } + php_http_option_register(registry, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE, IS_STRING); + php_http_option_register(registry, ZEND_STRL("keypasswd"), CURLOPT_SSLKEYPASSWD, IS_STRING); + 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))) { + ZVAL_BOOL(&opt->defval, 1); + } + if ((opt = php_http_option_register(registry, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST, IS_BOOL))) { + ZVAL_BOOL(&opt->defval, 1); + opt->setter = php_http_curl_client_option_set_ssl_verifyhost; + } + 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_CURL_CLIENT_OPTION_CHECK_STRLEN; + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR; +#ifdef PHP_HTTP_CURL_CAINFO + ZVAL_STRING(&opt->defval, PHP_HTTP_CURL_CAINFO, 0); #endif + } + if ((opt = php_http_option_register(registry, ZEND_STRL("capath"), CURLOPT_CAPATH, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR; + } + if ((opt = php_http_option_register(registry, ZEND_STRL("random_file"), CURLOPT_RANDOM_FILE, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR; + } + if ((opt = php_http_option_register(registry, ZEND_STRL("egdsocket"), CURLOPT_EGDSOCKET, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR; + } #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); - } -#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); - } -#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); - } + if ((opt = php_http_option_register(registry, ZEND_STRL("issuercert"), CURLOPT_ISSUERCERT, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR; + } +# ifdef PHP_HTTP_HAVE_OPENSSL + if ((opt = php_http_option_register(registry, ZEND_STRL("crlfile"), CURLOPT_CRLFILE, IS_STRING))) { + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN; + opt->flags |= PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR; + } +# endif #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); - } +#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) + php_http_option_register(registry, ZEND_STRL("certinfo"), CURLOPT_CERTINFO, IS_BOOL); #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); } -#endif +} - /* END::CURLINFO */ +static zval *php_http_curl_client_get_option(php_http_option_t *opt, HashTable *options, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + zval *option; -#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) - { - int i; - zval *ci_array; - struct curl_certinfo *ci; - char *colon, *keyname; + 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); + } + return option; +} - if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) { - MAKE_STD_ZVAL(ci_array); - array_init(ci_array); +static STATUS php_http_curl_client_set_option(php_http_option_t *opt, zval *val, void *userdata) +{ + php_http_client_t *h = userdata; + php_http_curl_client_t *curl = h->ctx; + CURL *ch = curl->handle; + zval tmp; + STATUS rv = SUCCESS; + TSRMLS_FETCH_FROM_CTX(h->ts); - for (i = 0; i < ci->num_of_certs; ++i) { - s = ci->certinfo[i]; + if (!val) { + val = &opt->defval; + } - MAKE_STD_ZVAL(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); - efree(keyname); - } else { - add_next_index_string(subarray, p->data, 1); - } - } + switch (opt->type) { + case IS_BOOL: + if (opt->setter) { + rv = opt->setter(opt, val, h); + } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) Z_BVAL_P(val))) { + rv = FAILURE; + } + break; + + case IS_LONG: + if (opt->setter) { + rv = opt->setter(opt, val, h); + } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, Z_LVAL_P(val))) { + rv = FAILURE; + } + break; + + case IS_STRING: + if (!(opt->flags & PHP_HTTP_CURL_CLIENT_OPTION_CHECK_STRLEN) || Z_STRLEN_P(val)) { + if (!(opt->flags & PHP_HTTP_CURL_CLIENT_OPTION_CHECK_BASEDIR) || !Z_STRVAL_P(val) || SUCCESS == php_check_open_basedir(Z_STRVAL_P(val) TSRMLS_CC)) { + if (opt->setter) { + rv = opt->setter(opt, val, h); + } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, Z_STRVAL_P(val))) { + rv = FAILURE; } - add_next_index_zval(ci_array, subarray); } - add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array); } - } -#endif - add_assoc_string_ex(&array, "error", sizeof("error"), get_storage(ch)->errorbuffer, 1); + break; - return SUCCESS; -} + case IS_DOUBLE: + if (opt->flags & PHP_HTTP_CURL_CLIENT_OPTION_TRANSFORM_MS) { + tmp = *val; + Z_DVAL(tmp) *= 1000; + val = &tmp; + } + if (opt->setter) { + rv = opt->setter(opt, val, h); + } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) Z_DVAL_P(val))) { + rv = FAILURE; + } + break; + + case IS_ARRAY: + if (opt->setter) { + rv = opt->setter(opt, val, h); + } else if (Z_TYPE_P(val) != IS_NULL) { + rv = php_http_options_apply(&opt->suboptions, Z_ARRVAL_P(val), h); + } + break; + default: + if (opt->setter) { + rv = opt->setter(opt, val, h); + } else { + rv = FAILURE; + } + break; + } + if (rv != SUCCESS) { + php_http_error(HE_NOTICE, PHP_HTTP_E_CLIENT, "Could not set option %s", opt->name.s); + } + return rv; +} -/* request handler ops */ +/* client ops */ static STATUS php_http_curl_client_reset(php_http_client_t *h); @@ -829,6 +1020,7 @@ static php_http_client_t *php_http_curl_client_init(php_http_client_t *h, void * ctx = ecalloc(1, sizeof(*ctx)); ctx->handle = handle; php_http_buffer_init(&ctx->options.cookies); + php_http_buffer_init(&ctx->options.ranges); zend_hash_init(&ctx->options.cache, 0, NULL, ZVAL_PTR_DTOR, 0); h->ctx = ctx; @@ -883,6 +1075,7 @@ static void php_http_curl_client_dtor(php_http_client_t *h) php_http_resource_factory_handle_dtor(h->rf, ctx->handle TSRMLS_CC); + php_http_buffer_dtor(&ctx->options.ranges); php_http_buffer_dtor(&ctx->options.cookies); zend_hash_destroy(&ctx->options.cache); @@ -895,6 +1088,7 @@ static void php_http_curl_client_dtor(php_http_client_t *h) efree(ctx); h->ctx = NULL; } + static STATUS php_http_curl_client_reset(php_http_client_t *h) { php_http_curl_client_t *curl = h->ctx; @@ -914,122 +1108,13 @@ static STATUS php_http_curl_client_reset(php_http_client_t *h) } curl_easy_setopt(ch, CURLOPT_URL, NULL); -#if PHP_HTTP_CURL_VERSION(7,19,4) - curl_easy_setopt(ch, CURLOPT_NOPROXY, NULL); -#endif - curl_easy_setopt(ch, CURLOPT_PROXY, NULL); - curl_easy_setopt(ch, CURLOPT_PROXYPORT, 0L); - curl_easy_setopt(ch, CURLOPT_PROXYTYPE, 0L); /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */ #if PHP_HTTP_CURL_VERSION(7,19,1) curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, NULL); curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, NULL); -#endif - curl_easy_setopt(ch, CURLOPT_PROXYAUTH, 0L); - curl_easy_setopt(ch, CURLOPT_HTTPPROXYTUNNEL, 0L); - curl_easy_setopt(ch, CURLOPT_DNS_CACHE_TIMEOUT, 60L); - curl_easy_setopt(ch, CURLOPT_IPRESOLVE, 0); -#if PHP_HTTP_CURL_VERSION(7,21,3) - curl_easy_setopt(ch, CURLOPT_RESOLVE, NULL); -#endif -#if PHP_HTTP_CURL_VERSION(7,24,0) - curl_easy_setopt(ch, CURLOPT_DNS_SERVERS, NULL); -#endif - curl_easy_setopt(ch, CURLOPT_LOW_SPEED_LIMIT, 0L); - curl_easy_setopt(ch, CURLOPT_LOW_SPEED_TIME, 0L); - /* LFS weirdance - curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0); - curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0); - */ - /* crashes - curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, 5L); */ - curl_easy_setopt(ch, CURLOPT_FRESH_CONNECT, 0L); - curl_easy_setopt(ch, CURLOPT_FORBID_REUSE, 0L); - curl_easy_setopt(ch, CURLOPT_INTERFACE, NULL); - curl_easy_setopt(ch, CURLOPT_PORT, 0L); -#if PHP_HTTP_CURL_VERSION(7,19,0) - curl_easy_setopt(ch, CURLOPT_ADDRESS_SCOPE, 0L); -#endif - curl_easy_setopt(ch, CURLOPT_LOCALPORT, 0L); - curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, 0L); - /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */ -#if PHP_HTTP_CURL_VERSION(7,19,1) curl_easy_setopt(ch, CURLOPT_USERNAME, NULL); curl_easy_setopt(ch, CURLOPT_PASSWORD, NULL); #endif - curl_easy_setopt(ch, CURLOPT_HTTPAUTH, 0L); - curl_easy_setopt(ch, CURLOPT_ENCODING, NULL); - /* we do this ourself anyway */ - curl_easy_setopt(ch, CURLOPT_HTTP_CONTENT_DECODING, 0L); - curl_easy_setopt(ch, CURLOPT_HTTP_TRANSFER_DECODING, 0L); - curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, 0L); -#if PHP_HTTP_CURL_VERSION(7,19,1) - curl_easy_setopt(ch, CURLOPT_POSTREDIR, 0L); -#else - curl_easy_setopt(ch, CURLOPT_POST301, 0L); -#endif - curl_easy_setopt(ch, CURLOPT_UNRESTRICTED_AUTH, 0L); - curl_easy_setopt(ch, CURLOPT_REFERER, NULL); - curl_easy_setopt(ch, CURLOPT_USERAGENT, "PECL::HTTP/" PHP_HTTP_EXT_VERSION " (PHP/" PHP_VERSION ")"); - curl_easy_setopt(ch, CURLOPT_HTTPHEADER, NULL); - curl_easy_setopt(ch, CURLOPT_COOKIE, NULL); - curl_easy_setopt(ch, CURLOPT_COOKIESESSION, 0L); - /* these options would enable curl's cookie engine by default which we don't want - curl_easy_setopt(ch, CURLOPT_COOKIEFILE, NULL); - curl_easy_setopt(ch, CURLOPT_COOKIEJAR, NULL); */ - curl_easy_setopt(ch, CURLOPT_COOKIELIST, NULL); - curl_easy_setopt(ch, CURLOPT_RANGE, NULL); - curl_easy_setopt(ch, CURLOPT_RESUME_FROM, 0L); - curl_easy_setopt(ch, CURLOPT_MAXFILESIZE, 0L); - curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0L); - curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0L); - curl_easy_setopt(ch, CURLOPT_TIMEOUT, 0L); - curl_easy_setopt(ch, CURLOPT_CONNECTTIMEOUT, 3L); -#if PHP_HTTP_CURL_VERSION(7,25,0) - curl_easy_setopt(ch, CURLOPT_TCP_KEEPALIVE, 0L); - curl_easy_setopt(ch, CURLOPT_TCP_KEEPIDLE, 60L); - curl_easy_setopt(ch, CURLOPT_TCP_KEEPINTVL, 60L); -#endif - curl_easy_setopt(ch, CURLOPT_SSLCERT, NULL); - curl_easy_setopt(ch, CURLOPT_SSLCERTTYPE, NULL); - curl_easy_setopt(ch, CURLOPT_SSLCERTPASSWD, NULL); - curl_easy_setopt(ch, CURLOPT_SSLKEY, NULL); - curl_easy_setopt(ch, CURLOPT_SSLKEYTYPE, NULL); - curl_easy_setopt(ch, CURLOPT_SSLKEYPASSWD, NULL); - curl_easy_setopt(ch, CURLOPT_SSLENGINE, NULL); - curl_easy_setopt(ch, CURLOPT_SSLVERSION, 0L); - curl_easy_setopt(ch, CURLOPT_SSL_VERIFYPEER, 0L); - curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, 0L); - curl_easy_setopt(ch, CURLOPT_SSL_CIPHER_LIST, NULL); -#if PHP_HTTP_CURL_VERSION(7,19,0) - curl_easy_setopt(ch, CURLOPT_ISSUERCERT, NULL); -#if defined(PHP_HTTP_HAVE_OPENSSL) - curl_easy_setopt(ch, CURLOPT_CRLFILE, NULL); -#endif -#endif -#if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL) - curl_easy_setopt(ch, CURLOPT_CERTINFO, NULL); -#endif -#ifdef PHP_HTTP_CURL_CAINFO - curl_easy_setopt(ch, CURLOPT_CAINFO, PHP_HTTP_CURL_CAINFO); -#else - curl_easy_setopt(ch, CURLOPT_CAINFO, NULL); -#endif - curl_easy_setopt(ch, CURLOPT_CAPATH, NULL); - curl_easy_setopt(ch, CURLOPT_RANDOM_FILE, NULL); - curl_easy_setopt(ch, CURLOPT_EGDSOCKET, NULL); - curl_easy_setopt(ch, CURLOPT_POSTFIELDS, NULL); - curl_easy_setopt(ch, CURLOPT_POSTFIELDSIZE, 0L); - curl_easy_setopt(ch, CURLOPT_HTTPPOST, NULL); - curl_easy_setopt(ch, CURLOPT_IOCTLDATA, NULL); - curl_easy_setopt(ch, CURLOPT_READDATA, NULL); - curl_easy_setopt(ch, CURLOPT_INFILESIZE, 0L); - curl_easy_setopt(ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE); - curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, NULL); - curl_easy_setopt(ch, CURLOPT_NOBODY, 0L); - curl_easy_setopt(ch, CURLOPT_POST, 0L); - curl_easy_setopt(ch, CURLOPT_UPLOAD, 0L); - curl_easy_setopt(ch, CURLOPT_HTTPGET, 1L); #if PHP_HTTP_CURL_VERSION(7,21,3) if (curl->options.resolve) { @@ -1040,6 +1125,7 @@ static STATUS php_http_curl_client_reset(php_http_client_t *h) curl->options.retry.count = 0; curl->options.retry.delay = 0; curl->options.redirects = 0; + curl->options.encode_cookies = 1; if (curl->options.headers) { curl_slist_free_all(curl->options.headers); @@ -1047,6 +1133,7 @@ static STATUS php_http_curl_client_reset(php_http_client_t *h) } php_http_buffer_reset(&curl->options.cookies); + php_http_buffer_reset(&curl->options.ranges); return SUCCESS; } @@ -1199,7 +1286,7 @@ static STATUS php_http_curl_client_setopt(php_http_client_t *h, php_http_client_ switch (opt) { case PHP_HTTP_CLIENT_OPT_SETTINGS: - return set_options(h, arg); + return php_http_options_apply(&php_http_curl_client_options, arg, h); break; case PHP_HTTP_CLIENT_OPT_PROGRESS_CALLBACK: @@ -1344,10 +1431,19 @@ zend_object_value php_http_curl_client_object_new_ex(zend_class_entry *ce, php_h PHP_MINIT_FUNCTION(http_curl_client) { + php_http_options_t *options; + if (SUCCESS != php_http_persistent_handle_provide(ZEND_STRL("http_client.curl"), &php_http_curl_client_resource_factory_ops, NULL, NULL)) { return FAILURE; } + if ((options = php_http_options_init(&php_http_curl_client_options, 1))) { + options->getter = php_http_curl_client_get_option; + options->setter = php_http_curl_client_set_option; + + php_http_curl_client_options_init(options TSRMLS_CC); + } + PHP_HTTP_REGISTER_CLASS(http\\Curl, Client, http_curl_client, php_http_client_get_class_entry(), 0); php_http_curl_client_class_entry->create_object = php_http_curl_client_object_new; @@ -1410,6 +1506,12 @@ PHP_MINIT_FUNCTION(http_curl_client) return SUCCESS; } +PHP_MSHUTDOWN_FUNCTION(http_curl_client) +{ + php_http_options_dtor(&php_http_curl_client_options); + return SUCCESS; +} + /* * Local variables: * tab-width: 4 diff --git a/php_http_curl_client.h b/php_http_curl_client.h index 4baddf1..64aacf0 100644 --- a/php_http_curl_client.h +++ b/php_http_curl_client.h @@ -26,9 +26,11 @@ typedef struct php_http_curl_client { struct curl_slist *headers; struct curl_slist *resolve; php_http_buffer_t cookies; + php_http_buffer_t ranges; long redirects; unsigned range_request:1; + unsigned encode_cookies:1; struct { uint count; @@ -69,6 +71,7 @@ zend_object_value php_http_curl_client_object_new(zend_class_entry *ce TSRMLS_DC zend_object_value php_http_curl_client_object_new_ex(zend_class_entry *ce, php_http_client_t *r, php_http_client_object_t **ptr TSRMLS_DC); PHP_MINIT_FUNCTION(http_curl_client); +PHP_MSHUTDOWN_FUNCTION(http_curl_client); #endif /* PHP_HTTP_HAVE_CURL */ #endif /* PHP_HTTP_CURL_CLIENT_H */ diff --git a/php_http_curl_client_pool.c b/php_http_curl_client_pool.c index 6ae1da6..5e51a28 100644 --- a/php_http_curl_client_pool.c +++ b/php_http_curl_client_pool.c @@ -556,16 +556,25 @@ PHP_MINIT_FUNCTION(http_curl_client_pool) return SUCCESS; } +#if PHP_HTTP_HAVE_EVENT PHP_RINIT_FUNCTION(http_curl_client_pool) { -#if PHP_HTTP_HAVE_EVENT if (!PHP_HTTP_G->curl.event_base && !(PHP_HTTP_G->curl.event_base = event_base_new())) { return FAILURE; } + return SUCCESS; +} #endif +#if PHP_HTTP_HAVE_EVENT +PHP_RSHUTDOWN_FUNCTION(http_curl_client_pool) +{ + if (PHP_HTTP_G->curl.event_base) { + event_base_free(PHP_HTTP_G->curl.event_base); + } return SUCCESS; } +#endif #endif /* PHP_HTTP_HAVE_CURL */ diff --git a/php_http_curl_client_pool.h b/php_http_curl_client_pool.h index 69203fb..5d6a19c 100644 --- a/php_http_curl_client_pool.h +++ b/php_http_curl_client_pool.h @@ -28,6 +28,7 @@ struct php_http_curl_globals { }; PHP_RINIT_FUNCTION(http_curl_client_pool); +PHP_RSHUTDOWN_FUNCTION(http_curl_client_pool); #endif PHP_MINIT_FUNCTION(http_curl_client_pool); diff --git a/php_http_misc.c b/php_http_misc.c index bb99062..4115042 100644 --- a/php_http_misc.c +++ b/php_http_misc.c @@ -142,6 +142,30 @@ int php_http_select_str(const char *cmp, int argc, ...) /* ARRAYS */ +PHP_HTTP_API unsigned php_http_array_list(zval *hash TSRMLS_DC, unsigned argc, ...) +{ + HashTable *ht = HASH_OF(hash); + HashPosition pos; + unsigned argl = 0; + va_list argv; + + va_start(argv, argc); + for ( zend_hash_internal_pointer_reset_ex(ht, &pos); + zend_hash_has_more_elements_ex(ht, &pos) && (argl < argc); + zend_hash_move_forward_ex(ht, &pos)) + { + zval **data, ***argp = (zval ***) va_arg(argv, zval ***); + + if (SUCCESS == zend_hash_get_current_data_ex(ht, (void *) &data, &pos)) { + *argp = data; + ++argl; + } + } + va_end(argv); + + return argl; +} + int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) { int flags; diff --git a/php_http_misc.h b/php_http_misc.h index 54e1ac5..2ee38e1 100644 --- a/php_http_misc.h +++ b/php_http_misc.h @@ -402,6 +402,8 @@ static inline STATUS php_http_ini_entry(const char *name_str, size_t name_len, c /* ARRAYS */ +PHP_HTTP_API unsigned php_http_array_list(zval *hash TSRMLS_DC, unsigned argc, ...); + typedef struct php_http_array_hashkey { char *str; uint len; diff --git a/php_http_options.c b/php_http_options.c new file mode 100644 index 0000000..bbc10c9 --- /dev/null +++ b/php_http_options.c @@ -0,0 +1,126 @@ +/* + +--------------------------------------------------------------------+ + | 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-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#include "php_http_api.h" + +PHP_HTTP_API php_http_options_t *php_http_options_init(php_http_options_t *registry, zend_bool persistent) +{ + if (!registry) { + registry = pecalloc(1, sizeof(*registry), persistent); + } else { + memset(registry, 0, sizeof(*registry)); + } + + registry->persistent = persistent; + zend_hash_init(®istry->options, 0, NULL, (dtor_func_t) zend_hash_destroy, persistent); + + return registry; +} + +PHP_HTTP_API STATUS php_http_options_apply(php_http_options_t *registry, HashTable *options, void *userdata) +{ + HashPosition pos; + zval *val; + php_http_option_t *opt; + + FOREACH_HASH_VAL(pos, ®istry->options, opt) { + if (!(val = registry->getter(opt, options, userdata))) { + val = &opt->defval; + } + if (registry->setter) { + if (SUCCESS != registry->setter(opt, val, userdata)) { + return FAILURE; + } + } else if (!opt->setter || SUCCESS != opt->setter(opt, val, userdata)) { + return FAILURE; + } + } + return SUCCESS; +} + +PHP_HTTP_API void php_http_options_dtor(php_http_options_t *registry) +{ + zend_hash_destroy(®istry->options); +} + +PHP_HTTP_API void php_http_options_free(php_http_options_t **registry) +{ + if (*registry) { + php_http_options_dtor(*registry); + pefree(*registry, (*registry)->persistent); + *registry = NULL; + } +} + +PHP_HTTP_API php_http_option_t *php_http_option_register(php_http_options_t *registry, const char *name_str, size_t name_len, ulong option, zend_uchar type) +{ + php_http_option_t opt, *dst = NULL; + + memset(&opt, 0, sizeof(opt)); + + php_http_options_init(&opt.suboptions, registry->persistent); + opt.suboptions.getter = registry->getter; + opt.suboptions.setter = registry->setter; + + opt.name.h = zend_hash_func(opt.name.s = name_str, opt.name.l = name_len + 1); + opt.type = type; + opt.option = option; + + INIT_ZVAL(opt.defval); + switch ((opt.type = type)) { + case IS_BOOL: + ZVAL_BOOL(&opt.defval, 0); + break; + + case IS_LONG: + ZVAL_LONG(&opt.defval, 0); + break; + + case IS_STRING: + ZVAL_STRINGL(&opt.defval, NULL, 0, 0); + break; + + case IS_DOUBLE: + ZVAL_DOUBLE(&opt.defval, 0); + break; + + default: + ZVAL_NULL(&opt.defval); + break; + } + + zend_hash_quick_update(®istry->options, opt.name.s, opt.name.l, opt.name.h, (void *) &opt, sizeof(opt), (void *) &dst); + return dst; +} + +PHP_HTTP_API zval *php_http_option_get(php_http_option_t *opt, HashTable *options, void *userdata) +{ + if (options) { + zval **zoption; + + if (SUCCESS == zend_hash_quick_find(options, opt->name.s, opt->name.l, opt->name.h, (void *) &zoption)) { + return *zoption; + } + } + + return NULL; +} + + +/* + * 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 --git a/php_http_options.h b/php_http_options.h new file mode 100644 index 0000000..1146f05 --- /dev/null +++ b/php_http_options.h @@ -0,0 +1,65 @@ +/* + +--------------------------------------------------------------------+ + | 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-2011, Michael Wallner | + +--------------------------------------------------------------------+ +*/ + +#ifndef PHP_HTTP_OPTIONS_H +#define PHP_HTTP_OPTIONS_H + +typedef struct php_http_option php_http_option_t; +typedef struct php_http_options php_http_options_t; + +typedef STATUS (*php_http_option_set_callback_t)(php_http_option_t *opt, zval *val, void *userdata); +typedef zval *(*php_http_option_get_callback_t)(php_http_option_t *opt, HashTable *options, void *userdata); + +struct php_http_options { + HashTable options; + + php_http_option_get_callback_t getter; + php_http_option_set_callback_t setter; + + unsigned persistent:1; +}; + +struct php_http_option { + php_http_options_t suboptions; + + struct { + const char *s; + size_t l; + ulong h; + } name; + + ulong option; + zend_uchar type; + unsigned flags; + zval defval; + + php_http_option_set_callback_t setter; +}; + +PHP_HTTP_API php_http_options_t *php_http_options_init(php_http_options_t *registry, zend_bool persistent); +PHP_HTTP_API STATUS php_http_options_apply(php_http_options_t *registry, HashTable *options, void *userdata); +PHP_HTTP_API void php_http_options_dtor(php_http_options_t *registry); +PHP_HTTP_API void php_http_options_free(php_http_options_t **registry); + +PHP_HTTP_API php_http_option_t *php_http_option_register(php_http_options_t *registry, const char *name_str, size_t name_len, ulong option, zend_uchar type); +PHP_HTTP_API zval *php_http_option_get(php_http_option_t *opt, HashTable *options, void *userdata); + +#endif /* PHP_HTTP_OPTIONS_H */ + +/* + * 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 + */ -- 2.30.2