add Client::configure option to disable the share
authorMichael Wallner <mike@php.net>
Tue, 24 May 2016 14:43:11 +0000 (16:43 +0200)
committerMichael Wallner <mike@php.net>
Tue, 24 May 2016 14:46:33 +0000 (16:46 +0200)
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
tests/client021.phpt

index 5dab9e210037a95b7fe055759f4fadaf68029ec6..8e53572c9ae4983ce320ed1e5a6dc03092300e79 100644 (file)
@@ -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)
index cad16e0dcb5576709b1983f80efab8878c733eb6..ef649093e077d243b11379f2df031d4581155478 100644 (file)
@@ -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===