From f8fa56fe55bf50da96d77559cf89db1aec3c6dac Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Thu, 30 Jul 2020 12:38:52 +0200 Subject: [PATCH 1/3] Update to reactphp/http v1.0.0 --- README.md | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c990f5e..fc1d15d 100644 --- a/README.md +++ b/README.md @@ -203,12 +203,29 @@ $connector->connect('tls://smtp.googlemail.com:465')->then(function (ConnectionI #### HTTP requests -HTTP operates on a higher layer than this low-level HTTP CONNECT implementation. -If you want to issue HTTP requests, you can add a dependency for -[clue/reactphp-buzz](https://github.com/clue/reactphp-buzz). -It can interact with this library by issuing all -[HTTP requests through a HTTP CONNECT proxy server](https://github.com/clue/reactphp-buzz#http-proxy). -This works for both plain HTTP and TLS-encrypted HTTPS requests. +This library also allows you to send HTTP requests through an HTTP CONNECT proxy server. + +In order to send HTTP requests, you first have to add a dependency for [ReactPHP's async HTTP client](https://github.com/reactphp/http#client-usage). This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like this: + +```php +$proxy = new Clue\React\HttpProxy\ProxyConnector( + 'http://127.0.0.1:8080', + new React\Socket\Connector($loop) +); + +$connector = new React\Socket\Connector($loop, array( + 'tcp' => $proxy, + 'dns' => false +)); + +$browser = new React\Http\Browser($loop, $connector); + +$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { + var_dump($response->getHeaders(), (string) $response->getBody()); +}); +``` + +See also [ReactPHP's HTTP client](https://github.com/reactphp/http#client-usage) for more details. #### Connection timeout From 903ac34cc75302d94e68bd07c778b17419ebfcef Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Tue, 4 Aug 2020 15:29:06 +0200 Subject: [PATCH 2/3] Add improved HTTP examples --- README.md | 2 +- composer.json | 1 + examples/01-http-request.php | 33 ++++++++++++++++++ examples/02-optional-proxy-http-request.php | 34 +++++++++++++++++++ ...ps.php => 11-proxy-raw-https-protocol.php} | 10 +++++- ... 12-optional-proxy-raw-https-protocol.php} | 6 +++- ...eaders.php => 13-custom-proxy-headers.php} | 10 +++++- ...mtp.php => 21-proxy-raw-smtp-protocol.php} | 12 +++++-- ...ps.php => 22-proxy-raw-smtps-protocol.php} | 12 +++++-- 9 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 examples/01-http-request.php create mode 100644 examples/02-optional-proxy-http-request.php rename examples/{01-proxy-https.php => 11-proxy-raw-https-protocol.php} (73%) rename examples/{02-optional-proxy-https.php => 12-optional-proxy-raw-https-protocol.php} (85%) rename examples/{03-custom-proxy-headers.php => 13-custom-proxy-headers.php} (75%) rename examples/{11-proxy-smtp.php => 21-proxy-raw-smtp-protocol.php} (65%) rename examples/{12-proxy-smtps.php => 22-proxy-raw-smtps-protocol.php} (70%) diff --git a/README.md b/README.md index fc1d15d..1497557 100644 --- a/README.md +++ b/README.md @@ -225,7 +225,7 @@ $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseI }); ``` -See also [ReactPHP's HTTP client](https://github.com/reactphp/http#client-usage) for more details. +See also [ReactPHP's HTTP client](https://github.com/reactphp/http#client-usage) and any of the [examples](examples) for more details. #### Connection timeout diff --git a/composer.json b/composer.json index bfdf556..0a3ce33 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "require-dev": { "phpunit/phpunit": "^9.0 || ^7.0 || ^5.0 || ^4.8", "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", + "react/http": "^1.0", "clue/block-react": "^1.1" } } diff --git a/examples/01-http-request.php b/examples/01-http-request.php new file mode 100644 index 0000000..a7cedec --- /dev/null +++ b/examples/01-http-request.php @@ -0,0 +1,33 @@ + $proxy, + 'dns' => false +)); + +$browser = new React\Http\Browser($loop, $connector); + +$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { + var_dump($response->getHeaders(), (string) $response->getBody()); +}, 'printf'); + +$loop->run(); diff --git a/examples/02-optional-proxy-http-request.php b/examples/02-optional-proxy-http-request.php new file mode 100644 index 0000000..2d26c12 --- /dev/null +++ b/examples/02-optional-proxy-http-request.php @@ -0,0 +1,34 @@ + $proxy, + 'timeout' => 3.0, + 'dns' => false + )); +} + +$browser = new React\Http\Browser($loop, $connector); + +$browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { + var_dump($response->getHeaders(), (string) $response->getBody()); +}, 'printf'); + +$loop->run(); diff --git a/examples/01-proxy-https.php b/examples/11-proxy-raw-https-protocol.php similarity index 73% rename from examples/01-proxy-https.php rename to examples/11-proxy-raw-https-protocol.php index c91f31d..36969dd 100644 --- a/examples/01-proxy-https.php +++ b/examples/11-proxy-raw-https-protocol.php @@ -1,10 +1,18 @@ Date: Thu, 3 Sep 2020 15:14:39 +0200 Subject: [PATCH 3/3] Clean up examples --- examples/01-http-request.php | 15 +++++++++----- examples/02-optional-proxy-http-request.php | 6 +++++- examples/11-proxy-raw-https-protocol.php | 14 +++++++++---- .../12-optional-proxy-raw-https-protocol.php | 20 ++++++++++++------- examples/13-custom-proxy-headers.php | 14 +++++++++---- examples/21-proxy-raw-smtp-protocol.php | 13 +++++++++--- examples/22-proxy-raw-smtps-protocol.php | 13 +++++++++--- 7 files changed, 68 insertions(+), 27 deletions(-) diff --git a/examples/01-http-request.php b/examples/01-http-request.php index a7cedec..66bd786 100644 --- a/examples/01-http-request.php +++ b/examples/01-http-request.php @@ -3,18 +3,23 @@ // A simple example which uses an HTTP client to request https://example.com/ through an HTTP CONNECT proxy. // You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this: // -// $ php leproxy-latest.php +// $ php leproxy.php // -// To run the example, go to the project root and run: +// The proxy defaults to localhost:8080. +// To run the example go to the project root and run: // // $ php examples/01-http-requests.php // -// The proxy can be given as first argument and defaults to localhost:8080 otherwise. -use React\HTTP\Browser; +// To run the same example with your proxy, the proxy URL can be given as an environment variable: +// +// $ http_proxy=127.0.0.2:8080 php examples/01-http-requests.php require __DIR__ . '/../vendor/autoload.php'; -$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:8080'; +$url = getenv('http_proxy'); +if ($url === false) { + $url = 'localhost:8080'; +} $loop = React\EventLoop\Factory::create(); $proxy = new Clue\React\HttpProxy\ProxyConnector($url, new React\Socket\Connector($loop)); diff --git a/examples/02-optional-proxy-http-request.php b/examples/02-optional-proxy-http-request.php index 2d26c12..170ea7a 100644 --- a/examples/02-optional-proxy-http-request.php +++ b/examples/02-optional-proxy-http-request.php @@ -5,9 +5,13 @@ // // $ php examples/02-optional-proxy-http-request.php // +// If you chose the optional route, you can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this: +// +// $ php leproxy.php +// // To run the same example with your proxy, the proxy URL can be given as an environment variable: // -// $ http_proxy=127.0.0.1.8181 php examples/02-optional-proxy-http-request.php +// $ http_proxy=127.0.0.2:8080 php examples/02-optional-proxy-http-request.php require __DIR__ . '/../vendor/autoload.php'; diff --git a/examples/11-proxy-raw-https-protocol.php b/examples/11-proxy-raw-https-protocol.php index 36969dd..873b39b 100644 --- a/examples/11-proxy-raw-https-protocol.php +++ b/examples/11-proxy-raw-https-protocol.php @@ -3,16 +3,19 @@ // A simple example which requests https://google.com/ through an HTTP CONNECT proxy. // You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this: // -// $ php leproxy-latest.php +// $ php leproxy.php // +// The proxy defaults to localhost:8080. // To run the example, go to the project root and run: // // $ php examples/11-proxy-raw-https-protocol.php // -// The proxy can be given as first argument and defaults to localhost:8080 otherwise. +// To run the same example with your proxy, the proxy URL can be given as an environment variable: +// +// $ http_proxy=127.0.0.2:8080 php examples/11-proxy-raw-https-protocol.php // // For illustration purposes only. If you want to send HTTP requests in a real -// world project, take a look at example #01 and https://github.com/reactphp/http#client-usage. +// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage. use Clue\React\HttpProxy\ProxyConnector; use React\Socket\Connector; @@ -20,7 +23,10 @@ require __DIR__ . '/../vendor/autoload.php'; -$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:8080'; +$url = getenv('http_proxy'); +if ($url === false) { + $url = 'localhost:8080'; +} $loop = React\EventLoop\Factory::create(); diff --git a/examples/12-optional-proxy-raw-https-protocol.php b/examples/12-optional-proxy-raw-https-protocol.php index 690b68d..ed206fe 100644 --- a/examples/12-optional-proxy-raw-https-protocol.php +++ b/examples/12-optional-proxy-raw-https-protocol.php @@ -1,18 +1,24 @@ $proxy, 'timeout' => 3.0, diff --git a/examples/13-custom-proxy-headers.php b/examples/13-custom-proxy-headers.php index 7c0b866..09163af 100644 --- a/examples/13-custom-proxy-headers.php +++ b/examples/13-custom-proxy-headers.php @@ -3,16 +3,19 @@ // A simple example which requests https://google.com/ through an HTTP CONNECT proxy. // You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this: // -// $ php leproxy-latest.php +// $ php leproxy.php // +// The proxy defaults to localhost:8080. // To run the example, go to the project root and run: // // $ php examples/13-custom-proxy-headers.php // -// The proxy can be given as first argument and defaults to localhost:8080 otherwise. +// To run the same example with your proxy, the proxy URL can be given as an environment variable: +// +// $ http_proxy=127.0.0.2:8080 php examples/13-custom-proxy-headers.php // // For illustration purposes only. If you want to send HTTP requests in a real -// world project, take a look at example #01 and https://github.com/reactphp/http#client-usage. +// world project, take a look at example #01, example #02 and https://github.com/reactphp/http#client-usage. use Clue\React\HttpProxy\ProxyConnector; use React\Socket\Connector; @@ -20,7 +23,10 @@ require __DIR__ . '/../vendor/autoload.php'; -$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:8080'; +$url = getenv('http_proxy'); +if ($url === false) { + $url = 'localhost:8080'; +} $loop = React\EventLoop\Factory::create(); diff --git a/examples/21-proxy-raw-smtp-protocol.php b/examples/21-proxy-raw-smtp-protocol.php index de407a0..bab1605 100644 --- a/examples/21-proxy-raw-smtp-protocol.php +++ b/examples/21-proxy-raw-smtp-protocol.php @@ -3,13 +3,17 @@ // A simple example which requests https://google.com/ through an HTTP CONNECT proxy. // You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this: // -// $ php leproxy-latest.php +// $ php leproxy.php // +// The proxy defaults to localhost:8080. // To run the example, go to the project root and run: // // $ php examples/21-proxy-raw-smtp-protocol.php // -// The proxy can be given as first argument and defaults to localhost:8080 otherwise. +// To run the same example with your proxy, the proxy URL can be given as an environment variable: +// +// $ http_proxy=127.0.0.2:8080 php examples/21-proxy-raw-smtp-protocol.php +// // Please note that MANY public proxies do not allow SMTP connections, YMMV. use Clue\React\HttpProxy\ProxyConnector; @@ -18,7 +22,10 @@ require __DIR__ . '/../vendor/autoload.php'; -$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:8080'; +$url = getenv('http_proxy'); +if ($url === false) { + $url = 'localhost:8080'; +} $loop = React\EventLoop\Factory::create(); diff --git a/examples/22-proxy-raw-smtps-protocol.php b/examples/22-proxy-raw-smtps-protocol.php index f889517..82aa2cf 100644 --- a/examples/22-proxy-raw-smtps-protocol.php +++ b/examples/22-proxy-raw-smtps-protocol.php @@ -3,13 +3,17 @@ // A simple example which requests https://google.com/ through an HTTP CONNECT proxy. // You can use any kind of proxy, for example https://github.com/leproxy/leproxy and execute it like this: // -// $ php leproxy-latest.php +// $ php leproxy.php // +// The proxy defaults to localhost:8080. // To run the example, go to the project root and run: // // $ php examples/22-proxy-raw-smtps-protocol.php // -// The proxy can be given as first argument and defaults to localhost:8080 otherwise. +// To run the same example with your proxy, the proxy URL can be given as an environment variable: +// +// $ http_proxy=127.0.0.2:8080 php examples/22-proxy-raw-smtps-protocol.php +// // This example highlights how changing from plain connections (see previous // example) to using a secure connection actually adds very little complexity // and does not mess with your actual network protocol otherwise. @@ -21,7 +25,10 @@ require __DIR__ . '/../vendor/autoload.php'; -$url = isset($argv[1]) ? $argv[1] : '127.0.0.1:8080'; +$url = getenv('http_proxy'); +if ($url === false) { + $url = 'localhost:8080'; +} $loop = React\EventLoop\Factory::create();