Skip to content

Commit

Permalink
Merge pull request #572 from BelleNottelling/fix/output-compression
Browse files Browse the repository at this point in the history
Fix content length when using response body callbacks
  • Loading branch information
n0nag0n authored Apr 11, 2024
2 parents d92c65f + 77d3131 commit 0011efd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
26 changes: 13 additions & 13 deletions flight/net/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ public function sendHeaders(): self
);
}

if ($this->content_length === true) {
// Send content length
$length = $this->getContentLength();

if ($length > 0) {
$this->setHeader('Content-Length', (string) $length);
}
}

// Send other headers
foreach ($this->headers as $field => $value) {
if (\is_array($value)) {
Expand All @@ -352,15 +361,6 @@ public function sendHeaders(): self
}
}

if ($this->content_length) {
// Send content length
$length = $this->getContentLength();

if ($length > 0) {
$this->setRealHeader('Content-Length: ' . $length);
}
}

return $this;
}

Expand Down Expand Up @@ -432,15 +432,15 @@ public function send(): void
}
}

if (!headers_sent()) {
$this->sendHeaders(); // @codeCoverageIgnore
}

// Only for the v3 output buffering.
if ($this->v2_output_buffering === false) {
$this->processResponseCallbacks();
}

if (headers_sent() === false) {
$this->sendHeaders(); // @codeCoverageIgnore
}

echo $this->body;

$this->sent = true;
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<exclude name="Generic.Commenting.DocComment.ContentBeforeClose" />
<exclude name="Generic.Commenting.DocComment.MissingShort" />
<exclude name="Generic.Commenting.DocComment.SpacingBeforeShort" />
<exclude name="Generic.Formatting.NoSpaceAfterCast.SpaceFound" />
<exclude name="Generic.Functions.OpeningFunctionBraceKernighanRitchie.BraceOnNewLine" />
<exclude name="Generic.Formatting.MultipleStatementAlignment.NotSameWarning" />
<exclude name="Generic.Functions.OpeningFunctionBraceBsdAllman.BraceOnSameLine" />
Expand Down
16 changes: 16 additions & 0 deletions tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ public function testResponseBodyCallback()
$this->assertEquals('grfg', $rot13_body);
}

public function testResponseBodyCallbackGzip()
{
$response = new Response();
$response->content_length = true;
$response->write('test');
$gzip = function ($body) {
return gzencode($body);
};
$response->addResponseBodyCallback($gzip);
ob_start();
$response->send();
$gzip_body = ob_get_clean();
$this->assertEquals('H4sIAAAAAAAAAytJLS4BAAx+f9gEAAAA', base64_encode($gzip_body));
$this->assertEquals(strlen(gzencode('test')), strlen($gzip_body));
}

public function testResponseBodyCallbackMultiple()
{
$response = new Response();
Expand Down

0 comments on commit 0011efd

Please sign in to comment.