Skip to content

Commit 45a7a24

Browse files
committed
Use main Connector class where possible
1 parent e664936 commit 45a7a24

File tree

6 files changed

+54
-43
lines changed

6 files changed

+54
-43
lines changed

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ secure HTTPS request to google.com through a local HTTP proxy server:
2121

2222
```php
2323
$loop = React\EventLoop\Factory::create();
24-
$connector = new TcpConnector($loop);
25-
$proxy = new ProxyConnector('127.0.0.1:8080', $connector);
26-
$ssl = new SecureConnector($proxy, $loop);
2724

28-
$ssl->connect('google.com:443')->then(function (ConnectionInterface $stream) {
25+
$proxy = new ProxyConnector('127.0.0.1:8080', new Connector($loop));
26+
$connector = new Connector($loop, array(
27+
'tcp' => $proxy,
28+
'timeout' => 3.0,
29+
'dns' => false
30+
));
31+
32+
$connector->connect('tls://google.com:443')->then(function (ConnectionInterface $stream) {
2933
$stream->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n");
3034
$stream->on('data', function ($chunk) {
3135
echo $chunk;
@@ -91,15 +95,15 @@ Its constructor simply accepts an HTTP proxy URL and a connector used to connect
9195
to the proxy server address:
9296

9397
```php
94-
$connector = new TcpConnector($loop);
98+
$connector = new Connector($loop);
9599
$proxy = new ProxyConnector('127.0.0.1:8080', $connector);
96100
```
97101

98102
The proxy URL may or may not contain a scheme and port definition. The default
99103
port will be `80` for HTTP (or `443` for HTTPS), but many common HTTP proxy
100104
servers use custom ports.
101105
In its most simple form, the given connector will be a
102-
[`TcpConnector`](https://github.com/reactphp/socket#tcpconnector) if you
106+
[`\React\Socket\Connector`](https://github.com/reactphp/socket#connector) if you
103107
want to connect to a given IP address as above.
104108

105109
This is the main class in this package.
@@ -121,7 +125,7 @@ connector is actually inherently a general-purpose plain TCP/IP connector:
121125
```php
122126
$proxy = new ProxyConnector('127.0.0.1:8080', $connector);
123127

