NEW Add version to HTTPRequest and create raw string representation

This commit is contained in:
Daniel Hensby 2018-06-13 15:28:37 +01:00
parent 1b425570cf
commit a88257efac
No known key found for this signature in database
GPG Key ID: B00D1E9767F0B06E

View File

@ -19,7 +19,7 @@ class HTTPResponse
/** /**
* @var array * @var array
*/ */
protected static $status_codes = array( protected static $status_codes = [
100 => 'Continue', 100 => 'Continue',
101 => 'Switching Protocols', 101 => 'Switching Protocols',
200 => 'OK', 200 => 'OK',
@ -61,20 +61,25 @@ class HTTPResponse
503 => 'Service Unavailable', 503 => 'Service Unavailable',
504 => 'Gateway Timeout', 504 => 'Gateway Timeout',
505 => 'HTTP Version Not Supported', 505 => 'HTTP Version Not Supported',
); ];
/** /**
* @var array * @var array
*/ */
protected static $redirect_codes = array( protected static $redirect_codes = [
301, 301,
302, 302,
303, 303,
304, 304,
305, 305,
307, 307,
308 308,
); ];
/**
* @var string
*/
protected $version = '1.0';
/** /**
* @var int * @var int
@ -92,9 +97,9 @@ class HTTPResponse
* @see http://en.wikipedia.org/wiki/List_of_HTTP_headers * @see http://en.wikipedia.org/wiki/List_of_HTTP_headers
* @var array * @var array
*/ */
protected $headers = array( protected $headers = [
"content-type" => "text/html; charset=utf-8", "content-type" => "text/html; charset=utf-8",
); ];
/** /**
* @var string * @var string
@ -109,12 +114,31 @@ class HTTPResponse
* @param string $statusDescription The text to be given alongside the status code. * @param string $statusDescription The text to be given alongside the status code.
* See {@link setStatusCode()} for more information. * See {@link setStatusCode()} for more information.
*/ */
public function __construct($body = null, $statusCode = null, $statusDescription = null) public function __construct($body = null, $statusCode = null, $statusDescription = null, $version = null)
{ {
$this->setBody($body); $this->setBody($body);
if ($statusCode) { if ($statusCode) {
$this->setStatusCode($statusCode, $statusDescription); $this->setStatusCode($statusCode, $statusDescription);
} }
if (!$version) {
if (preg_match('/HTTP\/(\d+(\.\d+)?)/i', $_SERVER['SERVER_PROTOCOL'], $matches)) {
$version = $matches[1];
}
}
$this->setVersion($version);
}
/**
* The HTTP version used to respond to this request (typically 1.0 or 1.1)
*
* @param string $version
*
* @return $this
*/
public function setVersion($version)
{
$this->version = $version;
return $this;
} }
/** /**
@ -123,6 +147,7 @@ class HTTPResponse
* No newlines are allowed in the description. * No newlines are allowed in the description.
* If omitted, will default to the standard HTTP description * If omitted, will default to the standard HTTP description
* for the given $code value (see {@link $status_codes}). * for the given $code value (see {@link $status_codes}).
*
* @return $this * @return $this
*/ */
public function setStatusCode($code, $description = null) public function setStatusCode($code, $description = null)
@ -146,6 +171,7 @@ class HTTPResponse
* Caution: Will be overwritten by {@link setStatusCode()}. * Caution: Will be overwritten by {@link setStatusCode()}.
* *
* @param string $description * @param string $description
*
* @return $this * @return $this
*/ */
public function setStatusDescription($description) public function setStatusDescription($description)
@ -154,6 +180,14 @@ class HTTPResponse
return $this; return $this;
} }
/**
* @return string
*/
public function getVersion()
{
return $this->version;
}
/** /**
* @return int * @return int
*/ */
@ -167,7 +201,7 @@ class HTTPResponse
*/ */
public function getStatusDescription() public function getStatusDescription()
{ {
return str_replace(array("\r","\n"), '', $this->statusDescription); return str_replace(["\r", "\n"], '', $this->statusDescription);
} }
/** /**
@ -183,11 +217,12 @@ class HTTPResponse
/** /**
* @param string $body * @param string $body
*
* @return $this * @return $this
*/ */
public function setBody($body) public function setBody($body)
{ {
$this->body = $body ? (string) $body : $body; // Don't type-cast false-ish values, eg null is null not '' $this->body = $body ? (string)$body : $body; // Don't type-cast false-ish values, eg null is null not ''
return $this; return $this;
} }
@ -204,6 +239,7 @@ class HTTPResponse
* *
* @param string $header Example: "content-type" * @param string $header Example: "content-type"
* @param string $value Example: "text/xml" * @param string $value Example: "text/xml"
*
* @return $this * @return $this
*/ */
public function addHeader($header, $value) public function addHeader($header, $value)
@ -217,6 +253,7 @@ class HTTPResponse
* Return the HTTP header of the given name. * Return the HTTP header of the given name.
* *
* @param string $header * @param string $header
*
* @return string * @return string
*/ */
public function getHeader($header) public function getHeader($header)
@ -241,6 +278,7 @@ class HTTPResponse
* e.g. "Content-Type". * e.g. "Content-Type".
* *
* @param string $header * @param string $header
*
* @return $this * @return $this
*/ */
public function removeHeader($header) public function removeHeader($header)
@ -253,6 +291,7 @@ class HTTPResponse
/** /**
* @param string $dest * @param string $dest
* @param int $code * @param int $code
*
* @return $this * @return $this
*/ */
public function redirect($dest, $code = 302) public function redirect($dest, $code = 302)
@ -322,7 +361,7 @@ EOT
); );
header($method); header($method);
foreach ($this->getHeaders() as $header => $value) { foreach ($this->getHeaders() as $header => $value) {
header("{$header}: {$value}", true, $this->getStatusCode()); header("{$header}: {$value}", true, $this->getStatusCode());
} }
} elseif ($this->getStatusCode() >= 300) { } elseif ($this->getStatusCode() >= 300) {
// It's critical that these status codes are sent; we need to report a failure if not. // It's critical that these status codes are sent; we need to report a failure if not.
@ -351,9 +390,9 @@ EOT
/** @var HandlerInterface $handler */ /** @var HandlerInterface $handler */
$handler = Injector::inst()->get(HandlerInterface::class); $handler = Injector::inst()->get(HandlerInterface::class);
$formatter = $handler->getFormatter(); $formatter = $handler->getFormatter();
echo $formatter->format(array( echo $formatter->format([
'code' => $this->statusCode 'code' => $this->statusCode,
)); ]);
} else { } else {
echo $this->body; echo $this->body;
} }
@ -379,4 +418,23 @@ EOT
{ {
return in_array($this->getStatusCode(), self::$redirect_codes); return in_array($this->getStatusCode(), self::$redirect_codes);
} }
/**
* The HTTP response represented as a raw string
*
* @return string
*/
public function __toString()
{
$headers = [];
foreach ($this->getHeaders() as $header => $values) {
foreach ((array)$values as $value) {
$headers[] = sprintf('%s: %s', $header, $value);
}
}
return
sprintf('HTTP/%s %s %s', $this->getVersion(), $this->getStatusCode(), $this->getStatusDescription()) . "\r\n" .
implode("\r\n", $headers) . "\r\n" . "\r\n" .
$this->getBody();
}
} }