Skip to content

Fix Cookie handling as per RFC 6265 Section 5.4 #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/Plugin/CookiePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down
7 changes: 6 additions & 1 deletion src/Plugin/CookiePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down