@@ -12,35 +12,35 @@ |
||
12 | 12 | */ |
13 | 13 | trait Fetcher |
14 | 14 | { |
15 | - /** |
|
16 | - * Configuration for the Guzzle client |
|
17 | - * |
|
18 | - * @var array |
|
19 | - */ |
|
20 | - protected $clientConfig = []; |
|
15 | + /** |
|
16 | + * Configuration for the Guzzle client |
|
17 | + * |
|
18 | + * @var array |
|
19 | + */ |
|
20 | + protected $clientConfig = []; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Merges configuration arrays and returns the result |
|
24 | - * |
|
25 | - * @param array $extraConfig |
|
26 | - * @return array |
|
27 | - */ |
|
28 | - private function getClientConfig(array $extraConfig = []) : array |
|
29 | - { |
|
30 | - return array_merge($this->clientConfig, $extraConfig); |
|
31 | - } |
|
22 | + /** |
|
23 | + * Merges configuration arrays and returns the result |
|
24 | + * |
|
25 | + * @param array $extraConfig |
|
26 | + * @return array |
|
27 | + */ |
|
28 | + private function getClientConfig(array $extraConfig = []) : array |
|
29 | + { |
|
30 | + return array_merge($this->clientConfig, $extraConfig); |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * Fetch a response for a URL using Guzzle client. |
|
35 | - * |
|
36 | - * @param string $url |
|
37 | - * @param array|null $extraConfig Extra configuration |
|
38 | - * @return ResponseInterface |
|
39 | - */ |
|
40 | - public function fetchResponse(string $url, ? array $extraConfig = []) : ? ResponseInterface |
|
41 | - { |
|
42 | - $config = $this->getClientConfig($extraConfig); |
|
43 | - $client = new Client($config); |
|
44 | - return $client->get($url); |
|
45 | - } |
|
33 | + /** |
|
34 | + * Fetch a response for a URL using Guzzle client. |
|
35 | + * |
|
36 | + * @param string $url |
|
37 | + * @param array|null $extraConfig Extra configuration |
|
38 | + * @return ResponseInterface |
|
39 | + */ |
|
40 | + public function fetchResponse(string $url, ? array $extraConfig = []) : ? ResponseInterface |
|
41 | + { |
|
42 | + $config = $this->getClientConfig($extraConfig); |
|
43 | + $client = new Client($config); |
|
44 | + return $client->get($url); |
|
45 | + } |
|
46 | 46 | } |
@@ -20,201 +20,201 @@ |
||
20 | 20 | */ |
21 | 21 | class CacheHeadersCheck implements EnvironmentCheck |
22 | 22 | { |
23 | - use Configurable; |
|
24 | - use Fetcher; |
|
25 | - |
|
26 | - /** |
|
27 | - * URL to check |
|
28 | - * |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - protected $url; |
|
32 | - |
|
33 | - /** |
|
34 | - * Settings that must be included in the Cache-Control header |
|
35 | - * |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $mustInclude = []; |
|
39 | - |
|
40 | - /** |
|
41 | - * Settings that must be excluded in the Cache-Control header |
|
42 | - * |
|
43 | - * @var array |
|
44 | - */ |
|
45 | - protected $mustExclude = []; |
|
46 | - |
|
47 | - /** |
|
48 | - * Result to keep track of status and messages for all checks, reuses |
|
49 | - * ValidationResult for convenience. |
|
50 | - * |
|
51 | - * @var ValidationResult |
|
52 | - */ |
|
53 | - protected $result; |
|
54 | - |
|
55 | - /** |
|
56 | - * Set up with URL, arrays of header settings to check. |
|
57 | - * |
|
58 | - * @param string $url |
|
59 | - * @param array $mustInclude Settings that must be included in Cache-Control |
|
60 | - * @param array $mustExclude Settings that must be excluded in Cache-Control |
|
61 | - */ |
|
62 | - public function __construct($url = '', $mustInclude = [], $mustExclude = []) |
|
63 | - { |
|
64 | - $this->url = $url; |
|
65 | - $this->mustInclude = $mustInclude; |
|
66 | - $this->mustExclude = $mustExclude; |
|
67 | - |
|
68 | - $this->clientConfig = [ |
|
69 | - 'base_uri' => Director::absoluteBaseURL(), |
|
70 | - 'timeout' => 10.0, |
|
71 | - ]; |
|
72 | - |
|
73 | - // Using a validation result to capture messages |
|
74 | - $this->result = new ValidationResult(); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Check that correct caching headers are present. |
|
79 | - * |
|
80 | - * @return void |
|
81 | - */ |
|
82 | - public function check() |
|
83 | - { |
|
84 | - $response = $this->fetchResponse($this->url); |
|
85 | - $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
86 | - if ($response === null) { |
|
87 | - return [ |
|
88 | - EnvironmentCheck::ERROR, |
|
89 | - "Cache headers check request failed for $fullURL", |
|
90 | - ]; |
|
91 | - } |
|
92 | - |
|
93 | - //Check that Etag exists |
|
94 | - $this->checkEtag($response); |
|
95 | - |
|
96 | - // Check Cache-Control settings |
|
97 | - $this->checkCacheControl($response); |
|
98 | - |
|
99 | - if ($this->result->isValid()) { |
|
100 | - return [ |
|
101 | - EnvironmentCheck::OK, |
|
102 | - $this->getMessage(), |
|
103 | - ]; |
|
104 | - } else { |
|
105 | - // @todo Ability to return a warning |
|
106 | - return [ |
|
107 | - EnvironmentCheck::ERROR, |
|
108 | - $this->getMessage(), |
|
109 | - ]; |
|
110 | - } |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Collate messages from ValidationResult so that it is clear which parts |
|
115 | - * of the check passed and which failed. |
|
116 | - * |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - private function getMessage(): string |
|
120 | - { |
|
121 | - $ret = ''; |
|
122 | - // Filter good messages |
|
123 | - $goodTypes = [ValidationResult::TYPE_GOOD, ValidationResult::TYPE_INFO]; |
|
124 | - $good = array_filter( |
|
125 | - $this->result->getMessages(), |
|
126 | - function ($val, $key) use ($goodTypes) { |
|
127 | - if (in_array($val['messageType'], $goodTypes)) { |
|
128 | - return true; |
|
129 | - } |
|
130 | - return false; |
|
131 | - }, |
|
132 | - ARRAY_FILTER_USE_BOTH |
|
133 | - ); |
|
134 | - if (!empty($good)) { |
|
135 | - $ret .= "GOOD: " . implode('; ', array_column($good, 'message')) . " "; |
|
136 | - } |
|
137 | - |
|
138 | - // Filter bad messages |
|
139 | - $badTypes = [ValidationResult::TYPE_ERROR, ValidationResult::TYPE_WARNING]; |
|
140 | - $bad = array_filter( |
|
141 | - $this->result->getMessages(), |
|
142 | - function ($val, $key) use ($badTypes) { |
|
143 | - if (in_array($val['messageType'], $badTypes)) { |
|
144 | - return true; |
|
145 | - } |
|
146 | - return false; |
|
147 | - }, |
|
148 | - ARRAY_FILTER_USE_BOTH |
|
149 | - ); |
|
150 | - if (!empty($bad)) { |
|
151 | - $ret .= "BAD: " . implode('; ', array_column($bad, 'message')); |
|
152 | - } |
|
153 | - return $ret; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Check that ETag header exists |
|
158 | - * |
|
159 | - * @param ResponseInterface $response |
|
160 | - * @return void |
|
161 | - */ |
|
162 | - private function checkEtag(ResponseInterface $response): void |
|
163 | - { |
|
164 | - $eTag = $response->getHeaderLine('ETag'); |
|
165 | - $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
166 | - |
|
167 | - if ($eTag) { |
|
168 | - $this->result->addMessage( |
|
169 | - "$fullURL includes an Etag header in response", |
|
170 | - ValidationResult::TYPE_GOOD |
|
171 | - ); |
|
172 | - return; |
|
173 | - } |
|
174 | - $this->result->addError( |
|
175 | - "$fullURL is missing an Etag header", |
|
176 | - ValidationResult::TYPE_WARNING |
|
177 | - ); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Check that the correct header settings are either included or excluded. |
|
182 | - * |
|
183 | - * @param ResponseInterface $response |
|
184 | - * @return void |
|
185 | - */ |
|
186 | - private function checkCacheControl(ResponseInterface $response): void |
|
187 | - { |
|
188 | - $cacheControl = $response->getHeaderLine('Cache-Control'); |
|
189 | - $vals = array_map('trim', explode(',', $cacheControl)); |
|
190 | - $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
191 | - |
|
192 | - // All entries from must contain should be present |
|
193 | - if ($this->mustInclude == array_intersect($this->mustInclude, $vals)) { |
|
194 | - $matched = implode(",", $this->mustInclude); |
|
195 | - $this->result->addMessage( |
|
196 | - "$fullURL includes all settings: {$matched}", |
|
197 | - ValidationResult::TYPE_GOOD |
|
198 | - ); |
|
199 | - } else { |
|
200 | - $missing = implode(",", array_diff($this->mustInclude, $vals)); |
|
201 | - $this->result->addError( |
|
202 | - "$fullURL is excluding some settings: {$missing}" |
|
203 | - ); |
|
204 | - } |
|
205 | - |
|
206 | - // All entries from must exclude should not be present |
|
207 | - if (empty(array_intersect($this->mustExclude, $vals))) { |
|
208 | - $missing = implode(",", $this->mustExclude); |
|
209 | - $this->result->addMessage( |
|
210 | - "$fullURL excludes all settings: {$missing}", |
|
211 | - ValidationResult::TYPE_GOOD |
|
212 | - ); |
|
213 | - } else { |
|
214 | - $matched = implode(",", array_intersect($this->mustExclude, $vals)); |
|
215 | - $this->result->addError( |
|
216 | - "$fullURL is including some settings: {$matched}" |
|
217 | - ); |
|
218 | - } |
|
219 | - } |
|
23 | + use Configurable; |
|
24 | + use Fetcher; |
|
25 | + |
|
26 | + /** |
|
27 | + * URL to check |
|
28 | + * |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + protected $url; |
|
32 | + |
|
33 | + /** |
|
34 | + * Settings that must be included in the Cache-Control header |
|
35 | + * |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $mustInclude = []; |
|
39 | + |
|
40 | + /** |
|
41 | + * Settings that must be excluded in the Cache-Control header |
|
42 | + * |
|
43 | + * @var array |
|
44 | + */ |
|
45 | + protected $mustExclude = []; |
|
46 | + |
|
47 | + /** |
|
48 | + * Result to keep track of status and messages for all checks, reuses |
|
49 | + * ValidationResult for convenience. |
|
50 | + * |
|
51 | + * @var ValidationResult |
|
52 | + */ |
|
53 | + protected $result; |
|
54 | + |
|
55 | + /** |
|
56 | + * Set up with URL, arrays of header settings to check. |
|
57 | + * |
|
58 | + * @param string $url |
|
59 | + * @param array $mustInclude Settings that must be included in Cache-Control |
|
60 | + * @param array $mustExclude Settings that must be excluded in Cache-Control |
|
61 | + */ |
|
62 | + public function __construct($url = '', $mustInclude = [], $mustExclude = []) |
|
63 | + { |
|
64 | + $this->url = $url; |
|
65 | + $this->mustInclude = $mustInclude; |
|
66 | + $this->mustExclude = $mustExclude; |
|
67 | + |
|
68 | + $this->clientConfig = [ |
|
69 | + 'base_uri' => Director::absoluteBaseURL(), |
|
70 | + 'timeout' => 10.0, |
|
71 | + ]; |
|
72 | + |
|
73 | + // Using a validation result to capture messages |
|
74 | + $this->result = new ValidationResult(); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Check that correct caching headers are present. |
|
79 | + * |
|
80 | + * @return void |
|
81 | + */ |
|
82 | + public function check() |
|
83 | + { |
|
84 | + $response = $this->fetchResponse($this->url); |
|
85 | + $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
86 | + if ($response === null) { |
|
87 | + return [ |
|
88 | + EnvironmentCheck::ERROR, |
|
89 | + "Cache headers check request failed for $fullURL", |
|
90 | + ]; |
|
91 | + } |
|
92 | + |
|
93 | + //Check that Etag exists |
|
94 | + $this->checkEtag($response); |
|
95 | + |
|
96 | + // Check Cache-Control settings |
|
97 | + $this->checkCacheControl($response); |
|
98 | + |
|
99 | + if ($this->result->isValid()) { |
|
100 | + return [ |
|
101 | + EnvironmentCheck::OK, |
|
102 | + $this->getMessage(), |
|
103 | + ]; |
|
104 | + } else { |
|
105 | + // @todo Ability to return a warning |
|
106 | + return [ |
|
107 | + EnvironmentCheck::ERROR, |
|
108 | + $this->getMessage(), |
|
109 | + ]; |
|
110 | + } |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Collate messages from ValidationResult so that it is clear which parts |
|
115 | + * of the check passed and which failed. |
|
116 | + * |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + private function getMessage(): string |
|
120 | + { |
|
121 | + $ret = ''; |
|
122 | + // Filter good messages |
|
123 | + $goodTypes = [ValidationResult::TYPE_GOOD, ValidationResult::TYPE_INFO]; |
|
124 | + $good = array_filter( |
|
125 | + $this->result->getMessages(), |
|
126 | + function ($val, $key) use ($goodTypes) { |
|
127 | + if (in_array($val['messageType'], $goodTypes)) { |
|
128 | + return true; |
|
129 | + } |
|
130 | + return false; |
|
131 | + }, |
|
132 | + ARRAY_FILTER_USE_BOTH |
|
133 | + ); |
|
134 | + if (!empty($good)) { |
|
135 | + $ret .= "GOOD: " . implode('; ', array_column($good, 'message')) . " "; |
|
136 | + } |
|
137 | + |
|
138 | + // Filter bad messages |
|
139 | + $badTypes = [ValidationResult::TYPE_ERROR, ValidationResult::TYPE_WARNING]; |
|
140 | + $bad = array_filter( |
|
141 | + $this->result->getMessages(), |
|
142 | + function ($val, $key) use ($badTypes) { |
|
143 | + if (in_array($val['messageType'], $badTypes)) { |
|
144 | + return true; |
|
145 | + } |
|
146 | + return false; |
|
147 | + }, |
|
148 | + ARRAY_FILTER_USE_BOTH |
|
149 | + ); |
|
150 | + if (!empty($bad)) { |
|
151 | + $ret .= "BAD: " . implode('; ', array_column($bad, 'message')); |
|
152 | + } |
|
153 | + return $ret; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Check that ETag header exists |
|
158 | + * |
|
159 | + * @param ResponseInterface $response |
|
160 | + * @return void |
|
161 | + */ |
|
162 | + private function checkEtag(ResponseInterface $response): void |
|
163 | + { |
|
164 | + $eTag = $response->getHeaderLine('ETag'); |
|
165 | + $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
166 | + |
|
167 | + if ($eTag) { |
|
168 | + $this->result->addMessage( |
|
169 | + "$fullURL includes an Etag header in response", |
|
170 | + ValidationResult::TYPE_GOOD |
|
171 | + ); |
|
172 | + return; |
|
173 | + } |
|
174 | + $this->result->addError( |
|
175 | + "$fullURL is missing an Etag header", |
|
176 | + ValidationResult::TYPE_WARNING |
|
177 | + ); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Check that the correct header settings are either included or excluded. |
|
182 | + * |
|
183 | + * @param ResponseInterface $response |
|
184 | + * @return void |
|
185 | + */ |
|
186 | + private function checkCacheControl(ResponseInterface $response): void |
|
187 | + { |
|
188 | + $cacheControl = $response->getHeaderLine('Cache-Control'); |
|
189 | + $vals = array_map('trim', explode(',', $cacheControl)); |
|
190 | + $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
191 | + |
|
192 | + // All entries from must contain should be present |
|
193 | + if ($this->mustInclude == array_intersect($this->mustInclude, $vals)) { |
|
194 | + $matched = implode(",", $this->mustInclude); |
|
195 | + $this->result->addMessage( |
|
196 | + "$fullURL includes all settings: {$matched}", |
|
197 | + ValidationResult::TYPE_GOOD |
|
198 | + ); |
|
199 | + } else { |
|
200 | + $missing = implode(",", array_diff($this->mustInclude, $vals)); |
|
201 | + $this->result->addError( |
|
202 | + "$fullURL is excluding some settings: {$missing}" |
|
203 | + ); |
|
204 | + } |
|
205 | + |
|
206 | + // All entries from must exclude should not be present |
|
207 | + if (empty(array_intersect($this->mustExclude, $vals))) { |
|
208 | + $missing = implode(",", $this->mustExclude); |
|
209 | + $this->result->addMessage( |
|
210 | + "$fullURL excludes all settings: {$missing}", |
|
211 | + ValidationResult::TYPE_GOOD |
|
212 | + ); |
|
213 | + } else { |
|
214 | + $matched = implode(",", array_intersect($this->mustExclude, $vals)); |
|
215 | + $this->result->addError( |
|
216 | + "$fullURL is including some settings: {$matched}" |
|
217 | + ); |
|
218 | + } |
|
219 | + } |
|
220 | 220 | } |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | $goodTypes = [ValidationResult::TYPE_GOOD, ValidationResult::TYPE_INFO]; |
124 | 124 | $good = array_filter( |
125 | 125 | $this->result->getMessages(), |
126 | - function ($val, $key) use ($goodTypes) { |
|
126 | + function($val, $key) use ($goodTypes) { |
|
127 | 127 | if (in_array($val['messageType'], $goodTypes)) { |
128 | 128 | return true; |
129 | 129 | } |
@@ -132,14 +132,14 @@ discard block |
||
132 | 132 | ARRAY_FILTER_USE_BOTH |
133 | 133 | ); |
134 | 134 | if (!empty($good)) { |
135 | - $ret .= "GOOD: " . implode('; ', array_column($good, 'message')) . " "; |
|
135 | + $ret .= "GOOD: ".implode('; ', array_column($good, 'message'))." "; |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | // Filter bad messages |
139 | 139 | $badTypes = [ValidationResult::TYPE_ERROR, ValidationResult::TYPE_WARNING]; |
140 | 140 | $bad = array_filter( |
141 | 141 | $this->result->getMessages(), |
142 | - function ($val, $key) use ($badTypes) { |
|
142 | + function($val, $key) use ($badTypes) { |
|
143 | 143 | if (in_array($val['messageType'], $badTypes)) { |
144 | 144 | return true; |
145 | 145 | } |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | ARRAY_FILTER_USE_BOTH |
149 | 149 | ); |
150 | 150 | if (!empty($bad)) { |
151 | - $ret .= "BAD: " . implode('; ', array_column($bad, 'message')); |
|
151 | + $ret .= "BAD: ".implode('; ', array_column($bad, 'message')); |
|
152 | 152 | } |
153 | 153 | return $ret; |
154 | 154 | } |
@@ -17,70 +17,70 @@ |
||
17 | 17 | */ |
18 | 18 | class SessionCheck implements EnvironmentCheck |
19 | 19 | { |
20 | - use Configurable; |
|
21 | - use Fetcher; |
|
20 | + use Configurable; |
|
21 | + use Fetcher; |
|
22 | 22 | |
23 | - /** |
|
24 | - * URL to check |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - protected $url; |
|
23 | + /** |
|
24 | + * URL to check |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + protected $url; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Set up check with URL |
|
32 | - * |
|
33 | - * @param string $url The route, excluding the domain |
|
34 | - * @inheritdoc |
|
35 | - */ |
|
36 | - public function __construct($url = '') |
|
37 | - { |
|
38 | - $this->url = $url; |
|
39 | - $this->clientConfig = [ |
|
40 | - 'base_uri' => Director::absoluteBaseURL(), |
|
41 | - 'timeout' => 10.0, |
|
42 | - ]; |
|
43 | - } |
|
30 | + /** |
|
31 | + * Set up check with URL |
|
32 | + * |
|
33 | + * @param string $url The route, excluding the domain |
|
34 | + * @inheritdoc |
|
35 | + */ |
|
36 | + public function __construct($url = '') |
|
37 | + { |
|
38 | + $this->url = $url; |
|
39 | + $this->clientConfig = [ |
|
40 | + 'base_uri' => Director::absoluteBaseURL(), |
|
41 | + 'timeout' => 10.0, |
|
42 | + ]; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * Check that the response for URL does not create a session |
|
47 | - * |
|
48 | - * @return array |
|
49 | - */ |
|
50 | - public function check(): array |
|
51 | - { |
|
52 | - $response = $this->fetchResponse($this->url); |
|
53 | - $cookie = $this->getCookie($response); |
|
54 | - $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
45 | + /** |
|
46 | + * Check that the response for URL does not create a session |
|
47 | + * |
|
48 | + * @return array |
|
49 | + */ |
|
50 | + public function check(): array |
|
51 | + { |
|
52 | + $response = $this->fetchResponse($this->url); |
|
53 | + $cookie = $this->getCookie($response); |
|
54 | + $fullURL = Controller::join_links(Director::absoluteBaseURL(), $this->url); |
|
55 | 55 | |
56 | - if ($cookie) { |
|
57 | - return [ |
|
58 | - EnvironmentCheck::ERROR, |
|
59 | - "Sessions are being set for {$fullURL} : Set-Cookie => " . $cookie, |
|
60 | - ]; |
|
61 | - } |
|
62 | - return [ |
|
63 | - EnvironmentCheck::OK, |
|
64 | - "Sessions are not being created for {$fullURL} |
@@ -56,7 +56,7 @@ |
||
56 | 56 | if ($cookie) { |
57 | 57 | return [ |
58 | 58 | EnvironmentCheck::ERROR, |
59 | - "Sessions are being set for {$fullURL} : Set-Cookie => " . $cookie, |
|
59 | + "Sessions are being set for {$fullURL} : Set-Cookie => ".$cookie, |
|
60 | 60 | ]; |
61 | 61 | } |
62 | 62 | return [ |