Skip to content

Commit e1171cb

Browse files
committed
Add finishResponse function in ResponseTrait and trigger post_response event after sending response
1 parent f4b5beb commit e1171cb

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎system/CodeIgniter.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
297297

298298
$this->sendResponse();
299299

300+
Events::trigger('post_response');
301+
300302
return null;
301303
}
302304

‎system/HTTP/ResponseTrait.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,34 @@ public function send()
385385
$this->sendCookies();
386386
$this->sendBody();
387387

388+
$this->finishResponse();
389+
388390
return $this;
389391
}
390392

393+
private function finishResponse(): void
394+
{
395+
if (function_exists('fastcgi_finish_request')) {
396+
fastcgi_finish_request();
397+
} elseif (function_exists('litespeed_finish_request')) {
398+
litespeed_finish_request();
399+
} elseif (!in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
400+
$this->closeOutputBuffers();
401+
flush();
402+
}
403+
}
404+
405+
private function closeOutputBuffers(): void
406+
{
407+
$status = ob_get_status(true);
408+
$level = count($status);
409+
$flags = PHP_OUTPUT_HANDLER_REMOVABLE | PHP_OUTPUT_HANDLER_FLUSHABLE;
410+
411+
while ($level-- > 0 && ($s = $status[$level]) && (!isset($s['del']) ? !isset($s['flags']) || ($s['flags'] & $flags) === $flags : $s['del'])) {
412+
ob_end_flush();
413+
}
414+
}
415+
391416
/**
392417
* Decides whether {@see ContentSecurityPolicy::finalize()} should run for
393418
* this response. Keeping the CSP class unloaded on requests that do not

0 commit comments

Comments
 (0)