Fixed Http\Response::setCode producing invalid HTTP header#113
Fixed Http\Response::setCode producing invalid HTTP header#113dg merged 1 commit intonette:masterfrom slevomat:code
Conversation
|
I'd rather use default reason for each code or empty string for unknown codes. |
|
It seems that it is related to webserver. It works with Apache or CGI and doesn't work with nginx. |
|
Yep, it doesn't work in nginx + php-fpm, I don't know about Apache. But the code is obviously missing the additional required space: Maybe Apache appends it automatically and nginx doesn't, but I don't expect my webserver to do that. |
|
Yes, because it was completed automatically, I've used this solution. I didn't know it doesn't work with nginx. Thanks for the fix. |
|
Great, thanks! 👍 |
|
@dg @ondrejmirtes This change is a BC break for people running PHP as an Apache module. See https://secure.php.net/manual/en/function.http-response-code.php#114996 for details. Edit: So, it turns out it is probably not a BC break, because The So I think that best we can do, is to change the public function setCode($code, $reasonPhrase = NULL)
{
$code = (int) $code;
if ($code < 100 || $code > 599) {
throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
}
self::checkHeaders();
$this->code = $code;
if ($reasonPhrase !== NULL) {
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
header("$protocol $code $reasonPhrase", TRUE, $code);
} else {
http_response_code($code);
}
return $this;
} |
|
@JanTvrdik http_response_code() works the same way as header() worked. |
|
@JanTvrdik I have added something similar. |
According to RFC 7230 (HTTP/1.1), the first line of a HTTP response must be:
When setting a custom code through the setCode method, Nette omits this part:
This is easily fixed by calling the
http_response_codefunction that's in PHP.We bumped into this issue when trying to call our own application with Guzzle which has a strict parsing rules of HTTP responses. It requires the space to be there: