Permalink
Browse files

bug #23098 Cache ipCheck (2.7) (gonzalovilaseca)

This PR was merged into the 2.7 branch.

Discussion
----------

Cache ipCheck (2.7)

In our app we use trusted proxies. Using Blackfire we found `IpUtils::checkIp` was being called 454 times taking 3.15ms.
Caching the result saves those 3ms.

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

bcb8056 Cache ipCheck
  • Loading branch information...
fabpot committed Jun 8, 2017
2 parents ccb6543 + bcb8056 commit 589f2b1a6c834d0c72d9d19a177503408ba426d1
Showing with 20 additions and 8 deletions.
  1. +20 −8 src/Symfony/Component/HttpFoundation/IpUtils.php
@@ -18,6 +18,8 @@
*/
class IpUtils
{
private static $checkedIps = array();
/**
* This class should not be instantiated.
*/
@@ -61,26 +63,31 @@ public static function checkIp($requestIp, $ips)
*/
public static function checkIp4($requestIp, $ip)
{
$cacheKey = $requestIp.'-'.$ip;
if (isset(self::$checkedIps[$cacheKey])) {
return self::$checkedIps[$cacheKey];
}
if (!filter_var($requestIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return false;
return self::$checkedIps[$cacheKey] = false;
}
if (false !== strpos($ip, '/')) {
list($address, $netmask) = explode('/', $ip, 2);
if ($netmask === '0') {
return filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
return self::$checkedIps[$cacheKey] = filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4);
}
if ($netmask < 0 || $netmask > 32) {
return false;
return self::$checkedIps[$cacheKey] = false;
}
} else {
$address = $ip;
$netmask = 32;
}
return 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
return self::$checkedIps[$cacheKey] = 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
}
/**
@@ -100,6 +107,11 @@ public static function checkIp4($requestIp, $ip)
*/
public static function checkIp6($requestIp, $ip)
{
$cacheKey = $requestIp.'-'.$ip;
if (isset(self::$checkedIps[$cacheKey])) {
return self::$checkedIps[$cacheKey];
}
if (!((extension_loaded('sockets') && defined('AF_INET6')) || @inet_pton('::1'))) {
throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
}
@@ -108,7 +120,7 @@ public static function checkIp6($requestIp, $ip)
list($address, $netmask) = explode('/', $ip, 2);
if ($netmask < 1 || $netmask > 128) {
return false;
return self::$checkedIps[$cacheKey] = false;
}
} else {
$address = $ip;
@@ -119,18 +131,18 @@ public static function checkIp6($requestIp, $ip)
$bytesTest = unpack('n*', @inet_pton($requestIp));
if (!$bytesAddr || !$bytesTest) {
return false;
return self::$checkedIps[$cacheKey] = false;
}
for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
$left = $netmask - 16 * ($i - 1);
$left = ($left <= 16) ? $left : 16;
$mask = ~(0xffff >> $left) & 0xffff;
if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
return false;
return self::$checkedIps[$cacheKey] = false;
}
}
return true;
return self::$checkedIps[$cacheKey] = true;
}
}

0 comments on commit 589f2b1

Please sign in to comment.