@@ -176,24 +176,25 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
176
176
}
177
177
}
178
178
179
- $ path = $ request ->getPathInfo ();
180
- if ($ qs = $ request ->getQueryString ()) {
181
- $ path .= '? ' .$ qs ;
182
- }
183
- $ this ->traces [$ request ->getMethod ().' ' .$ path ] = array ();
179
+ $ this ->traces [$ this ->getTraceKey ($ request )] = array ();
184
180
185
181
if (!$ request ->isMethodSafe (false )) {
186
182
$ response = $ this ->invalidate ($ request , $ catch );
187
183
} elseif ($ request ->headers ->has ('expect ' ) || !$ request ->isMethodCacheable ()) {
188
184
$ response = $ this ->pass ($ request , $ catch );
185
+ } elseif ($ this ->options ['allow_reload ' ] && $ request ->isNoCache ()) {
186
+ /*
187
+ If allow_reload is configured and the client requests "Cache-Control: no-cache",
188
+ reload the cache by fetching a fresh response and caching it (if possible).
189
+ */
190
+ $ this ->record ($ request , 'reload ' );
191
+ $ response = $ this ->fetch ($ request , $ catch );
189
192
} else {
190
193
$ response = $ this ->lookup ($ request , $ catch );
191
194
}
192
195
193
196
$ this ->restoreResponseBody ($ request , $ response );
194
197
195
- $ response ->setDate (\DateTime::createFromFormat ('U ' , time (), new \DateTimeZone ('UTC ' )));
196
-
197
198
if (HttpKernelInterface::MASTER_REQUEST === $ type && $ this ->options ['debug ' ]) {
198
199
$ response ->headers ->set ('X-Symfony-Cache ' , $ this ->getLog ());
199
200
}
@@ -299,13 +300,6 @@ protected function invalidate(Request $request, $catch = false)
299
300
*/
300
301
protected function lookup (Request $ request , $ catch = false )
301
302
{
302
- // if allow_reload and no-cache Cache-Control, allow a cache reload
303
- if ($ this ->options ['allow_reload ' ] && $ request ->isNoCache ()) {
304
- $ this ->record ($ request , 'reload ' );
305
-
306
- return $ this ->fetch ($ request , $ catch );
307
- }
308
-
309
303
try {
310
304
$ entry = $ this ->store ->lookup ($ request );
311
305
} catch (\Exception $ e ) {
@@ -403,9 +397,8 @@ protected function validate(Request $request, Response $entry, $catch = false)
403
397
}
404
398
405
399
/**
406
- * Forwards the Request to the backend and determines whether the response should be stored.
407
- *
408
- * This methods is triggered when the cache missed or a reload is required.
400
+ * Unconditionally fetches a fresh response from the backend and
401
+ * stores it in the cache if is cacheable.
409
402
*
410
403
* @param Request $request A Request instance
411
404
* @param bool $catch whether to process exceptions
@@ -437,6 +430,9 @@ protected function fetch(Request $request, $catch = false)
437
430
/**
438
431
* Forwards the Request to the backend and returns the Response.
439
432
*
433
+ * All backend requests (cache passes, fetches, cache validations)
434
+ * run through this method.
435
+ *
440
436
* @param Request $request A Request instance
441
437
* @param bool $catch Whether to catch exceptions or not
442
438
* @param Response $entry A Response instance (the stale entry if present, null otherwise)
@@ -484,6 +480,17 @@ protected function forward(Request $request, $catch = false, Response $entry = n
484
480
}
485
481
}
486
482
483
+ /*
484
+ RFC 7231 Sect. 7.1.1.2 says that a server that does not have a reasonably accurate
485
+ clock MUST NOT send a "Date" header, although it MUST send one in most other cases
486
+ except for 1xx or 5xx responses where it MAY do so.
487
+
488
+ Anyway, a client that received a message without a "Date" header MUST add it.
489
+ */
490
+ if (!$ response ->headers ->has ('Date ' )) {
491
+ $ response ->setDate (\DateTime::createFromFormat ('U ' , time ()));
492
+ }
493
+
487
494
$ this ->processResponseBody ($ request , $ response );
488
495
489
496
if ($ this ->isPrivateRequest ($ request ) && !$ response ->headers ->hasCacheControlDirective ('public ' )) {
@@ -584,9 +591,6 @@ protected function lock(Request $request, Response $entry)
584
591
*/
585
592
protected function store (Request $ request , Response $ response )
586
593
{
587
- if (!$ response ->headers ->has ('Date ' )) {
588
- $ response ->setDate (\DateTime::createFromFormat ('U ' , time ()));
589
- }
590
594
try {
591
595
$ this ->store ->write ($ request , $ response );
592
596
@@ -683,11 +687,24 @@ private function isPrivateRequest(Request $request)
683
687
* @param string $event The event name
684
688
*/
685
689
private function record (Request $ request , $ event )
690
+ {
691
+ $ this ->traces [$ this ->getTraceKey ($ request )][] = $ event ;
692
+ }
693
+
694
+ /**
695
+ * Calculates the key we use in the "trace" array for a given request.
696
+ *
697
+ * @param Request $request
698
+ *
699
+ * @return string
700
+ */
701
+ private function getTraceKey (Request $ request )
686
702
{
687
703
$ path = $ request ->getPathInfo ();
688
704
if ($ qs = $ request ->getQueryString ()) {
689
705
$ path .= '? ' .$ qs ;
690
706
}
691
- $ this ->traces [$ request ->getMethod ().' ' .$ path ][] = $ event ;
707
+
708
+ return $ request ->getMethod ().' ' .$ path ;
692
709
}
693
710
}
0 commit comments