Skip to content

Commit

Permalink
feat: refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
dewanakl committed Dec 29, 2023
1 parent a974eff commit 3c646bc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
10 changes: 3 additions & 7 deletions app/Middleware/CorsMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ public function handle(Request $request, Closure $next)
$header = respond()->getHeader();
$header->set('Access-Control-Allow-Origin', '*');

if ($header->has('Vary')) {
$vary = explode(', ', $header->get('Vary'));
$vary = array_unique([...$vary, 'Accept', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Origin', 'User-Agent']);
$header->set('Vary', join(', ', $vary));
} else {
$header->set('Vary', 'Accept, Access-Control-Request-Method, Access-Control-Request-Headers, Origin, User-Agent');
}
$vary = (!$header->has('Vary')) ? [] : explode(', ', $header->get('Vary'));
$vary = array_unique([...$vary, 'Accept', 'Access-Control-Request-Method', 'Access-Control-Request-Headers', 'Origin', 'User-Agent']);
$header->set('Vary', join(', ', $vary));

if (!$request->method(Request::OPTIONS)) {
return $next($request);
Expand Down
12 changes: 4 additions & 8 deletions app/Middleware/GzipMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function handle(Request $request, Closure $next): Stream|Respond
return $response;
}

if (!in_array('gzip', explode(', ', $request->server->get('HTTP_ACCEPT_ENCODING')))) {
if (str_contains($request->server->get('HTTP_ACCEPT_ENCODING'), 'gzip')) {
return $response;
}

Expand All @@ -40,15 +40,11 @@ public function handle(Request $request, Closure $next): Stream|Respond

$response->setContent($compressed);

if ($response->headers->has('Vary')) {
$vary = explode(', ', $response->headers->get('Vary'));
$vary = array_unique([...$vary, 'Accept-Encoding']);
$response->headers->set('Vary', join(', ', $vary));
} else {
$response->headers->set('Vary', 'Accept-Encoding');
}
$vary = (!$response->headers->has('Vary')) ? [] : explode(', ', $response->headers->get('Vary'));
$vary = array_unique([...$vary, 'Accept-Encoding']);

$response->headers
->set('Vary', join(', ', $vary))
->set('Content-Encoding', 'gzip')
->set('Content-Length', strlen($compressed));

Expand Down

0 comments on commit 3c646bc

Please sign in to comment.