From a79e64b2ca56c7db17f3f602cffd304355842d0e Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Wed, 29 Apr 2015 12:38:07 -0700 Subject: [PATCH] Fix #174: Correctly set cookie value when using http_build_query(). http_build_query() url encodes the cookie data array. Use urldecode() to undo the URL-encoding and allow characters including the colon (":"). --- src/Curl/Curl.php | 2 +- tests/PHPCurlClass/PHPCurlClassTest.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index afd41d3..dbca6f6 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -535,7 +535,7 @@ class Curl public function setCookie($key, $value) { $this->cookies[$key] = $value; - $this->setOpt(CURLOPT_COOKIE, str_replace('+', '%20', http_build_query($this->cookies, '', '; '))); + $this->setOpt(CURLOPT_COOKIE, str_replace(' ', '%20', urldecode(http_build_query($this->cookies, '', '; ')))); } /** diff --git a/tests/PHPCurlClass/PHPCurlClassTest.php b/tests/PHPCurlClass/PHPCurlClassTest.php index 5055751..ae16a2a 100644 --- a/tests/PHPCurlClass/PHPCurlClassTest.php +++ b/tests/PHPCurlClass/PHPCurlClassTest.php @@ -603,7 +603,7 @@ class CurlTest extends PHPUnit_Framework_TestCase ))); } - public function testCookieEncoding() + public function testCookieEncodingSpace() { $curl = new Curl(); $curl->setCookie('cookie', 'Om nom nom nom'); @@ -615,6 +615,18 @@ class CurlTest extends PHPUnit_Framework_TestCase $this->assertEquals('cookie=Om%20nom%20nom%20nom', $options[CURLOPT_COOKIE]); } + public function testCookieEncodingColon() + { + $curl = new Curl(); + $curl->setCookie('JSESSIONID', '0000wd-PcsB3bZ-KzYGAqm_rKlm:17925chrl'); + + $reflectionClass = new ReflectionClass('\Curl\Curl'); + $reflectionProperty = $reflectionClass->getProperty('options'); + $reflectionProperty->setAccessible(true); + $options = $reflectionProperty->getValue($curl); + $this->assertEquals('JSESSIONID=0000wd-PcsB3bZ-KzYGAqm_rKlm:17925chrl', $options[CURLOPT_COOKIE]); + } + public function testCookieFile() { $cookie_file = dirname(__FILE__) . '/cookies.txt';