From a2aa970a129880a247baea08927f0d84188a0b19 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Tue, 24 May 2016 16:43:11 +0200 Subject: [PATCH] add Client::configure option to disable the share Provide http\Client::configure() options (share_cookies, share_ssl) to en/disable sharing of cookies and ssl session info through CURLSH. --- src/php_http_client_curl.c | 37 +++++++++++++++++ tests/client021.phpt | 84 +++++++++++++++++++++++++++++++++++--- 2 files changed, 115 insertions(+), 6 deletions(-) diff --git a/src/php_http_client_curl.c b/src/php_http_client_curl.c index 5dab9e2..8e53572 100644 --- a/src/php_http_client_curl.c +++ b/src/php_http_client_curl.c @@ -1760,6 +1760,34 @@ static ZEND_RESULT_CODE php_http_curlm_option_set_use_eventloop(php_http_option_ } #endif +static ZEND_RESULT_CODE php_http_curlm_option_set_share_cookies(php_http_option_t *opt, zval *value, void *userdata) +{ + php_http_client_t *client = userdata; + php_http_client_curl_t *curl = client->ctx; + CURLSHcode rc; + + if (Z_BVAL_P(value)) { + rc = curl_share_setopt(curl->handle->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); + } else { + rc = curl_share_setopt(curl->handle->share, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE); + } + return CURLSHE_OK == rc ? SUCCESS : FAILURE; +} + +static ZEND_RESULT_CODE php_http_curlm_option_set_share_ssl(php_http_option_t *opt, zval *value, void *userdata) +{ + php_http_client_t *client = userdata; + php_http_client_curl_t *curl = client->ctx; + CURLSHcode rc; + + if (Z_BVAL_P(value)) { + rc = curl_share_setopt(curl->handle->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION); + } else { + rc = curl_share_setopt(curl->handle->share, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_SSL_SESSION); + } + return CURLSHE_OK == rc ? SUCCESS : FAILURE; +} + static void php_http_curlm_options_init(php_http_options_t *registry TSRMLS_DC) { php_http_option_t *opt; @@ -1811,6 +1839,15 @@ static void php_http_curlm_options_init(php_http_options_t *registry TSRMLS_DC) opt->setter = php_http_curlm_option_set_use_eventloop; } #endif + /* share */ + if ((opt = php_http_option_register(registry, ZEND_STRL("share_cookies"), 0, IS_BOOL))) { + opt->setter = php_http_curlm_option_set_share_cookies; + ZVAL_BOOL(&opt->defval, 1); + } + if ((opt = php_http_option_register(registry, ZEND_STRL("share_ssl"), 0, IS_BOOL))) { + opt->setter = php_http_curlm_option_set_share_ssl; + ZVAL_BOOL(&opt->defval, 1); + } } static ZEND_RESULT_CODE php_http_curlm_set_option(php_http_option_t *opt, zval *val, void *userdata) diff --git a/tests/client021.phpt b/tests/client021.phpt index cad16e0..ef64909 100644 --- a/tests/client021.phpt +++ b/tests/client021.phpt @@ -25,27 +25,66 @@ function cookies($client) { $tmpfile = tempnam(sys_get_temp_dir(), "cookie."); $request = new http\Client\Request("GET", "http://localhost"); + +server("cookie.inc", function($port) use($request, $tmpfile) { + $request->setOptions(array("port" => $port)); + $client = new http\Client; + cookies($client->requeue($request)->send()); +dump($tmpfile); +}); +server("cookie.inc", function($port) use($request, $tmpfile) { + $request->setOptions(array("port" => $port)); + $client = new http\Client; + cookies($client->requeue($request)->send()); +dump($tmpfile); +}); +server("cookie.inc", function($port) use($request, $tmpfile) { + $request->setOptions(array("port" => $port)); + $client = new http\Client; + cookies($client->requeue($request)->send()); +dump($tmpfile); +}); + +server("cookie.inc", function($port) use($request, $tmpfile) { + $request->setOptions(array("port" => $port)); + $client = new http\Client("curl", "test"); + cookies($client->requeue($request)->send()); +dump($tmpfile); +}); +server("cookie.inc", function($port) use($request, $tmpfile) { + $request->setOptions(array("port" => $port)); + $client = new http\Client("curl", "test"); + cookies($client->requeue($request)->send()); +dump($tmpfile); +}); +server("cookie.inc", function($port) use($request, $tmpfile) { + $request->setOptions(array("port" => $port)); + $client = new http\Client("curl", "test"); + cookies($client->requeue($request)->send()); +dump($tmpfile); +}); + $request->setOptions(array("cookiestore" => $tmpfile)); server("cookie.inc", function($port) use($request, $tmpfile) { $request->setOptions(array("port" => $port)); $client = new http\Client; cookies($client->requeue($request)->send()); -#dump($tmpfile); +dump($tmpfile); cookies($client->requeue($request)->send()); -#dump($tmpfile); +dump($tmpfile); cookies($client->requeue($request)->send()); -#dump($tmpfile); +dump($tmpfile); }); server("cookie.inc", function($port) use($request, $tmpfile) { $request->setOptions(array("port" => $port)); $client = new http\Client; cookies($client->requeue($request)->send()); -#dump($tmpfile); +dump($tmpfile); cookies($client->requeue($request)->send()); -#dump($tmpfile); +dump($tmpfile); cookies($client->requeue($request)->send()); -#dump($tmpfile); +dump($tmpfile); }); server("cookie.inc", function($port) use($request, $tmpfile) { @@ -70,6 +109,30 @@ dump($tmpfile); dump($tmpfile); }); + +(new http\Client("curl", "test"))->configure(["share_cookies" => false]); +$request->setOptions(["cookiestore" => null]); + +server("cookie.inc", function($port) use($request, $tmpfile) { + $request->setOptions(array("port" => $port)); + $client = new http\Client("curl", "test"); + cookies($client->requeue($request)->send()); +dump($tmpfile); +}); +server("cookie.inc", function($port) use($request, $tmpfile) { + $request->setOptions(array("port" => $port)); + $client = new http\Client("curl", "test"); + cookies($client->requeue($request)->send()); +dump($tmpfile); +}); +server("cookie.inc", function($port) use($request, $tmpfile) { + $request->setOptions(array("port" => $port)); + $client = new http\Client("curl", "test"); + cookies($client->requeue($request)->send()); +dump($tmpfile); +}); + + unlink($tmpfile); ?> @@ -77,6 +140,12 @@ unlink($tmpfile); --EXPECT-- Test counter=1; +counter=1; +counter=1; +counter=1; +counter=2; +counter=3; +counter=1; counter=2; counter=3; counter=4; @@ -88,4 +157,7 @@ counter=1; counter=2; counter=3; counter=4; +counter=1; +counter=1; +counter=1; ===DONE=== -- 2.30.2