Prevent response from sending more than once.

pull/340/head v1.3.5
Mike Cao 7 years ago
parent 5b4916d4e3
commit e146b8c0dd

@ -1 +1 @@
1.3.4
1.3.5

@ -284,7 +284,7 @@ class Engine {
$response = $this->response();
$router = $this->router();
// Allow post-filters to run
// Allow filters to run
$this->after('start', function() use ($self) {
$self->stop();
});
@ -331,11 +331,29 @@ class Engine {
*
* @param int $code HTTP status code
*/
public function _stop($code = 200) {
$this->response()
->status($code)
->write(ob_get_clean())
->send();
public function _stop($code = null) {
$response = $this->response();
if (!$response->sent()) {
if ($code !== null) {
$response->status($code);
}
$response->write(ob_get_clean());
$response->send();
}
}
/**
* Routes a URL to a callback function.
*
* @param string $pattern URL pattern to match
* @param callback $callback Callback function
* @param boolean $pass_route Pass the matching route object to the callback
*/
public function _route($pattern, $callback, $pass_route = false) {
$this->router()->map($pattern, $callback, $pass_route);
}
/**
@ -366,7 +384,8 @@ class Engine {
);
try {
$this->response(false)
$this->response()
->clear()
->status(500)
->write($msg)
->send();
@ -383,7 +402,8 @@ class Engine {
* Sends an HTTP 404 response when a URL is not found.
*/
public function _notFound() {
$this->response(false)
$this->response()
->clear()
->status(404)
->write(
'<h1>404 Not Found</h1>'.
@ -393,17 +413,6 @@ class Engine {
->send();
}
/**
* Routes a URL to a callback function.
*
* @param string $pattern URL pattern to match
* @param callback $callback Callback function
* @param boolean $pass_route Pass the matching route object to the callback
*/
public function _route($pattern, $callback, $pass_route = false) {
$this->router()->map($pattern, $callback, $pass_route);
}
/**
* Redirects the current request to another URL.
*
@ -422,7 +431,8 @@ class Engine {
$url = $base . preg_replace('#/+#', '/', '/' . $url);
}
$this->response(false)
$this->response()
->clear()
->status($code)
->header('Location', $url)
->write($url)

@ -29,6 +29,11 @@ class Response {
*/
protected $body;
/**
* @var bool HTTP response sent
*/
protected $sent = false;
/**
* @var array HTTP status codes
*/
@ -247,9 +252,7 @@ class Response {
}
// Send content length
$length = (extension_loaded('mbstring')) ?
mb_strlen($this->body, 'latin1') :
strlen($this->body);
$length = $this->getContentLength();
if ($length > 0) {
header('Content-Length: '.$length);
@ -258,6 +261,24 @@ class Response {
return $this;
}
/**
* Gets the content length.
*
* @return string Content length
*/
public function getContentLength() {
return extension_loaded('mbstring') ?
mb_strlen($this->body, 'latin1') :
strlen($this->body);
}
/**
* Gets whether response was sent.
*/
public function sent() {
return $this->sent;
}
/**
* Sends a HTTP response.
*/
@ -271,6 +292,8 @@ class Response {
}
echo $this->body;
$this->sent = true;
}
}

Loading…
Cancel
Save