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

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

Loading…
Cancel
Save