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

Skip to content

Commit 2b3026f

Browse files
committed
review
1 parent 2eca954 commit 2b3026f

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

UPGRADE-6.3.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ UPGRADE FROM 6.2 to 6.3
44
HttpFoundation
55
--------------
66

7-
* Deprecate calling `Response::sendHeaders()` without any arguments
7+
* `Response::sendHeaders()` now takes an optional `$statusCode` parameter

src/Symfony/Component/HttpFoundation/Response.php

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ class Response
211211
511 => 'Network Authentication Required', // RFC6585
212212
];
213213

214+
/**
215+
* @var array<string, bool>
216+
*
217+
* Tracks headers already sent in informational responses
218+
*/
219+
private array $sentHeaders;
220+
214221
/**
215222
* @param int $status The HTTP status code (200 "OK" by default)
216223
*
@@ -332,47 +339,51 @@ public function prepare(Request $request): static
332339
*/
333340
public function sendHeaders(/* ?int $statusCode = null */): static
334341
{
335-
if (1 > \func_num_args()) {
336-
trigger_deprecation('symfony/http-foundation', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
337-
338-
$statusCode = null;
339-
} else {
340-
$statusCode = func_get_arg(0) ?: null;
341-
}
342-
343342
// headers have already been sent by the developer
344343
if (headers_sent()) {
345344
return $this;
346345
}
347346

347+
$statusCode = \func_num_args() > 0 ? func_get_arg(0) : null;
348+
$informationalResponse = $statusCode >= 100 && $statusCode < 200;
349+
if ($informationalResponse && !\function_exists('headers_send')) {
350+
// skip informational responses if not supported by the SAPI
351+
return $this;
352+
}
353+
348354
// headers
349355
foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) {
356+
$previousValues = $this->sentHeaders[$name] ?? null;
357+
if ($previousValues === $values) {
358+
// Header already sent in a previous response, it will be automatically copied in this response by PHP
359+
continue;
360+
}
361+
350362
$replace = 0 === strcasecmp($name, 'Content-Type');
351-
foreach ($values as $value) {
363+
364+
$newValues = null === $previousValues ? $values : array_diff($values, $previousValues);
365+
foreach ($newValues as $value) {
352366
header($name.': '.$value, $replace, $this->statusCode);
353367
}
368+
369+
if ($informationalResponse) {
370+
$this->sentHeaders[$name] = $values;
371+
}
354372
}
355373

356374
// cookies
357375
foreach ($this->headers->getCookies() as $cookie) {
358376
header('Set-Cookie: '.$cookie, false, $this->statusCode);
359377
}
360378

361-
if ($statusCode) {
362-
if (\function_exists('headers_send')) {
363-
headers_send($statusCode);
379+
if ($informationalResponse) {
380+
headers_send($statusCode);
364381

365-
return $this;
366-
}
367-
368-
if ($statusCode >= 100 && $statusCode < 200) {
369-
// skip informational responses if not supported by the SAPI
370-
return $this;
371-
}
372-
} else {
373-
$statusCode = $this->statusCode;
382+
return $this;
374383
}
375384

385+
$statusCode ??= $this->statusCode;
386+
376387
// status
377388
header(sprintf('HTTP/%s %s %s', $this->version, $statusCode, $this->statusText), true, $statusCode);
378389

src/Symfony/Component/HttpFoundation/StreamedResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function setCallback(callable $callback): static
6363
*
6464
* @return $this
6565
*/
66-
public function sendHeaders(): static
66+
public function sendHeaders(/* ?int $statusCode = null */): static
6767
{
6868
if ($this->headersSent) {
6969
return $this;

0 commit comments

Comments
 (0)