Skip to content

Commit

Permalink
Merge branch 'gzip-response'
Browse files Browse the repository at this point in the history
  • Loading branch information
timkelty committed Dec 20, 2023
2 parents e9b6039 + f59746b commit 7588465
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/web/ResponseBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,37 @@ public function events(): array
];
}

public function afterPrepare(Event $event): void
public function gzip(): void
{
$this->joinMultiValueHeaders();
$accepts = preg_split(
'/\s*\,\s*/',
Craft::$app->getRequest()->getHeaders()->get('Accept-Encoding') ?? ''
);

if ($this->owner->stream) {
$this->serveBinaryFromS3();
if (Collection::make($accepts)->doesntContain('gzip')) {
return;
}

$this->owner->content = gzencode($this->owner->content, 9);
$this->owner->getHeaders()->set('Content-Encoding', 'gzip');
}

public function afterPrepare(Event $event): void
{
$this->joinMultiValueHeaders();
$this->gzip();
$this->serveBinaryFromS3();
}

/**
* @throws ServerErrorHttpException
*/
protected function serveBinaryFromS3(): void
{
if (!$this->owner->stream) {
return;
}

/** @var TmpFs $fs */
$fs = Craft::createObject([
'class' => TmpFs::class,
Expand Down

0 comments on commit 7588465

Please sign in to comment.