Skip to content

Commit e980204

Browse files
committed
merged branch jdesrosiers/ticket_8097 (PR #8098)
This PR was submitted for the 2.0 branch but it was merged into the 2.1 branch instead (closes #8098). Discussion ---------- [HttpKernel] Fixed two bugs in HttpCache | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8097 | License | MIT | Doc PR | 1. 304 responses always send "Content-Type: text/html; charset=UTF-8" header I discovered that the HttpCache::handle method calls Response::prepare after calling Response::isModified. Response::isModified removes the Content-Type header as it should, but Response::handle adds in the default Content-Type header when none is set. If the default Content-Type is not the correct Content-Type, then the Content-Type in the cache gets clobered. I solved this problem by moving the Response::isModified call after the Response::prepare call. I updated the testRespondsWith304WhenIfModifiedSinceMatchesLastModified and testRespondsWith304WhenIfNoneMatchMatchesETag tests to verify that the Content-Type header was not being sent for 304 responses. 2. Failure to invalidate cached entities referred to by the Location header I discovered that the Store::invalidate method was looking for Location and Content-Location headers to invalidate, but it was looking in the request headers instead of the response headers. Because the Store::invalidate method doesn't take a response, I decided it was better to move this logic to the HttpCache::invalidate method instead. I updated the testInvalidatesCachedResponsesOnPost test to verify that Location headers are getting invalidated correctly. Commits ------- a4251bd [HttpKernel] Fixed two bugs in HttpCache
2 parents 92399ff + 5321600 commit e980204

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/Symfony/Component/HttpKernel/HttpCache/HttpCache.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,6 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
193193
$response = $this->lookup($request, $catch);
194194
}
195195

196-
$response->isNotModified($request);
197-
198196
$this->restoreResponseBody($request, $response);
199197

200198
$response->setDate(new \DateTime(null, new \DateTimeZone('UTC')));
@@ -213,6 +211,8 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
213211

214212
$response->prepare($request);
215213

214+
$response->isNotModified($request);
215+
216216
return $response;
217217
}
218218

@@ -262,6 +262,15 @@ protected function invalidate(Request $request, $catch = false)
262262
try {
263263
$this->store->invalidate($request, $catch);
264264

265+
// As per the RFC, invalidate Location and Content-Location URLs if present
266+
foreach (array('Location', 'Content-Location') as $header) {
267+
if ($uri = $response->headers->get($header)) {
268+
$subRequest = $request::create($uri, 'get', array(), array(), array(), $request->server->all());
269+
270+
$this->store->invalidate($subRequest);
271+
}
272+
}
273+
265274
$this->record($request, 'invalidate');
266275
} catch (\Exception $e) {
267276
$this->record($request, 'invalidate-failed');

src/Symfony/Component/HttpKernel/HttpCache/Store.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,6 @@ public function invalidate(Request $request)
231231
throw new \RuntimeException('Unable to store the metadata.');
232232
}
233233
}
234-
235-
// As per the RFC, invalidate Location and Content-Location URLs if present
236-
foreach (array('Location', 'Content-Location') as $header) {
237-
if ($uri = $request->headers->get($header)) {
238-
$subRequest = $request::create($uri, 'get', array(), array(), array(), $request->server->all());
239-
240-
$this->invalidate($subRequest);
241-
}
242-
}
243234
}
244235

245236
/**

src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function testRespondsWith304WhenIfModifiedSinceMatchesLastModified()
145145

146146
$this->assertHttpKernelIsCalled();
147147
$this->assertEquals(304, $this->response->getStatusCode());
148-
$this->assertEquals('text/html; charset=UTF-8', $this->response->headers->get('Content-Type'));
148+
$this->assertEquals('', $this->response->headers->get('Content-Type'));
149149
$this->assertEmpty($this->response->getContent());
150150
$this->assertTraceContains('miss');
151151
$this->assertTraceContains('store');
@@ -158,7 +158,7 @@ public function testRespondsWith304WhenIfNoneMatchMatchesETag()
158158

159159
$this->assertHttpKernelIsCalled();
160160
$this->assertEquals(304, $this->response->getStatusCode());
161-
$this->assertEquals('text/html; charset=UTF-8', $this->response->headers->get('Content-Type'));
161+
$this->assertEquals('', $this->response->headers->get('Content-Type'));
162162
$this->assertTrue($this->response->headers->has('ETag'));
163163
$this->assertEmpty($this->response->getContent());
164164
$this->assertTraceContains('miss');
@@ -845,7 +845,7 @@ public function testInvalidatesCachedResponsesOnPost()
845845
$this->assertTraceContains('fresh');
846846

847847
// now POST to same URL
848-
$this->request('POST', '/');
848+
$this->request('POST', '/helloworld');
849849
$this->assertHttpKernelIsCalled();
850850
$this->assertEquals('/', $this->response->headers->get('Location'));
851851
$this->assertTraceContains('invalidate');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy