-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from clue-labs/resolving
Support Connector without DNS
- Loading branch information
Showing
7 changed files
with
245 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace React\SocketClient; | ||
|
||
use React\EventLoop\LoopInterface; | ||
use React\Dns\Resolver\Resolver; | ||
use React\Stream\Stream; | ||
use React\Promise; | ||
use React\Promise\Deferred; | ||
|
||
class DnsConnector implements ConnectorInterface | ||
{ | ||
private $connector; | ||
private $resolver; | ||
|
||
public function __construct(ConnectorInterface $connector, Resolver $resolver) | ||
{ | ||
$this->connector = $connector; | ||
$this->resolver = $resolver; | ||
} | ||
|
||
public function create($host, $port) | ||
{ | ||
$connector = $this->connector; | ||
|
||
return $this | ||
->resolveHostname($host) | ||
->then(function ($address) use ($connector, $port) { | ||
return $connector->create($address, $port); | ||
}); | ||
} | ||
|
||
private function resolveHostname($host) | ||
{ | ||
if (false !== filter_var($host, FILTER_VALIDATE_IP)) { | ||
return Promise\resolve($host); | ||
} | ||
|
||
return $this->resolver->resolve($host); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
namespace React\SocketClient; | ||
|
||
use React\EventLoop\LoopInterface; | ||
use React\Dns\Resolver\Resolver; | ||
use React\Stream\Stream; | ||
use React\Promise; | ||
use React\Promise\Deferred; | ||
|
||
class TcpConnector implements ConnectorInterface | ||
{ | ||
private $loop; | ||
|
||
public function __construct(LoopInterface $loop) | ||
{ | ||
$this->loop = $loop; | ||
} | ||
|
||
public function create($ip, $port) | ||
{ | ||
if (false === filter_var($ip, FILTER_VALIDATE_IP)) { | ||
return Promise\reject(new \InvalidArgumentException('Given parameter "' . $ip . '" is not a valid IP')); | ||
} | ||
|
||
$url = $this->getSocketUrl($ip, $port); | ||
|
||
$socket = stream_socket_client($url, $errno, $errstr, 0, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT); | ||
|
||
if (!$socket) { | ||
return Promise\reject(new \RuntimeException( | ||
sprintf("Connection to %s:%d failed: %s", $ip, $port, $errstr), | ||
$errno | ||
)); | ||
} | ||
|
||
stream_set_blocking($socket, 0); | ||
|
||
// wait for connection | ||
|
||
return $this | ||
->waitForStreamOnce($socket) | ||
->then(array($this, 'checkConnectedSocket')) | ||
->then(array($this, 'handleConnectedSocket')); | ||
} | ||
|
||
private function waitForStreamOnce($stream) | ||
{ | ||
$deferred = new Deferred(); | ||
|
||
$loop = $this->loop; | ||
|
||
$this->loop->addWriteStream($stream, function ($stream) use ($loop, $deferred) { | ||
$loop->removeWriteStream($stream); | ||
|
||
$deferred->resolve($stream); | ||
}); | ||
|
||
return $deferred->promise(); | ||
} | ||
|
||
/** @internal */ | ||
public function checkConnectedSocket($socket) | ||
{ | ||
// The following hack looks like the only way to | ||
// detect connection refused errors with PHP's stream sockets. | ||
if (false === stream_socket_get_name($socket, true)) { | ||
return Promise\reject(new ConnectionException('Connection refused')); | ||
} | ||
|
||
return Promise\resolve($socket); | ||
} | ||
|
||
/** @internal */ | ||
public function handleConnectedSocket($socket) | ||
{ | ||
return new Stream($socket, $this->loop); | ||
} | ||
|
||
private function getSocketUrl($ip, $port) | ||
{ | ||
if (strpos($ip, ':') !== false) { | ||
// enclose IPv6 addresses in square brackets before appending port | ||
$ip = '[' . $ip . ']'; | ||
} | ||
return sprintf('tcp://%s:%s', $ip, $port); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace React\Tests\SocketClient; | ||
|
||
use React\SocketClient\DnsConnector; | ||
use React\Promise; | ||
|
||
class DnsConnectorTest extends TestCase | ||
{ | ||
private $tcp; | ||
private $resolver; | ||
private $connector; | ||
|
||
public function setUp() | ||
{ | ||
$this->tcp = $this->getMock('React\SocketClient\ConnectorInterface'); | ||
$this->resolver = $this->getMockBuilder('React\Dns\Resolver\Resolver')->disableOriginalConstructor()->getMock(); | ||
|
||
$this->connector = new DnsConnector($this->tcp, $this->resolver); | ||
} | ||
|
||
public function testPassByResolverIfGivenIp() | ||
{ | ||
$this->resolver->expects($this->never())->method('resolve'); | ||
$this->tcp->expects($this->once())->method('create')->with($this->equalTo('127.0.0.1'), $this->equalTo(80)); | ||
|
||
$this->connector->create('127.0.0.1', 80); | ||
} | ||
|
||
public function testPassThroughResolverIfGivenHost() | ||
{ | ||
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('google.com'))->will($this->returnValue(Promise\resolve('1.2.3.4'))); | ||
$this->tcp->expects($this->once())->method('create')->with($this->equalTo('1.2.3.4'), $this->equalTo(80)); | ||
|
||
$this->connector->create('google.com', 80); | ||
} | ||
|
||
public function testSkipConnectionIfDnsFails() | ||
{ | ||
$this->resolver->expects($this->once())->method('resolve')->with($this->equalTo('example.invalid'))->will($this->returnValue(Promise\reject())); | ||
$this->tcp->expects($this->never())->method('create'); | ||
|
||
$this->connector->create('example.invalid', 80); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.