Skip to content

Commit

Permalink
Update PicoResponse.php
Browse files Browse the repository at this point in the history
  • Loading branch information
kamshory committed Oct 12, 2024
1 parent 91b747a commit 508b243
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/Response/PicoResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 508b243

Please sign in to comment.