Skip to content

Commit

Permalink
Merge 52e3bb1 into d03c0f6
Browse files Browse the repository at this point in the history
  • Loading branch information
kelunik committed Jun 10, 2019
2 parents d03c0f6 + 52e3bb1 commit 94c147d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/Cookie/ResponseCookie.php
Expand Up @@ -22,6 +22,9 @@ final class ResponseCookie
'D M d H:i:s Y T',
];

/** @var string[] */
private $unknownAttributes = [];

/**
* Parses a cookie from a 'set-cookie' header.
*
Expand Down Expand Up @@ -49,6 +52,7 @@ public static function fromHeader(string $string)

// httpOnly must default to false for parsing
$meta = CookieAttributes::empty();
$unknownAttributes = [];

foreach ($parts as $part) {
$pieces = \array_map('trim', \explode('=', $part, 2));
Expand All @@ -63,6 +67,10 @@ public static function fromHeader(string $string)
case 'httponly':
$meta = $meta->withHttpOnly();
break;

default:
$unknownAttributes[] = $part;
break;
}
} else {
switch ($key) {
Expand Down Expand Up @@ -94,12 +102,19 @@ public static function fromHeader(string $string)
case 'domain':
$meta = $meta->withDomain($pieces[1]);
break;

default:
$unknownAttributes[] = $part;
break;
}
}
}

try {
return new self($name, $value, $meta);
$cookie = new self($name, $value, $meta);
$cookie->unknownAttributes = $unknownAttributes;

return $cookie;
} catch (InvalidCookieException $e) {
return null;
}
Expand Down Expand Up @@ -327,6 +342,11 @@ public function __toString(): string
$line = $this->name . '=' . $this->value;
$line .= $this->attributes;

$unknownAttributes = \implode('; ', $this->unknownAttributes);
if ($unknownAttributes !== '') {
$line .= '; ' . $unknownAttributes;
}

return $line;
}
}
10 changes: 10 additions & 0 deletions test/Cookie/ResponseCookieTest.php
Expand Up @@ -248,4 +248,14 @@ public function testInvalidCookieValueModify()

$cookie->withValue('what is this');
}

public function testPreservesUnknownAttributes()
{
$cookie = ResponseCookie::fromHeader('key=value; HttpOnly; SameSite=strict;Foobar');
$this->assertNotNull($cookie);
$this->assertSame('key', $cookie->getName());
$this->assertSame('value', $cookie->getValue());
$this->assertTrue($cookie->isHttpOnly());
$this->assertSame('key=value; HttpOnly; SameSite=strict; Foobar', (string) $cookie);
}
}

0 comments on commit 94c147d

Please sign in to comment.