Skip to content

Commit

Permalink
feat: enable http compression by sending data not with chunked encodi…
Browse files Browse the repository at this point in the history
…ng - if possible
  • Loading branch information
DeepDiver1975 committed May 3, 2023
1 parent f3ae57e commit 13b7fc1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Swoole/SwooleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,15 @@ protected function sendResponseContent(OctaneResponse $octaneResponse, SwooleRes
return;
}

// if the content fits into one chunk -> perform no chunked encoding
if ($length <= $this->chunkSize) {
$swooleResponse->write($content);
} else {
for ($offset = 0; $offset < $length; $offset += $this->chunkSize) {
$swooleResponse->write(substr($content, $offset, $this->chunkSize));
}
$swooleResponse->end($content);

return;
}

for ($offset = 0; $offset < $length; $offset += $this->chunkSize) {
$swooleResponse->write(substr($content, $offset, $this->chunkSize));
}

$swooleResponse->end();
Expand Down
44 changes: 44 additions & 0 deletions tests/SwooleClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public function test_static_file_can_be_served()
$client->serveStaticFile($request, $context);
}

/** @doesNotPerformAssertions @test */
public function test_static_file_headers_can_be_sent()
{
$client = new SwooleClient;
Expand Down Expand Up @@ -297,4 +298,47 @@ public function test_error_method_sends_detailed_error_response_to_swoole_in_deb
$swooleResponse->shouldHaveReceived('header')->with('Content-Type', 'text/plain');
$swooleResponse->shouldHaveReceived('end')->with((string) $e);
}

/** @doesNotPerformAssertions @test */
public function test_respond_method_send_not_chunked_response_to_swoole(): void
{
$client = new SwooleClient;

$swooleResponse = Mockery::mock('Swoole\Http\Response');

$swooleResponse->shouldReceive('status')->once()->with(200);
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
$swooleResponse->shouldReceive('write')->never();
$swooleResponse->shouldReceive('end')->once()->with('Hello World');

$response = new Response('Hello World', 200, ['Content-Type' => 'text/html']);

$client->respond(new RequestContext([
'swooleResponse' => $swooleResponse,
]), new OctaneResponse($response));
}

/** @doesNotPerformAssertions @test */
public function test_respond_method_send_chunked_response_to_swoole(): void
{
$client = new SwooleClient(6);

$swooleResponse = Mockery::mock('Swoole\Http\Response');

$swooleResponse->shouldReceive('status')->once()->with(200);
$swooleResponse->shouldReceive('header')->once()->with('Cache-Control', 'no-cache, private');
$swooleResponse->shouldReceive('header')->once()->with('Content-Type', 'text/html');
$swooleResponse->shouldReceive('header')->once()->with('Date', Mockery::type('string'));
$swooleResponse->shouldReceive('write')->once()->with('Hello ');
$swooleResponse->shouldReceive('write')->once()->with('World');
$swooleResponse->shouldReceive('end')->once();

$response = new Response('Hello World', 200, ['Content-Type' => 'text/html']);

$client->respond(new RequestContext([
'swooleResponse' => $swooleResponse,
]), new OctaneResponse($response));
}
}

0 comments on commit 13b7fc1

Please sign in to comment.