From 87aab1daa8d74c2edb1a4502a31b2e9a888f7c7e Mon Sep 17 00:00:00 2001 From: "Nathanael d. Noblet" Date: Tue, 8 May 2018 10:58:14 -0600 Subject: [PATCH] Fix Cookie handling as per RFC 6265 Section 5.4 Concatenate multiple cookies --- CHANGELOG.md | 6 ++++++ spec/Plugin/CookiePluginSpec.php | 21 +++++++++++++++++++++ src/Plugin/CookiePlugin.php | 7 ++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59fc078..a32a18d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## 1.8.2 (unreleased) + +### Changed + +- When multiple cookies exist, a single header with all cookies is sent as per RFC 6265 Section 5.4 + ## 1.8.1 - 2018-10-09 ### Fixed diff --git a/spec/Plugin/CookiePluginSpec.php b/spec/Plugin/CookiePluginSpec.php index 675b8bc..2bb47e7 100644 --- a/spec/Plugin/CookiePluginSpec.php +++ b/spec/Plugin/CookiePluginSpec.php @@ -51,6 +51,27 @@ function it_loads_cookie(RequestInterface $request, UriInterface $uri, Promise $ }, function () {}); } + function it_combines_multiple_cookies_into_one_header(RequestInterface $request, UriInterface $uri, Promise $promise) + { + $cookie = new Cookie('name', 'value', 86400, 'test.com'); + $cookie2 = new Cookie('name2', 'value2', 86400, 'test.com'); + + $this->cookieJar->addCookie($cookie); + $this->cookieJar->addCookie($cookie2); + + $request->getUri()->willReturn($uri); + $uri->getHost()->willReturn('test.com'); + $uri->getPath()->willReturn('/'); + + $request->withAddedHeader('Cookie', 'name=value; name2=value2')->willReturn($request); + + $this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) { + if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) { + return $promise->getWrappedObject(); + } + }, function () {}); + } + function it_does_not_load_cookie_if_expired(RequestInterface $request, UriInterface $uri, Promise $promise) { $cookie = new Cookie('name', 'value', null, 'test.com', false, false, null, (new \DateTime())->modify('-1 day')); diff --git a/src/Plugin/CookiePlugin.php b/src/Plugin/CookiePlugin.php index 59ee90d..523628f 100644 --- a/src/Plugin/CookiePlugin.php +++ b/src/Plugin/CookiePlugin.php @@ -38,6 +38,7 @@ public function __construct(CookieJar $cookieJar) */ public function handleRequest(RequestInterface $request, callable $next, callable $first) { + $cookies = []; foreach ($this->cookieJar->getCookies() as $cookie) { if ($cookie->isExpired()) { continue; @@ -55,7 +56,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl continue; } - $request = $request->withAddedHeader('Cookie', sprintf('%s=%s', $cookie->getName(), $cookie->getValue())); + $cookies[] = sprintf('%s=%s', $cookie->getName(), $cookie->getValue()); + } + + if (!empty($cookies)) { + $request = $request->withAddedHeader('Cookie', implode('; ', array_unique($cookies))); } return $next($request)->then(function (ResponseInterface $response) use ($request) {