diff --git a/flight/net/Response.php b/flight/net/Response.php
index 9161f18b..1798de51 100644
--- a/flight/net/Response.php
+++ b/flight/net/Response.php
@@ -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)) {
@@ -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;
}
@@ -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;
diff --git a/phpcs.xml.dist b/phpcs.xml.dist
index bafe06d9..79f3ff0e 100644
--- a/phpcs.xml.dist
+++ b/phpcs.xml.dist
@@ -26,6 +26,7 @@
+
diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php
index 3226cc93..443ff893 100644
--- a/tests/ResponseTest.php
+++ b/tests/ResponseTest.php
@@ -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();