Skip to content

Commit

Permalink
Merge pull request #329 from rakutentech/feature/gzip-fallback
Browse files Browse the repository at this point in the history
fixes #327 gzip failed to fetch
  • Loading branch information
kevincobain2000 authored Feb 2, 2024
2 parents 811b058 + 4aa9ec7 commit 95dacc0
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/LaravelRequestDocsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,29 @@ public function handle(Request $request, Closure $next): Response
$jsonContent = json_encode($content);

if (in_array('gzip', $request->getEncodings()) && function_exists('gzencode')) {
$level = 9; // best compression;
$jsonContent = gzencode($jsonContent, $level);
$level = 9; // Best compression.
$compressedContent = gzencode($jsonContent, $level);

// Create a new response object with compressed content.
$response = new Response($compressedContent);

// Add necessary headers.
$response->headers->add([
'Content-type' => 'application/json; charset=utf-8',
'Content-Length' => strlen($jsonContent),
'Content-Type' => 'application/json; charset=utf-8',
'Content-Length' => strlen($compressedContent),
'Content-Encoding' => 'gzip',
]);

return $response; // Return the response object directly.
} else {
// Fallback for clients that do not support gzip.
$response = new Response($jsonContent);
$response->headers->add([
'Content-Type' => 'application/json; charset=utf-8',
]);

return $response;
}
$response->setContent($jsonContent);
return $response;
}

public function listenToDB(): void
Expand Down

0 comments on commit 95dacc0

Please sign in to comment.