1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Lcobucci\Clock\Clock; |
6
|
|
|
use Lcobucci\Clock\SystemClock; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use RingCentral\Psr7\Response; |
10
|
|
|
use function RingCentral\Psr7\stream_for; |
11
|
|
|
|
12
|
|
|
final class CacheConfiguration implements CacheConfigurationInterface |
13
|
|
|
{ |
14
|
|
|
private const PREFIX_WITHOUT_QUERY = '***'; |
15
|
|
|
private const PREFIX_WITH_QUERY = '???'; |
16
|
|
|
private const PREFIXES = [ |
17
|
|
|
self::PREFIX_WITH_QUERY, |
18
|
|
|
self::PREFIX_WITHOUT_QUERY, |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $staticUrls = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
private $prefixUrlsWithoutQuery = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $prefixUrlsWithQuery = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $headers; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Clock |
43
|
|
|
*/ |
44
|
|
|
private $clock; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var null|callable |
48
|
|
|
*/ |
49
|
|
|
private $ttl; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $urls |
53
|
|
|
* @param array $headers |
54
|
|
|
* @param Clock|null $clock |
55
|
|
|
* @param callable|null $ttl |
56
|
|
|
*/ |
57
|
1 |
|
public function __construct(array $urls, array $headers = [], Clock $clock = null, callable $ttl = null) |
58
|
|
|
{ |
59
|
1 |
|
$this->sortUrls($urls); |
60
|
1 |
|
$this->headers = $headers; |
61
|
1 |
|
$this->clock = $clock instanceof Clock ? $clock : new SystemClock(); |
62
|
1 |
|
$this->ttl = $ttl; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
1 |
|
public function requestIsCacheable(ServerRequestInterface $request): bool |
66
|
|
|
{ |
67
|
1 |
|
if ($request->getMethod() !== 'GET') { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$uri = $request->getUri()->getPath(); |
72
|
1 |
|
if (!\in_array($uri, $this->staticUrls, true) && !$this->matchesPrefixUrl($uri)) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public function responseIsCacheable(ServerRequestInterface $request, ResponseInterface $response): bool |
80
|
|
|
{ |
81
|
1 |
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
public function cacheKey(ServerRequestInterface $request): string |
85
|
|
|
{ |
86
|
1 |
|
$key = $request->getUri()->getPath(); |
87
|
1 |
|
$query = $request->getUri()->getQuery(); |
88
|
1 |
|
if (\strlen($query) > 0 && $this->queryInKey($key)) { |
89
|
1 |
|
$key .= '?' . $query; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return $key; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function cacheTtl(ServerRequestInterface $request, ResponseInterface $response): ?int |
96
|
|
|
{ |
97
|
1 |
|
if ($this->ttl === null) { |
98
|
1 |
|
return null; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return ($this->ttl)($request, $response); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
public function cacheEncode(ResponseInterface $response): string |
105
|
|
|
{ |
106
|
1 |
|
$headers = []; |
107
|
1 |
|
foreach ($this->headers as $header) { |
108
|
1 |
|
if (!$response->hasHeader($header)) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
$headers[$header] = $response->getHeaderLine($header); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
return msgpack_pack([ |
116
|
1 |
|
'code' => $response->getStatusCode(), |
117
|
1 |
|
'time' => (int)$this->clock->now()->format('U'), |
118
|
1 |
|
'headers' => $headers, |
119
|
1 |
|
'body' => (string)$response->getBody(), |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
public function cacheDecode(string $response): ResponseInterface |
124
|
|
|
{ |
125
|
1 |
|
$response = msgpack_unpack($response); |
126
|
1 |
|
$response['headers'] = (array)$response['headers']; |
127
|
1 |
|
$response['headers']['Age'] = (int)$this->clock->now()->format('U') - (int)$response['time']; |
128
|
|
|
|
129
|
1 |
|
return new Response($response['code'], $response['headers'], stream_for($response['body'])); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
private function sortUrls(array $urls): void |
133
|
|
|
{ |
134
|
1 |
|
foreach ($urls as $url) { |
135
|
1 |
|
if (!(\strlen($url) >= 3 && \in_array(\substr($url, -3), self::PREFIXES, true))) { |
136
|
1 |
|
$this->staticUrls[] = $url; |
137
|
|
|
|
138
|
1 |
|
continue; |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
if (\strlen($url) >= 3 && \substr($url, -3) === self::PREFIX_WITHOUT_QUERY) { |
142
|
1 |
|
$this->prefixUrlsWithoutQuery[] = \substr($url, 0, -3); |
143
|
|
|
|
144
|
1 |
|
continue; |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
if (\strlen($url) >= 3 && \substr($url, -3) === self::PREFIX_WITH_QUERY) { |
148
|
1 |
|
$this->prefixUrlsWithQuery[] = \substr($url, 0, -3); |
149
|
|
|
|
150
|
1 |
|
continue; |
151
|
|
|
} |
152
|
|
|
} |
153
|
1 |
|
} |
154
|
|
|
|
155
|
1 |
|
private function matchesPrefixUrl(string $uri): bool |
156
|
|
|
{ |
157
|
1 |
|
if ($this->urlMatchesPrefixes($this->prefixUrlsWithoutQuery, $uri)) { |
158
|
1 |
|
return true; |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri); |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
private function queryInKey(string $uri): bool |
165
|
|
|
{ |
166
|
1 |
|
return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri); |
167
|
|
|
} |
168
|
|
|
|
169
|
1 |
|
private function urlMatchesPrefixes(array $urls, string $uri): bool |
170
|
|
|
{ |
171
|
1 |
|
foreach ($urls as $url) { |
172
|
1 |
|
if (\strpos($uri, $url) === 0) { |
173
|
1 |
|
return true; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
return false; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|