Skip to content

Commit

Permalink
Merge pull request #42 from clue-labs/socket
Browse files Browse the repository at this point in the history
Simplify usage by supporting new Socket API without nullable loop
  • Loading branch information
clue authored Aug 5, 2021
2 parents a7a0d47 + 59f6309 commit a8be2b4
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 47 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ secure HTTPS request to google.com through a local HTTP proxy server:
```php
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand Down Expand Up @@ -115,7 +115,7 @@ proxy servers etc.), you can explicitly pass a custom instance of the
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):

```php
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'dns' => '127.0.0.1',
'tcp' => array(
'bindto' => '192.168.10.1:0'
Expand Down Expand Up @@ -175,7 +175,7 @@ in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
```php
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => false
));
Expand All @@ -201,7 +201,7 @@ ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
```php
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => false
));
Expand All @@ -228,12 +228,12 @@ This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like th
```php
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => false
));

$browser = new React\Http\Browser(null, $connector);
$browser = new React\Http\Browser($connector);

$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string) $response->getBody());
Expand Down Expand Up @@ -263,7 +263,7 @@ underlying connection attempt if it takes too long:
```php
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => false,
'timeout' => 3.0
Expand Down Expand Up @@ -307,7 +307,7 @@ other examples explicitly disable DNS resolution like this:
```php
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => false
));
Expand All @@ -319,7 +319,7 @@ If you want to explicitly use *local DNS resolution*, you can use the following
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');

// set up Connector which uses Google's public DNS (8.8.8.8)
$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => '8.8.8.8'
));
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"require": {
"php": ">=5.3",
"react/promise": " ^2.1 || ^1.2.1",
"react/socket": "^1.8",
"react/socket": "^1.9",
"ringcentral/psr7": "^1.2"
},
"require-dev": {
"clue/block-react": "^1.1",
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8",
"react/event-loop": "^1.2",
"react/http": "^1.4",
"clue/block-react": "^1.1"
"react/http": "^1.5"
}
}
4 changes: 2 additions & 2 deletions examples/01-http-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@

$proxy = new Clue\React\HttpProxy\ProxyConnector($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'dns' => false
));

$browser = new React\Http\Browser(null, $connector);
$browser = new React\Http\Browser($connector);

$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string) $response->getBody());
Expand Down
4 changes: 2 additions & 2 deletions examples/02-optional-proxy-http-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
if ($url !== false) {
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
));
}

$browser = new React\Http\Browser(null, $connector);
$browser = new React\Http\Browser($connector);

$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
var_dump($response->getHeaders(), (string) $response->getBody());
Expand Down
2 changes: 1 addition & 1 deletion examples/11-proxy-raw-https-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

$proxy = new Clue\React\HttpProxy\ProxyConnector($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand Down
2 changes: 1 addition & 1 deletion examples/12-optional-proxy-raw-https-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
if ($url !== false) {
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand Down
2 changes: 1 addition & 1 deletion examples/13-custom-proxy-headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
)
);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false,
Expand Down
2 changes: 1 addition & 1 deletion examples/21-proxy-raw-smtp-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

$proxy = new Clue\React\HttpProxy\ProxyConnector($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand Down
2 changes: 1 addition & 1 deletion examples/22-proxy-raw-smtps-protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

$proxy = new Clue\React\HttpProxy\ProxyConnector($url);

$connector = new React\Socket\Connector(null, array(
$connector = new React\Socket\Connector(array(
'tcp' => $proxy,
'timeout' => 3.0,
'dns' => false
Expand Down
4 changes: 1 addition & 3 deletions tests/AbstractTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use PHPUnit\Framework\TestCase;

abstract class AbstractTestCase extends \PHPUnit\Framework\TestCase
abstract class AbstractTestCase extends TestCase
{
protected function expectCallableNever()
{
Expand Down Expand Up @@ -77,6 +77,4 @@ public function setExpectedException($exception, $exceptionMessage = '', $except
parent::setExpectedException($exception, $exceptionMessage, $exceptionCode);
}
}

}

31 changes: 9 additions & 22 deletions tests/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,14 @@

use Clue\React\Block;
use Clue\React\HttpProxy\ProxyConnector;
use React\EventLoop\Factory;
use React\Socket\Connector;
use React\EventLoop\Loop;

/** @group internet */
class FunctionalTest extends AbstractTestCase
{
private $loop;
private $connector;

/**
* @before
*/
public function setUpConnector()
{
$this->loop = Factory::create();
$this->connector = new Connector($this->loop);
}

public function testNonListeningSocketRejectsConnection()
{
$proxy = new ProxyConnector('127.0.0.1:9999', $this->connector);
$proxy = new ProxyConnector('127.0.0.1:9999');

$promise = $proxy->connect('google.com:80');

Expand All @@ -33,12 +20,12 @@ public function testNonListeningSocketRejectsConnection()
'Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)',
defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111
);
Block\await($promise, $this->loop, 3.0);
Block\await($promise, Loop::get(), 3.0);
}

public function testPlainGoogleDoesNotAcceptConnectMethod()
{
$proxy = new ProxyConnector('google.com', $this->connector);
$proxy = new ProxyConnector('google.com');

$promise = $proxy->connect('google.com:80');

Expand All @@ -47,7 +34,7 @@ public function testPlainGoogleDoesNotAcceptConnectMethod()
'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)',
defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111
);
Block\await($promise, $this->loop, 3.0);
Block\await($promise, Loop::get(), 3.0);
}

public function testSecureGoogleDoesNotAcceptConnectMethod()
Expand All @@ -56,7 +43,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod()
$this->markTestSkipped('TLS not supported on legacy HHVM');
}

$proxy = new ProxyConnector('https://google.com:443', $this->connector);
$proxy = new ProxyConnector('https://google.com:443');

$promise = $proxy->connect('google.com:80');

Expand All @@ -65,7 +52,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod()
'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)',
defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111
);
Block\await($promise, $this->loop, 3.0);
Block\await($promise, Loop::get(), 3.0);
}

public function testSecureGoogleDoesNotAcceptPlainStream()
Expand All @@ -79,15 +66,15 @@ public function testSecureGoogleDoesNotAcceptPlainStream()
'Connection to tcp://google.com:80 failed because connection to proxy was lost while waiting for response (ECONNRESET)',
defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104
);
Block\await($promise, $this->loop, 3.0);
Block\await($promise, Loop::get(), 3.0);
}

/**
* @requires PHP 7
*/
public function testCancelWhileConnectingShouldNotCreateGarbageCycles()
{
$proxy = new ProxyConnector('google.com', $this->connector);
$proxy = new ProxyConnector('google.com');

gc_collect_cycles();
gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on
Expand Down
2 changes: 1 addition & 1 deletion tests/ProxyConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Clue\Tests\React\HttpProxy;

use Clue\React\HttpProxy\ProxyConnector;
use React\Promise\Promise;
use React\Socket\ConnectionInterface;
use React\Promise\Deferred;
use React\Promise\Promise;

class ProxyConnectorTest extends AbstractTestCase
{
Expand Down

0 comments on commit a8be2b4

Please sign in to comment.