Skip to content

Commit

Permalink
Always compute the Content Digest, but avoid storing content that is …
Browse files Browse the repository at this point in the history
…already avaible
  • Loading branch information
mpdude committed May 16, 2020
1 parent e798b16 commit aa18ef9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
29 changes: 15 additions & 14 deletions src/Symfony/Component/HttpKernel/HttpCache/Store.php
Expand Up @@ -177,19 +177,15 @@ public function write(Request $request, Response $response)
$key = $this->getCacheKey($request);
$storedEnv = $this->persistRequest($request);

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

if (!$this->save($digest, $response->getContent())) {
throw new \RuntimeException('Unable to store the entity.');
}

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

if (!$response->headers->has('Transfer-Encoding')) {
$response->headers->set('Content-Length', \strlen($response->getContent()));
}
if (!$response->headers->has('Transfer-Encoding')) {
$response->headers->set('Content-Length', \strlen($response->getContent()));
}

// read existing cache entries, remove non-varying, and add this one to the list
Expand Down Expand Up @@ -362,15 +358,20 @@ private function load($key)
/**
* Save data for the given key.
*
* @param string $key The store key
* @param string $data The data to store
* @param string $key The store key
* @param string $data The data to store
* @param bool $noOverwriteIfExists Don't overwrite data if the key already exists
*
* @return bool
*/
private function save($key, $data)
private function save($key, $data, $noOverwriteIfExists = false)
{
$path = $this->getPath($key);

if ($noOverwriteIfExists && file_exists($path)) {
return true;
}

if (isset($this->locks[$key])) {
$fp = $this->locks[$key];
@ftruncate($fp, 0);
Expand Down
Expand Up @@ -103,7 +103,7 @@ public function testDoesNotTrustXContentDigestFromUpstream()

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

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

0 comments on commit aa18ef9

Please sign in to comment.