From 443aa73851cb71d793ccfd22cd1c88f904964e87 Mon Sep 17 00:00:00 2001 From: Maxim Epishchev Date: Tue, 27 Oct 2015 11:19:18 +0300 Subject: [PATCH 1/3] Cookie: preserve encode space to '%20' in delimiter, when multiple cookies available --- src/Curl/Curl.php | 6 ++++-- tests/PHPCurlClass/PHPCurlClassTest.php | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 3d184d5..1e9156d 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -543,8 +543,10 @@ class Curl */ public function setCookie($key, $value) { - $this->cookies[$key] = $value; - $this->setOpt(CURLOPT_COOKIE, str_replace(' ', '%20', urldecode(http_build_query($this->cookies, '', '; ')))); + $this->cookies[$key] = $value; + $this->setOpt(CURLOPT_COOKIE, implode('; ', array_map(function($name) { + return $name . '=' . str_replace(' ', '%20', $this->cookies[$name]); + }, array_keys($this->cookies)))); } /** diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 01d7337..61f902e 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -661,6 +661,19 @@ class CurlTest extends PHPUnit_Framework_TestCase $options = $reflectionProperty->getValue($curl); $this->assertEquals('cookie=Om%20nom%20nom%20nom', $options[CURLOPT_COOKIE]); } + + public function testMultipleCookies() + { + $curl = new Curl(); + $curl->setCookie('cookie', 'Om nom nom nom'); + $curl->setCookie('foo', 'bar'); + + $reflectionClass = new ReflectionClass('\Curl\Curl'); + $reflectionProperty = $reflectionClass->getProperty('options'); + $reflectionProperty->setAccessible(true); + $options = $reflectionProperty->getValue($curl); + $this->assertEquals('cookie=Om%20nom%20nom%20nom; foo=bar', $options[CURLOPT_COOKIE]); + } public function testCookieEncodingColon() { From f0bc8588971982d2ca0fb82e0d25001d608cc538 Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Tue, 27 Oct 2015 21:29:38 -0700 Subject: [PATCH 2/3] Fix #257: Preserve space in cookie delimiter --- src/Curl/Curl.php | 8 ++++---- tests/PHPCurlClass/PHPCurlClassTest.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 1e9156d..a089ad5 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -543,10 +543,10 @@ class Curl */ public function setCookie($key, $value) { - $this->cookies[$key] = $value; - $this->setOpt(CURLOPT_COOKIE, implode('; ', array_map(function($name) { - return $name . '=' . str_replace(' ', '%20', $this->cookies[$name]); - }, array_keys($this->cookies)))); + $this->cookies[$key] = $value; + $this->setOpt(CURLOPT_COOKIE, implode('; ', array_map(function($this, $name) { + return $name . '=' . str_replace(' ', '%20', $this->cookies[$name]); + }, array($this), array_keys($this->cookies)))); } /** diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 61f902e..3072b51 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -661,7 +661,7 @@ class CurlTest extends PHPUnit_Framework_TestCase $options = $reflectionProperty->getValue($curl); $this->assertEquals('cookie=Om%20nom%20nom%20nom', $options[CURLOPT_COOKIE]); } - + public function testMultipleCookies() { $curl = new Curl(); From 4e7f1d7ba8eeb9ab5b97039a84da528d5a39a75e Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Tue, 27 Oct 2015 22:00:05 -0700 Subject: [PATCH 3/3] Fix PHP 5.3 "Fatal error: Using $this when not in object context" --- src/Curl/Curl.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index a089ad5..902924c 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -544,9 +544,9 @@ class Curl public function setCookie($key, $value) { $this->cookies[$key] = $value; - $this->setOpt(CURLOPT_COOKIE, implode('; ', array_map(function($this, $name) { - return $name . '=' . str_replace(' ', '%20', $this->cookies[$name]); - }, array($this), array_keys($this->cookies)))); + $this->setOpt(CURLOPT_COOKIE, implode('; ', array_map(function($k, $v) { + return $k . '=' . str_replace(' ', '%20', $v); + }, array_keys($this->cookies), array_values($this->cookies)))); } /**