@@ -150,8 +150,8 @@ public function lookup(Request $request)
150
150
}
151
151
152
152
$ headers = $ match [1 ];
153
- if (file_exists ($ body = $ this ->getPath ($ headers ['x-content-digest ' ][0 ]))) {
154
- return $ this ->restoreResponse ($ headers , $ body );
153
+ if (file_exists ($ path = $ this ->getPath ($ headers ['x-content-digest ' ][0 ]))) {
154
+ return $ this ->restoreResponse ($ headers , $ path );
155
155
}
156
156
157
157
// TODO the metaStore referenced an entity that doesn't exist in
@@ -175,15 +175,28 @@ public function write(Request $request, Response $response)
175
175
$ key = $ this ->getCacheKey ($ request );
176
176
$ storedEnv = $ this ->persistRequest ($ request );
177
177
178
- $ digest = $ this ->generateContentDigest ($ response );
179
- $ response ->headers ->set ('X-Content-Digest ' , $ digest );
178
+ if ($ response ->headers ->has ('X-Body-File ' )) {
179
+ // Assume the response came from disk, but at least perform some safeguard checks
180
+ if (!$ response ->headers ->has ('X-Content-Digest ' )) {
181
+ throw new \RuntimeException ('A restored response must have the X-Content-Digest header. ' );
182
+ }
180
183
181
- if (!$ this ->save ($ digest , $ response ->getContent (), false )) {
182
- throw new \RuntimeException ('Unable to store the entity. ' );
183
- }
184
+ $ digest = $ response ->headers ->get ('X-Content-Digest ' );
185
+ if ($ this ->getPath ($ digest ) !== $ response ->headers ->get ('X-Body-File ' )) {
186
+ throw new \RuntimeException ('X-Body-File and X-Content-Digest do not match. ' );
187
+ }
188
+ // Everything seems ok, omit writing content to disk
189
+ } else {
190
+ $ digest = $ this ->generateContentDigest ($ response );
191
+ $ response ->headers ->set ('X-Content-Digest ' , $ digest );
184
192
185
- if (!$ response ->headers ->has ('Transfer-Encoding ' )) {
186
- $ response ->headers ->set ('Content-Length ' , \strlen ($ response ->getContent ()));
193
+ if (!$ this ->save ($ digest , $ response ->getContent (), false )) {
194
+ throw new \RuntimeException ('Unable to store the entity. ' );
195
+ }
196
+
197
+ if (!$ response ->headers ->has ('Transfer-Encoding ' )) {
198
+ $ response ->headers ->set ('Content-Length ' , \strlen ($ response ->getContent ()));
199
+ }
187
200
}
188
201
189
202
// read existing cache entries, remove non-varying, and add this one to the list
@@ -446,15 +459,15 @@ private function persistResponse(Response $response): array
446
459
/**
447
460
* Restores a Response from the HTTP headers and body.
448
461
*/
449
- private function restoreResponse (array $ headers , string $ body = null ): Response
462
+ private function restoreResponse (array $ headers , string $ path = null ): Response
450
463
{
451
464
$ status = $ headers ['X-Status ' ][0 ];
452
465
unset($ headers ['X-Status ' ]);
453
466
454
- if (null !== $ body ) {
455
- $ headers ['X-Body-File ' ] = [$ body ];
467
+ if (null !== $ path ) {
468
+ $ headers ['X-Body-File ' ] = [$ path ];
456
469
}
457
470
458
- return new Response ($ body , $ status , $ headers );
471
+ return new Response ($ path , $ status , $ headers );
459
472
}
460
473
}
0 commit comments