Update Saqel Platform: 2026-09-09 00:24:41
This commit is contained in:
@@ -64,6 +64,46 @@ class Response
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send JSON response, flush and disconnect client via fastcgi_finish_request,
|
||||
* but keep PHP script running in background for heavy async tasks.
|
||||
*
|
||||
* @param mixed $data
|
||||
* @param int|null $code
|
||||
* @return void
|
||||
*/
|
||||
public function jsonAndFinish($data, ?int $code = null): void
|
||||
{
|
||||
if ($code !== null) {
|
||||
$this->setStatusCode($code);
|
||||
}
|
||||
$this->setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||
|
||||
$allowedOrigin = getenv('ALLOWED_ORIGIN') ?: '*';
|
||||
$this->setHeader('Access-Control-Allow-Origin', $allowedOrigin);
|
||||
$this->setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
$this->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
|
||||
$this->setHeader('Vary', 'Origin');
|
||||
|
||||
$this->sendHeaders();
|
||||
http_response_code($this->statusCode);
|
||||
|
||||
if (ob_get_length()) {
|
||||
ob_clean();
|
||||
}
|
||||
|
||||
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
if (function_exists('fastcgi_finish_request')) {
|
||||
fastcgi_finish_request();
|
||||
} else {
|
||||
if (ob_get_level() > 0) {
|
||||
ob_end_flush();
|
||||
}
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send HTML response and terminate execution
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user