From 4f6d6c95bba134d5ec974582e3a790558c1e6569 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Tue, 20 Dec 2016 11:58:16 +0100 Subject: [PATCH] Response::setCode() added $reason --- src/Http/Response.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Http/Response.php b/src/Http/Response.php index 4dc38697..46db6df6 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -54,11 +54,12 @@ public function __construct() /** * Sets HTTP response code. * @param int + * @param string * @return static * @throws Nette\InvalidArgumentException if code is invalid * @throws Nette\InvalidStateException if HTTP headers have been sent */ - public function setCode($code) + public function setCode($code, $reason = NULL) { $code = (int) $code; if ($code < 100 || $code > 599) { @@ -66,7 +67,20 @@ public function setCode($code) } self::checkHeaders(); $this->code = $code; - http_response_code($code); + + static $hasReason = [ // hardcoded in PHP + 100, 101, + 200, 201, 202, 203, 204, 205, 206, + 300, 301, 302, 303, 304, 305, 307, 308, + 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 426, 428, 429, 431, + 500, 501, 502, 503, 504, 505, 506, 511, + ]; + if ($reason || !in_array($code, $hasReason, TRUE)) { + $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'; + header("$protocol $code " . ($reason ?: 'Unknown status')); + } else { + http_response_code($code); + } return $this; }