Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit aa18ef9

Browse filesBrowse files
committed
Always compute the Content Digest, but avoid storing content that is already avaible
1 parent e798b16 commit aa18ef9
Copy full SHA for aa18ef9

File tree

2 files changed

+16
-15
lines changed
Filter options

2 files changed

+16
-15
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/HttpCache/Store.php
+15-14Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,19 +177,15 @@ public function write(Request $request, Response $response)
177177
$key = $this->getCacheKey($request);
178178
$storedEnv = $this->persistRequest($request);
179179

180-
// write the response body to the entity store if this is the original response
181-
if (!$response->headers->has('X-Content-Digest')) {
182-
$digest = $this->generateContentDigest($response);
180+
$digest = $this->generateContentDigest($response);
181+
$response->headers->set('X-Content-Digest', $digest);
183182

184-
if (!$this->save($digest, $response->getContent())) {
185-
throw new \RuntimeException('Unable to store the entity.');
186-
}
187-
188-
$response->headers->set('X-Content-Digest', $digest);
183+
if (!$this->save($digest, $response->getContent(), true)) {
184+
throw new \RuntimeException('Unable to store the entity.');
185+
}
189186

190-
if (!$response->headers->has('Transfer-Encoding')) {
191-
$response->headers->set('Content-Length', \strlen($response->getContent()));
192-
}
187+
if (!$response->headers->has('Transfer-Encoding')) {
188+
$response->headers->set('Content-Length', \strlen($response->getContent()));
193189
}
194190

195191
// read existing cache entries, remove non-varying, and add this one to the list
@@ -362,15 +358,20 @@ private function load($key)
362358
/**
363359
* Save data for the given key.
364360
*
365-
* @param string $key The store key
366-
* @param string $data The data to store
361+
* @param string $key The store key
362+
* @param string $data The data to store
363+
* @param bool $noOverwriteIfExists Don't overwrite data if the key already exists
367364
*
368365
* @return bool
369366
*/
370-
private function save($key, $data)
367+
private function save($key, $data, $noOverwriteIfExists = false)
371368
{
372369
$path = $this->getPath($key);
373370

371+
if ($noOverwriteIfExists && file_exists($path)) {
372+
return true;
373+
}
374+
374375
if (isset($this->locks[$key])) {
375376
$fp = $this->locks[$key];
376377
@ftruncate($fp, 0);

‎src/Symfony/Component/HttpKernel/Tests/HttpCache/StoreTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/HttpCache/StoreTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function testDoesNotTrustXContentDigestFromUpstream()
103103

104104
$cacheKey = $this->store->write($this->request, $response);
105105
$entries = $this->getStoreMetadata($cacheKey);
106-
[, $res] = $entries[0];
106+
list(, $res) = $entries[0];
107107

108108
$this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $res['x-content-digest'][0]);
109109
$this->assertEquals('en9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08', $response->headers->get('X-Content-Digest'));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.