Skip to content
This repository has been archived by the owner on Sep 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #456 from hezachary/master
Browse files Browse the repository at this point in the history
Add Gzip support on response chunk mode
  • Loading branch information
Arkanius authored Feb 22, 2021
2 parents 41fe378 + b2da170 commit 5e80d23
Show file tree
Hide file tree
Showing 3 changed files with 337 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Server/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public function onRequest($swooleRequest, $swooleResponse)
$illuminateResponse = $sandbox->run($illuminateRequest);

// send response
Response::make($illuminateResponse, $swooleResponse)->send();
Response::make($illuminateResponse, $swooleResponse, $swooleRequest)->send();
} catch (Throwable $e) {
try {
$exceptionResponse = $this->app
Expand All @@ -234,7 +234,7 @@ public function onRequest($swooleRequest, $swooleResponse)
$illuminateRequest,
$this->normalizeException($e)
);
Response::make($exceptionResponse, $swooleResponse)->send();
Response::make($exceptionResponse, $swooleResponse, $swooleRequest)->send();
} catch (Throwable $e) {
$this->logServerError($e);
}
Expand Down
60 changes: 55 additions & 5 deletions src/Transformers/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Response as IlluminateResponse;
use Swoole\Http\Response as SwooleResponse;
use Swoole\Http\Request as SwooleRequest;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
Expand All @@ -17,6 +18,11 @@ class Response
*/
protected $swooleResponse;

/**
* @var \Swoole\Http\Request
*/
protected $swooleRequest;

/**
* @var \Illuminate\Http\Response
*/
Expand All @@ -27,24 +33,27 @@ class Response
*
* @param $illuminateResponse
* @param \Swoole\Http\Response $swooleResponse
* @param \Swoole\Http\Request $swooleRequest
*
* @return \SwooleTW\Http\Transformers\Response
*/
public static function make($illuminateResponse, SwooleResponse $swooleResponse)
public static function make($illuminateResponse, SwooleResponse $swooleResponse, SwooleRequest $swooleRequest)
{
return new static($illuminateResponse, $swooleResponse);
return new static($illuminateResponse, $swooleResponse, $swooleRequest);
}

/**
* Response constructor.
*
* @param mixed $illuminateResponse
* @param \Swoole\Http\Response $swooleResponse
* @param \Swoole\Http\Request $swooleRequest
*/
public function __construct($illuminateResponse, SwooleResponse $swooleResponse)
public function __construct($illuminateResponse, SwooleResponse $swooleResponse, SwooleRequest $swooleRequest)
{
$this->setIlluminateResponse($illuminateResponse);
$this->setSwooleResponse($swooleResponse);
$this->setSwooleRequest($swooleRequest);
}

/**
Expand Down Expand Up @@ -117,22 +126,30 @@ protected function sendContent()
} elseif ($illuminateResponse instanceof BinaryFileResponse) {
$this->swooleResponse->sendfile($illuminateResponse->getFile()->getPathname());
} else {
$this->sendInChunk($illuminateResponse->getContent());
$chunkGzip = $this->canGzipContent($illuminateResponse->headers->get('Content-Encoding'));
$this->sendInChunk($illuminateResponse->getContent(), $chunkGzip);
}
}

/**
* Send content in chunk
*
* @param string $content
* @param bool $chunkGzip
*/
protected function sendInChunk($content)
protected function sendInChunk($content, $chunkGzip)
{
if (strlen($content) <= static::CHUNK_SIZE) {
$this->swooleResponse->end($content);
return;
}

// Swoole Chunk mode does not support compress by default, this patch only supports gzip
if ($chunkGzip) {
$this->swooleResponse->header('Content-Encoding', 'gzip');
$content = gzencode($content, config('swoole_http.server.options.http_compression_level', 3));
}

foreach (str_split($content, static::CHUNK_SIZE) as $chunk) {
$this->swooleResponse->write($chunk);
}
Expand Down Expand Up @@ -184,4 +201,37 @@ public function getIlluminateResponse()
{
return $this->illuminateResponse;
}

/**
* @param \Swoole\Http\Request $swooleRequest
*
* @return \SwooleTW\Http\Transformers\Response
*/
protected function setSwooleRequest(SwooleRequest $swooleRequest)
{
$this->swooleRequest = $swooleRequest;

return $this;
}

/**
* @return \Swoole\Http\Request
*/
public function getSwooleRequest()
{
return $this->swooleRequest;
}

/**
* @param string $responseContentEncoding
* @return bool
*/
protected function canGzipContent($responseContentEncoding)
{
return empty($responseContentEncoding) &&
config('swoole_http.server.options.http_compression', true) &&
!empty($this->swooleRequest->header['accept-encoding']) &&
strpos($this->swooleRequest->header['accept-encoding'], 'gzip') !== false &&
function_exists('gzencode');
}
}
Loading

0 comments on commit 5e80d23

Please sign in to comment.