1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PragmaRX\Health\Checkers; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\TransferStats; |
6
|
|
|
use GuzzleHttp\Client as Guzzle; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
use PragmaRX\Health\Support\Result; |
9
|
|
|
|
10
|
|
|
class Http extends Base |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @return Result |
14
|
|
|
*/ |
15
|
|
|
protected $secure = false; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
protected $guzzle; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var |
24
|
|
|
*/ |
25
|
|
|
private $totalTime; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var |
29
|
|
|
*/ |
30
|
|
|
private $url; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* HTTP Checker. |
34
|
|
|
* |
35
|
|
|
* @return Result |
36
|
|
|
*/ |
37
|
1 |
|
public function check() |
38
|
|
|
{ |
39
|
|
|
try { |
40
|
1 |
|
$health = []; |
|
|
|
|
41
|
|
|
|
42
|
1 |
|
foreach ($this->getResourceUrlArray() as $url) { |
43
|
1 |
|
list($healthy, $message) = $this->checkWebPage( |
44
|
1 |
|
$this->makeUrlWithScheme($url, $this->secure), |
45
|
1 |
|
$this->secure |
46
|
|
|
); |
47
|
|
|
|
48
|
1 |
|
if (!$healthy) { |
49
|
1 |
|
return $this->makeResult($healthy, $message); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return $this->makeHealthyResult(); |
54
|
1 |
|
} catch (\Exception $exception) { |
55
|
1 |
|
report($exception); |
56
|
|
|
|
57
|
1 |
|
return $this->makeResultFromException($exception); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get array of resource urls. |
63
|
|
|
* |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
1 |
|
private function getResourceUrlArray() |
67
|
|
|
{ |
68
|
1 |
|
if (is_a($this->target->urls, Collection::class)) { |
69
|
1 |
|
return $this->target->urls->toArray(); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return (array) $this->target->urls; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check web pages. |
77
|
|
|
* |
78
|
|
|
* @param $url |
79
|
|
|
* @param bool $ssl |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
1 |
|
private function checkWebPage($url, $ssl = false) |
83
|
|
|
{ |
84
|
1 |
|
$success = $this->requestSuccessful($url, $ssl); |
85
|
|
|
|
86
|
1 |
|
return [$success, $success ? '' : $this->getErrorMessage()]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Send an http request and fetch the response. |
91
|
|
|
* |
92
|
|
|
* @param $url |
93
|
|
|
* @param $ssl |
94
|
|
|
* @return mixed|\Psr\Http\Message\ResponseInterface |
95
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
96
|
|
|
*/ |
97
|
1 |
|
private function fetchResponse($url, $ssl) |
98
|
|
|
{ |
99
|
1 |
|
$this->url = $url; |
100
|
|
|
|
101
|
1 |
|
return (new Guzzle())->request( |
102
|
1 |
|
'GET', |
103
|
1 |
|
$this->url, |
104
|
1 |
|
$this->getConnectionOptions($ssl) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get http connection options. |
110
|
|
|
* |
111
|
|
|
* @param $ssl |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
1 |
|
private function getConnectionOptions($ssl) |
115
|
|
|
{ |
116
|
|
|
return [ |
117
|
1 |
|
'connect_timeout' => $this->getConnectionTimeout(), |
118
|
1 |
|
'timeout' => $this->getConnectionTimeout(), |
119
|
1 |
|
'verify' => $ssl, |
120
|
1 |
|
'on_stats' => $this->onStatsCallback(), |
121
|
|
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the error message. |
126
|
|
|
* |
127
|
|
|
* @return string |
128
|
|
|
*/ |
129
|
|
|
private function getErrorMessage() |
130
|
|
|
{ |
131
|
|
|
$message = $this->target->resource->timeoutMessage; |
132
|
|
|
|
133
|
|
|
return sprintf( |
134
|
|
|
$message, |
135
|
|
|
$this->url, |
136
|
|
|
$this->totalTime, |
137
|
|
|
$this->getRoundtripTimeout() |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* The the connection timeout. |
143
|
|
|
* |
144
|
|
|
* @return int |
145
|
|
|
*/ |
146
|
1 |
|
private function getConnectionTimeout() |
147
|
|
|
{ |
148
|
1 |
|
return $this->target->resource->connectionTimeout; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* The the roundtrip timeout. |
153
|
|
|
* |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
1 |
|
private function getRoundtripTimeout() |
157
|
|
|
{ |
158
|
1 |
|
return $this->target->resource->roundtripTimeout; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Make a url with a proper scheme. |
163
|
|
|
* |
164
|
|
|
* @param $url |
165
|
|
|
* @param $secure |
166
|
|
|
* @return mixed |
167
|
|
|
*/ |
168
|
1 |
|
private function makeUrlWithScheme($url, $secure) |
169
|
|
|
{ |
170
|
1 |
|
return preg_replace( |
171
|
1 |
|
'|^((https?:)?\/\/)?(.*)|', |
172
|
1 |
|
'http' . ($secure ? 's' : '') . '://\\3', |
173
|
1 |
|
$url |
174
|
|
|
); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Guzzle OnStats callback. |
179
|
|
|
* |
180
|
|
|
* @return \Closure |
181
|
|
|
*/ |
182
|
1 |
|
private function onStatsCallback() |
183
|
|
|
{ |
184
|
|
|
return function (TransferStats $stats) { |
185
|
1 |
|
$this->totalTime = $stats->getTransferTime(); |
186
|
1 |
|
}; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Send a request and get the result. |
191
|
|
|
* |
192
|
|
|
* @param $url |
193
|
|
|
* @param $ssl |
194
|
|
|
* @return bool |
195
|
|
|
* @internal param $response |
196
|
|
|
*/ |
197
|
1 |
|
private function requestSuccessful($url, $ssl) |
198
|
|
|
{ |
199
|
|
|
return ( |
200
|
1 |
|
$this->fetchResponse($url, $ssl)->getStatusCode() == 200 && |
201
|
1 |
|
!$this->requestTimedout() |
202
|
|
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Check if the request timed out. |
207
|
|
|
* |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
1 |
|
private function requestTimedout() |
211
|
|
|
{ |
212
|
1 |
|
return $this->totalTime > $this->getRoundtripTimeout(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.