Skip to content

Commit

Permalink
bug #43998 [HttpKernel] [HttpCache] Don't throw on 304 Not Modified (…
Browse files Browse the repository at this point in the history
…aleho)

This PR was merged into the 4.4 branch.

Discussion
----------

[HttpKernel] [HttpCache] Don't throw on 304 Not Modified

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fixes  #43997
| License       | MIT
| Doc PR        | ~

If the response cache keeps a `Last-Modified` header clients will request with `If-Modified-Since`.
The surrogate will not handle a `304 Not Modified` correctly, resulting in a 500 and a failed request.

This fixes that request / response cycle, as observed in testing PR #42355.

Commits
-------

d27f02a [HttpKernel] [HttpCache] Don't throw on 304 Not Modified
  • Loading branch information
fabpot committed Jul 20, 2022
2 parents 180eade + d27f02a commit fa063a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Expand Up @@ -95,7 +95,7 @@ public function handle(HttpCache $cache, $uri, $alt, $ignoreErrors)
try {
$response = $cache->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);

if (!$response->isSuccessful()) {
if (!$response->isSuccessful() && Response::HTTP_NOT_MODIFIED !== $response->getStatusCode()) {
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %d).', $subRequest->getUri(), $response->getStatusCode()));
}

Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/EsiTest.php
Expand Up @@ -221,6 +221,15 @@ public function testHandleWhenResponseIsNot200AndAltIsPresent()
$this->assertEquals('bar', $esi->handle($cache, '/', '/alt', false));
}

public function testHandleWhenResponseIsNotModified()
{
$esi = new Esi();
$response = new Response('');
$response->setStatusCode(304);
$cache = $this->getCache(Request::create('/'), $response);
$this->assertEquals('', $esi->handle($cache, '/', '/alt', true));
}

protected function getCache($request, $response)
{
$cache = $this->getMockBuilder(HttpCache::class)->setMethods(['getRequest', 'handle'])->disableOriginalConstructor()->getMock();
Expand Down

0 comments on commit fa063a1

Please sign in to comment.