Skip to content

Commit 60fb967

Browse files
committed
Simplify usage by making Connector optional
1 parent b4682b8 commit 60fb967

11 files changed

+75
-124
lines changed

README.md

Lines changed: 39 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ The following example code demonstrates how this library can be used to send a
7171
secure HTTPS request to google.com through a local HTTP proxy server:
7272

7373
```php
74-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
75-
'127.0.0.1:8080',
76-
new React\Socket\Connector()
77-
);
74+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
7875

7976
$connector = new React\Socket\Connector(null, array(
8077
'tcp' => $proxy,
@@ -103,22 +100,34 @@ any destination by using an intermediary HTTP CONNECT proxy.
103100
[you] -> [proxy] -> [destination]
104101
```
105102

106-
Its constructor simply accepts an HTTP proxy URL and a connector used to connect
107-
to the proxy server address:
103+
Its constructor simply accepts an HTTP proxy URL with the proxy server address:
108104

109105
```php
110-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
111-
'http://127.0.0.1:8080',
112-
new React\Socket\Connector()
113-
);
106+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
114107
```
115108

116109
The proxy URL may or may not contain a scheme and port definition. The default
117110
port will be `80` for HTTP (or `443` for HTTPS), but many common HTTP proxy
118111
servers use custom ports (often the alternative HTTP port `8080`).
119-
In its most simple form, the given connector will be a
120-
[`\React\Socket\Connector`](https://github.com/reactphp/socket#connector) if you
121-
want to connect to a given IP address as above.
112+
113+
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
114+
proxy servers etc.), you can explicitly pass a custom instance of the
115+
[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface):
116+
117+
```php
118+
$connector = new React\Socket\Connector(null, array(
119+
'dns' => '127.0.0.1',
120+
'tcp' => array(
121+
'bindto' => '192.168.10.1:0'
122+
),
123+
'tls' => array(
124+
'verify_peer' => false,
125+
'verify_peer_name' => false
126+
)
127+
));
128+
129+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080', $connector);
130+
```
122131

123132
This is the main class in this package.
124133
Because it implements ReactPHP's standard
@@ -137,7 +146,7 @@ higher-level component:
137146

138147
```diff
139148
- $acme = new AcmeApi($connector);
140-
+ $proxy = new Clue\React\HttpProxy\ProxyConnector('http://127.0.0.1:8080', $connector);
149+
+ $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080', $connector);
141150
+ $acme = new AcmeApi($proxy);
142151
```
143152

@@ -150,10 +159,7 @@ As documented above, you can simply invoke its `connect()` method to establish
150159
a streaming plain TCP/IP connection and use any higher level protocol like so:
151160

152161
```php
153-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
154-
'127.0.0.1:8080',
155-
new React\Socket\Connector()
156-
);
162+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
157163

158164
$proxy->connect('tcp://smtp.googlemail.com:587')->then(function (React\Socket\ConnectionInterface $connection) {
159165
$connection->write("EHLO local\r\n");
@@ -167,10 +173,7 @@ You can either use the `ProxyConnector` directly or you may want to wrap this co
167173
in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
168174

169175
```php
170-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
171-
'127.0.0.1:8080',
172-
new React\Socket\Connector()
173-
);
176+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
174177

