From 43c85e3c9a24bf9b48953ff977439d8f10e3a4ed Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Tue, 23 Nov 2010 19:39:17 +0000 Subject: [PATCH] ENHANCEMENT Added SS_HTTPResponse->setStatusDescription() as equivalent to setStatusCode(). Added documentation. BUGFIX Strip newlines and carriage returns from SS_HTTPResponse->getStatusDescription() (fixes #6222, thanks mattclegg) (from r114082) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@114083 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- core/control/HTTPResponse.php | 40 ++++++++++++++++++++++++++++-- tests/control/HTTPResponseTest.php | 16 ++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 tests/control/HTTPResponseTest.php diff --git a/core/control/HTTPResponse.php b/core/control/HTTPResponse.php index c58c1e896..74e6d1d85 100755 --- a/core/control/HTTPResponse.php +++ b/core/control/HTTPResponse.php @@ -6,6 +6,10 @@ * @subpackage control */ class SS_HTTPResponse { + + /** + * @var array + */ protected static $status_codes = array( 100 => 'Continue', 101 => 'Switching Protocols', @@ -48,6 +52,9 @@ class SS_HTTPResponse { 505 => 'HTTP Version Not Supported', ); + /** + * @var array + */ protected static $redirect_codes = array( 301, 302, @@ -57,7 +64,14 @@ class SS_HTTPResponse { 307 ); + /** + * @var Int + */ protected $statusCode = 200; + + /** + * @var String + */ protected $statusDescription = "OK"; /** @@ -77,15 +91,24 @@ class SS_HTTPResponse { /** * Create a new HTTP response + * * @param $body The body of the response * @param $statusCode The numeric status code - 200, 404, etc - * @param $statusDescription The text to be given alongside the status code. This can be accessed by javascript + * @param $statusDescription The text to be given alongside the status code. + * See {@link setStatusCode()} for more information. */ function __construct($body = null, $statusCode = null, $statusDescription = null) { $this->body = $body; if($statusCode) $this->setStatusCode($statusCode, $statusDescription); } + /** + * @param String $code + * @param String $description Optional. See {@link setStatusDescription()}. + * No newlines are allowed in the description. + * If omitted, will default to the standard HTTP description + * for the given $code value (see {@link $status_codes}). + */ function setStatusCode($code, $description = null) { if(isset(self::$status_codes[$code])) $this->statusCode = $code; else user_error("Unrecognised HTTP status code '$code'", E_USER_WARNING); @@ -94,6 +117,19 @@ class SS_HTTPResponse { else $this->statusDescription = self::$status_codes[$code]; } + /** + * The text to be given alongside the status code ("reason phrase"). + * Caution: Will be overwritten by {@link setStatusCode()}. + * + * @param String $description + */ + function setStatusDescription($description) { + $this->statusDescription = $description; + } + + /** + * @return Int + */ function getStatusCode() { return $this->statusCode; } @@ -102,7 +138,7 @@ class SS_HTTPResponse { * @return string Description for a HTTP status code */ function getStatusDescription() { - return $this->statusDescription; + return str_replace(array("\r","\n"), '', $this->statusDescription); } /** diff --git a/tests/control/HTTPResponseTest.php b/tests/control/HTTPResponseTest.php new file mode 100644 index 000000000..9524d5199 --- /dev/null +++ b/tests/control/HTTPResponseTest.php @@ -0,0 +1,16 @@ +assertEquals( + "my description with newlines and carriage returns", + $r->getStatusDescription() + ); + } + +} \ No newline at end of file