Skip to content

Commit

Permalink
Refactoring header sending in Response to remove annoying exception
Browse files Browse the repository at this point in the history
Also add a fastcgi fast shutdown function in order to squeeze a few
more requests from the webserver
  • Loading branch information
lorenzo committed Sep 13, 2014
1 parent 6f0347f commit 07e1b67
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/Network/Response.php
Expand Up @@ -416,23 +416,43 @@ public function send() {
$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);
}
}
}

/**
Expand Down Expand Up @@ -519,14 +539,8 @@ protected function _setContentLength() {
* @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 {
Expand Down

0 comments on commit 07e1b67

Please sign in to comment.