Skip to content

Commit a7a0d47

Browse files
authored
Merge pull request #41 from clue-labs/default-loop
Simplify usage by updating to new default loop and making `Connector` optional
2 parents c067ae7 + 60fb967 commit a7a0d47

12 files changed

+97
-172
lines changed

README.md

Lines changed: 47 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,9 @@ 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-
$loop = React\EventLoop\Factory::create();
74+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
7575

76-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
77-
'127.0.0.1:8080',
78-
new React\Socket\Connector($loop)
79-
);
80-
$connector = new React\Socket\Connector($loop, array(
76+
$connector = new React\Socket\Connector(null, array(
8177
'tcp' => $proxy,
8278
'timeout' => 3.0,
8379
'dns' => false
@@ -89,8 +85,6 @@ $connector->connect('tls://google.com:443')->then(function (React\Socket\Connect
8985
echo $chunk;
9086
});
9187
}, 'printf');
92-
93-
$loop->run();
9488
```
9589

9690
See also the [examples](examples).
@@ -106,20 +100,34 @@ any destination by using an intermediary HTTP CONNECT proxy.
106100
[you] -> [proxy] -> [destination]
107101
```
108102

109-
Its constructor simply accepts an HTTP proxy URL and a connector used to connect
110-
to the proxy server address:
103+
Its constructor simply accepts an HTTP proxy URL with the proxy server address:
111104

112105
```php
113-
$connector = new React\Socket\Connector($loop);
114-
$proxy = new Clue\React\HttpProxy\ProxyConnector('http://127.0.0.1:8080', $connector);
106+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
115107
```
116108

117109
The proxy URL may or may not contain a scheme and port definition. The default
118110
port will be `80` for HTTP (or `443` for HTTPS), but many common HTTP proxy
119111
servers use custom ports (often the alternative HTTP port `8080`).
120-
In its most simple form, the given connector will be a
121-
[`\React\Socket\Connector`](https://github.com/reactphp/socket#connector) if you
122-
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+
```
123131

124132
This is the main class in this package.
125133
Because it implements ReactPHP's standard
@@ -138,7 +146,7 @@ higher-level component:
138146

139147
```diff
140148
- $acme = new AcmeApi($connector);
141-
+ $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);
142150
+ $acme = new AcmeApi($proxy);
143151
```
144152

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

153161
```php
154-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
155-
'127.0.0.1:8080',
156-
new React\Socket\Connector($loop)
157-
);
162+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
158163

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

170175
```php
171-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
172-
'127.0.0.1:8080',
173-
new React\Socket\Connector($loop)
174-
);
176+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
175177

176-
$connector = new React\Socket\Connector($loop, array(
178+
$connector = new React\Socket\Connector(null, array(
177179
'tcp' => $proxy,
178180
'dns' => false
179181
));
@@ -194,16 +196,12 @@ Many (public) proxy servers do in fact limit this to HTTPS (443) only.
194196
This class can also be used if you want to establish a secure TLS connection
195197
(formerly known as SSL) between you and your destination, such as when using
196198
secure HTTPS to your destination site. You can simply wrap this connector in
197-
ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector) or the
198-
low-level [`SecureConnector`](https://github.com/reactphp/socket#secureconnector):
199+
ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector):
199200

200201
```php
201-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
202-
'127.0.0.1:8080',
203-
new React\Socket\Connector($loop)
204-
);
202+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
205203

