2007-08-17 05:32:14 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\Control;
|
|
|
|
|
|
|
|
use InvalidArgumentException;
|
2017-05-11 07:38:29 +02:00
|
|
|
use Monolog\Handler\HandlerInterface;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Convert;
|
2017-03-02 03:24:38 +01:00
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
|
|
|
use SilverStripe\View\Requirements;
|
|
|
|
|
2007-08-17 05:32:14 +02:00
|
|
|
/**
|
API CHANGE: Renamed conflicting classes to have an "SS_" namespace, and renamed existing "SS" namespace to "SS_". The affected classes are: HTTPRequest, HTTPResponse, Query, Database, SSBacktrace, SSCli, SSDatetime, SSDatetimeTest, SSLog, SSLogTest, SSLogEmailWriter, SSLogErrorEmailFormatter, SSLogErrorFileFormatter, SSLogFileWriter and SSZendLog.
MINOR: Replaced usage of renamed classes with the new namespaced name.
From: Andrew Short <andrewjshort@gmail.com>
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@90075 467b73ca-7a2a-4603-9d3b-597d59a354a9
2009-10-26 04:06:31 +01:00
|
|
|
* Represents a response returned by a controller.
|
2007-08-17 05:32:14 +02:00
|
|
|
*/
|
2016-11-29 00:31:16 +01:00
|
|
|
class HTTPResponse
|
|
|
|
{
|
2017-03-02 03:24:38 +01:00
|
|
|
use Injectable;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-06-13 16:28:37 +02:00
|
|
|
protected static $status_codes = [
|
2016-11-29 00:31:16 +01:00
|
|
|
100 => 'Continue',
|
|
|
|
101 => 'Switching Protocols',
|
|
|
|
200 => 'OK',
|
|
|
|
201 => 'Created',
|
|
|
|
202 => 'Accepted',
|
|
|
|
203 => 'Non-Authoritative Information',
|
|
|
|
204 => 'No Content',
|
|
|
|
205 => 'Reset Content',
|
|
|
|
206 => 'Partial Content',
|
|
|
|
301 => 'Moved Permanently',
|
|
|
|
302 => 'Found',
|
|
|
|
303 => 'See Other',
|
|
|
|
304 => 'Not Modified',
|
|
|
|
305 => 'Use Proxy',
|
|
|
|
307 => 'Temporary Redirect',
|
2017-10-18 21:27:00 +02:00
|
|
|
308 => 'Permanent Redirect',
|
2016-11-29 00:31:16 +01:00
|
|
|
400 => 'Bad Request',
|
|
|
|
401 => 'Unauthorized',
|
|
|
|
403 => 'Forbidden',
|
|
|
|
404 => 'Not Found',
|
|
|
|
405 => 'Method Not Allowed',
|
|
|
|
406 => 'Not Acceptable',
|
|
|
|
407 => 'Proxy Authentication Required',
|
|
|
|
408 => 'Request Timeout',
|
|
|
|
409 => 'Conflict',
|
|
|
|
410 => 'Gone',
|
|
|
|
411 => 'Length Required',
|
|
|
|
412 => 'Precondition Failed',
|
|
|
|
413 => 'Request Entity Too Large',
|
|
|
|
414 => 'Request-URI Too Long',
|
|
|
|
415 => 'Unsupported Media Type',
|
|
|
|
416 => 'Request Range Not Satisfiable',
|
|
|
|
417 => 'Expectation Failed',
|
|
|
|
422 => 'Unprocessable Entity',
|
|
|
|
429 => 'Too Many Requests',
|
|
|
|
500 => 'Internal Server Error',
|
|
|
|
501 => 'Not Implemented',
|
|
|
|
502 => 'Bad Gateway',
|
|
|
|
503 => 'Service Unavailable',
|
|
|
|
504 => 'Gateway Timeout',
|
|
|
|
505 => 'HTTP Version Not Supported',
|
2018-06-13 16:28:37 +02:00
|
|
|
];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-06-13 16:28:37 +02:00
|
|
|
protected static $redirect_codes = [
|
2016-11-29 00:31:16 +01:00
|
|
|
301,
|
|
|
|
302,
|
|
|
|
303,
|
|
|
|
304,
|
|
|
|
305,
|
2017-10-18 21:27:00 +02:00
|
|
|
307,
|
2018-06-13 16:28:37 +02:00
|
|
|
308,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $version = '1.0';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $statusCode = 200;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $statusDescription = "OK";
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
2017-09-13 17:02:12 +02:00
|
|
|
* HTTP Headers like "content-type: text/xml"
|
2016-11-29 00:31:16 +01:00
|
|
|
*
|
|
|
|
* @see http://en.wikipedia.org/wiki/List_of_HTTP_headers
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-06-13 16:28:37 +02:00
|
|
|
protected $headers = [
|
2017-09-13 17:02:12 +02:00
|
|
|
"content-type" => "text/html; charset=utf-8",
|
2018-06-13 16:28:37 +02:00
|
|
|
];
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $body = null;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Create a new HTTP response
|
|
|
|
*
|
|
|
|
* @param string $body The body of the response
|
|
|
|
* @param int $statusCode The numeric status code - 200, 404, etc
|
|
|
|
* @param string $statusDescription The text to be given alongside the status code.
|
|
|
|
* See {@link setStatusCode()} for more information.
|
|
|
|
*/
|
2018-06-13 16:28:37 +02:00
|
|
|
public function __construct($body = null, $statusCode = null, $statusDescription = null, $version = null)
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
|
|
|
$this->setBody($body);
|
|
|
|
if ($statusCode) {
|
|
|
|
$this->setStatusCode($statusCode, $statusDescription);
|
|
|
|
}
|
2018-06-13 16:28:37 +02:00
|
|
|
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;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param int $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}).
|
2018-06-13 16:28:37 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setStatusCode($code, $description = null)
|
|
|
|
{
|
|
|
|
if (isset(self::$status_codes[$code])) {
|
|
|
|
$this->statusCode = $code;
|
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException("Unrecognised HTTP status code '$code'");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
if ($description) {
|
|
|
|
$this->statusDescription = $description;
|
|
|
|
} else {
|
|
|
|
$this->statusDescription = self::$status_codes[$code];
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* The text to be given alongside the status code ("reason phrase").
|
|
|
|
* Caution: Will be overwritten by {@link setStatusCode()}.
|
|
|
|
*
|
|
|
|
* @param string $description
|
2018-06-13 16:28:37 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setStatusDescription($description)
|
|
|
|
{
|
|
|
|
$this->statusDescription = $description;
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2018-06-13 16:28:37 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getVersion()
|
|
|
|
{
|
|
|
|
return $this->version;
|
|
|
|
}
|
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getStatusCode()
|
|
|
|
{
|
|
|
|
return $this->statusCode;
|
|
|
|
}
|
2008-08-11 05:03:52 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @return string Description for a HTTP status code
|
|
|
|
*/
|
|
|
|
public function getStatusDescription()
|
|
|
|
{
|
2018-06-13 16:28:37 +02:00
|
|
|
return str_replace(["\r", "\n"], '', $this->statusDescription);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this HTTP response is in error
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isError()
|
|
|
|
{
|
2017-05-12 05:17:38 +02:00
|
|
|
$statusCode = $this->getStatusCode();
|
|
|
|
return $statusCode && ($statusCode < 200 || $statusCode > 399);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param string $body
|
2018-06-13 16:28:37 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setBody($body)
|
|
|
|
{
|
2018-06-13 16:28:37 +02:00
|
|
|
$this->body = $body ? (string)$body : $body; // Don't type-cast false-ish values, eg null is null not ''
|
2016-11-29 00:31:16 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getBody()
|
|
|
|
{
|
|
|
|
return $this->body;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Add a HTTP header to the response, replacing any header of the same name.
|
|
|
|
*
|
2017-09-13 17:02:12 +02:00
|
|
|
* @param string $header Example: "content-type"
|
2016-11-29 00:31:16 +01:00
|
|
|
* @param string $value Example: "text/xml"
|
2018-06-13 16:28:37 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function addHeader($header, $value)
|
|
|
|
{
|
2017-09-13 17:02:12 +02:00
|
|
|
$header = strtolower($header);
|
2016-11-29 00:31:16 +01:00
|
|
|
$this->headers[$header] = $value;
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Return the HTTP header of the given name.
|
|
|
|
*
|
|
|
|
* @param string $header
|
2018-06-13 16:28:37 +02:00
|
|
|
*
|
2018-06-13 07:56:47 +02:00
|
|
|
* @return string
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function getHeader($header)
|
|
|
|
{
|
2017-09-13 17:02:12 +02:00
|
|
|
$header = strtolower($header);
|
2016-11-29 00:31:16 +01:00
|
|
|
if (isset($this->headers[$header])) {
|
|
|
|
return $this->headers[$header];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getHeaders()
|
|
|
|
{
|
|
|
|
return $this->headers;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Remove an existing HTTP header by its name,
|
|
|
|
* e.g. "Content-Type".
|
|
|
|
*
|
|
|
|
* @param string $header
|
2018-06-13 16:28:37 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function removeHeader($header)
|
|
|
|
{
|
2018-01-16 11:20:52 +01:00
|
|
|
$header = strtolower($header);
|
2016-11-29 00:31:16 +01:00
|
|
|
unset($this->headers[$header]);
|
|
|
|
return $this;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param string $dest
|
|
|
|
* @param int $code
|
2018-06-13 16:28:37 +02:00
|
|
|
*
|
2016-11-29 00:31:16 +01:00
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function redirect($dest, $code = 302)
|
|
|
|
{
|
|
|
|
if (!in_array($code, self::$redirect_codes)) {
|
|
|
|
trigger_error("Invalid HTTP redirect code {$code}", E_USER_WARNING);
|
|
|
|
$code = 302;
|
|
|
|
}
|
|
|
|
$this->setStatusCode($code);
|
2017-09-13 17:02:12 +02:00
|
|
|
$this->addHeader('location', $dest);
|
2016-11-29 00:31:16 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2007-08-17 05:32:14 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
2017-05-12 05:17:38 +02:00
|
|
|
* Send this HTTPResponse to the browser
|
2016-11-29 00:31:16 +01:00
|
|
|
*/
|
|
|
|
public function output()
|
|
|
|
{
|
|
|
|
// Attach appropriate X-Include-JavaScript and X-Include-CSS headers
|
|
|
|
if (Director::is_ajax()) {
|
|
|
|
Requirements::include_in_response($this);
|
|
|
|
}
|
2008-11-28 06:29:52 +01:00
|
|
|
|
2017-05-12 05:17:38 +02:00
|
|
|
if ($this->isRedirect() && headers_sent()) {
|
|
|
|
$this->htmlRedirect();
|
|
|
|
} else {
|
|
|
|
$this->outputHeaders();
|
|
|
|
$this->outputBody();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a browser redirect without setting headers
|
|
|
|
*/
|
|
|
|
protected function htmlRedirect()
|
|
|
|
{
|
|
|
|
$headersSent = headers_sent($file, $line);
|
2017-09-13 17:02:12 +02:00
|
|
|
$location = $this->getHeader('location');
|
2017-05-12 05:17:38 +02:00
|
|
|
$url = Director::absoluteURL($location);
|
|
|
|
$urlATT = Convert::raw2htmlatt($url);
|
|
|
|
$urlJS = Convert::raw2js($url);
|
|
|
|
$title = (Director::isDev() && $headersSent)
|
|
|
|
? "{$urlATT}... (output started on {$file}, line {$line})"
|
|
|
|
: "{$urlATT}...";
|
|
|
|
echo <<<EOT
|
2015-02-04 23:23:26 +01:00
|
|
|
<p>Redirecting to <a href="{$urlATT}" title="Click this link if your browser does not redirect you">{$title}</a></p>
|
|
|
|
<meta http-equiv="refresh" content="1; url={$urlATT}" />
|
2016-01-24 06:12:45 +01:00
|
|
|
<script type="application/javascript">setTimeout(function(){
|
2015-02-04 23:23:26 +01:00
|
|
|
window.location.href = "{$urlJS}";
|
2016-03-07 07:59:18 +01:00
|
|
|
}, 50);</script>
|
2015-02-04 23:23:26 +01:00
|
|
|
EOT
|
2017-05-12 05:17:38 +02:00
|
|
|
;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-05-12 05:17:38 +02:00
|
|
|
/**
|
|
|
|
* Output HTTP headers to the browser
|
|
|
|
*/
|
|
|
|
protected function outputHeaders()
|
|
|
|
{
|
|
|
|
$headersSent = headers_sent($file, $line);
|
|
|
|
if (!$headersSent) {
|
|
|
|
$method = sprintf(
|
|
|
|
"%s %d %s",
|
|
|
|
$_SERVER['SERVER_PROTOCOL'],
|
|
|
|
$this->getStatusCode(),
|
|
|
|
$this->getStatusDescription()
|
|
|
|
);
|
|
|
|
header($method);
|
|
|
|
foreach ($this->getHeaders() as $header => $value) {
|
2018-06-13 16:28:37 +02:00
|
|
|
header("{$header}: {$value}", true, $this->getStatusCode());
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2017-05-12 05:17:38 +02:00
|
|
|
} elseif ($this->getStatusCode() >= 300) {
|
|
|
|
// It's critical that these status codes are sent; we need to report a failure if not.
|
|
|
|
user_error(
|
|
|
|
sprintf(
|
|
|
|
"Couldn't set response type to %d because of output on line %s of %s",
|
|
|
|
$this->getStatusCode(),
|
|
|
|
$line,
|
|
|
|
$file
|
|
|
|
),
|
|
|
|
E_USER_WARNING
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output body of this response to the browser
|
|
|
|
*/
|
|
|
|
protected function outputBody()
|
|
|
|
{
|
|
|
|
// Only show error pages or generic "friendly" errors if the status code signifies
|
|
|
|
// an error, and the response doesn't have any body yet that might contain
|
|
|
|
// a more specific error description.
|
|
|
|
$body = $this->getBody();
|
|
|
|
if ($this->isError() && empty($body)) {
|
|
|
|
/** @var HandlerInterface $handler */
|
|
|
|
$handler = Injector::inst()->get(HandlerInterface::class);
|
|
|
|
$formatter = $handler->getFormatter();
|
2018-06-13 16:28:37 +02:00
|
|
|
echo $formatter->format([
|
|
|
|
'code' => $this->statusCode,
|
|
|
|
]);
|
2017-05-12 05:17:38 +02:00
|
|
|
} else {
|
|
|
|
echo $this->body;
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Returns true if this response is "finished", that is, no more script execution should be done.
|
|
|
|
* Specifically, returns true if a redirect has already been requested
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isFinished()
|
|
|
|
{
|
2017-05-12 05:17:38 +02:00
|
|
|
return $this->isRedirect() || $this->isError();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine if this response is a redirect
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isRedirect()
|
|
|
|
{
|
|
|
|
return in_array($this->getStatusCode(), self::$redirect_codes);
|
2016-11-29 00:31:16 +01:00
|
|
|
}
|
2018-06-13 16:28:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
2009-03-10 23:17:26 +01:00
|
|
|
}
|