Skip to content

Commit 35ac500

Browse files
committed
Improve cookie handling for multicurl requests.
Fix Curl::responseCookies set in headerCallback() being overwritten during multicurl request. Use rfc cookie encoding when using MultiCurl::setCookie(). Make MultiCurl::responseCookies publicly accessible.
1 parent 4c47bac commit 35ac500

File tree

6 files changed

+31
-17
lines changed

6 files changed

+31
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ MultiCurl::setDigestAuthentication($username, $password = '')
259259
MultiCurl::setHeader($key, $value)
260260
MultiCurl::setJsonDecoder($function)
261261
MultiCurl::setOpt($option, $value)
262+
MultiCurl::setOpts($options)
262263
MultiCurl::setReferer($referer)
263264
MultiCurl::setReferrer($referrer)
264265
MultiCurl::setTimeout($seconds)

src/Curl/Curl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class Curl
6060
public $requestHeaders = null;
6161
public $responseHeaders = null;
6262
public $rawResponseHeaders = '';
63+
public $responseCookies = array();
6364
public $response = null;
6465
public $rawResponse = null;
6566

@@ -71,7 +72,6 @@ class Curl
7172
public $fileHandle = null;
7273

7374
private $cookies = array();
74-
private $responseCookies = array();
7575
private $headers = array();
7676
private $options = array();
7777

@@ -336,8 +336,8 @@ public function error($callback)
336336
*/
337337
public function exec($ch = null)
338338
{
339-
$this->responseCookies = array();
340339
if ($ch === null) {
340+
$this->responseCookies = array();
341341
$this->call($this->beforeSendFunction);
342342
$this->rawResponse = curl_exec($this->curl);
343343
$this->curlErrorCode = curl_errno($this->curl);

src/Curl/MultiCurl.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ public function setDigestAuthentication($username, $password = '')
383383
public function setCookie($key, $value)
384384
{
385385
$this->cookies[$key] = $value;
386-
$this->setOpt(CURLOPT_COOKIE, str_replace('+', '%20', http_build_query($this->cookies, '', '; ')));
387386
}
388387

389388
/**
@@ -699,6 +698,9 @@ private function initHandle($curl)
699698
foreach ($this->headers as $key => $value) {
700699
$curl->setHeader($key, $value);
701700
}
701+
foreach ($this->cookies as $key => $value) {
702+
$curl->setCookie($key, $value);
703+
}
702704
$curl->setJsonDecoder($this->jsonDecoder);
703705
$curl->setXmlDecoder($this->xmlDecoder);
704706

@@ -708,6 +710,7 @@ private function initHandle($curl)
708710
}
709711

710712
$this->activeCurls[$curl->id] = $curl;
713+
$this->responseCookies = array();
711714
$curl->call($curl->beforeSendFunction);
712715
}
713716
}

tests/PHPCurlClass/PHPCurlClassTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,9 +877,8 @@ public function testMultipleCookieResponse()
877877
$test->server('multiple_cookie', 'GET');
878878
$this->assertEquals('cookie1=scrumptious,cookie2=mouthwatering', $test->curl->responseHeaders['Set-Cookie']);
879879

880-
$response_cookies = $test->curl->getResponseCookies();
881-
$this->assertEquals('scrumptious', $response_cookies['cookie1']);
882-
$this->assertEquals('mouthwatering', $response_cookies['cookie2']);
880+
$this->assertEquals('scrumptious', $test->curl->responseCookies['cookie1']);
881+
$this->assertEquals('mouthwatering', $test->curl->responseCookies['cookie2']);
883882
}
884883

885884
public function testDefaultTimeout()

tests/PHPCurlClass/PHPMultiCurlClassTest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,31 +2001,37 @@ public function testDigestHttpAuthSuccess()
20012001

20022002
public function testCookies()
20032003
{
2004-
$data = array('key' => 'mycookie');
2005-
20062004
$multi_curl = new MultiCurl();
2007-
$multi_curl->setHeader('X-DEBUG-TEST', 'cookie');
2005+
$multi_curl->setHeader('X-DEBUG-TEST', 'setcookie');
20082006
$multi_curl->setCookie('mycookie', 'yum');
2007+
$multi_curl->setCookie('cookie-for-all-before', 'a');
20092008

2010-
$get_1 = $multi_curl->addGet(Test::TEST_URL, $data);
2009+
$get_1 = $multi_curl->addGet(Test::TEST_URL);
2010+
$get_1->setCookie('cookie-for-1st-request', '1');
20112011
$get_1->complete(function ($instance) {
2012-
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
2013-
PHPUnit_Framework_Assert::assertEquals('yum', $instance->response);
2012+
PHPUnit_Framework_Assert::assertEquals('yum', $instance->responseCookies['mycookie']);
2013+
PHPUnit_Framework_Assert::assertEquals('a', $instance->responseCookies['cookie-for-all-before']);
2014+
PHPUnit_Framework_Assert::assertEquals('b', $instance->responseCookies['cookie-for-all-after']);
2015+
PHPUnit_Framework_Assert::assertEquals('1', $instance->responseCookies['cookie-for-1st-request']);
20142016
});
20152017

2016-
$get_2 = $multi_curl->addGet(Test::TEST_URL, $data);
2018+
$get_2 = $multi_curl->addGet(Test::TEST_URL);
2019+
$get_2->setCookie('cookie-for-2nd-request', '2');
20172020
$get_2->beforeSend(function ($instance) {
20182021
$instance->setCookie('mycookie', 'yummy');
20192022
});
20202023
$get_2->complete(function ($instance) {
2021-
PHPUnit_Framework_Assert::assertInstanceOf('Curl\Curl', $instance);
2022-
PHPUnit_Framework_Assert::assertEquals('yummy', $instance->response);
2024+
PHPUnit_Framework_Assert::assertEquals('yummy', $instance->responseCookies['mycookie']);
2025+
PHPUnit_Framework_Assert::assertEquals('a', $instance->responseCookies['cookie-for-all-before']);
2026+
PHPUnit_Framework_Assert::assertEquals('b', $instance->responseCookies['cookie-for-all-after']);
2027+
PHPUnit_Framework_Assert::assertEquals('2', $instance->responseCookies['cookie-for-2nd-request']);
20232028
});
20242029

2030+
$multi_curl->setCookie('cookie-for-all-after', 'b');
20252031
$multi_curl->start();
20262032

2027-
$this->assertEquals('yum', $get_1->response);
2028-
$this->assertEquals('yummy', $get_2->response);
2033+
$this->assertEquals('yum', $get_1->responseCookies['mycookie']);
2034+
$this->assertEquals('yummy', $get_2->responseCookies['mycookie']);
20292035
}
20302036

20312037
public function testJSONDecoder()

tests/PHPCurlClass/server.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@
143143
} elseif ($test === 'request_uri') {
144144
echo $_SERVER['REQUEST_URI'];
145145
exit;
146+
} elseif ($test === 'setcookie') {
147+
foreach ($_COOKIE as $key => $value) {
148+
setcookie($key, $value);
149+
}
150+
exit;
146151
} elseif ($test === 'cookiejar') {
147152
setcookie('mycookie', 'yum');
148153
exit;

0 commit comments

Comments
 (0)