From 74835ba54eca99a38f374f7a6d932fa510124773 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Wed, 14 Aug 2024 15:55:58 +0200 Subject: [PATCH] Fix conversion of partitioned cookies in the PSR-7 bridge --- Factory/HttpFoundationFactory.php | 79 +------------------------------ 1 file changed, 1 insertion(+), 78 deletions(-) diff --git a/Factory/HttpFoundationFactory.php b/Factory/HttpFoundationFactory.php index b1ee25a..cad798e 100644 --- a/Factory/HttpFoundationFactory.php +++ b/Factory/HttpFoundationFactory.php @@ -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) {