-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[HttpFoundation] Add support for the 103 status code (Early Hints) and other 1XX statuses #48128
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -211,6 +211,11 @@ class Response | |||||||||||||||||||||||||||||||||||||||||
511 => 'Network Authentication Required', // RFC6585 | ||||||||||||||||||||||||||||||||||||||||||
]; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||
* Tracks headers already sent in informational responses. | ||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||
private array $sentHeaders; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||
* @param int $status The HTTP status code (200 "OK" by default) | ||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -326,30 +331,71 @@ public function prepare(Request $request): static | |||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||
* Sends HTTP headers. | ||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||
* @param null|positive-int $statusCode The status code to use, override the statusCode property if set and not null | ||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||
* @return $this | ||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||
public function sendHeaders(): static | ||||||||||||||||||||||||||||||||||||||||||
public function sendHeaders(/* int $statusCode = null */): static | ||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||
// headers have already been sent by the developer | ||||||||||||||||||||||||||||||||||||||||||
if (headers_sent()) { | ||||||||||||||||||||||||||||||||||||||||||
dunglas marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||
return $this; | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
$statusCode = \func_num_args() > 0 ? func_get_arg(0) : null; | ||||||||||||||||||||||||||||||||||||||||||
$informationalResponse = $statusCode >= 100 && $statusCode < 200; | ||||||||||||||||||||||||||||||||||||||||||
if ($informationalResponse && !\function_exists('headers_send')) { | ||||||||||||||||||||||||||||||||||||||||||
// skip informational responses if not supported by the SAPI | ||||||||||||||||||||||||||||||||||||||||||
return $this; | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// headers | ||||||||||||||||||||||||||||||||||||||||||
foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) { | ||||||||||||||||||||||||||||||||||||||||||
$replace = 0 === strcasecmp($name, 'Content-Type'); | ||||||||||||||||||||||||||||||||||||||||||
foreach ($values as $value) { | ||||||||||||||||||||||||||||||||||||||||||
$newValues = $values; | ||||||||||||||||||||||||||||||||||||||||||
$replace = false; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// As recommended by RFC 8297, PHP automatically copies headers from previous 103 responses, we need to deal with that if headers changed | ||||||||||||||||||||||||||||||||||||||||||
if (103 === $statusCode) { | ||||||||||||||||||||||||||||||||||||||||||
$previousValues = $this->sentHeaders[$name] ?? null; | ||||||||||||||||||||||||||||||||||||||||||
if ($previousValues === $values) { | ||||||||||||||||||||||||||||||||||||||||||
// Header already sent in a previous response, it will be automatically copied in this response by PHP | ||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
$replace = 0 === strcasecmp($name, 'Content-Type'); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
if (null !== $previousValues && array_diff($previousValues, $values)) { | ||||||||||||||||||||||||||||||||||||||||||
header_remove($name); | ||||||||||||||||||||||||||||||||||||||||||
$previousValues = null; | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
$newValues = null === $previousValues ? $values : array_diff($values, $previousValues); | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
foreach ($newValues as $value) { | ||||||||||||||||||||||||||||||||||||||||||
header($name.': '.$value, $replace, $this->statusCode); | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
if ($informationalResponse) { | ||||||||||||||||||||||||||||||||||||||||||
$this->sentHeaders[$name] = $values; | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// cookies | ||||||||||||||||||||||||||||||||||||||||||
foreach ($this->headers->getCookies() as $cookie) { | ||||||||||||||||||||||||||||||||||||||||||
header('Set-Cookie: '.$cookie, false, $this->statusCode); | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
if ($informationalResponse) { | ||||||||||||||||||||||||||||||||||||||||||
headers_send($statusCode); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
return $this; | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
384
to
+393
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally would not send the As the session and so on could be changing and that handling already complex in the
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
$statusCode ??= $this->statusCode; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// status | ||||||||||||||||||||||||||||||||||||||||||
header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode); | ||||||||||||||||||||||||||||||||||||||||||
header(sprintf('HTTP/%s %s %s', $this->version, $statusCode, $this->statusText), true, $statusCode); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
return $this; | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would exposing a getter improve testability of this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest keeping that entirely internal. WDYT?