Permalink
Browse files

ENHANCEMENT Added SS_HTTPResponse->setStatusDescription() as equivale…

…nt to setStatusCode(). Added documentation.

BUGFIX Strip newlines and carriage returns from SS_HTTPResponse->getStatusDescription() (fixes #6222, thanks mattclegg)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@114082 467b73ca-7a2a-4603-9d3b-597d59a354a9
  • Loading branch information...
1 parent 3725ab7 commit b15019a95f7a6e71ff1d7b49f57f868a6ebfe686 @chillu chillu committed Nov 23, 2010
Showing with 54 additions and 2 deletions.
  1. +38 −2 core/control/HTTPResponse.php
  2. +16 −0 tests/control/HTTPResponseTest.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 @@ function setStatusCode($code, $description = null) {
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 @@ function getStatusCode() {
* @return string Description for a HTTP status code
*/
function getStatusDescription() {
- return $this->statusDescription;
+ return str_replace(array("\r","\n"), '', $this->statusDescription);
}
/**
@@ -0,0 +1,16 @@
+<?php
+/**
+ * @package sapphire
+ * @subpackage tests
+ */
+class HTTPResponseTest extends SapphireTest {
+
+ function testStatusDescriptionStripsNewlines() {
+ $r = new SS_HTTPResponse('my body', 200, "my description \nwith newlines \rand carriage returns");
+ $this->assertEquals(
+ "my description with newlines and carriage returns",
+ $r->getStatusDescription()
+ );
+ }
+
+}

0 comments on commit b15019a

Please sign in to comment.