From f06febdb2d12ce25a06639443090bcb545b9356d Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Sat, 29 Jun 2024 16:11:07 -0600 Subject: [PATCH 1/2] changed default cache behavior --- flight/net/Response.php | 29 ++++++++++++++++++-------- tests/ResponseTest.php | 46 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/flight/net/Response.php b/flight/net/Response.php index 1798de51..b02535fd 100644 --- a/flight/net/Response.php +++ b/flight/net/Response.php @@ -288,13 +288,7 @@ public function cache($expires): self { if ($expires === false) { $this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; - - $this->headers['Cache-Control'] = [ - 'no-store, no-cache, must-revalidate', - 'post-check=0, pre-check=0', - 'max-age=0', - ]; - + $this->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate'; $this->headers['Pragma'] = 'no-cache'; } else { $expires = \is_int($expires) ? $expires : strtotime($expires); @@ -437,8 +431,14 @@ public function send(): void $this->processResponseCallbacks(); } - if (headers_sent() === false) { - $this->sendHeaders(); // @codeCoverageIgnore + if ($this->headersSent() === false) { + + // If you haven't set a Cache-Control header, we'll assume you don't want caching + if($this->getHeader('Cache-Control') === null) { + $this->cache(false); + } + + $this->sendHeaders(); } echo $this->body; @@ -446,6 +446,17 @@ public function send(): void $this->sent = true; } + /** + * Headers have been sent + * + * @return bool + * @codeCoverageIgnore + */ + public function headersSent(): bool + { + return headers_sent(); + } + /** * Adds a callback to process the response body before it's sent. These are processed in the order * they are added diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index a163e7ea..de461ebf 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -164,11 +164,7 @@ public function testCacheFalseExpiresValue() $response->cache(false); $this->assertEquals([ 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', - 'Cache-Control' => [ - 'no-store, no-cache, must-revalidate', - 'post-check=0, pre-check=0', - 'max-age=0', - ], + 'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Pragma' => 'no-cache' ], $response->headers()); } @@ -239,6 +235,46 @@ public function setRealHeader(string $header_string, bool $replace = true, int $ $this->assertTrue($response->sent()); } + public function testSendWithNoHeadersSent() + { + $response = new class extends Response { + protected $test_sent_headers = []; + + public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): self + { + $this->test_sent_headers[] = $header_string; + return $this; + } + + public function getSentHeaders(): array + { + return $this->test_sent_headers; + } + + public function headersSent(): bool + { + return false; + } + }; + $response->header('Content-Type', 'text/html'); + $response->header('X-Test', 'test'); + $response->write('Something'); + + $this->expectOutputString('Something'); + + $response->send(); + $sent_headers = $response->getSentHeaders(); + $this->assertEquals([ + 'HTTP/1.1 200 OK', + 'Content-Type: text/html', + 'X-Test: test', + 'Expires: Mon, 26 Jul 1997 05:00:00 GMT', + 'Cache-Control: no-store, no-cache, must-revalidate', + 'Pragma: no-cache', + 'Content-Length: 9' + ], $sent_headers); + } + public function testClearBody() { $response = new Response(); From ba80e047b1bd7459cdd9029a6b104b46324be600 Mon Sep 17 00:00:00 2001 From: Austin Collier Date: Sat, 29 Jun 2024 16:14:08 -0600 Subject: [PATCH 2/2] prettified and added max-age=0 --- flight/net/Response.php | 31 +++++++++++++++---------------- tests/FlightTest.php | 3 +-- tests/ResponseTest.php | 22 +++++++++++----------- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/flight/net/Response.php b/flight/net/Response.php index b02535fd..362fe5d6 100644 --- a/flight/net/Response.php +++ b/flight/net/Response.php @@ -288,7 +288,7 @@ public function cache($expires): self { if ($expires === false) { $this->headers['Expires'] = 'Mon, 26 Jul 1997 05:00:00 GMT'; - $this->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate'; + $this->headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, max-age=0'; $this->headers['Pragma'] = 'no-cache'; } else { $expires = \is_int($expires) ? $expires : strtotime($expires); @@ -432,11 +432,10 @@ public function send(): void } if ($this->headersSent() === false) { - - // If you haven't set a Cache-Control header, we'll assume you don't want caching - if($this->getHeader('Cache-Control') === null) { - $this->cache(false); - } + // If you haven't set a Cache-Control header, we'll assume you don't want caching + if ($this->getHeader('Cache-Control') === null) { + $this->cache(false); + } $this->sendHeaders(); } @@ -446,16 +445,16 @@ public function send(): void $this->sent = true; } - /** - * Headers have been sent - * - * @return bool - * @codeCoverageIgnore - */ - public function headersSent(): bool - { - return headers_sent(); - } + /** + * Headers have been sent + * + * @return bool + * @codeCoverageIgnore + */ + public function headersSent(): bool + { + return headers_sent(); + } /** * Adds a callback to process the response body before it's sent. These are processed in the order diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 5d196d6f..cf0c9f01 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -361,8 +361,7 @@ public function testDoesNotPreserveVarsWhenFlagIsDisabled( string $output, array $renderParams, string $regexp - ): void - { + ): void { Flight::view()->preserveVars = false; $this->expectOutputString($output); diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index de461ebf..b6462943 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -164,7 +164,7 @@ public function testCacheFalseExpiresValue() $response->cache(false); $this->assertEquals([ 'Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', - 'Cache-Control' => 'no-store, no-cache, must-revalidate', + 'Cache-Control' => 'no-store, no-cache, must-revalidate, max-age=0', 'Pragma' => 'no-cache' ], $response->headers()); } @@ -235,8 +235,8 @@ public function setRealHeader(string $header_string, bool $replace = true, int $ $this->assertTrue($response->sent()); } - public function testSendWithNoHeadersSent() - { + public function testSendWithNoHeadersSent() + { $response = new class extends Response { protected $test_sent_headers = []; @@ -251,16 +251,16 @@ public function getSentHeaders(): array return $this->test_sent_headers; } - public function headersSent(): bool - { - return false; - } + public function headersSent(): bool + { + return false; + } }; $response->header('Content-Type', 'text/html'); $response->header('X-Test', 'test'); $response->write('Something'); - $this->expectOutputString('Something'); + $this->expectOutputString('Something'); $response->send(); $sent_headers = $response->getSentHeaders(); @@ -268,9 +268,9 @@ public function headersSent(): bool 'HTTP/1.1 200 OK', 'Content-Type: text/html', 'X-Test: test', - 'Expires: Mon, 26 Jul 1997 05:00:00 GMT', - 'Cache-Control: no-store, no-cache, must-revalidate', - 'Pragma: no-cache', + 'Expires: Mon, 26 Jul 1997 05:00:00 GMT', + 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0', + 'Pragma: no-cache', 'Content-Length: 9' ], $sent_headers); }