From 508b24319e33ed7d76e7ce457719ad5781dafa5e Mon Sep 17 00:00:00 2001 From: "Kamshory, MT" Date: Sun, 13 Oct 2024 06:54:58 +0700 Subject: [PATCH] Update PicoResponse.php --- src/Response/PicoResponse.php | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Response/PicoResponse.php b/src/Response/PicoResponse.php index 069b389c..7cb06de9 100644 --- a/src/Response/PicoResponse.php +++ b/src/Response/PicoResponse.php @@ -26,7 +26,7 @@ class PicoResponse public static function sendJSON($data, $prettify = false, $headers = null, $httpStatusCode = PicoHttpStatus::HTTP_OK) { $body = null; - if ($data != null) { + if ($data !== null) { if (is_string($data)) { $body = $data; } else { @@ -141,26 +141,35 @@ public static function sendBody($body, $async = false) if (function_exists('ignore_user_abort')) { ignore_user_abort(true); } - ob_start(); + ob_start(); // Mulai output buffering + } - if ($body != null) { - echo $body; - } + // Jika body tidak null, kirimkan + if ($body !== null) { + echo $body; // Tampilkan body } + + // Mengatur header koneksi header("Connection: close"); + // Jika dalam mode asinkron, lakukan flush if ($async) { - ob_end_flush(); - ob_flush(); - flush(); + ob_end_flush(); // Selesaikan buffer + header("Content-Length: " . strlen($body)); // Tentukan panjang konten + flush(); // Kirim output ke klien if (function_exists('fastcgi_finish_request')) { - fastcgi_finish_request(); + fastcgi_finish_request(); // Selesaikan permintaan FastCGI + } + } else { + // Jika tidak asinkron, atur Content-Length + if ($body !== null) { + header("Content-Length: " . strlen($body)); // Tentukan panjang konten + echo $body; // Tampilkan body } - } else if ($body != null) { - echo $body; } } + /** * Get default content type based on the provided content type. * @@ -244,7 +253,7 @@ public static function getHttpResponseCode($code) * * @return void */ - public function redirectToItself() + public static function redirectToItself() { header("Location: ".$_SERVER['REQUEST_URI']); exit(); // Ensures no further code execution after redirection