175178
$connector = new React\Socket\Connector(null, array(
176179
'tcp' => $proxy,
@@ -193,14 +196,10 @@ Many (public) proxy servers do in fact limit this to HTTPS (443) only.
193196
This class can also be used if you want to establish a secure TLS connection
194197
(formerly known as SSL) between you and your destination, such as when using
195198
secure HTTPS to your destination site. You can simply wrap this connector in
196-
ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector) or the
197-
low-level [`SecureConnector`](https://github.com/reactphp/socket#secureconnector):
199+
ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
198200

199201
```php
200-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
201-
'127.0.0.1:8080',
202-
new React\Socket\Connector()
203-
);
202+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
204203

205204
$connector = new React\Socket\Connector(null, array(
206205
'tcp' => $proxy,
@@ -227,10 +226,7 @@ In order to send HTTP requests, you first have to add a dependency for
227226
This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like this:
228227

229228
```php
230-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
231-
'127.0.0.1:8080',
232-
new React\Socket\Connector()
233-
);
229+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
234230

235231
$connector = new React\Socket\Connector(null, array(
236232
'tcp' => $proxy,
@@ -260,17 +256,12 @@ Many use cases require more control over the timeout and likely values much
260256
smaller, usually in the range of a few seconds only.
261257

262258
You can use ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector)
263-
or the low-level
264-
[`TimeoutConnector`](https://github.com/reactphp/socket#timeoutconnector)
265259
to decorate any given `ConnectorInterface` instance.
266260
It provides the same `connect()` method, but will automatically reject the
267261
underlying connection attempt if it takes too long:
268262

269263
```php
270-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
271-
'127.0.0.1:8080',
272-
new React\Socket\Connector()
273-
);
264+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
274265

275266
$connector = new React\Socket\Connector(null, array(
276267
'tcp' => $proxy,
@@ -314,10 +305,7 @@ Given that remote DNS resolution is assumed to be the preferred mode, all
314305
other examples explicitly disable DNS resolution like this:
315306

316307
```php
317-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
318-
'127.0.0.1:8080',
319-
new React\Socket\Connector()
320-
);
308+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
321309

322310
$connector = new React\Socket\Connector(null, array(
323311
'tcp' => $proxy,
@@ -328,10 +316,7 @@ $connector = new React\Socket\Connector(null, array(
328316
If you want to explicitly use *local DNS resolution*, you can use the following code:
329317

330318
```php
331-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
332-
'127.0.0.1:8080',
333-
new React\Socket\Connector()
334-
);
319+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
335320

336321
// set up Connector which uses Google's public DNS (8.8.8.8)
337322
$connector = new React\Socket\Connector(null, array(
@@ -349,10 +334,7 @@ If your HTTP proxy server requires authentication, you may pass the username and
349334
password as part of the HTTP proxy URL like this:
350335

351336
```php
352-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
353-
'http://user:[email protected]:8080',
354-
new React\Socket\Connector()
355-
);
337+
$proxy = new Clue\React\HttpProxy\ProxyConnector('user:[email protected]:8080');
356338
```
357339

358340
Note that both the username and password must be percent-encoded if they contain
@@ -361,11 +343,9 @@ special characters:
361343
```php
362344
$user = 'he:llo';
363345
$pass = 'p@ss';
346+
$url = rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1:8080';
364347

365-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
366-
rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1:8080',
367-
$connector
368-
);
348+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);
369349
```
370350

371351
> The authentication details will be used for basic authentication and will be
@@ -388,7 +368,7 @@ you may simply pass an assoc array of additional request headers like this:
388368
```php
389369
$proxy = new Clue\React\HttpProxy\ProxyConnector(
390370
'127.0.0.1:8080',
391-
$connector,
371+
null,
392372
array(
393373
'Proxy-Authorization' => 'Bearer abc123',
394374
'User-Agent' => 'ReactPHP'
@@ -404,16 +384,10 @@ setup, because you can still establish a TLS connection between you and the
404384
destination host as above.
405385

406386
If you want to connect to a (rather rare) HTTPS proxy, you may want use the
407-
`https://` scheme (HTTPS default port 443) and use ReactPHP's
408-
[`Connector`](https://github.com/reactphp/socket#connector) or the low-level
409-
[`SecureConnector`](https://github.com/reactphp/socket#secureconnector)
410-
instance to create a secure connection to the proxy:
387+
`https://` scheme (HTTPS default port 443) to create a secure connection to the proxy:
411388

412389
```php
413-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
414-
'https://127.0.0.1:443',
415-
new React\Socket\Connector()
416-
);
390+
$proxy = new Clue\React\HttpProxy\ProxyConnector('https://127.0.0.1:443');
417391

418392
$proxy->connect('tcp://smtp.googlemail.com:587');
419393
```
@@ -430,10 +404,7 @@ having to rely on explicit [authentication](#authentication).
430404
You can simply use the `http+unix://` URI scheme like this:
431405

432406
```php
433-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
434-
'http+unix:///tmp/proxy.sock',
435-
new React\Socket\Connector()
436-
);
407+
$proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix:///tmp/proxy.sock');
437408

438409
$proxy->connect('tcp://google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
439410
// connected…
@@ -444,10 +415,7 @@ Similarly, you can also combine this with [authentication](#authentication)
444415
like this:
445416

446417
```php
447-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
448-
'http+unix://user:pass@/tmp/proxy.sock',
449-
new React\Socket\Connector()
450-
);
418+
$proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix://user:pass@/tmp/proxy.sock');
451419
```
452420

453421
> Note that Unix domain sockets (UDS) are considered advanced usage and PHP only

examples/01-http-request.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@
2121
$url = 'localhost:8080';
2222
}
2323

24-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
25-
$url,
26-
new React\Socket\Connector()
27-
);
24+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);
2825

2926
$connector = new React\Socket\Connector(null, array(
3027
'tcp' => $proxy,

examples/02-optional-proxy-http-request.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
$connector = null;
1919
$url = getenv('http_proxy');
2020
if ($url !== false) {
21-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
22-
$url,
23-
new React\Socket\Connector()
24-
);
21+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);
2522

2623
$connector = new React\Socket\Connector(null, array(
2724
'tcp' => $proxy,

examples/11-proxy-raw-https-protocol.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@
2424
$url = 'localhost:8080';
2525
}
2626

27-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
28-
$url,
29-
new React\Socket\Connector()
30-
);
27+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);
3128

3229
$connector = new React\Socket\Connector(null, array(
3330
'tcp' => $proxy,

examples/12-optional-proxy-raw-https-protocol.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,17 @@
2222

2323
require __DIR__ . '/../vendor/autoload.php';
2424

25-
$connector = new React\Socket\Connector();
26-
2725
$url = getenv('http_proxy');
2826
if ($url !== false) {
29-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
30-
$url,
31-
$connector
32-
);
27+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);
3328

3429
$connector = new React\Socket\Connector(null, array(
3530
'tcp' => $proxy,
3631
'timeout' => 3.0,
3732
'dns' => false
3833
));
34+
} else {
35+
$connector = new React\Socket\Connector();
3936
}
4037

4138
$connector->connect('tls://google.com:443')->then(function (React\Socket\ConnectionInterface $connection) {

examples/13-custom-proxy-headers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
$proxy = new Clue\React\HttpProxy\ProxyConnector(
2828
$url,
29-
new React\Socket\Connector(),
29+
null,
3030
array(
3131
'X-Custom-Header-1' => 'Value-1',
3232
'X-Custom-Header-2' => 'Value-2',

examples/21-proxy-raw-smtp-protocol.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
$url = 'localhost:8080';
2424
}
2525

26-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
27-
$url,
28-
new React\Socket\Connector()
29-
);
26+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);
3027

3128
$connector = new React\Socket\Connector(null, array(
3229
'tcp' => $proxy,

examples/22-proxy-raw-smtps-protocol.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
$url = 'localhost:8080';
2727
}
2828

29-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
30-
$url,
31-
new React\Socket\Connector()
32-
);
29+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);
3330

3431
$connector = new React\Socket\Connector(null, array(
3532
'tcp' => $proxy,

src/ProxyConnector.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
use React\Promise;
1010
use React\Promise\Deferred;
1111
use React\Socket\ConnectionInterface;
12+
use React\Socket\Connector;
1213
use React\Socket\ConnectorInterface;
1314
use React\Socket\FixedUriConnector;
15+
use React\Socket\UnixConnector;
1416

1517
/**
1618
* A simple Connector that uses an HTTP CONNECT proxy to create plain TCP/IP connections to any destination
@@ -51,13 +53,11 @@ class ProxyConnector implements ConnectorInterface
5153
* @param string $proxyUrl The proxy URL may or may not contain a scheme and
5254
* port definition. The default port will be `80` for HTTP (or `443` for
5355
* HTTPS), but many common HTTP proxy servers use custom ports.
54-
* @param ConnectorInterface $connector In its most simple form, the given
55-
* connector will be a \React\Socket\Connector if you want to connect to
56-
* a given IP address.
56+
* @param ?ConnectorInterface $connector (Optional) Connector to use.
5757
* @param array $httpHeaders Custom HTTP headers to be sent to the proxy.
5858
* @throws InvalidArgumentException if the proxy URL is invalid
5959
*/
60-
public function __construct($proxyUrl, ConnectorInterface $connector, array $httpHeaders = array())
60+
public function __construct($proxyUrl, ConnectorInterface $connector = null, array $httpHeaders = array())
6161
{
6262
// support `http+unix://` scheme for Unix domain socket (UDS) paths
6363
if (preg_match('/^http\+unix:\/\/(.*?@)?(.+?)$/', $proxyUrl, $match)) {
@@ -67,7 +67,7 @@ public function __construct($proxyUrl, ConnectorInterface $connector, array $htt
6767
// connector uses Unix transport scheme and explicit path given
6868
$connector = new FixedUriConnector(
6969
'unix://' . $match[2],
70-
$connector
70+
$connector ?: new UnixConnector()
7171
);
7272
}
7373

@@ -86,7 +86,7 @@ public function __construct($proxyUrl, ConnectorInterface $connector, array $htt
8686
}
8787
$parts['scheme'] = $parts['scheme'] === 'https' ? 'tls' : 'tcp';
8888

89-
$this->connector = $connector;
89+
$this->connector = $connector ?: new Connector();
9090
$this->proxyUri = $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port'];
9191

9292
// prepare Proxy-Authorization header if URI contains username/password

0 commit comments

Comments
 (0)