Skip to content

Commit

Permalink
Improved performance when checking for client IP addresses by early r…
Browse files Browse the repository at this point in the history
…eturning once a header is found
  • Loading branch information
acelaya committed May 4, 2020
1 parent 5224123 commit 3678edf
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/Log/AccessLogDataMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Psr\Http\Message\ResponseInterface as PsrResponse;
use Swoole\Http\Request as SwooleHttpRequest;

use function array_reduce;
use function filter_var;
use function function_exists;
use function getcwd;
Expand Down Expand Up @@ -95,12 +94,15 @@ public static function createWithStaticResource(
*/
public function getClientIp() : string
{
$headers = ['x-forwarded-for', 'client-ip', 'x-real-ip'];
$headerValue = array_reduce($headers, function (?string $default, string $header) {
return $this->request->header[$header] ?? $default;
});
$headers = ['x-real-ip', 'client-ip', 'x-forwarded-for'];

return $headerValue ?? $this->getServerParamIp('REMOTE_ADDR');
foreach ($headers as $header) {
if (isset($this->request->header[$header])) {
return $this->request->header[$header];
}
}

return $this->getServerParamIp('REMOTE_ADDR');
}

/**
Expand Down

0 comments on commit 3678edf

Please sign in to comment.