Skip to content

Commit

Permalink
Add IPv6 support
Browse files Browse the repository at this point in the history
  • Loading branch information
gordalina committed Mar 29, 2020
1 parent 244df06 commit 68d443e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
# Unreleased

- [77](https://github.com/gordalina/cachetool/issues/77) Add IPv6 support

# 5.0.0

- **Breaking Change**: PHP 7.2 is now required
Expand Down
20 changes: 15 additions & 5 deletions src/Adapter/FastCGI.php
Expand Up @@ -71,12 +71,22 @@ public function __construct($host = null, $chroot = null)
$this->host = $host;

if (false !== strpos($host, ':')) {
[$host, $port] = explode(':', $host);
$last = strrpos($host, ':');
$port = substr($host, $last + 1, strlen($host));
$host = substr($host, 0, $last);

$IPv6 = '/^(?:[A-F0-9]{0,4}:){1,7}[A-F0-9]{0,4}$/';
if (preg_match($IPv6, $host) === 1) {
// IPv6 addresses need to be surrounded by brackets
// see: https://www.php.net/manual/en/function.stream-socket-client.php#refsect1-function.stream-socket-client-notes
$host = "[${host}]";
}

$this->connection = new NetworkSocket(
$host, # Hostname
$port, # Port
5000, # Connect timeout in milliseconds (default: 5000)
120000 # Read/write timeout in milliseconds (default: 5000)
$host, # Hostname
$port, # Port
5000, # Connect timeout in milliseconds (default: 5000)
120000 # Read/write timeout in milliseconds (default: 5000)
);
} else {
$this->connection = new UnixDomainSocket(
Expand Down
35 changes: 34 additions & 1 deletion tests/Adapter/FastCGITest.php
Expand Up @@ -4,6 +4,7 @@

use CacheTool\Code;
use CacheTool\PhpFpmRunner;
use CacheTool\Adapter\FastCGI;
use \hollodotme\FastCGI\SocketConnections\NetworkSocket;
use \hollodotme\FastCGI\Requests\PostRequest;
use \hollodotme\FastCGI\Responses\Response;
Expand Down Expand Up @@ -47,7 +48,7 @@ public function testGetScriptFileNameWithoutChroot()

public function testRunWithChroot()
{
$fcgi = $this->getMockBuilder(\CacheTool\Adapter\FastCGI::class)
$fcgi = $this->getMockBuilder(FastCGI::class)
->setMethods(['getScriptFileName'])
->setConstructorArgs(['127.0.0.1:9000', sys_get_temp_dir()])
->getMock();
Expand Down Expand Up @@ -83,4 +84,36 @@ public function testRunWithChroot()
$result = $fcgi->run($code);
$this->assertTrue($result);
}

public function testRunWithIPv4()
{
$fcgi = new FastCGI('127.0.0.1:9000');

$reflection = new \ReflectionClass($fcgi);
$reflectionConn = $reflection->getProperty('connection');
$reflectionConn->setAccessible(true);
$connection = $reflectionConn->getValue($fcgi);

$reflection = new \ReflectionClass($connection);
$reflectionHost = $reflection->getProperty('host');
$reflectionHost->setAccessible(true);

$this->assertSame('127.0.0.1', $reflectionHost->getValue($connection));
}

public function testRunWithIPv6()
{
$fcgi = new FastCGI(':::9000');

$reflection = new \ReflectionClass($fcgi);
$reflectionConn = $reflection->getProperty('connection');
$reflectionConn->setAccessible(true);
$connection = $reflectionConn->getValue($fcgi);

$reflection = new \ReflectionClass($connection);
$reflectionHost = $reflection->getProperty('host');
$reflectionHost->setAccessible(true);

$this->assertSame('[::]', $reflectionHost->getValue($connection));
}
}

0 comments on commit 68d443e

Please sign in to comment.