206-
$connector = new React\Socket\Connector($loop, array(
204+
$connector = new React\Socket\Connector(null, array(
207205
'tcp' => $proxy,
208206
'dns' => false
209207
));
@@ -228,17 +226,14 @@ In order to send HTTP requests, you first have to add a dependency for
228226
This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like this:
229227

230228
```php
231-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
232-
'127.0.0.1:8080',
233-
new React\Socket\Connector($loop)
234-
);
229+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
235230

236-
$connector = new React\Socket\Connector($loop, array(
231+
$connector = new React\Socket\Connector(null, array(
237232
'tcp' => $proxy,
238233
'dns' => false
239234
));
240235

241-
$browser = new React\Http\Browser($loop, $connector);
236+
$browser = new React\Http\Browser(null, $connector);
242237

243238
$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
244239
var_dump($response->getHeaders(), (string) $response->getBody());
@@ -261,19 +256,14 @@ Many use cases require more control over the timeout and likely values much
261256
smaller, usually in the range of a few seconds only.
262257

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

270263
```php
271-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
272-
'127.0.0.1:8080',
273-
new React\Socket\Connector($loop)
274-
);
264+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
275265

276-
$connector = new React\Socket\Connector($loop, array(
266+
$connector = new React\Socket\Connector(null, array(
277267
'tcp' => $proxy,
278268
'dns' => false,
279269
'timeout' => 3.0
@@ -315,12 +305,9 @@ Given that remote DNS resolution is assumed to be the preferred mode, all
315305
other examples explicitly disable DNS resolution like this:
316306

317307
```php
318-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
319-
'127.0.0.1:8080',
320-
new React\Socket\Connector($loop)
321-
);
308+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
322309

323-
$connector = new React\Socket\Connector($loop, array(
310+
$connector = new React\Socket\Connector(null, array(
324311
'tcp' => $proxy,
325312
'dns' => false
326313
));
@@ -329,13 +316,10 @@ $connector = new React\Socket\Connector($loop, array(
329316
If you want to explicitly use *local DNS resolution*, you can use the following code:
330317

331318
```php
332-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
333-
'127.0.0.1:8080',
334-
new React\Socket\Connector($loop)
335-
);
319+
$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080');
336320

337321
// set up Connector which uses Google's public DNS (8.8.8.8)
338-
$connector = new React\Socket\Connector($loop, array(
322+
$connector = new React\Socket\Connector(null, array(
339323
'tcp' => $proxy,
340324
'dns' => '8.8.8.8'
341325
));
@@ -350,10 +334,7 @@ If your HTTP proxy server requires authentication, you may pass the username and
350334
password as part of the HTTP proxy URL like this:
351335

352336
```php
353-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
354-
'http://user:[email protected]:8080',
355-
new React\Socket\Connector($loop)
356-
);
337+
$proxy = new Clue\React\HttpProxy\ProxyConnector('user:[email protected]:8080');
357338
```
358339

359340
Note that both the username and password must be percent-encoded if they contain
@@ -362,11 +343,9 @@ special characters:
362343
```php
363344
$user = 'he:llo';
364345
$pass = 'p@ss';
346+
$url = rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1:8080';
365347

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

372351
> The authentication details will be used for basic authentication and will be
@@ -389,7 +368,7 @@ you may simply pass an assoc array of additional request headers like this:
389368
```php
390369
$proxy = new Clue\React\HttpProxy\ProxyConnector(
391370
'127.0.0.1:8080',
392-
$connector,
371+
null,
393372
array(
394373
'Proxy-Authorization' => 'Bearer abc123',
395374
'User-Agent' => 'ReactPHP'
@@ -405,16 +384,10 @@ setup, because you can still establish a TLS connection between you and the
405384
destination host as above.
406385

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

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

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

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

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

447417
```php
448-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
449-
'http+unix://user:pass@/tmp/proxy.sock',
450-
new React\Socket\Connector($loop)
451-
);
418+
$proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix://user:pass@/tmp/proxy.sock');
452419
```
453420

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

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
"require": {
2020
"php": ">=5.3",
2121
"react/promise": " ^2.1 || ^1.2.1",
22-
"react/socket": "^1.1",
22+
"react/socket": "^1.8",
2323
"ringcentral/psr7": "^1.2"
2424
},
2525
"require-dev": {
2626
"phpunit/phpunit": "^9.3 || ^5.7 || ^4.8",
27-
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
28-
"react/http": "^1.0",
27+
"react/event-loop": "^1.2",
28+
"react/http": "^1.4",
2929
"clue/block-react": "^1.1"
3030
}
3131
}

examples/01-http-request.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,17 @@
2121
$url = 'localhost:8080';
2222
}
2323

24-
$loop = React\EventLoop\Factory::create();
25-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
26-
$url,
27-
new React\Socket\Connector($loop)
28-
);
24+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);
2925

30-
$connector = new React\Socket\Connector($loop, array(
26+
$connector = new React\Socket\Connector(null, array(
3127
'tcp' => $proxy,
3228
'dns' => false
3329
));
3430

35-
$browser = new React\Http\Browser($loop, $connector);
31+
$browser = new React\Http\Browser(null, $connector);
3632

3733
$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
3834
var_dump($response->getHeaders(), (string) $response->getBody());
3935
}, function (Exception $e) {
4036
echo 'Error: ' . $e->getMessage() . PHP_EOL;
4137
});
42-
43-
$loop->run();

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,22 @@
1515

1616
require __DIR__ . '/../vendor/autoload.php';
1717

18-
$loop = React\EventLoop\Factory::create();
19-
2018
$connector = null;
2119
$url = getenv('http_proxy');
2220
if ($url !== false) {
23-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
24-
$url,
25-
new React\Socket\Connector($loop)
26-
);
27-
$connector = new React\Socket\Connector($loop, array(
21+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);
22+
23+
$connector = new React\Socket\Connector(null, array(
2824
'tcp' => $proxy,
2925
'timeout' => 3.0,
3026
'dns' => false
3127
));
3228
}
3329

34-
$browser = new React\Http\Browser($loop, $connector);
30+
$browser = new React\Http\Browser(null, $connector);
3531

3632
$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
3733
var_dump($response->getHeaders(), (string) $response->getBody());
3834
}, function (Exception $e) {
3935
echo 'Error: ' . $e->getMessage() . PHP_EOL;
4036
});
41-
42-
$loop->run();

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,9 @@
2424
$url = 'localhost:8080';
2525
}
2626

27-
$loop = React\EventLoop\Factory::create();
27+
$proxy = new Clue\React\HttpProxy\ProxyConnector($url);
2828

29-
$proxy = new Clue\React\HttpProxy\ProxyConnector(
30-
$url,
31-
new React\Socket\Connector($loop)
32-
);
33-
34-
$connector = new React\Socket\Connector($loop, array(
29+
$connector = new React\Socket\Connector(null, array(
3530
'tcp' => $proxy,
3631
'timeout' => 3.0,
3732
'dns' => false
@@ -45,5 +40,3 @@
4540
}, function (Exception $e) {
4641
echo 'Error: ' . $e->getMessage() . PHP_EOL;
4742
});
48-
49-
$loop->run();

0 commit comments

Comments
 (0)