Browse Source

Refactoring header sending in Response to remove annoying exception

Also add a fastcgi fast shutdown function in order to squeeze a few
more requests from the webserver
Jose Lorenzo Rodriguez 11 years ago
parent
commit
07e1b6773c
1 changed files with 29 additions and 15 deletions
  1. 29 15
      src/Network/Response.php

+ 29 - 15
src/Network/Response.php

@@ -416,23 +416,43 @@ class Response {
 			$this->statusCode(302);
 		}
 
-		$codeMessage = $this->_statusCodes[$this->_status];
-		$this->_setCookies();
-		$this->_sendHeader("{$this->_protocol} {$this->_status} {$codeMessage}");
 		$this->_setContent();
 		$this->_setContentLength();
-		$this->_setContentType();
-		foreach ($this->_headers as $header => $values) {
-			foreach ((array)$values as $value) {
-				$this->_sendHeader($header, $value);
-			}
-		}
+		$this->sendHeaders();
+
 		if ($this->_file) {
 			$this->_sendFile($this->_file, $this->_fileRange);
 			$this->_file = $this->_fileRange = null;
 		} else {
 			$this->_sendContent($this->_body);
 		}
+
+		if (function_exists('fastcgi_finish_request')) {
+			fastcgi_finish_request();
+		}
+	}
+
+/**
+ * Sends the HTTP headers and cookies.
+ *
+ * @return void
+ */
+	public function sendHeaders() {
+		if (headers_sent()) {
+			return;
+		}
+
+		$this->_setCookies();
+
+		$codeMessage = $this->_statusCodes[$this->_status];
+		$this->_sendHeader("{$this->_protocol} {$this->_status} {$codeMessage}");
+		$this->_setContentType();
+
+		foreach ($this->_headers as $header => $values) {
+			foreach ((array)$values as $value) {
+				$this->_sendHeader($header, $value);
+			}
+		}
 	}
 
 /**
@@ -519,14 +539,8 @@ class Response {
  * @param string $name the header name
  * @param string $value the header value
  * @return void
- * @throws \RuntimeException When headers have already been sent
  */
 	protected function _sendHeader($name, $value = null) {
-		if (headers_sent($filename, $linenum)) {
-			throw new RuntimeException(
-				sprintf('Headers already sent in %d on line %s', $linenum, $filename)
-			);
-		}
 		if ($value === null) {
 			header($name);
 		} else {