@@ -71,13 +71,9 @@ The following example code demonstrates how this library can be used to send a
71
71
secure HTTPS request to google.com through a local HTTP proxy server:
72
72
73
73
``` php
74
- $loop = React\EventLoop\Factory::create( );
74
+ $proxy = new Clue\ React\HttpProxy\ProxyConnector('127.0.0.1:8080' );
75
75
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(
81
77
'tcp' => $proxy,
82
78
'timeout' => 3.0,
83
79
'dns' => false
@@ -89,8 +85,6 @@ $connector->connect('tls://google.com:443')->then(function (React\Socket\Connect
89
85
echo $chunk;
90
86
});
91
87
}, 'printf');
92
-
93
- $loop->run();
94
88
```
95
89
96
90
See also the [ examples] ( examples ) .
@@ -106,20 +100,34 @@ any destination by using an intermediary HTTP CONNECT proxy.
106
100
[you] -> [proxy] -> [destination]
107
101
```
108
102
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:
111
104
112
105
``` 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');
115
107
```
116
108
117
109
The proxy URL may or may not contain a scheme and port definition. The default
118
110
port will be ` 80 ` for HTTP (or ` 443 ` for HTTPS), but many common HTTP proxy
119
111
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
+ ```
123
131
124
132
This is the main class in this package.
125
133
Because it implements ReactPHP's standard
@@ -138,7 +146,7 @@ higher-level component:
138
146
139
147
``` diff
140
148
- $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);
142
150
+ $acme = new AcmeApi($proxy);
143
151
```
144
152
@@ -151,10 +159,7 @@ As documented above, you can simply invoke its `connect()` method to establish
151
159
a streaming plain TCP/IP connection and use any higher level protocol like so:
152
160
153
161
``` 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');
158
163
159
164
$proxy->connect('tcp://smtp.googlemail.com:587')->then(function (React\Socket\ConnectionInterface $connection) {
160
165
$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
168
173
in ReactPHP's [ ` Connector ` ] ( https://github.com/reactphp/socket#connector ) :
169
174
170
175
``` 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');
175
177
176
- $connector = new React\Socket\Connector($loop , array(
178
+ $connector = new React\Socket\Connector(null , array(
177
179
'tcp' => $proxy,
178
180
'dns' => false
179
181
));
@@ -194,16 +196,12 @@ Many (public) proxy servers do in fact limit this to HTTPS (443) only.
194
196
This class can also be used if you want to establish a secure TLS connection
195
197
(formerly known as SSL) between you and your destination, such as when using
196
198
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 ) :
199
200
200
201
``` 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');
205
203
206
- $connector = new React\Socket\Connector($loop , array(
204
+ $connector = new React\Socket\Connector(null , array(
207
205
'tcp' => $proxy,
208
206
'dns' => false
209
207
));
@@ -228,17 +226,14 @@ In order to send HTTP requests, you first have to add a dependency for
228
226
This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like this:
229
227
230
228
``` 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');
235
230
236
- $connector = new React\Socket\Connector($loop , array(
231
+ $connector = new React\Socket\Connector(null , array(
237
232
'tcp' => $proxy,
238
233
'dns' => false
239
234
));
240
235
241
- $browser = new React\Http\Browser($loop , $connector);
236
+ $browser = new React\Http\Browser(null , $connector);
242
237
243
238
$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) {
244
239
var_dump($response->getHeaders(), (string) $response->getBody());
@@ -261,19 +256,14 @@ Many use cases require more control over the timeout and likely values much
261
256
smaller, usually in the range of a few seconds only.
262
257
263
258
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 )
266
259
to decorate any given ` ConnectorInterface ` instance.
267
260
It provides the same ` connect() ` method, but will automatically reject the
268
261
underlying connection attempt if it takes too long:
269
262
270
263
``` 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');
275
265
276
- $connector = new React\Socket\Connector($loop , array(
266
+ $connector = new React\Socket\Connector(null , array(
277
267
'tcp' => $proxy,
278
268
'dns' => false,
279
269
'timeout' => 3.0
@@ -315,12 +305,9 @@ Given that remote DNS resolution is assumed to be the preferred mode, all
315
305
other examples explicitly disable DNS resolution like this:
316
306
317
307
``` 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');
322
309
323
- $connector = new React\Socket\Connector($loop , array(
310
+ $connector = new React\Socket\Connector(null , array(
324
311
'tcp' => $proxy,
325
312
'dns' => false
326
313
));
@@ -329,13 +316,10 @@ $connector = new React\Socket\Connector($loop, array(
329
316
If you want to explicitly use * local DNS resolution* , you can use the following code:
330
317
331
318
``` 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');
336
320
337
321
// 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(
339
323
'tcp' => $proxy,
340
324
'dns' => '8.8.8.8'
341
325
));
@@ -350,10 +334,7 @@ If your HTTP proxy server requires authentication, you may pass the username and
350
334
password as part of the HTTP proxy URL like this:
351
335
352
336
``` 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');
357
338
```
358
339
359
340
Note that both the username and password must be percent-encoded if they contain
@@ -362,11 +343,9 @@ special characters:
362
343
``` php
363
344
$user = 'he:llo';
364
345
$pass = 'p@ss';
346
+ $url = rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1:8080';
365
347
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);
370
349
```
371
350
372
351
> 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:
389
368
``` php
390
369
$proxy = new Clue\React\HttpProxy\ProxyConnector(
391
370
'127.0.0.1:8080',
392
- $connector ,
371
+ null ,
393
372
array(
394
373
'Proxy-Authorization' => 'Bearer abc123',
395
374
'User-Agent' => 'ReactPHP'
@@ -405,16 +384,10 @@ setup, because you can still establish a TLS connection between you and the
405
384
destination host as above.
406
385
407
386
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:
412
388
413
389
``` 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');
418
391
419
392
$proxy->connect('tcp://smtp.googlemail.com:587');
420
393
```
@@ -431,10 +404,7 @@ having to rely on explicit [authentication](#authentication).
431
404
You can simply use the ` http+unix:// ` URI scheme like this:
432
405
433
406
``` 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');
438
408
439
409
$proxy->connect('tcp://google.com:80')->then(function (React\Socket\ConnectionInterface $connection) {
440
410
// connected…
@@ -445,10 +415,7 @@ Similarly, you can also combine this with [authentication](#authentication)
445
415
like this:
446
416
447
417
``` 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');
452
419
```
453
420
454
421
> Note that Unix domain sockets (UDS) are considered advanced usage and PHP only
0 commit comments