Skip to content

Commit

Permalink
ignore compression on file, stream or whenever Content-Encoding heade…
Browse files Browse the repository at this point in the history
…r is present
  • Loading branch information
d8vjork committed Feb 14, 2024
1 parent dd244e1 commit 0c74ea4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/ResponseCompression.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace OpenSoutheners\LaravelVaporResponseCompression;

use Closure;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;

class ResponseCompression
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
Expand Down Expand Up @@ -40,12 +41,14 @@ public function handle($request, Closure $next)
/**
* Determine if response should be compressed.
*
* @param \Illuminate\Http\Response $response
* @return bool
* @param \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response $response
*/
protected function shouldCompressResponse($response): bool
{
return config('response-compression.enable', true)
return ! $response instanceof BinaryFileResponse
&& ! $response instanceof StreamedResponse
&& ! $response->headers->has('Content-Encoding')
&& config('response-compression.enable', true)
&& strlen($response->getContent()) >= config('response-compression.threshold', 10000);
}

Expand Down
38 changes: 37 additions & 1 deletion tests/ResponseCompressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function testClientGetRawResponseWhenThresholdNotReached()
['content' => $this->lightResponseContent]
);
}

public function testClientGetRawResponseWhenNotEnabled()
{
config(['response-compression.enable' => false]);
Expand Down Expand Up @@ -95,6 +95,42 @@ public function testClientGetResponseAskingAnUnrecognisedEncodingReceivesRawResp
);
}

public function testClientGetResponseWithContentEncodingHeaderAlreadyAttachedReceivesRawResponse()
{
Route::get('/heavy-ignored', function () {
return response()->json([
'content' => $this->heavyResponseContent,
], 200, ['Content-Encoding' => CompressionEncoding::DEFLATE]);
})->middleware(ResponseCompression::class);

$response = $this->get('/heavy-ignored', ['Accept-Encoding' => CompressionEncoding::GZIP]);

$response->assertHeader('Content-Encoding', CompressionEncoding::DEFLATE);

$this->assertEquals(
$response->json(),
['content' => $this->heavyResponseContent]
);
}

public function testClientGetStreamDownloadResponseReceivesRawResponse()
{
Route::get('/download', function () {
return response()->streamDownload(function () {
echo $this->heavyResponseContent;
});
})->middleware(ResponseCompression::class);

$response = $this->get('/download', ['Accept-Encoding' => CompressionEncoding::DEFLATE]);

$response->assertHeaderMissing('Content-Encoding');

$this->assertEquals(
$response->streamedContent(),
$this->heavyResponseContent
);
}

// TODO: vdechenaux/brotli is not compatible with Laravel dependencies...
// public function testClientGetResponseUsingBrotliEncoding()
// {
Expand Down

0 comments on commit 0c74ea4

Please sign in to comment.