Skip to content

Commit b9f06ea

Browse files
committed
Response::setCode() added $reason
1 parent 0221544 commit b9f06ea

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/Http/Response.php

+19-2
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,36 @@ public function __construct()
5454
/**
5555
* Sets HTTP response code.
5656
* @param int
57+
* @param string
5758
* @return static
5859
* @throws Nette\InvalidArgumentException if code is invalid
5960
* @throws Nette\InvalidStateException if HTTP headers have been sent
6061
*/
61-
public function setCode($code)
62+
public function setCode($code, $reason = NULL)
6263
{
6364
$code = (int) $code;
6465
if ($code < 100 || $code > 599) {
6566
throw new Nette\InvalidArgumentException("Bad HTTP response '$code'.");
6667
}
6768
self::checkHeaders();
6869
$this->code = $code;
69-
http_response_code($code);
70+
71+
static $hasReason = [ // hardcoded in PHP
72+
100, 101,
73+
200, 201, 202, 203, 204, 205, 206,
74+
300, 301, 302, 303, 304, 305, 307, 308,
75+
400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 426, 428, 429, 431,
76+
500, 501, 502, 503, 504, 505, 506, 511,
77+
];
78+
if (!$reason && !in_array($code, $hasReason, TRUE)) {
79+
$reason = 'Unknown status';
80+
}
81+
if ($reason) {
82+
$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1';
83+
header("$protocol $code $reason");
84+
} else {
85+
http_response_code($code);
86+
}
7087
return $this;
7188
}
7289

0 commit comments

Comments
 (0)