124-
$proxy->connect('smtp.googlemail.com:587')->then(function (ConnectionInterface $stream) {
128+
$proxy->connect('tcp://smtp.googlemail.com:587')->then(function (ConnectionInterface $stream) {
125129
$stream->write("EHLO local\r\n");
126130
$stream->on('data', function ($chunk) use ($stream) {
127131
echo $chunk;
@@ -133,15 +137,18 @@ Note that HTTP CONNECT proxies often restrict which ports one may connect to.
133137
Many (public) proxy servers do in fact limit this to HTTPS (443) only.
134138

135139
If you want to establish a TLS connection (such as HTTPS) between you and
136-
your destination, you may want to wrap this connector in a
137-
[`SecureConnector`](https://github.com/reactphp/socket#secureconnector)
138-
instance:
140+
your destination, you may want to wrap this connector in React's
141+
[`Connector`](https://github.com/reactphp/socket#connector) or the low-level
142+
[`SecureConnector`](https://github.com/reactphp/socket#secureconnector):
139143

140144
```php
141145
$proxy = new ProxyConnector('127.0.0.1:8080', $connector);
142-
$ssl = new SecureConnector($proxy, $loop);
146+
$connector = new Connector($loop, array(
147+
'tcp' => $proxy,
148+
'dns' => false
149+
));
143150

144-
$ssl->connect('smtp.googlemail.com:465')->then(function (ConnectionInterface $stream) {
151+
$connector->connect('tls://smtp.googlemail.com:465')->then(function (ConnectionInterface $stream) {
145152
$stream->write("EHLO local\r\n");
146153
$stream->on('data', function ($chunk) use ($stream) {
147154
echo $chunk;

examples/01-proxy-https.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
// The proxy can be given as first argument and defaults to localhost:8080 otherwise.
55

66
use Clue\React\HttpProxy\ProxyConnector;
7-
use React\Socket\TcpConnector;
8-
use React\Socket\SecureConnector;
7+
use React\Socket\Connector;
98
use React\Socket\ConnectionInterface;
109

1110
require __DIR__ . '/../vendor/autoload.php';
@@ -14,11 +13,14 @@
1413

1514
$loop = React\EventLoop\Factory::create();
1615

17-
$connector = new TcpConnector($loop);
18-
$proxy = new ProxyConnector($url, $connector);
19-
$ssl = new SecureConnector($proxy, $loop);
16+
$proxy = new ProxyConnector($url, new Connector($loop));
17+
$connector = new Connector($loop, array(
18+
'tcp' => $proxy,
19+
'timeout' => 3.0,
20+
'dns' => false
21+
));
2022

21-
$ssl->connect('google.com:443')->then(function (ConnectionInterface $stream) {
23+
$connector->connect('tls://google.com:443')->then(function (ConnectionInterface $stream) {
2224
$stream->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n");
2325
$stream->on('data', function ($chunk) {
2426
echo $chunk;

examples/02-optional-proxy-https.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,26 @@
88
// network protocol otherwise.
99

1010
use Clue\React\HttpProxy\ProxyConnector;
11-
use React\Socket\TcpConnector;
12-
use React\Socket\SecureConnector;
13-
use React\Socket\DnsConnector;
14-
use React\Dns\Resolver\Factory;
11+
use React\Socket\Connector;
1512
use React\Socket\ConnectionInterface;
1613

1714
require __DIR__ . '/../vendor/autoload.php';
1815

1916
$loop = React\EventLoop\Factory::create();
2017

21-
$tcp = new TcpConnector($loop);
22-
$dnsFactory = new Factory();
23-
$resolver = $dnsFactory->create('8.8.8.8', $loop);
24-
$dns = new DnsConnector($tcp, $resolver);
18+
$connector = new Connector($loop);
2519

2620
// first argument given? use this as the proxy URL
2721
if (isset($argv[1])) {
28-
$proxy = new ProxyConnector($argv[1], $dns);
29-
$connector = new SecureConnector($proxy, $loop);
30-
} else {
31-
$connector = new SecureConnector($dns, $loop);
22+
$proxy = new ProxyConnector($argv[1], $connector);
23+
$connector = new Connector($loop, array(
24+
'tcp' => $proxy,
25+
'timeout' => 3.0,
26+
'dns' => false
27+
));
3228
}
3329

34-
$connector->connect('google.com:443')->then(function (ConnectionInterface $stream) {
30+
$connector->connect('tls://google.com:443')->then(function (ConnectionInterface $stream) {
3531
$stream->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n");
3632
$stream->on('data', function ($chunk) {
3733
echo $chunk;

examples/11-proxy-smtp.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Please note that MANY public proxies do not allow SMTP connections, YMMV.
66

77
use Clue\React\HttpProxy\ProxyConnector;
8-
use React\Socket\TcpConnector;
8+
use React\Socket\Connector;
99
use React\Socket\ConnectionInterface;
1010

1111
require __DIR__ . '/../vendor/autoload.php';
@@ -14,10 +14,14 @@
1414

1515
$loop = React\EventLoop\Factory::create();
1616

17-
$connector = new TcpConnector($loop);
18-
$proxy = new ProxyConnector($url, $connector);
17+
$proxy = new ProxyConnector($url, new Connector($loop));
18+
$connector = new Connector($loop, array(
19+
'tcp' => $proxy,
20+
'timeout' => 3.0,
21+
'dns' => false
22+
));
1923

20-
$proxy->connect('smtp.googlemail.com:587')->then(function (ConnectionInterface $stream) {
24+
$connector->connect('tcp://smtp.googlemail.com:587')->then(function (ConnectionInterface $stream) {
2125
$stream->write("EHLO local\r\n");
2226
$stream->on('data', function ($chunk) use ($stream) {
2327
echo $chunk;

examples/12-proxy-smtps.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
// Please note that MANY public proxies do not allow SMTP connections, YMMV.
99

1010
use Clue\React\HttpProxy\ProxyConnector;
11-
use React\Socket\TcpConnector;
12-
use React\Socket\SecureConnector;
11+
use React\Socket\Connector;
1312
use React\Socket\ConnectionInterface;
1413

1514
require __DIR__ . '/../vendor/autoload.php';
@@ -18,11 +17,14 @@
1817

1918
$loop = React\EventLoop\Factory::create();
2019

21-
$connector = new TcpConnector($loop);
22-
$proxy = new ProxyConnector($url, $connector);
23-
$ssl = new SecureConnector($proxy, $loop);
20+
$proxy = new ProxyConnector($url, new Connector($loop));
21+
$connector = new Connector($loop, array(
22+
'tcp' => $proxy,
23+
'timeout' => 3.0,
24+
'dns' => false
25+
));
2426

25-
$ssl->connect('smtp.googlemail.com:465')->then(function (ConnectionInterface $stream) {
27+
$connector->connect('tls://smtp.googlemail.com:465')->then(function (ConnectionInterface $stream) {
2628
$stream->write("EHLO local\r\n");
2729
$stream->on('data', function ($chunk) use ($stream) {
2830
echo $chunk;

src/ProxyConnector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class ProxyConnector implements ConnectorInterface
4949
* port definition. The default port will be `80` for HTTP (or `443` for
5050
* HTTPS), but many common HTTP proxy servers use custom ports.
5151
* @param ConnectorInterface $connector In its most simple form, the given
52-
* connector will be a TcpConnector if you want to connect to a given IP
53-
* address.
52+
* connector will be a \React\Socket\Connector if you want to connect to
53+
* a given IP address.
5454
* @throws InvalidArgumentException if the proxy URL is invalid
5555
*/
5656
public function __construct($proxyUrl, ConnectorInterface $connector)

0 commit comments

Comments
 (0)