[FEATURE] Optimised Request::getIp method

- Added unit-tests for Request::getIp
- Optimised existing RequestTest unit-tests.
This commit is contained in:
Simon Sessingø
2021-03-25 14:18:56 +01:00
parent 11313a31dc
commit cf6750aaf3
2 changed files with 62 additions and 22 deletions
+8 -14
View File
@@ -220,22 +220,16 @@ class Request
*/
public function getIp(bool $safe = false): ?string
{
$client_header = null;
if(!$safe){
$client_header = $this->getHeader(
$headers = ['remote-addr'];
if($safe === false) {
$headers = array_merge($headers, [
'http-cf-connecting-ip',
$this->getHeader(
'http-client-ip',
$this->getHeader(
'http-x-forwarded-for',
$this->getHeader('remote-addr')
)
)
);
'http-client-ip',
'http-x-forwarded-for',
]);
}
if($client_header === null)
$client_header = $this->getHeader('remote-addr');
return filter_var($client_header, FILTER_VALIDATE_IP) ? $client_header : null;
return $this->getFirstHeader($headers);
}
/**