Allow message exception to signal message controller to return an HTTP status code
Allow message exception to signal message controller to return an HTTP status code

--- a/include/MessageException.class.php
+++ b/include/MessageException.class.php
@@ -18,6 +18,8 @@
 {
 
 	public $Error;
+
+	public $StatusCode;
 	
 	/**
 	 * Constructor
@@ -25,12 +27,14 @@
 	 * @access public
 	 * @param string $message message string
 	 * @param boolean $error true if this is an error rather than informational
+	 * @param integer $statusCode HTTP status code to return
 	 * @param integer $code exception code
 	 * @param Exception $previous previous exception
 	 * @return Exception message exception object
 	 */
-	public function __construct($message, $error = false, $code = 0) {
+	public function __construct($message, $error = false, $statusCode = 200, $code = 0) {
 		$this->Error = $error;
+		$this->StatusCode = $statusCode;
 		parent::__construct($message, $code);
 	}
 }

--- a/include/controller/Controller_Message.class.php
+++ b/include/controller/Controller_Message.class.php
@@ -87,6 +87,30 @@
 	}
 
 	/**
+	 * LoadHeaders
+	 *
+	 * Loads headers for this template
+	 *
+	 * @access protected
+	 */
+	protected function LoadHeaders()
+	{
+		if (isset($this->params['statuscode']) && !empty($this->params['statuscode'])) {
+			$partialHeader = $this->StatusCodeHeader($this->params['statuscode']);
+			if (!empty($partialHeader)) {
+				if (substr(php_sapi_name(), 0, 8) == 'cgi-fcgi') {
+					/*
+					 * FastCGI requires a different header
+					 */
+					$this->headers[] = 'Status: ' . $partialHeader;
+				} else {
+					$this->headers[] = 'HTTP/1.1 ' . $partialHeader;
+				}
+			}
+		}
+	}
+
+	/**
 	 * LoadData
 	 *
 	 * Loads data for this template
@@ -101,5 +125,20 @@
 		}
 	}
 
+	/**
+	 * StatusCodeHeader
+	 *
+	 * Gets the header for an HTTP status code
+	 *
+	 * @access private
+	 * @param integer $code status code
+	 * @return string header
+	 */
+	private function StatusCodeHeader($code)
+	{
+		switch ($code) {
+		}
+	}
+
 }
 

file:a/index.php -> file:b/index.php
--- a/index.php
+++ b/index.php
@@ -147,9 +147,11 @@
 	$controller->SetParam('message', $e->getMessage());
 	if ($e instanceof GitPHP_MessageException) {
 		$controller->SetParam('error', $e->Error);
+		$controller->SetParam('statuscode', $e->StatusCode);
 	} else {
 		$controller->SetParam('error', true);
 	}
+	$controller->RenderHeaders();
 	$controller->Render();
 
 }

comments