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 React\Cache\ArrayCache; |
10
|
|
|
use React\Cache\CacheInterface; |
11
|
|
|
use React\Http\Io\HttpBodyStream; |
12
|
|
|
use React\Http\Response; |
13
|
|
|
use function React\Promise\resolve; |
14
|
|
|
use function RingCentral\Psr7\stream_for; |
15
|
|
|
|
16
|
|
|
final class ResponseCacheMiddleware |
17
|
|
|
{ |
18
|
|
|
const PREFIX_WITHOUT_QUERY = '***'; |
19
|
|
|
const PREFIX_WITH_QUERY = '???'; |
20
|
|
|
const PREFIXES = [ |
21
|
|
|
self::PREFIX_WITH_QUERY, |
22
|
|
|
self::PREFIX_WITHOUT_QUERY, |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $staticUrls = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $prefixUrlsWithoutQuery = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $prefixUrlsWithQuery = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var CacheInterface |
42
|
|
|
*/ |
43
|
|
|
private $cache; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Clock |
47
|
|
|
*/ |
48
|
|
|
private $clock; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param array $urls |
52
|
|
|
* @param array $headers |
53
|
|
|
* @param CacheInterface|null $cache |
54
|
|
|
* @param Clock $clock |
55
|
|
|
*/ |
56
|
1 |
|
public function __construct(array $urls, array $headers = [], CacheInterface $cache = null, Clock $clock = null) |
57
|
|
|
{ |
58
|
1 |
|
$this->sortUrls($urls); |
59
|
1 |
|
$this->headers = $headers; |
|
|
|
|
60
|
1 |
|
$this->cache = $cache instanceof CacheInterface ? $cache : new ArrayCache(); |
61
|
1 |
|
$this->clock = $clock instanceof Clock ? $clock : new SystemClock(); |
62
|
1 |
|
} |
63
|
|
|
|
64
|
1 |
|
public function __invoke(ServerRequestInterface $request, callable $next) |
65
|
|
|
{ |
66
|
1 |
|
if ($request->getMethod() !== 'GET') { |
67
|
|
|
return resolve($next($request)); |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
$uri = $request->getUri()->getPath(); |
71
|
1 |
|
if (!in_array($uri, $this->staticUrls, true) && !$this->matchesPrefixUrl($uri)) { |
72
|
|
|
return resolve($next($request)); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$key = $uri; |
76
|
1 |
|
$query = $request->getUri()->getQuery(); |
77
|
1 |
|
if (strlen($query) > 0 && $this->queryInKey($uri)) { |
78
|
1 |
|
$key .= '?' . $query; |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return $this->cache->get($key)->then(function ($json) { |
82
|
1 |
|
$cachedResponse = json_decode($json); |
83
|
1 |
|
$headers = (array)$cachedResponse->headers; |
84
|
|
|
// Todo at 2 October 2018 remove the check around this assignment |
85
|
1 |
|
if (isset($cachedResponse->time)) { |
86
|
1 |
|
$headers['Age'] = (int)$this->clock->now()->format('U') - (string)$cachedResponse->time; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
return new Response($cachedResponse->code, $headers, stream_for($cachedResponse->body)); |
90
|
|
|
}, function () use ($next, $request, $key) { |
91
|
1 |
|
return resolve($next($request))->then(function (ResponseInterface $response) use ($key) { |
92
|
1 |
|
if ($response->getBody() instanceof HttpBodyStream) { |
93
|
1 |
|
return $response; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$body = (string)$response->getBody(); |
97
|
1 |
|
$headers = []; |
98
|
1 |
|
foreach ($this->headers as $header) { |
99
|
1 |
|
if (!$response->hasHeader($header)) { |
100
|
|
|
continue; |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
$headers[$header] = $response->getHeaderLine($header); |
104
|
|
|
} |
105
|
1 |
|
$cachedResponse = json_encode([ |
106
|
1 |
|
'body' => $body, |
107
|
1 |
|
'headers' => $headers, |
108
|
1 |
|
'code' => $response->getStatusCode(), |
109
|
1 |
|
'time' => (int)$this->clock->now()->format('U'), |
110
|
|
|
]); |
111
|
1 |
|
$this->cache->set($key, $cachedResponse); |
112
|
|
|
|
113
|
1 |
|
return $response->withBody(stream_for($body)); |
114
|
1 |
|
}); |
115
|
1 |
|
}); |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
private function sortUrls(array $urls) |
119
|
|
|
{ |
120
|
1 |
|
foreach ($urls as $url) { |
121
|
1 |
|
if (!(strlen($url) >= 3 && in_array(substr($url, -3), self::PREFIXES, true))) { |
122
|
1 |
|
$this->staticUrls[] = $url; |
123
|
|
|
|
124
|
1 |
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
if (strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITHOUT_QUERY) { |
128
|
1 |
|
$this->prefixUrlsWithoutQuery[] = substr($url, 0, -3); |
129
|
|
|
|
130
|
1 |
|
continue; |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
if (strlen($url) >= 3 && substr($url, -3) === self::PREFIX_WITH_QUERY) { |
134
|
1 |
|
$this->prefixUrlsWithQuery[] = substr($url, 0, -3); |
135
|
|
|
|
136
|
1 |
|
continue; |
137
|
|
|
} |
138
|
|
|
} |
139
|
1 |
|
} |
140
|
|
|
|
141
|
1 |
|
private function matchesPrefixUrl(string $uri): bool |
142
|
|
|
{ |
143
|
1 |
|
if ($this->urlMatchesPrefixes($this->prefixUrlsWithoutQuery, $uri)) { |
144
|
1 |
|
return true; |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
private function queryInKey(string $uri): bool |
151
|
|
|
{ |
152
|
1 |
|
return $this->urlMatchesPrefixes($this->prefixUrlsWithQuery, $uri); |
153
|
|
|
} |
154
|
|
|
|
155
|
1 |
|
private function urlMatchesPrefixes(array $urls, string $uri): bool |
156
|
|
|
{ |
157
|
1 |
|
foreach ($urls as $url) { |
158
|
1 |
|
$urlLength = strlen($url); |
159
|
1 |
|
if (substr($uri, 0, $urlLength) === $url) { |
160
|
1 |
|
return true; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return false; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|