-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Connector without DNS #46
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1a4b1c6
Split Connector into TcpConnector and DnsConnector
clue 9fe3407
Reject hostnames for TcpConnector and improve test coverage
clue b468654
Add legacy Connector as BC layer
clue 0516389
Mark internals as such in order to avoid a future BC break
clue 0f07289
Improve exception error message
clue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: connection -> Connection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for spotting, fixed! :)
For the reference: This code has been like this ever since the very first commit in this repo.