@@ -152,8 +152,8 @@ public function lookup(Request $request)
152152 }
153153
154154 $ headers = $ match [1 ];
155- if (file_exists ($ body = $ this ->getPath ($ headers ['x-content-digest ' ][0 ]))) {
156- return $ this ->restoreResponse ($ headers , $ body );
155+ if (file_exists ($ path = $ this ->getPath ($ headers ['x-content-digest ' ][0 ]))) {
156+ return $ this ->restoreResponse ($ headers , $ path );
157157 }
158158
159159 // TODO the metaStore referenced an entity that doesn't exist in
@@ -177,15 +177,28 @@ public function write(Request $request, Response $response)
177177 $ key = $ this ->getCacheKey ($ request );
178178 $ storedEnv = $ this ->persistRequest ($ request );
179179
180- $ digest = $ this ->generateContentDigest ($ response );
181- $ response ->headers ->set ('X-Content-Digest ' , $ digest );
180+ if ($ response ->headers ->has ('X-Body-File ' )) {
181+ // Assume the response came from disk, but at least perform some safeguard checks
182+ if (!$ response ->headers ->has ('X-Content-Digest ' )) {
183+ throw new \RuntimeException ('A restored response must have the X-Content-Digest header. ' );
184+ }
182185
183- if (!$ this ->save ($ digest , $ response ->getContent (), false )) {
184- throw new \RuntimeException ('Unable to store the entity. ' );
185- }
186+ $ digest = $ response ->headers ->get ('X-Content-Digest ' );
187+ if ($ this ->getPath ($ digest ) !== $ response ->headers ->get ('X-Body-File ' )) {
188+ throw new \RuntimeException ('X-Body-File and X-Content-Digest do not match. ' );
189+ }
190+ // Everything seems ok, omit writing content to disk
191+ } else {
192+ $ digest = $ this ->generateContentDigest ($ response );
193+ $ response ->headers ->set ('X-Content-Digest ' , $ digest );
186194
187- if (!$ response ->headers ->has ('Transfer-Encoding ' )) {
188- $ response ->headers ->set ('Content-Length ' , \strlen ($ response ->getContent ()));
195+ if (!$ this ->save ($ digest , $ response ->getContent (), false )) {
196+ throw new \RuntimeException ('Unable to store the entity. ' );
197+ }
198+
199+ if (!$ response ->headers ->has ('Transfer-Encoding ' )) {
200+ $ response ->headers ->set ('Content-Length ' , \strlen ($ response ->getContent ()));
201+ }
189202 }
190203
191204 // read existing cache entries, remove non-varying, and add this one to the list
@@ -477,19 +490,19 @@ private function persistResponse(Response $response)
477490 * Restores a Response from the HTTP headers and body.
478491 *
479492 * @param array $headers An array of HTTP headers for the Response
480- * @param string $body The Response body
493+ * @param string $path Path to the Response body
481494 *
482495 * @return Response
483496 */
484- private function restoreResponse ($ headers , $ body = null )
497+ private function restoreResponse ($ headers , $ path = null )
485498 {
486499 $ status = $ headers ['X-Status ' ][0 ];
487500 unset($ headers ['X-Status ' ]);
488501
489- if (null !== $ body ) {
490- $ headers ['X-Body-File ' ] = [$ body ];
502+ if (null !== $ path ) {
503+ $ headers ['X-Body-File ' ] = [$ path ];
491504 }
492505
493- return new Response ($ body , $ status , $ headers );
506+ return new Response ($ path , $ status , $ headers );
494507 }
495508}
0 commit comments