Response should only be sent once.

pull/87/head
Mike Cao 11 years ago
parent b8026d9828
commit 6a1029a187

@ -29,6 +29,11 @@ class Response {
*/ */
protected $body; protected $body;
/**
* @var bool If a response has already been sent
*/
protected static $sent = false;
/** /**
* @var array HTTP status codes * @var array HTTP status codes
*/ */
@ -168,42 +173,40 @@ class Response {
* *
* @return object Self reference * @return object Self reference
*/ */
public function sendHeaders() { public function sendHeaders() {
if (!headers_sent()) { // Send status code header
// Send status code header if (strpos(php_sapi_name(), 'cgi') !== false) {
if (strpos(php_sapi_name(), 'cgi') !== false) { header(
header( sprintf(
sprintf( 'Status: %d %s',
'Status: %d %s', $this->status,
$this->status, self::$codes[$this->status]
self::$codes[$this->status] ),
), true
true );
); }
} else {
else { header(
header( sprintf(
sprintf( '%s %d %s',
'%s %d %s', (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'),
(isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.1'), $this->status,
$this->status, self::$codes[$this->status]),
self::$codes[$this->status]), true,
true, $this->status
$this->status );
); }
}
// Send other headers // Send other headers
foreach ($this->headers as $field => $value) { foreach ($this->headers as $field => $value) {
if (is_array($value)) { if (is_array($value)) {
foreach ($value as $v) { foreach ($value as $v) {
header($field.': '.$v, false); header($field.': '.$v, false);
}
}
else {
header($field.': '.$value);
} }
} }
else {
header($field.': '.$value);
}
} }
return $this; return $this;
@ -219,9 +222,15 @@ class Response {
ob_end_clean(); ob_end_clean();
} }
$this->sendHeaders(); if (!self::$sent) {
if (!headers_sent()) {
$this->sendHeaders();
}
echo $this->body;
echo $this->body; self::$sent = true;
}
return $this; return $this;
} }

Loading…
Cancel
Save