1 | <?php |
||
8 | class HstsMiddleware |
||
9 | { |
||
10 | /** |
||
11 | * Next handler for the Guzzle middleware |
||
12 | * |
||
13 | * @var callable |
||
14 | */ |
||
15 | private $nextHandler; |
||
16 | |||
17 | /** |
||
18 | * Store instances cache |
||
19 | * |
||
20 | * @var StoreInterface[] |
||
21 | */ |
||
22 | private $storeInstances = []; |
||
23 | |||
24 | /** |
||
25 | * HstsMiddleware constructor |
||
26 | * |
||
27 | * @param callable $nextHandler Next handler to invoke. |
||
28 | */ |
||
29 | 18 | public function __construct(callable $nextHandler) |
|
33 | |||
34 | /** |
||
35 | * Invoke the guzzle middleware |
||
36 | * |
||
37 | * @param Request $request |
||
38 | * @param array $options |
||
39 | * @return \GuzzleHttp\Promise\PromiseInterface |
||
40 | */ |
||
41 | 18 | public function __invoke(Request $request, array $options) |
|
54 | |||
55 | /** |
||
56 | * Rewrite the requested uri if the requested host is a known HSTS host |
||
57 | * |
||
58 | * @param Request $request |
||
59 | * @param StoreInterface $store |
||
60 | * @return Request |
||
61 | */ |
||
62 | 17 | private function handleHstsRewrite(Request $request, StoreInterface $store) |
|
78 | |||
79 | /** |
||
80 | * Register the host as a known HSTS host if the header is set properly |
||
81 | * |
||
82 | * @param Response $response |
||
83 | * @param Request $request |
||
84 | * @param StoreInterface $store |
||
85 | * @return Response |
||
86 | */ |
||
87 | 17 | private function handleHstsRegistering(Response $response, Request $request, StoreInterface $store) |
|
116 | |||
117 | /** |
||
118 | * Check if the given domain is a known HSTS host |
||
119 | * |
||
120 | * @param StoreInterface $store |
||
121 | * @param string $domainName |
||
122 | * @param bool $includeSubDomains |
||
123 | * @return bool |
||
124 | */ |
||
125 | 16 | private function checkHstsDomain(StoreInterface $store, $domainName, $includeSubDomains) |
|
131 | |||
132 | /** |
||
133 | * Check if the given domain's superdomains are known HSTS hosts |
||
134 | * |
||
135 | * @param StoreInterface $store |
||
136 | * @param string $domainName |
||
137 | * @return bool |
||
138 | */ |
||
139 | 11 | private function checkHstsSuperdomains(StoreInterface $store, $domainName) |
|
154 | |||
155 | /** |
||
156 | * Check if the given domain or a superdomain is a known HSTS host |
||
157 | * |
||
158 | * @param StoreInterface $store |
||
159 | * @param string $domainName |
||
160 | * @return bool |
||
161 | */ |
||
162 | 16 | private function isKnownHstsHosts(StoreInterface $store, $domainName) |
|
167 | |||
168 | /** |
||
169 | * Get the store instance, possibly cached |
||
170 | * |
||
171 | * @param array $options |
||
172 | * @return StoreInterface |
||
173 | * @throws InvalidArgumentException |
||
174 | */ |
||
175 | 18 | private function getStoreInstance(array $options) |
|
197 | |||
198 | /** |
||
199 | * Parse the HSTS header |
||
200 | * |
||
201 | * @param string $header |
||
202 | * @return array |
||
203 | */ |
||
204 | 13 | private function parseHeader($header) |
|
225 | |||
226 | /** |
||
227 | * Check if a host is an ip address |
||
228 | * |
||
229 | * @param string $host |
||
230 | * @return bool |
||
231 | */ |
||
232 | 17 | private function isIpAddress($host) |
|
236 | |||
237 | /** |
||
238 | * Handler for registering the middleware |
||
239 | * |
||
240 | * @return \Closure |
||
241 | */ |
||
242 | public static function handler() |
||
248 | } |
||
249 |