Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ccdcd9b

Browse files
committed
bug #58010 [PsrHttpMessageBridge] Fix conversion of partitioned cookies in the PSR-7 bridge (stof)
This PR was submitted for the 7.2 branch but it was merged into the 6.4 branch instead. Discussion ---------- [PsrHttpMessageBridge] Fix conversion of partitioned cookies in the PSR-7 bridge | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | n/a | License | MIT `symfony/http-foundation` supports partitioned cookies in 6.4+. However, the PSR-7 was failing to convert such partitioned cookies when creating an HttpFoundation Response because it used its own parsing logic for the `Set-Cookie` header instead of reusing the logic exposed by HttpFoundation (since Symfony 3.3), and that logic was outdated. I fixed that by using `Cookie::fromString` for the parsing of the cookie. Note that the logic in the bridge was also only testing a few cases, while HttpFoundation has a more extensive test coverage for its parsing algorithm. And there is no reason to maintain such parsing twice (with totally different implementations) Commits ------- be27495 Fix conversion of partitioned cookies in the PSR-7 bridge
2 parents 6733efd + be27495 commit ccdcd9b

File tree

1 file changed

+1
-78
lines changed

1 file changed

+1
-78
lines changed

src/Symfony/Bridge/PsrHttpMessage/Factory/HttpFoundationFactory.php

Lines changed: 1 addition & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -132,89 +132,12 @@ public function createResponse(ResponseInterface $psrResponse, bool $streamed =
132132
$response->setProtocolVersion($psrResponse->getProtocolVersion());
133133

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

138138
return $response;
139139
}
140140

141-
/**
142-
* Creates a Cookie instance from a cookie string.
143-
*
144-
* Some snippets have been taken from the Guzzle project: https://github.com/guzzle/guzzle/blob/5.3/src/Cookie/SetCookie.php#L34
145-
*
146-
* @throws \InvalidArgumentException
147-
*/
148-
private function createCookie(string $cookie): Cookie
149-
{
150-
foreach (explode(';', $cookie) as $part) {
151-
$part = trim($part);
152-
153-
$data = explode('=', $part, 2);
154-
$name = $data[0];
155-
$value = isset($data[1]) ? trim($data[1], " \n\r\t\0\x0B\"") : null;
156-
157-
if (!isset($cookieName)) {
158-
$cookieName = $name;
159-
$cookieValue = $value;
160-
161-
continue;
162-
}
163-
164-
if ('expires' === strtolower($name) && null !== $value) {
165-
$cookieExpire = new \DateTime($value);
166-
167-
continue;
168-
}
169-
170-
if ('path' === strtolower($name) && null !== $value) {
171-
$cookiePath = $value;
172-
173-
continue;
174-
}
175-
176-
if ('domain' === strtolower($name) && null !== $value) {
177-
$cookieDomain = $value;
178-
179-
continue;
180-
}
181-
182-
if ('secure' === strtolower($name)) {
183-
$cookieSecure = true;
184-
185-
continue;
186-
}
187-
188-
if ('httponly' === strtolower($name)) {
189-
$cookieHttpOnly = true;
190-
191-
continue;
192-
}
193-
194-
if ('samesite' === strtolower($name) && null !== $value) {
195-
$samesite = $value;
196-
197-
continue;
198-
}
199-
}
200-
201-
if (!isset($cookieName)) {
202-
throw new \InvalidArgumentException('The value of the Set-Cookie header is malformed.');
203-
}
204-
205-
return new Cookie(
206-
$cookieName,
207-
$cookieValue,
208-
$cookieExpire ?? 0,
209-
$cookiePath ?? '/',
210-
$cookieDomain ?? null,
211-
isset($cookieSecure),
212-
isset($cookieHttpOnly),
213-
true,
214-
$samesite ?? null
215-
);
216-
}
217-
218141
private function createStreamedResponseCallback(StreamInterface $body): callable
219142
{
220143
return function () use ($body) {

0 commit comments

Comments
 (0)