8000 [Response] `getMaxAge()` returns non-negative integer by pkruithof · Pull Request #48880 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Response] getMaxAge() returns non-negative integer #48880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Symfony/Component/HttpFoundation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,10 @@ public function getMaxAge(): ?int
return (int) $this->headers->getCacheControlDirective('max-age');
}

if (null !== $this->getExpires()) {
return (int) $this->getExpires()->format('U') - (int) $this->getDate()->format('U');
if (null !== $expires = $this->getExpires()) {
$maxAge = (int) $expires->format('U') - (int) $this->getDate()->format('U');

return max($maxAge, 0);
}

return null;
Expand Down Expand Up @@ -819,7 +821,7 @@ public function setSharedMaxAge(int $value): object
*
* It returns null when no freshness information is present in the response.
*
* When the responses TTL is <= 0, the response may not be served from cache without first
* When the response's TTL is 0, the response may not be served from cache without first
* revalidating with the origin.
*
* @final
Expand All @@ -828,7 +830,7 @@ public function getTtl(): ?int
{
$maxAge = $this->getMaxAge();

return null !== $maxAge ? $maxAge - $this->getAge() : null;
return null !== $maxAge ? max($maxAge - $this->getAge(), 0) : null;
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,8 @@ public function testGetMaxAge()
$this->assertEquals(3600, $response->getMaxAge(), '->getMaxAge() falls back to Expires when no max-age or s-maxage directive present');

$response = new Response();
$response->headers->set('Cache-Control', 'must-revalidate');
$response->headers->set('Expires', -1);
$this->assertLessThanOrEqual(time() - 2 * 86400, $response->getExpires()->format('U'));
$this->assertSame(0, $response->getMaxAge());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test was for getMaxAge() however in this case it was testing getExpires(), which seemed wrong.


$response = new Response();
$this->assertNull($response->getMaxAge(), '->getMaxAge() returns null if no freshness information available');
Expand Down Expand Up @@ -436,7 +435,7 @@ public function testGetTtl()

$response = new Response();
$response->headers->set('Expires', $this->createDateTimeOneHourAgo()->format(\DATE_RFC2822));
$this->assertLessThan(0, $response->getTtl(), '->getTtl() returns negative values when Expires is in past');
$this->assertSame(0, $response->getTtl(), '->getTtl() returns zero when Expires is in past');

$response = new Response();
$response->headers->set('Expires', $response->getDate()->format(\DATE_RFC2822));
Expand Down
6 changes: 5 additions & 1 deletion src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,11 @@ private function mayServeStaleWhileRevalidate(Response $entry): bool
$timeout = $this->options['stale_while_revalidate'];
}

return abs($entry->getTtl() ?? 0) < $timeout;
$age = $entry->getAge();
$maxAge = $entry->getMaxAge() ?? 0;
$ttl = $maxAge - $age;

return abs($ttl) < $timeout;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,30 @@ public function testResponseHeadersMaxAgeAndExpiresDefaultValuesIfSessionStarted
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
}

public function testPrivateResponseMaxAgeIsRespectedIfSessionStarted()
{
$kernel = $this->createMock(HttpKernelInterface::class);

$session = $this->createMock(Session::class);
$session->expects($this->once())->method('getUsageIndex')->willReturn(1);
$request = new Request([], [], [], [], [], ['SERVER_PROTOCOL' => 'HTTP/1.0']);
$request->setSession($session);

$response = new Response();
$response->headers->set('Cache-Control', 'no-cache');
$response->prepare($request);

$listener = new SessionListener(new Container());
$listener->onKernelResponse(new ResponseEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST, $response));

$this->assertSame(0, $response->getMaxAge());
$this->assertFalse($response->headers->hasCacheControlDirective('public'));
$this->assertTrue($response->headers->hasCacheControlDirective('private'));
$this->assertTrue($response->headers->hasCacheControlDirective('must-revalidate'));
$this->assertLessThanOrEqual(new \DateTimeImmutable('now', new \DateTimeZone('UTC')), new \DateTimeImmutable($response->headers->get('Expires')));
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
}

public function testSurrogateMainRequestIsPublic()
{
$session = $this->createMock(Session::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"symfony/deprecation-contracts": "^2.1|^3",
"symfony/error-handler": "^4.4|^5.0|^6.0",
"symfony/event-dispatcher": "^5.0|^6.0",
"symfony/http-foundation": "^5.3.7|^6.0",
"symfony/http-foundation": "^5.4.21|^6.2.7",
"symfony/polyfill-ctype": "^1.8",
"symfony/polyfill-php73": "^1.9",
"symfony/polyfill-php80": "^1.16",
Expand Down
0