Implemented HTTP->HTTPS redirect and fixed a bug with Output where a 500 is raised when no output is created.

This commit is contained in:
Abel Hoogeveen 2019-12-20 11:11:22 +01:00
parent 1180445dcc
commit b976dbeae3
No known key found for this signature in database
GPG Key ID: 96C2234920BF4292
3 changed files with 22 additions and 2 deletions

View File

@ -49,8 +49,11 @@ return [
'permitted_uri_chars' => 'a-z 0-9~%.:_\-',
'charset' => 'UTF-8',
// Whether to redirect http traffic to https
'redirect_to_https' => false,
// Whether to gzip the output when the client supports it
'compress_output' => true,
'compress_output' => false,
// Global switch for output cache. To use, must be enabled in view as well
'cache_output' => true,

View File

@ -145,7 +145,7 @@ class Output
$output = is_null($output) ? $this->output : $output;
// Write cache if requested to do so
if ($this->cacheTime > 0)
if ($this->cacheTime > 0 && !is_null($output))
$this->writeCache($output);
// First send status code

View File

@ -190,14 +190,31 @@ class WebComponent implements iComponent
/** @var Router $router */
/** @var URI $uri */
/** @var Input $input */
/** @var Output $output */
/** @var Security $security */
/** @var Resources $resources */
/** @var Config $config */
$router = Factory::getInstance('router');
$uri = Factory::getInstance('uri');
$input = Factory::getInstance('input');
$output = Factory::getInstance('output');
$security = Factory::getInstance('security');
$resources = Factory::getInstance('resources');
$config = Factory::getInstance('config');
// First check if this isn't https and we need to redirect
$redirect = $config->getConfig('web')->get('redirect_to_https');
if ($redirect && !$input->isHttps())
{
Logger::log("Redirecting http traffic to https...");
$httpsInputs = $input->server(['HTTPS', 'HTTP_HOST', 'REQUEST_URI']);
$location = 'https://' . $httpsInputs['HTTP_HOST'] . $httpsInputs['REQUEST_URI'];
$output->setStatusHeader(301);
$output->setHeader('Location: ' . $location);
return true;
}
// And start logging the request
Logger::newLevel("Routing web request...");