@@ -72,16 +72,19 @@ secure HTTPS request to google.com through a local HTTP proxy server:
72
72
``` php
73
73
$loop = React\EventLoop\Factory::create();
74
74
75
- $proxy = new ProxyConnector('127.0.0.1:8080', new Connector($loop));
76
- $connector = new Connector($loop, array(
75
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
76
+ '127.0.0.1:8080',
77
+ new React\Socket\Connector($loop)
78
+ );
79
+ $connector = new React\Socket\Connector($loop, array(
77
80
'tcp' => $proxy,
78
81
'timeout' => 3.0,
79
82
'dns' => false
80
83
));
81
84
82
- $connector->connect('tls://google.com:443')->then(function (ConnectionInterface $stream ) {
83
- $stream ->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n");
84
- $stream ->on('data', function ($chunk) {
85
+ $connector->connect('tls://google.com:443')->then(function (React\Socket\ ConnectionInterface $connection ) {
86
+ $connection ->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n");
87
+ $connection ->on('data', function ($chunk) {
85
88
echo $chunk;
86
89
});
87
90
}, 'printf');
@@ -106,8 +109,8 @@ Its constructor simply accepts an HTTP proxy URL and a connector used to connect
106
109
to the proxy server address:
107
110
108
111
``` php
109
- $connector = new Connector($loop);
110
- $proxy = new ProxyConnector('http://127.0.0.1:8080', $connector);
112
+ $connector = new React\Socket\ Connector($loop);
113
+ $proxy = new Clue\React\HttpProxy\ ProxyConnector('http://127.0.0.1:8080', $connector);
111
114
```
112
115
113
116
The proxy URL may or may not contain a scheme and port definition. The default
@@ -133,9 +136,9 @@ This makes it fairly simple to add HTTP CONNECT proxy support to pretty much any
133
136
higher-level component:
134
137
135
138
``` diff
136
- - $client = new SomeClient ($connector);
137
- + $proxy = new ProxyConnector('http://127.0.0.1:8080', $connector);
138
- + $client = new SomeClient ($proxy);
139
+ - $acme = new AcmeApi ($connector);
140
+ + $proxy = new Clue\React\HttpProxy\ ProxyConnector('http://127.0.0.1:8080', $connector);
141
+ + $acme = new AcmeApi ($proxy);
139
142
```
140
143
141
144
#### Plain TCP connections
@@ -147,11 +150,14 @@ As documented above, you can simply invoke its `connect()` method to establish
147
150
a streaming plain TCP/IP connection and use any higher level protocol like so:
148
151
149
152
``` php
150
- $proxy = new ProxyConnector('http://127.0.0.1:8080', $connector);
153
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
154
+ '127.0.0.1:8080',
155
+ new React\Socket\Connector($loop)
156
+ );
151
157
152
- $proxy->connect('tcp://smtp.googlemail.com:587')->then(function (ConnectionInterface $stream ) {
153
- $stream ->write("EHLO local\r\n");
154
- $stream ->on('data', function ($chunk) use ($stream ) {
158
+ $proxy->connect('tcp://smtp.googlemail.com:587')->then(function (React\Socket\ ConnectionInterface $connection ) {
159
+ $connection ->write("EHLO local\r\n");
160
+ $connection ->on('data', function ($chunk) use ($connection ) {
155
161
echo $chunk;
156
162
});
157
163
});
@@ -161,14 +167,19 @@ You can either use the `ProxyConnector` directly or you may want to wrap this co
161
167
in ReactPHP's [ ` Connector ` ] ( https://github.com/reactphp/socket#connector ) :
162
168
163
169
``` php
164
- $connector = new Connector($loop, array(
170
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
171
+ '127.0.0.1:8080',
172
+ new React\Socket\Connector($loop)
173
+ );
174
+
175
+ $connector = new React\Socket\Connector($loop, array(
165
176
'tcp' => $proxy,
166
177
'dns' => false
167
178
));
168
179
169
- $connector->connect('tcp://smtp.googlemail.com:587')->then(function (ConnectionInterface $stream ) {
170
- $stream ->write("EHLO local\r\n");
171
- $stream ->on('data', function ($chunk) use ($stream ) {
180
+ $connector->connect('tcp://smtp.googlemail.com:587')->then(function (React\Socket\ ConnectionInterface $connection ) {
181
+ $connection ->write("EHLO local\r\n");
182
+ $connection ->on('data', function ($chunk) use ($connection ) {
172
183
echo $chunk;
173
184
});
174
185
});
@@ -186,15 +197,19 @@ ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector) or the
186
197
low-level [ ` SecureConnector ` ] ( https://github.com/reactphp/socket#secureconnector ) :
187
198
188
199
``` php
189
- $proxy = new ProxyConnector('http://127.0.0.1:8080', $connector);
190
- $connector = new Connector($loop, array(
200
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
201
+ '127.0.0.1:8080',
202
+ new React\Socket\Connector($loop)
203
+ );
204
+
205
+ $connector = new React\Socket\Connector($loop, array(
191
206
'tcp' => $proxy,
192
207
'dns' => false
193
208
));
194
209
195
- $connector->connect('tls://smtp.googlemail.com:465')->then(function (ConnectionInterface $stream ) {
196
- $stream ->write("EHLO local\r\n");
197
- $stream ->on('data', function ($chunk) use ($stream ) {
210
+ $connector->connect('tls://smtp.googlemail.com:465')->then(function (React\Socket\ ConnectionInterface $connection ) {
211
+ $connection ->write("EHLO local\r\n");
212
+ $connection ->on('data', function ($chunk) use ($connection ) {
198
213
echo $chunk;
199
214
});
200
215
});
@@ -213,7 +228,7 @@ This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like th
213
228
214
229
``` php
215
230
$proxy = new Clue\React\HttpProxy\ProxyConnector(
216
- 'http:// 127.0.0.1:8080',
231
+ '127.0.0.1:8080',
217
232
new React\Socket\Connector($loop)
218
233
);
219
234
@@ -252,13 +267,18 @@ It provides the same `connect()` method, but will automatically reject the
252
267
underlying connection attempt if it takes too long:
253
268
254
269
``` php
255
- $connector = new Connector($loop, array(
270
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
271
+ '127.0.0.1:8080',
272
+ new React\Socket\Connector($loop)
273
+ );
274
+
275
+ $connector = new React\Socket\Connector($loop, array(
256
276
'tcp' => $proxy,
257
277
'dns' => false,
258
278
'timeout' => 3.0
259
279
));
260
280
261
- $connector->connect('tcp://google.com:80')->then(function ($stream ) {
281
+ $connector->connect('tcp://google.com:80')->then(function ($connection ) {
262
282
// connection succeeded within 3.0 seconds
263
283
});
264
284
```
@@ -294,7 +314,12 @@ Given that remote DNS resolution is assumed to be the preferred mode, all
294
314
other examples explicitly disable DNS resolution like this:
295
315
296
316
``` php
297
- $connector = new Connector($loop, array(
317
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
318
+ '127.0.0.1:8080',
319
+ new React\Socket\Connector($loop)
320
+ );
321
+
322
+ $connector = new React\Socket\Connector($loop, array(
298
323
'tcp' => $proxy,
299
324
'dns' => false
300
325
));
@@ -303,8 +328,13 @@ $connector = new Connector($loop, array(
303
328
If you want to explicitly use * local DNS resolution* , you can use the following code:
304
329
305
330
``` php
331
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
332
+ '127.0.0.1:8080',
333
+ new React\Socket\Connector($loop)
334
+ );
335
+
306
336
// set up Connector which uses Google's public DNS (8.8.8.8)
307
- $connector = new Connector($loop, array(
337
+ $connector = new React\Socket\ Connector($loop, array(
308
338
'tcp' => $proxy,
309
339
'dns' => '8.8.8.8'
310
340
));
@@ -319,7 +349,10 @@ If your HTTP proxy server requires authentication, you may pass the username and
319
349
password as part of the HTTP proxy URL like this:
320
350
321
351
``` php
322
- $proxy = new ProxyConnector('http://user:pass@127.0.0.1:8080', $connector);
352
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
353
+ 'http://user:pass@127.0.0.1:8080',
354
+ new React\Socket\Connector($loop)
355
+ );
323
356
```
324
357
325
358
Note that both the username and password must be percent-encoded if they contain
@@ -329,7 +362,7 @@ special characters:
329
362
$user = 'he:llo';
330
363
$pass = 'p@ss';
331
364
332
- $proxy = new ProxyConnector(
365
+ $proxy = new Clue\React\HttpProxy\ ProxyConnector(
333
366
rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1:8080',
334
367
$connector
335
368
);
@@ -353,10 +386,14 @@ in practice, but may be useful for some more advanced use cases. In this case,
353
386
you may simply pass an assoc array of additional request headers like this:
354
387
355
388
``` php
356
- $proxy = new ProxyConnector('127.0.0.1:8080', $connector, array(
357
- 'Proxy-Authorization' => 'Bearer abc123',
358
- 'User-Agent' => 'ReactPHP'
359
- ));
389
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
390
+ '127.0.0.1:8080',
391
+ $connector,
392
+ array(
393
+ 'Proxy-Authorization' => 'Bearer abc123',
394
+ 'User-Agent' => 'ReactPHP'
395
+ )
396
+ );
360
397
```
361
398
362
399
#### Advanced secure proxy connections
@@ -373,8 +410,10 @@ If you want to connect to a (rather rare) HTTPS proxy, you may want use the
373
410
instance to create a secure connection to the proxy:
374
411
375
412
``` php
376
- $connector = new Connector($loop);
377
- $proxy = new ProxyConnector('https://127.0.0.1:443', $connector);
413
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
414
+ 'https://127.0.0.1:443',
415
+ new React\Socket\Connector($loop)
416
+ );
378
417
379
418
$proxy->connect('tcp://smtp.googlemail.com:587');
380
419
```
@@ -391,9 +430,12 @@ having to rely on explicit [authentication](#authentication).
391
430
You can simply use the ` http+unix:// ` URI scheme like this:
392
431
393
432
``` php
394
- $proxy = new ProxyConnector('http+unix:///tmp/proxy.sock', $connector);
433
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
434
+ 'http+unix:///tmp/proxy.sock',
435
+ new React\Socket\Connector($loop)
436
+ );
395
437
396
- $proxy->connect('tcp://google.com:80')->then(function (ConnectionInterface $stream ) {
438
+ $proxy->connect('tcp://google.com:80')->then(function (React\Socket\ ConnectionInterface $connection ) {
397
439
// connected…
398
440
});
399
441
```
@@ -402,7 +444,10 @@ Similarly, you can also combine this with [authentication](#authentication)
402
444
like this:
403
445
404
446
``` php
405
- $proxy = new ProxyConnector('http+unix://user:pass@/tmp/proxy.sock', $connector);
447
+ $proxy = new Clue\React\HttpProxy\ProxyConnector(
448
+ 'http+unix://user:pass@/tmp/proxy.sock',
449
+ new React\Socket\Connector($loop)
450
+ );
406
451
```
407
452
408
453
> Note that Unix domain sockets (UDS) are considered advanced usage and PHP only
0 commit comments