Skip to content
Snippets Groups Projects
Commit ceab2397 authored by Andjelko Horvat's avatar Andjelko Horvat
Browse files

Custom HTTP codes in SimpleSAML_Error_Error (issue #566).

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@3267 44740490-163a-0410-bde0-09ae8108e29a
parent d09ac7b4
No related branches found
No related tags found
No related merge requests found
......@@ -29,6 +29,7 @@ class SimpleSAML_Error_BadRequest extends SimpleSAML_Error_Error {
$this->reason = $reason;
parent::__construct(array('BADREQUEST', '%REASON%' => $this->reason));
$this->httpCode = 400;
}
......@@ -41,16 +42,4 @@ class SimpleSAML_Error_BadRequest extends SimpleSAML_Error_Error {
return $this->reason;
}
/**
* Set the HTTP return code for this error.
*
* This should be overridden by subclasses who want a different return code than 500 Internal Server Error.
*/
protected function setHTTPCode() {
header('HTTP/1.0 400 Bad Request');
}
}
?>
\ No newline at end of file
......@@ -18,6 +18,14 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
private $errorCode;
/**
* The http code.
*
* @var integer
*/
protected $httpCode = 500;
/**
* The error title tag in dictionary.
*
......@@ -68,7 +76,7 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
* @param mixed $errorCode One of the error codes defined in the errors dictionary.
* @param Exception $cause The exception which caused this fatal error (if any).
*/
public function __construct($errorCode, Exception $cause = NULL) {
public function __construct($errorCode, Exception $cause = NULL, $httpCode = NULL) {
assert('is_string($errorCode) || is_array($errorCode)');
if (is_array($errorCode)) {
......@@ -80,6 +88,10 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
$this->errorCode = $errorCode;
}
if (isset($httpCode)) {
$this->httpCode = $httpCode;
}
$moduleCode = explode(':', $this->errorCode, 2);
if (count($moduleCode) === 2) {
$this->module = $moduleCode[0];
......@@ -153,7 +165,30 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
* This should be overridden by subclasses who want a different return code than 500 Internal Server Error.
*/
protected function setHTTPCode() {
header('HTTP/1.0 500 Internal Server Error');
// Some mostly used HTTP codes.
$httpCodesMap = array(
400 => 'HTTP/1.0 400 Bad Request',
403 => 'HTTP/1.0 403 Forbidden',
404 => 'HTTP/1.0 404 Not Found',
405 => 'HTTP/1.0 405 Method Not Allowed',
500 => 'HTTP/1.0 500 Internal Server Error',
501 => 'HTTP/1.0 501 Method Not Implemented',
503 => 'HTTP/1.0 503 Service Temporarily Unavailable',
);
$httpCode = $this->httpCode;
if (function_exists('http_response_code')) {
http_response_code($httpCode);
return;
}
if (!array_key_exists($this->httpCode, $httpCodesMap)) {
$httpCode = 500;
SimpleSAML_Logger::warning('HTTP response code not defined: ' . var_export($this->httpCode, TRUE));
}
header($httpCodesMap[$httpCode]);
}
......
......@@ -37,6 +37,7 @@ class SimpleSAML_Error_NotFound extends SimpleSAML_Error_Error {
}
$this->reason = $reason;
$this->httpCode = 404;
}
......@@ -49,16 +50,4 @@ class SimpleSAML_Error_NotFound extends SimpleSAML_Error_Error {
return $this->reason;
}
/**
* Set the HTTP return code for this error.
*
* This should be overridden by subclasses who want a different return code than 500 Internal Server Error.
*/
protected function setHTTPCode() {
header('HTTP/1.0 404 Not Found');
}
}
?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment