1
|
|
|
<?php |
2
|
|
|
namespace Yiisoft\Yii\Web\Middleware; |
3
|
|
|
|
4
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
8
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Yiisoft\Router\Method; |
11
|
|
|
use Yiisoft\Yii\Web\Session\SessionInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* HttpCache implements client-side caching by utilizing the `Last-Modified` and `ETag` HTTP headers. |
15
|
|
|
*/ |
16
|
|
|
final class HttpCache implements MiddlewareInterface |
17
|
|
|
{ |
18
|
|
|
private const DEFAULT_HEADER = 'public, max-age=3600'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var callable a PHP callback that returns the UNIX timestamp of the last modification time. |
22
|
|
|
* The callback's signature should be: |
23
|
|
|
* |
24
|
|
|
* ```php |
25
|
|
|
* function ($request, $params) |
26
|
|
|
* ``` |
27
|
|
|
* |
28
|
|
|
* where `$request` is the [[ServerRequestInterface]] object that this filter is currently handling; |
29
|
|
|
* `$params` takes the value of [[params]]. The callback should return a UNIX timestamp. |
30
|
|
|
* |
31
|
|
|
* @see http://tools.ietf.org/html/rfc7232#section-2.2 |
32
|
|
|
*/ |
33
|
|
|
private $lastModified; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var callable a PHP callback that generates the ETag seed string. |
37
|
|
|
* The callback's signature should be: |
38
|
|
|
* |
39
|
|
|
* ```php |
40
|
|
|
* function ($request, $params) |
41
|
|
|
* ``` |
42
|
|
|
* |
43
|
|
|
* where `$request` is the [[ServerRequestInterface]] object that this filter is currently handling; |
44
|
|
|
* `$params` takes the value of [[params]]. The callback should return a string serving |
45
|
|
|
* as the seed for generating an ETag. |
46
|
|
|
*/ |
47
|
|
|
private $etagSeed; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var bool whether to generate weak ETags. |
51
|
|
|
* |
52
|
|
|
* Weak ETags should be used if the content should be considered semantically equivalent, but not byte-equal. |
53
|
|
|
* |
54
|
|
|
* @see http://tools.ietf.org/html/rfc7232#section-2.3 |
55
|
|
|
*/ |
56
|
|
|
private bool $weakEtag = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var mixed additional parameters that should be passed to the [[lastModified]] and [[etagSeed]] callbacks. |
60
|
|
|
*/ |
61
|
|
|
private $params; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string the value of the `Cache-Control` HTTP header. If null, the header will not be sent. |
65
|
|
|
* @see http://tools.ietf.org/html/rfc2616#section-14.9 |
66
|
|
|
*/ |
67
|
|
|
private ?string $cacheControlHeader = self::DEFAULT_HEADER; |
68
|
|
|
|
69
|
|
|
private ResponseFactoryInterface $responseFactory; |
70
|
|
|
private SessionInterface $session; |
71
|
|
|
private LoggerInterface $logger; |
72
|
|
|
|
73
|
|
|
public function __construct( |
74
|
|
|
ResponseFactoryInterface $responseFactory, |
75
|
|
|
SessionInterface $session, |
76
|
|
|
LoggerInterface $logger |
77
|
|
|
) { |
78
|
|
|
$this->responseFactory = $responseFactory; |
79
|
|
|
$this->session = $session; |
80
|
|
|
$this->logger = $logger; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
84
|
|
|
{ |
85
|
|
|
$method = $request->getMethod(); |
86
|
|
|
if ( |
87
|
|
|
!\in_array($method, [Method::GET, Method::HEAD]) |
88
|
|
|
|| ($this->lastModified === null && $this->etagSeed === null) |
89
|
|
|
) { |
90
|
|
|
return $handler->handle($request); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$lastModified = $etag = null; |
94
|
|
|
if ($this->lastModified !== null) { |
95
|
|
|
$lastModified = call_user_func($this->lastModified, $request, $this->params); |
96
|
|
|
} |
97
|
|
|
if ($this->etagSeed !== null) { |
98
|
|
|
$seed = call_user_func($this->etagSeed, $request, $this->params); |
99
|
|
|
if ($seed !== null) { |
100
|
|
|
$etag = $this->generateEtag($seed); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$response = $handler->handle($request); |
105
|
|
|
if ($this->cacheControlHeader !== null) { |
106
|
|
|
$response = $response->withHeader('Cache-Control', $this->cacheControlHeader); |
107
|
|
|
} |
108
|
|
|
if ($etag !== null) { |
109
|
|
|
$response = $response->withHeader('Etag', $etag); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$cacheValid = $this->validateCache($request, $lastModified, $etag); |
113
|
|
|
// https://tools.ietf.org/html/rfc7232#section-4.1 |
114
|
|
|
if ($lastModified !== null && (!$cacheValid || ($cacheValid && $etag === null))) { |
115
|
|
|
$response = $response->withHeader( |
116
|
|
|
'Last-Modified', |
117
|
|
|
gmdate('D, d M Y H:i:s', $lastModified) . ' GMT' |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
if ($cacheValid) { |
121
|
|
|
$response = $this->responseFactory->createResponse(304); |
122
|
|
|
$response->getBody()->write('Not Modified'); |
123
|
|
|
return $response; |
124
|
|
|
} |
125
|
|
|
return $response; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Validates if the HTTP cache contains valid content. |
130
|
|
|
* If both Last-Modified and ETag are null, returns false. |
131
|
|
|
* @param ServerRequestInterface $request |
132
|
|
|
* @param int $lastModified the calculated Last-Modified value in terms of a UNIX timestamp. |
133
|
|
|
* If null, the Last-Modified header will not be validated. |
134
|
|
|
* @param string $etag the calculated ETag value. If null, the ETag header will not be validated. |
135
|
|
|
* @return bool whether the HTTP cache is still valid. |
136
|
|
|
*/ |
137
|
|
|
private function validateCache(ServerRequestInterface $request, $lastModified, $etag): bool |
138
|
|
|
{ |
139
|
|
|
if ($request->hasHeader('If-None-Match')) { |
140
|
|
|
// HTTP_IF_NONE_MATCH takes precedence over HTTP_IF_MODIFIED_SINCE |
141
|
|
|
// http://tools.ietf.org/html/rfc7232#section-3.3 |
142
|
|
|
return $etag !== null && \in_array($etag, $this->getETags($request), true); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($request->hasHeader('If-Modified-Since')) { |
146
|
|
|
$headers = $request->getHeader('If-Modified-Since'); |
147
|
|
|
$header = \reset($headers); |
148
|
|
|
return $lastModified !== null && @strtotime($header) >= $lastModified; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Generates an ETag from the given seed string. |
156
|
|
|
* @param string $seed Seed for the ETag |
157
|
|
|
* @return string the generated ETag |
158
|
|
|
*/ |
159
|
|
|
private function generateEtag($seed): string |
160
|
|
|
{ |
161
|
|
|
$etag = '"' . rtrim(base64_encode(sha1($seed, true)), '=') . '"'; |
162
|
|
|
return $this->weakEtag ? 'W/' . $etag : $etag; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Gets the Etags. |
167
|
|
|
* |
168
|
|
|
* @param ServerRequestInterface $request |
169
|
|
|
* @return array The entity tags |
170
|
|
|
*/ |
171
|
|
|
private function getETags(ServerRequestInterface $request): array |
172
|
|
|
{ |
173
|
|
|
if ($request->hasHeader('If-None-Match')) { |
174
|
|
|
$headers = $request->getHeader('If-None-Match'); |
175
|
|
|
$header = \str_replace('-gzip', '', \reset($headers)); |
176
|
|
|
return \preg_split('/[\s,]+/', $header, -1, PREG_SPLIT_NO_EMPTY) ?: []; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return []; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function setLastModified(callable $lastModified): void |
183
|
|
|
{ |
184
|
|
|
$this->lastModified = $lastModified; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function setEtagSeed(callable $etagSeed): void |
188
|
|
|
{ |
189
|
|
|
$this->etagSeed = $etagSeed; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function setWeakTag(bool $weakTag): void |
193
|
|
|
{ |
194
|
|
|
$this->weakEtag = $weakTag; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function setParams($params): void |
198
|
|
|
{ |
199
|
|
|
$this->params = $params; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
public function setCacheControlHeader(?string $header): void |
203
|
|
|
{ |
204
|
|
|
$this->cacheControlHeader = $header; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|