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