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
|
|
|
* @param array $urls |
48
|
|
|
* @param array $headers |
49
|
|
|
* @param Clock|null $clock |
50
|
|
|
*/ |
51
|
1 |
|
public function __construct(array $urls, array $headers = [], Clock $clock = null) |
52
|
|
|
{ |
53
|
1 |
|
$this->sortUrls($urls); |
54
|
1 |
|
$this->headers = $headers; |
55
|
1 |
|
$this->clock = $clock instanceof Clock ? $clock : new SystemClock(); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
1 |
|
public function requestIsCacheable(ServerRequestInterface $request): bool |
59
|
|
|
{ |
60
|
1 |
|
if ($request->getMethod() !== 'GET') { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
$uri = $request->getUri()->getPath(); |
65
|
1 |
|
if (!\in_array($uri, $this->staticUrls, true) && !$this->matchesPrefixUrl($uri)) { |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function responseIsCacheable(ServerRequestInterface $request, ResponseInterface $response): bool |
73
|
|
|
{ |
74
|
1 |
|
return true; |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
public function cacheKey(ServerRequestInterface $request): string |
78
|
|
|
{ |
79
|
1 |
|
$key = $request->getUri()->getPath(); |
80
|
1 |
|
$query = $request->getUri()->getQuery(); |
81
|
1 |
|
if (\strlen($query) > 0 && $this->queryInKey($key)) { |
82
|
1 |
|
$key .= '?' . $query; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
return $key; |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function cacheEncode(ResponseInterface $response): string |
89
|
|
|
{ |
90
|
1 |
|
$headers = []; |
91
|
1 |
|
foreach ($this->headers as $header) { |
92
|
1 |
|
if (!$response->hasHeader($header)) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$headers[$header] = $response->getHeaderLine($header); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return msgpack_pack([ |
100
|
1 |
|
'code' => $response->getStatusCode(), |
101
|
1 |
|
'time' => (int)$this->clock->now()->format('U'), |
102
|
1 |
|
'headers' => $headers, |
103
|
1 |
|
'body' => (string)$response->getBody(), |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
public function cacheDecode(string $response): ResponseInterface |
108
|
|
|
{ |
109
|
1 |
|
$response = msgpack_unpack($response); |
110
|
1 |
|
$response['headers'] = (array)$response['headers']; |
111
|
1 |
|
$response['headers']['Age'] = (int)$this->clock->now()->format('U') - (int)$response['time']; |
112
|
|
|
|
113
|
1 |
|
return new Response($response['code'], $response['headers'], stream_for($response['body'])); |
114
|
|
|
} |
115
|
|
|
|
116
|
1 |
|
private function sortUrls(array $urls) |
117
|
|
|
{ |
118
|
1 |
|
foreach ($urls as $url) { |
119
|
1 |
|
if (!(\strlen($url) >= 3 && \in_array(substr($url, -3), self::PREFIXES, true))) { |
120
|
1 |
|
$this->staticUrls[] = $url; |
121
|
|
|
|
122
|
1 |
|
continue; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
if (\strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITHOUT_QUERY) { |
126
|
1 |
|
$this->prefixUrlsWithoutQuery[] = substr($url, 0, -3); |
127
|
|
|
|
128
|
1 |
|
continue; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
if (\strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITH_QUERY) { |
132
|
1 |
|
$this->prefixUrlsWithQuery[] = substr($url, 0, -3); |
133
|
|
|
|
134
|
1 |
|
continue; |
135
|
|
|
} |
136
|
|
|
} |
137
|
1 |
|
} |
138
|
|
|
|
139
|
1 |
|
private function matchesPrefixUrl(string $uri): bool |
140
|
|
|
{ |
141
|
1 |
|
if ($this->urlMatchesPrefixes($this->prefixUrlsWithoutQuery, $uri)) { |
142
|
1 |
|
return true; |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri); |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
private function queryInKey(string $uri): bool |
149
|
|
|
{ |
150
|
1 |
|
return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri); |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
private function urlMatchesPrefixes(array $urls, string $uri): bool |
154
|
|
|
{ |
155
|
1 |
|
foreach ($urls as $url) { |
156
|
1 |
|
if (strpos($uri, $url) === 0) { |
157
|
1 |
|
return true; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
1 |
|
return false; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|