Merge branch 'request-ip' into 2.x

This commit is contained in:
mark_story 2016-03-13 23:00:47 -04:00
commit af046fc7d6
2 changed files with 10 additions and 20 deletions

View file

@ -417,20 +417,10 @@ class CakeRequest implements ArrayAccess {
public function clientIp($safe = true) { public function clientIp($safe = true) {
if (!$safe && env('HTTP_X_FORWARDED_FOR')) { if (!$safe && env('HTTP_X_FORWARDED_FOR')) {
$ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR')); $ipaddr = preg_replace('/(?:,.*)/', '', env('HTTP_X_FORWARDED_FOR'));
} elseif (!$safe && env('HTTP_CLIENT_IP')) {
$ipaddr = env('HTTP_CLIENT_IP');
} else { } else {
if (env('HTTP_CLIENT_IP')) { $ipaddr = env('REMOTE_ADDR');
$ipaddr = env('HTTP_CLIENT_IP');
} else {
$ipaddr = env('REMOTE_ADDR');
}
}
if (env('HTTP_CLIENTADDRESS')) {
$tmpipaddr = env('HTTP_CLIENTADDRESS');
if (!empty($tmpipaddr)) {
$ipaddr = preg_replace('/(?:,.*)/', '', $tmpipaddr);
}
} }
return trim($ipaddr); return trim($ipaddr);
} }

View file

@ -711,18 +711,18 @@ class CakeRequestTest extends CakeTestCase {
$_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com'; $_SERVER['HTTP_X_FORWARDED_FOR'] = '192.168.1.5, 10.0.1.1, proxy.com';
$_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2'; $_SERVER['HTTP_CLIENT_IP'] = '192.168.1.2';
$_SERVER['REMOTE_ADDR'] = '192.168.1.3'; $_SERVER['REMOTE_ADDR'] = '192.168.1.3';
$request = new CakeRequest('some/path'); $request = new CakeRequest('some/path');
$this->assertEquals('192.168.1.5', $request->clientIp(false)); $this->assertEquals('192.168.1.3', $request->clientIp(), 'Use remote_addr in safe mode');
$this->assertEquals('192.168.1.2', $request->clientIp()); $this->assertEquals('192.168.1.5', $request->clientIp(false), 'Use x-forwarded');
unset($_SERVER['HTTP_X_FORWARDED_FOR']); unset($_SERVER['HTTP_X_FORWARDED_FOR']);
$this->assertEquals('192.168.1.2', $request->clientIp()); $this->assertEquals('192.168.1.3', $request->clientIp(), 'safe uses remote_addr');
$this->assertEquals('192.168.1.2', $request->clientIp(false), 'unsafe reads from client_ip');
unset($_SERVER['HTTP_CLIENT_IP']); unset($_SERVER['HTTP_CLIENT_IP']);
$this->assertEquals('192.168.1.3', $request->clientIp()); $this->assertEquals('192.168.1.3', $request->clientIp(), 'use remote_addr');
$this->assertEquals('192.168.1.3', $request->clientIp(false), 'use remote_addr');
$_SERVER['HTTP_CLIENTADDRESS'] = '10.0.1.2, 10.0.1.1';
$this->assertEquals('10.0.1.2', $request->clientIp());
} }
/** /**