@@ -152,8 +152,8 @@ public function lookup(Request $request)
152
152
}
153
153
154
154
$ 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 );
157
157
}
158
158
159
159
// TODO the metaStore referenced an entity that doesn't exist in
@@ -177,15 +177,28 @@ public function write(Request $request, Response $response)
177
177
$ key = $ this ->getCacheKey ($ request );
178
178
$ storedEnv = $ this ->persistRequest ($ request );
179
179
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
+ }
182
185
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 );
186
194
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
+ }
189
202
}
190
203
191
204
// read existing cache entries, remove non-varying, and add this one to the list
@@ -477,19 +490,19 @@ private function persistResponse(Response $response)
477
490
* Restores a Response from the HTTP headers and body.
478
491
*
479
492
* @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
481
494
*
482
495
* @return Response
483
496
*/
484
- private function restoreResponse ($ headers , $ body = null )
497
+ private function restoreResponse ($ headers , $ path = null )
485
498
{
486
499
$ status = $ headers ['X-Status ' ][0 ];
487
500
unset($ headers ['X-Status ' ]);
488
501
489
- if (null !== $ body ) {
490
- $ headers ['X-Body-File ' ] = [$ body ];
502
+ if (null !== $ path ) {
503
+ $ headers ['X-Body-File ' ] = [$ path ];
491
504
}
492
505
493
- return new Response ($ body , $ status , $ headers );
506
+ return new Response ($ path , $ status , $ headers );
494
507
}
495
508
}
0 commit comments