Missing Pages to return Response Code 404 instead of 500. Add Fallback for proper Response Code in certain Webservers

This commit is contained in:
Gautham Warrier 2014-10-11 22:52:57 +05:30
parent bc2f2c33d4
commit 428e4b2ef8
3 changed files with 68 additions and 5 deletions

View File

@ -160,7 +160,7 @@
private function save_file($request, $content) {
$file = $this->get_file_from_request($request);
if ($file === false) return $this->generate_error_page('Page Not Found',
'The Page you requested is yet to be made. Try again later.', ErrorPage::NORMAL_ERROR_TYPE);
'The Page you requested is yet to be made. Try again later.', ErrorPage::MISSING_PAGE_ERROR_TYPE);
if ($file->write($content)) return new SimplePage('Success', 'Successfully Edited');
else return $this->generate_error_page('File Not Writable', 'The file you wish to write to is not writable.',
ErrorPage::FATAL_ERROR_TYPE);
@ -176,7 +176,7 @@
$params = $this->params;
$file = $this->get_file_from_request($request);
if ($file === false) return $this->generate_error_page('Page Not Found',
'The Page you requested is yet to be made. Try again later.', ErrorPage::NORMAL_ERROR_TYPE);
'The Page you requested is yet to be made. Try again later.', ErrorPage::MISSING_PAGE_ERROR_TYPE);
$params['request'] = $request;
$params['file_uri'] = $file->value;
if ($request !== 'index') $params['entry_page'] = $file->first_page;
@ -195,7 +195,8 @@
break;
case ErrorPage::NORMAL_ERROR_TYPE:
$params['error_type'] = ErrorPage::NORMAL_ERROR_TYPE;
case ErrorPage::MISSING_PAGE_ERROR_TYPE:
$params['error_type'] = $mode;
$params['index_key'] = 'index';
$params['docs_path'] = $this->docs_path;
$protocol = '//';

View File

@ -251,4 +251,65 @@ EOT;
}
}
if (!function_exists('http_response_code')) {
function http_response_code($code = NULL) {
if ($code !== NULL) {
switch ($code) {
case 100: $text = 'Continue'; break;
case 101: $text = 'Switching Protocols'; break;
case 200: $text = 'OK'; break;
case 201: $text = 'Created'; break;
case 202: $text = 'Accepted'; break;
case 203: $text = 'Non-Authoritative Information'; break;
case 204: $text = 'No Content'; break;
case 205: $text = 'Reset Content'; break;
case 206: $text = 'Partial Content'; break;
case 300: $text = 'Multiple Choices'; break;
case 301: $text = 'Moved Permanently'; break;
case 302: $text = 'Moved Temporarily'; break;
case 303: $text = 'See Other'; break;
case 304: $text = 'Not Modified'; break;
case 305: $text = 'Use Proxy'; break;
case 400: $text = 'Bad Request'; break;
case 401: $text = 'Unauthorized'; break;
case 402: $text = 'Payment Required'; break;
case 403: $text = 'Forbidden'; break;
case 404: $text = 'Not Found'; break;
case 405: $text = 'Method Not Allowed'; break;
case 406: $text = 'Not Acceptable'; break;
case 407: $text = 'Proxy Authentication Required'; break;
case 408: $text = 'Request Time-out'; break;
case 409: $text = 'Conflict'; break;
case 410: $text = 'Gone'; break;
case 411: $text = 'Length Required'; break;
case 412: $text = 'Precondition Failed'; break;
case 413: $text = 'Request Entity Too Large'; break;
case 414: $text = 'Request-URI Too Large'; break;
case 415: $text = 'Unsupported Media Type'; break;
case 500: $text = 'Internal Server Error'; break;
case 501: $text = 'Not Implemented'; break;
case 502: $text = 'Bad Gateway'; break;
case 503: $text = 'Service Unavailable'; break;
case 504: $text = 'Gateway Time-out'; break;
case 505: $text = 'HTTP Version not supported'; break;
default:
exit('Unknown http status code "' . htmlentities($code) . '"');
break;
}
$protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
header($protocol . ' ' . $code . ' ' . $text);
$GLOBALS['http_response_code'] = $code;
} else {
$code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200);
}
return $code;
}
}
?>

View File

@ -43,6 +43,7 @@
class ErrorPage extends SimplePage
{
const NORMAL_ERROR_TYPE = 'NORMAL_ERROR';
const MISSING_PAGE_ERROR_TYPE = 'MISSING_PAGE_ERROR';
const FATAL_ERROR_TYPE = 'FATAL_ERROR';
private $params;
@ -56,12 +57,12 @@
}
public function display() {
header('HTTP', true, 500);
http_response_code($this->type === static::MISSING_PAGE_ERROR_TYPE ? 404 : 500);
parent::display();
}
public function get_page_content() {
if ($this->type === static::NORMAL_ERROR_TYPE && is_null(static::$template)) {
if ($this->type !== static::FATAL_ERROR_TYPE && is_null(static::$template)) {
include_once($this->params['theme']['error-template']);
static::$template = new Template();
}