1 | <?php |
||
37 | class HttpDispatcher |
||
38 | { |
||
39 | /** |
||
40 | * @var HttpAsyncClient |
||
41 | */ |
||
42 | private $httpClient; |
||
43 | |||
44 | /** |
||
45 | * @var UriFactory |
||
46 | */ |
||
47 | private $uriFactory; |
||
48 | |||
49 | /** |
||
50 | * Queued requests. |
||
51 | * |
||
52 | * @var RequestInterface[] |
||
53 | */ |
||
54 | private $queue = []; |
||
55 | |||
56 | /** |
||
57 | * Caching proxy server host names or IP addresses. |
||
58 | * |
||
59 | * @var UriInterface[] |
||
60 | */ |
||
61 | private $servers; |
||
62 | |||
63 | /** |
||
64 | * Application host name and optional base URL. |
||
65 | * |
||
66 | * @var UriInterface |
||
67 | */ |
||
68 | private $baseUri; |
||
69 | |||
70 | /** |
||
71 | * If you specify a custom HTTP client, make sure that it converts HTTP |
||
72 | * errors to exceptions. |
||
73 | * |
||
74 | * If your proxy server IPs can not be statically configured, extend this |
||
75 | * class and overwrite getServers. Be sure to have some caching in |
||
76 | * getServers. |
||
77 | * |
||
78 | * @param string[] $servers Caching proxy server hostnames or IP |
||
79 | * addresses, including port if not port 80. |
||
80 | * E.g. ['127.0.0.1:6081'] |
||
81 | * @param string $baseUri Default application hostname, optionally |
||
82 | * including base URL, for purge and refresh |
||
83 | * requests (optional). This is required if |
||
84 | * you purge and refresh paths instead of |
||
85 | * absolute URLs |
||
86 | * @param HttpAsyncClient|null $httpClient Client capable of sending HTTP requests. If no |
||
87 | * client is supplied, a default one is created |
||
88 | * @param UriFactory|null $uriFactory Factory for PSR-7 URIs. If not specified, a |
||
89 | * default one is created |
||
90 | */ |
||
91 | 38 | public function __construct( |
|
92 | array $servers, |
||
93 | $baseUri = '', |
||
94 | HttpAsyncClient $httpClient = null, |
||
95 | UriFactory $uriFactory = null |
||
96 | ) { |
||
97 | 38 | if (!$httpClient) { |
|
98 | 26 | $httpClient = new PluginClient( |
|
99 | 26 | HttpAsyncClientDiscovery::find(), |
|
100 | 26 | [new ErrorPlugin()] |
|
101 | ); |
||
102 | } |
||
103 | 38 | $this->httpClient = $httpClient; |
|
104 | 38 | $this->uriFactory = $uriFactory ?: UriFactoryDiscovery::find(); |
|
105 | |||
106 | 38 | $this->setServers($servers); |
|
107 | 35 | $this->setBaseUri($baseUri); |
|
108 | 34 | } |
|
109 | |||
110 | /** |
||
111 | * Queue invalidation request. |
||
112 | * |
||
113 | * @param RequestInterface $invalidationRequest |
||
114 | */ |
||
115 | 33 | public function invalidate(RequestInterface $invalidationRequest) |
|
129 | |||
130 | /** |
||
131 | * Send all pending invalidation requests and make sure the requests have terminated and gather exceptions. |
||
132 | * |
||
133 | * @return int The number of cache invalidations performed per caching server |
||
134 | * |
||
135 | * @throws ExceptionCollection If any errors occurred during flush |
||
136 | */ |
||
137 | 33 | public function flush() |
|
138 | { |
||
139 | 33 | $queue = $this->queue; |
|
140 | 33 | $this->queue = []; |
|
141 | /** @var Promise[] $promises */ |
||
142 | 33 | $promises = []; |
|
143 | |||
144 | 33 | $exceptions = new ExceptionCollection(); |
|
145 | |||
146 | 33 | foreach ($queue as $request) { |
|
147 | 32 | foreach ($this->fanOut($request) as $proxyRequest) { |
|
148 | try { |
||
149 | 32 | $promises[] = $this->httpClient->sendAsyncRequest($proxyRequest); |
|
150 | 1 | } catch (\Exception $e) { |
|
151 | 32 | $exceptions->add(new InvalidArgumentException($e)); |
|
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | 33 | foreach ($promises as $promise) { |
|
157 | try { |
||
158 | 32 | $promise->wait(); |
|
159 | 4 | } catch (HttpException $exception) { |
|
160 | 2 | $exceptions->add(ProxyResponseException::proxyResponse($exception)); |
|
161 | 2 | } catch (RequestException $exception) { |
|
162 | 2 | $exceptions->add(ProxyUnreachableException::proxyUnreachable($exception)); |
|
163 | } catch (\Exception $exception) { |
||
164 | // @codeCoverageIgnoreStart |
||
165 | $exceptions->add(new InvalidArgumentException($exception)); |
||
166 | // @codeCoverageIgnoreEnd |
||
167 | } |
||
168 | } |
||
169 | |||
170 | 33 | if (count($exceptions)) { |
|
171 | 5 | throw $exceptions; |
|
172 | } |
||
173 | |||
174 | 31 | return count($queue); |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * Get the list of servers to send invalidation requests to. |
||
179 | * |
||
180 | * @return UriInterface[] |
||
181 | */ |
||
182 | 32 | protected function getServers() |
|
186 | |||
187 | /** |
||
188 | * Duplicate a request for each caching server. |
||
189 | * |
||
190 | * @param RequestInterface $request The request to duplicate for each configured server |
||
191 | * |
||
192 | * @return RequestInterface[] |
||
193 | */ |
||
194 | 32 | private function fanOut(RequestInterface $request) |
|
195 | { |
||
196 | 32 | $requests = []; |
|
197 | |||
198 | 32 | $uri = $request->getUri(); |
|
199 | |||
200 | // If a base URI is configured, try to make partial invalidation |
||
201 | // requests complete. |
||
202 | 32 | if ($this->baseUri) { |
|
203 | 30 | if ($uri->getHost()) { |
|
204 | // Absolute URI: does it already have a scheme? |
||
205 | 5 | if (!$uri->getScheme() && $this->baseUri->getScheme() !== '') { |
|
206 | 5 | $uri = $uri->withScheme($this->baseUri->getScheme()); |
|
207 | } |
||
208 | } else { |
||
209 | // Relative URI |
||
210 | 25 | if ($this->baseUri->getHost() !== '') { |
|
211 | 25 | $uri = $uri->withHost($this->baseUri->getHost()); |
|
212 | } |
||
213 | |||
214 | 25 | if ($this->baseUri->getPort()) { |
|
|
|||
215 | 16 | $uri = $uri->withPort($this->baseUri->getPort()); |
|
216 | } |
||
217 | |||
218 | // Base path |
||
219 | 25 | if ($this->baseUri->getPath() !== '') { |
|
220 | 1 | $path = $this->baseUri->getPath().'/'.ltrim($uri->getPath(), '/'); |
|
221 | 1 | $uri = $uri->withPath($path); |
|
222 | } |
||
223 | } |
||
224 | } |
||
225 | |||
226 | // Close connections to make sure invalidation (PURGE/BAN) requests |
||
227 | // will not interfere with content (GET) requests. |
||
228 | 32 | $request = $request->withUri($uri)->withHeader('Connection', 'Close'); |
|
229 | |||
230 | // Create a request to each caching proxy server |
||
231 | 32 | foreach ($this->getServers() as $server) { |
|
232 | 32 | $requests[] = $request->withUri( |
|
233 | $uri |
||
234 | 32 | ->withScheme($server->getScheme()) |
|
235 | 32 | ->withHost($server->getHost()) |
|
236 | 32 | ->withPort($server->getPort()), |
|
237 | 32 | true // Preserve application Host header |
|
238 | ); |
||
239 | } |
||
240 | |||
241 | 32 | return $requests; |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * Set caching proxy server URI objects, validating them. |
||
246 | * |
||
247 | * @param string[] $servers Caching proxy proxy server hostnames or IP |
||
248 | * addresses, including port if not port 80. |
||
249 | * E.g. ['127.0.0.1:6081'] |
||
250 | * |
||
251 | * @throws InvalidUrlException If server is invalid or contains URL |
||
252 | * parts other than scheme, host, port |
||
253 | */ |
||
254 | 38 | private function setServers(array $servers) |
|
255 | { |
||
256 | 38 | $this->servers = []; |
|
257 | 38 | foreach ($servers as $server) { |
|
258 | 38 | $this->servers[] = $this->filterUri($server, ['scheme', 'host', 'port']); |
|
259 | } |
||
260 | 35 | } |
|
261 | |||
262 | /** |
||
263 | * Set application base URI that will be prefixed to relative purge and |
||
264 | * refresh requests, and validate it. |
||
265 | * |
||
266 | * @param string $uriString Your application’s base URI |
||
267 | * |
||
268 | * @throws InvalidUrlException If the base URI is not a valid URI |
||
269 | */ |
||
270 | 35 | private function setBaseUri($uriString = null) |
|
280 | |||
281 | /** |
||
282 | * Filter a URL. |
||
283 | * |
||
284 | * Prefix the URL with "http://" if it has no scheme, then check the URL |
||
285 | * for validity. You can specify what parts of the URL are allowed. |
||
286 | * |
||
287 | * @param string $uriString |
||
288 | * @param string[] $allowedParts Array of allowed URL parts (optional) |
||
289 | * |
||
290 | * @return UriInterface Filtered URI (with default scheme if there was no scheme) |
||
291 | * |
||
292 | * @throws InvalidUrlException If URL is invalid, the scheme is not http or |
||
293 | * contains parts that are not expected |
||
294 | */ |
||
295 | 38 | private function filterUri($uriString, array $allowedParts = []) |
|
296 | { |
||
297 | 38 | if (!is_string($uriString)) { |
|
298 | 1 | throw new \InvalidArgumentException(sprintf( |
|
299 | 1 | 'URI parameter must be a string, %s given', |
|
300 | gettype($uriString) |
||
301 | )); |
||
302 | } |
||
303 | |||
304 | // Creating a PSR-7 URI without scheme (with parse_url) results in the |
||
305 | // original hostname to be seen as path. So first add a scheme if none |
||
306 | // is given. |
||
307 | 38 | if (false === strpos($uriString, '://')) { |
|
308 | 34 | $uriString = sprintf('%s://%s', 'http', $uriString); |
|
309 | } |
||
310 | |||
311 | try { |
||
312 | 38 | $uri = $this->uriFactory->createUri($uriString); |
|
313 | 1 | } catch (\InvalidArgumentException $e) { |
|
314 | 1 | throw InvalidUrlException::invalidUrl($uriString); |
|
315 | } |
||
316 | |||
317 | 37 | if (!$uri->getScheme()) { |
|
318 | 1 | throw InvalidUrlException::invalidUrl($uriString, 'empty scheme'); |
|
319 | } |
||
320 | |||
321 | 36 | if (count($allowedParts) > 0) { |
|
322 | 36 | $parts = parse_url((string) $uri); |
|
323 | 36 | $diff = array_diff(array_keys($parts), $allowedParts); |
|
324 | 36 | if (count($diff) > 0) { |
|
325 | 1 | throw InvalidUrlException::invalidUrlParts($uriString, $allowedParts); |
|
326 | } |
||
327 | } |
||
328 | |||
329 | 35 | return $uri; |
|
330 | } |
||
331 | |||
332 | /** |
||
333 | * Build a request signature based on the request data. Unique for every different request, identical |
||
334 | * for the same requests. |
||
335 | * |
||
336 | * This signature is used to avoid sending the same invalidation request twice. |
||
337 | * |
||
338 | * @param RequestInterface $request An invalidation request |
||
339 | * |
||
340 | * @return string A signature for this request |
||
341 | */ |
||
342 | 32 | private function getRequestSignature(RequestInterface $request) |
|
349 | } |
||
350 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: