Skip to content

Commit

Permalink
debug: compression
Browse files Browse the repository at this point in the history
  • Loading branch information
loks0n committed Nov 7, 2024
1 parent a501a56 commit 72239fb
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Utopia;

use Utopia\CLI\Console;
use Utopia\Compression\Compression;

class Response
Expand Down Expand Up @@ -315,7 +316,7 @@ public function setCompressionMinSize(int $compressionMinSize): static

/**
* Set supported compression algorithms
*
*
* @param mixed $compressionSupported
*/
public function setCompressionSupported(mixed $compressionSupported): static
Expand Down Expand Up @@ -540,15 +541,26 @@ public function send(string $body = ''): void
}

// Compress body
Console::log('Accept-Encoding: '.$this->acceptEncoding . ' - ' . $this->compressionMinSize);
Console::log('Content-Type: '.$this->contentType . ' - ' . $this->compressed[$this->contentType]);
Console::log('Content-Length: '.strlen($body) . ' - ' . $this->compressionMinSize);

if (
!empty($this->acceptEncoding) &&
isset($this->compressed[$this->contentType]) &&
strlen($body) > $this->compressionMinSize
) {
$algorithm = Compression::fromAcceptEncoding($this->acceptEncoding, $this->compressionSupported);

Console::log('Compression Algorithm: '.($algorithm ? $algorithm->getName() : 'none'));

if ($algorithm) {
Console::log('Body before compression: '.strlen($body));

$body = $algorithm->compress($body);

Console::log('Body after compression: '.strlen($body));

$this->addHeader('Content-Encoding', $algorithm->getContentEncoding());
$this->addHeader('Vary', 'Accept-Encoding');
}
Expand Down

0 comments on commit 72239fb

Please sign in to comment.