Skip to content

Commit

Permalink
Size header (#142)
Browse files Browse the repository at this point in the history
* Content-Length

* Header set always

* No compression
  • Loading branch information
annejan authored Jul 4, 2022
1 parent 479ef0c commit cb78d32
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 23 deletions.
70 changes: 47 additions & 23 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,29 @@

namespace App\Http;

use App\Http\Middleware\AddContentLength;
use App\Http\Middleware\AuthenticatorMiddleware;
use App\Http\Middleware\EncryptCookies;
use App\Http\Middleware\RedirectIfAuthenticated;
use App\Http\Middleware\ShareMessagesFromSession;
use App\Http\Middleware\TrimStrings;
use App\Http\Middleware\VerifyCsrfToken;
use Bepsvpt\SecureHeaders\SecureHeadersMiddleware;
use Fruitcake\Cors\HandleCors;
use Illuminate\Auth\Middleware\Authenticate;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
use Illuminate\Auth\Middleware\Authorize;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
use Illuminate\Foundation\Http\Middleware\ValidatePostSize;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use LaravelWebauthn\Http\Middleware\WebauthnMiddleware;
use OpenApi\Annotations as OA;

/**
Expand Down Expand Up @@ -80,12 +102,13 @@ class Kernel extends HttpKernel
* @var array<string>
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\Bepsvpt\SecureHeaders\SecureHeadersMiddleware::class,
\Fruitcake\Cors\HandleCors::class,
CheckForMaintenanceMode::class,
ValidatePostSize::class,
TrimStrings::class,
ConvertEmptyStringsToNull::class,
SecureHeadersMiddleware::class,
HandleCors::class,
AddContentLength::class
];

/**
Expand All @@ -95,14 +118,14 @@ class Kernel extends HttpKernel
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
// \Illuminate\Session\Middleware\AuthenticateSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\App\Http\Middleware\ShareMessagesFromSession::class,
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
ShareMessagesFromSession::class,
],

'api' => [
Expand All @@ -119,20 +142,21 @@ class Kernel extends HttpKernel
* @var array<string, string>
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'2fa' => \App\Http\Middleware\AuthenticatorMiddleware::class,
'webauthn' => \LaravelWebauthn\Http\Middleware\WebauthnMiddleware::class,
'auth' => Authenticate::class,
'auth.basic' => AuthenticateWithBasicAuth::class,
'bindings' => SubstituteBindings::class,
'can' => Authorize::class,
'guest' => RedirectIfAuthenticated::class,
'throttle' => ThrottleRequests::class,
'2fa' => AuthenticatorMiddleware::class,
'webauthn' => WebauthnMiddleware::class,
];

/**
* Returns the version of the application by fetching and displaying the version.json file
*
* @return string URL
* @throws \JsonException
*/
public static function applicationVersion(): string
{
Expand All @@ -142,7 +166,7 @@ public static function applicationVersion(): string
return 'Undefined';
}

$versionData = json_decode($versionJson, true);
$versionData = json_decode($versionJson, true, 512, JSON_THROW_ON_ERROR);
if (is_array($versionData) && array_key_exists('version', $versionData)) {
return $versionData['version'];
}
Expand Down
55 changes: 55 additions & 0 deletions app/Http/Middleware/AddContentLength.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Closure;

class AddContentLength
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);

// to be sure nothing was already output (by an echo statement or something)
if (headers_sent() || ob_get_contents() !== '') {
return $response;
}

// For now, we don't compress anything
ini_set('zlib.output_compression', '0');

$content = $response->content();
$contentLength = mb_strlen($content);
// $useCompressedOutput = ($contentLength && isset($_SERVER['HTTP_ACCEPT_ENCODING']) &&
// str_contains($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip'));
//
// if ($useCompressedOutput) {
// // In order to accurately set Content-Length, we have to compress the data ourselves
// // rather than letting PHP do it automatically.
// $compressedContent = gzencode($content, 9, FORCE_GZIP);
// $compressedContentLength = mb_strlen($compressedContent);
// if ($compressedContentLength / $contentLength < 0.9) {
// if (ini_get('zlib.output_compression')) {
// ini_set('zlib.output_compression', 'false');
// }
// $response->header('Content-Encoding', 'gzip');
// $response->setContent($compressedContent);
// $contentLength = $compressedContentLength;
// }
// }

// compressed or not, sets the Content-Length
$response->header('Content-Length', $contentLength);

return $response;
}
}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"ext-gd": "*",
"ext-gmp": "*",
"ext-json": "*",
"ext-zlib": "*",
"ext-mbstring": "*",
"asbiin/laravel-webauthn": "^2.0",
"bacon/bacon-qr-code": "^2.0",
"bepsvpt/secure-headers": "^7.2",
Expand Down

0 comments on commit cb78d32

Please sign in to comment.