Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

31597 - Do not buffering output #3

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 2 additions & 23 deletions src/Klein/Klein.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,8 @@ public function dispatch(
$params = array();
$apc = function_exists('apc_fetch');

// Start output buffering
ob_start();
$this->output_buffer_level = ob_get_level();
// Do not buffering
$this->output_buffer_level = 0;

try {
foreach ($this->routes as $route) {
Expand Down Expand Up @@ -689,14 +688,6 @@ public function dispatch(
if (strcasecmp($req_method, 'HEAD') === 0) {
// HEAD requests shouldn't return a body
$this->response->body('');

while (ob_get_level() >= $this->output_buffer_level) {
ob_end_clean();
}
} elseif (self::DISPATCH_NO_CAPTURE === $capture) {
while (ob_get_level() >= $this->output_buffer_level) {
ob_end_flush();
}
}
} catch (LockedResponseException $e) {
// Do nothing, since this is an automated behavior
Expand Down Expand Up @@ -950,24 +941,12 @@ protected function error($err)
} else {
$this->response->code(500);

while (ob_get_level() >= $this->output_buffer_level) {
ob_end_clean();
}

throw new UnhandledException($msg, $err->getCode(), $err);
}
} catch (Throwable $e) { // PHP 7 compatibility
// Make sure to clean the output buffer before bailing
while (ob_get_level() >= $this->output_buffer_level) {
ob_end_clean();
}

throw $e;
} catch (Exception $e) { // TODO: Remove this catch block once PHP 5.x support is no longer necessary.
// Make sure to clean the output buffer before bailing
while (ob_get_level() >= $this->output_buffer_level) {
ob_end_clean();
}

throw $e;
}
Expand Down