Skip to content

Commit

Permalink
Fix conversion of partitioned cookies in the PSR-7 bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
stof committed Aug 14, 2024
1 parent 89a2464 commit 74835ba
Showing 1 changed file with 1 addition and 78 deletions.
79 changes: 1 addition & 78 deletions Factory/HttpFoundationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,89 132,12 @@ public function createResponse(ResponseInterface $psrResponse, bool $streamed =
$response->setProtocolVersion($psrResponse->getProtocolVersion());

foreach ($cookies as $cookie) {
$response->headers->setCookie($this->createCookie($cookie));
$response->headers->setCookie(Cookie::fromString($cookie));
}

return $response;
}

/**
* Creates a Cookie instance from a cookie string.
*
* Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
*
* @throws \InvalidArgumentException
*/
private function createCookie(string $cookie): Cookie
{
foreach (explode(';', $cookie) as $part) {
$part = trim($part);

$data = explode('=', $part, 2);
$name = $data[0];
$value = isset($data[1]) ? trim($data[1], " \n\r\t\0\x0B\"") : null;

if (!isset($cookieName)) {
$cookieName = $name;
$cookieValue = $value;

continue;
}

if ('expires' === strtolower($name) && null !== $value) {
$cookieExpire = new \DateTime($value);

continue;
}

if ('path' === strtolower($name) && null !== $value) {
$cookiePath = $value;

continue;
}

if ('domain' === strtolower($name) && null !== $value) {
$cookieDomain = $value;

continue;
}

if ('secure' === strtolower($name)) {
$cookieSecure = true;

continue;
}

if ('httponly' === strtolower($name)) {
$cookieHttpOnly = true;

continue;
}

if ('samesite' === strtolower($name) && null !== $value) {
$samesite = $value;

continue;
}
}

if (!isset($cookieName)) {
throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.');
}

return new Cookie(
$cookieName,
$cookieValue,
$cookieExpire ?? 0,
$cookiePath ?? '/',
$cookieDomain ?? null,
isset($cookieSecure),
isset($cookieHttpOnly),
true,
$samesite ?? null
);
}

private function createStreamedResponseCallback(StreamInterface $body): callable
{
return function () use ($body) {
Expand Down

0 comments on commit 74835ba

Please sign in to comment.