From ce2a9b5d3b1339756baa1159ad51cd431fcaa307 Mon Sep 17 00:00:00 2001 From: Paul Date: Fri, 2 Jul 2021 16:53:08 +0200 Subject: [PATCH 01/27] Add badge to show number of project installations --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 44bcf69..8e7b7ea 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # clue/reactphp-http-proxy [![CI status](https://github.com/clue/reactphp-http-proxy/workflows/CI/badge.svg)](https://github.com/clue/reactphp-http-proxy/actions) +[![installs on Packagist](https://img.shields.io/packagist/dt/clue/http-proxy-react?color=blue&label=installs%20on%20Packagist)](https://packagist.org/packages/clue/http-proxy-react) Async HTTP proxy connector, tunnel any TCP/IP-based protocol through an HTTP CONNECT proxy server, built on top of [ReactPHP](https://reactphp.org). From b4682b807a0cd655a904b2689c7ae57858d04bc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 15 Jul 2021 16:16:32 +0200 Subject: [PATCH 02/27] Simplify examples by updating to new default loop --- README.md | 51 +++++++++---------- composer.json | 6 +-- examples/01-http-request.php | 9 ++-- examples/02-optional-proxy-http-request.php | 11 ++-- examples/11-proxy-raw-https-protocol.php | 8 +-- .../12-optional-proxy-raw-https-protocol.php | 9 ++-- examples/13-custom-proxy-headers.php | 8 +-- examples/21-proxy-raw-smtp-protocol.php | 8 +-- examples/22-proxy-raw-smtps-protocol.php | 8 +-- 9 files changed, 46 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index 8e7b7ea..9744480 100644 --- a/README.md +++ b/README.md @@ -71,13 +71,12 @@ The following example code demonstrates how this library can be used to send a secure HTTPS request to google.com through a local HTTP proxy server: ```php -$loop = React\EventLoop\Factory::create(); - $proxy = new Clue\React\HttpProxy\ProxyConnector( '127.0.0.1:8080', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); -$connector = new React\Socket\Connector($loop, array( + +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false @@ -89,8 +88,6 @@ $connector->connect('tls://google.com:443')->then(function (React\Socket\Connect echo $chunk; }); }, 'printf'); - -$loop->run(); ``` See also the [examples](examples). @@ -110,8 +107,10 @@ Its constructor simply accepts an HTTP proxy URL and a connector used to connect to the proxy server address: ```php -$connector = new React\Socket\Connector($loop); -$proxy = new Clue\React\HttpProxy\ProxyConnector('http://127.0.0.1:8080', $connector); +$proxy = new Clue\React\HttpProxy\ProxyConnector( + 'http://127.0.0.1:8080', + new React\Socket\Connector() +); ``` The proxy URL may or may not contain a scheme and port definition. The default @@ -153,7 +152,7 @@ a streaming plain TCP/IP connection and use any higher level protocol like so: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( '127.0.0.1:8080', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); $proxy->connect('tcp://smtp.googlemail.com:587')->then(function (React\Socket\ConnectionInterface $connection) { @@ -170,10 +169,10 @@ in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector): ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( '127.0.0.1:8080', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'dns' => false )); @@ -200,10 +199,10 @@ low-level [`SecureConnector`](https://github.com/reactphp/socket#secureconnector ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( '127.0.0.1:8080', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'dns' => false )); @@ -230,15 +229,15 @@ This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like th ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( '127.0.0.1:8080', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'dns' => false )); -$browser = new React\Http\Browser($loop, $connector); +$browser = new React\Http\Browser(null, $connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); @@ -270,10 +269,10 @@ underlying connection attempt if it takes too long: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( '127.0.0.1:8080', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'dns' => false, 'timeout' => 3.0 @@ -317,10 +316,10 @@ other examples explicitly disable DNS resolution like this: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( '127.0.0.1:8080', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'dns' => false )); @@ -331,11 +330,11 @@ If you want to explicitly use *local DNS resolution*, you can use the following ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( '127.0.0.1:8080', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); // set up Connector which uses Google's public DNS (8.8.8.8) -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'dns' => '8.8.8.8' )); @@ -352,7 +351,7 @@ password as part of the HTTP proxy URL like this: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( 'http://user:pass@127.0.0.1:8080', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); ``` @@ -413,7 +412,7 @@ instance to create a secure connection to the proxy: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( 'https://127.0.0.1:443', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); $proxy->connect('tcp://smtp.googlemail.com:587'); @@ -433,7 +432,7 @@ You can simply use the `http+unix://` URI scheme like this: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( 'http+unix:///tmp/proxy.sock', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); $proxy->connect('tcp://google.com:80')->then(function (React\Socket\ConnectionInterface $connection) { @@ -447,7 +446,7 @@ like this: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( 'http+unix://user:pass@/tmp/proxy.sock', - new React\Socket\Connector($loop) + new React\Socket\Connector() ); ``` diff --git a/composer.json b/composer.json index e34a14c..6e427a9 100644 --- a/composer.json +++ b/composer.json @@ -19,13 +19,13 @@ "require": { "php": ">=5.3", "react/promise": " ^2.1 || ^1.2.1", - "react/socket": "^1.1", + "react/socket": "^1.8", "ringcentral/psr7": "^1.2" }, "require-dev": { "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8", - "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3", - "react/http": "^1.0", + "react/event-loop": "^1.2", + "react/http": "^1.4", "clue/block-react": "^1.1" } } diff --git a/examples/01-http-request.php b/examples/01-http-request.php index 4b192ea..d98ce5d 100644 --- a/examples/01-http-request.php +++ b/examples/01-http-request.php @@ -21,23 +21,20 @@ $url = 'localhost:8080'; } -$loop = React\EventLoop\Factory::create(); $proxy = new Clue\React\HttpProxy\ProxyConnector( $url, - new React\Socket\Connector($loop) + new React\Socket\Connector() ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'dns' => false )); -$browser = new React\Http\Browser($loop, $connector); +$browser = new React\Http\Browser(null, $connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/02-optional-proxy-http-request.php b/examples/02-optional-proxy-http-request.php index 1c8005d..0e7beb6 100644 --- a/examples/02-optional-proxy-http-request.php +++ b/examples/02-optional-proxy-http-request.php @@ -15,28 +15,25 @@ require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); - $connector = null; $url = getenv('http_proxy'); if ($url !== false) { $proxy = new Clue\React\HttpProxy\ProxyConnector( $url, - new React\Socket\Connector($loop) + new React\Socket\Connector() ); - $connector = new React\Socket\Connector($loop, array( + + $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false )); } -$browser = new React\Http\Browser($loop, $connector); +$browser = new React\Http\Browser(null, $connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/11-proxy-raw-https-protocol.php b/examples/11-proxy-raw-https-protocol.php index d3c1f2b..200b51e 100644 --- a/examples/11-proxy-raw-https-protocol.php +++ b/examples/11-proxy-raw-https-protocol.php @@ -24,14 +24,12 @@ $url = 'localhost:8080'; } -$loop = React\EventLoop\Factory::create(); - $proxy = new Clue\React\HttpProxy\ProxyConnector( $url, - new React\Socket\Connector($loop) + new React\Socket\Connector() ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false @@ -45,5 +43,3 @@ }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/12-optional-proxy-raw-https-protocol.php b/examples/12-optional-proxy-raw-https-protocol.php index 9d3e919..9dce1db 100644 --- a/examples/12-optional-proxy-raw-https-protocol.php +++ b/examples/12-optional-proxy-raw-https-protocol.php @@ -22,9 +22,7 @@ require __DIR__ . '/../vendor/autoload.php'; -$loop = React\EventLoop\Factory::create(); - -$connector = new React\Socket\Connector($loop); +$connector = new React\Socket\Connector(); $url = getenv('http_proxy'); if ($url !== false) { @@ -32,7 +30,8 @@ $url, $connector ); - $connector = new React\Socket\Connector($loop, array( + + $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false @@ -47,5 +46,3 @@ }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/13-custom-proxy-headers.php b/examples/13-custom-proxy-headers.php index 2612ee1..2d1463f 100644 --- a/examples/13-custom-proxy-headers.php +++ b/examples/13-custom-proxy-headers.php @@ -24,18 +24,16 @@ $url = 'localhost:8080'; } -$loop = React\EventLoop\Factory::create(); - $proxy = new Clue\React\HttpProxy\ProxyConnector( $url, - new React\Socket\Connector($loop), + new React\Socket\Connector(), array( 'X-Custom-Header-1' => 'Value-1', 'X-Custom-Header-2' => 'Value-2', ) ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false, @@ -49,5 +47,3 @@ }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/21-proxy-raw-smtp-protocol.php b/examples/21-proxy-raw-smtp-protocol.php index f66d253..5613b40 100644 --- a/examples/21-proxy-raw-smtp-protocol.php +++ b/examples/21-proxy-raw-smtp-protocol.php @@ -23,14 +23,12 @@ $url = 'localhost:8080'; } -$loop = React\EventLoop\Factory::create(); - $proxy = new Clue\React\HttpProxy\ProxyConnector( $url, - new React\Socket\Connector($loop) + new React\Socket\Connector() ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false @@ -45,5 +43,3 @@ }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); diff --git a/examples/22-proxy-raw-smtps-protocol.php b/examples/22-proxy-raw-smtps-protocol.php index 796f59b..8338b34 100644 --- a/examples/22-proxy-raw-smtps-protocol.php +++ b/examples/22-proxy-raw-smtps-protocol.php @@ -26,14 +26,12 @@ $url = 'localhost:8080'; } -$loop = React\EventLoop\Factory::create(); - $proxy = new Clue\React\HttpProxy\ProxyConnector( $url, - new React\Socket\Connector($loop) + new React\Socket\Connector() ); -$connector = new React\Socket\Connector($loop, array( +$connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false @@ -48,5 +46,3 @@ }, function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); - -$loop->run(); From 60fb9671f7fbedfb799e6c0d7c34e6876f8a584c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 20 Jul 2021 11:18:28 +0200 Subject: [PATCH 03/27] Simplify usage by making `Connector` optional --- README.md | 110 +++++++----------- examples/01-http-request.php | 5 +- examples/02-optional-proxy-http-request.php | 5 +- examples/11-proxy-raw-https-protocol.php | 5 +- .../12-optional-proxy-raw-https-protocol.php | 9 +- examples/13-custom-proxy-headers.php | 2 +- examples/21-proxy-raw-smtp-protocol.php | 5 +- examples/22-proxy-raw-smtps-protocol.php | 5 +- src/ProxyConnector.php | 12 +- tests/FunctionalTest.php | 30 ++--- tests/ProxyConnectorTest.php | 11 ++ 11 files changed, 75 insertions(+), 124 deletions(-) diff --git a/README.md b/README.md index 9744480..ac9eb75 100644 --- a/README.md +++ b/README.md @@ -71,10 +71,7 @@ The following example code demonstrates how this library can be used to send a secure HTTPS request to google.com through a local HTTP proxy server: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, @@ -103,22 +100,34 @@ any destination by using an intermediary HTTP CONNECT proxy. [you] -> [proxy] -> [destination] ``` -Its constructor simply accepts an HTTP proxy URL and a connector used to connect -to the proxy server address: +Its constructor simply accepts an HTTP proxy URL with the proxy server address: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - 'http://127.0.0.1:8080', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); ``` The proxy URL may or may not contain a scheme and port definition. The default port will be `80` for HTTP (or `443` for HTTPS), but many common HTTP proxy servers use custom ports (often the alternative HTTP port `8080`). -In its most simple form, the given connector will be a -[`\React\Socket\Connector`](https://github.com/reactphp/socket#connector) if you -want to connect to a given IP address as above. + +If you need custom connector settings (DNS resolution, TLS parameters, timeouts, +proxy servers etc.), you can explicitly pass a custom instance of the +[`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface): + +```php +$connector = new React\Socket\Connector(null, array( + 'dns' => '127.0.0.1', + 'tcp' => array( + 'bindto' => '192.168.10.1:0' + ), + 'tls' => array( + 'verify_peer' => false, + 'verify_peer_name' => false + ) +)); + +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080', $connector); +``` This is the main class in this package. Because it implements ReactPHP's standard @@ -137,7 +146,7 @@ higher-level component: ```diff - $acme = new AcmeApi($connector); -+ $proxy = new Clue\React\HttpProxy\ProxyConnector('http://127.0.0.1:8080', $connector); ++ $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080', $connector); + $acme = new AcmeApi($proxy); ``` @@ -150,10 +159,7 @@ As documented above, you can simply invoke its `connect()` method to establish a streaming plain TCP/IP connection and use any higher level protocol like so: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); $proxy->connect('tcp://smtp.googlemail.com:587')->then(function (React\Socket\ConnectionInterface $connection) { $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 in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector): ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, @@ -193,14 +196,10 @@ Many (public) proxy servers do in fact limit this to HTTPS (443) only. This class can also be used if you want to establish a secure TLS connection (formerly known as SSL) between you and your destination, such as when using secure HTTPS to your destination site. You can simply wrap this connector in -ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector) or the -low-level [`SecureConnector`](https://github.com/reactphp/socket#secureconnector): +ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector): ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, @@ -227,10 +226,7 @@ In order to send HTTP requests, you first have to add a dependency for This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, @@ -260,17 +256,12 @@ Many use cases require more control over the timeout and likely values much smaller, usually in the range of a few seconds only. You can use ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector) -or the low-level -[`TimeoutConnector`](https://github.com/reactphp/socket#timeoutconnector) to decorate any given `ConnectorInterface` instance. It provides the same `connect()` method, but will automatically reject the underlying connection attempt if it takes too long: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, @@ -314,10 +305,7 @@ Given that remote DNS resolution is assumed to be the preferred mode, all other examples explicitly disable DNS resolution like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, @@ -328,10 +316,7 @@ $connector = new React\Socket\Connector(null, array( If you want to explicitly use *local DNS resolution*, you can use the following code: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - '127.0.0.1:8080', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); // set up Connector which uses Google's public DNS (8.8.8.8) $connector = new React\Socket\Connector(null, array( @@ -349,10 +334,7 @@ If your HTTP proxy server requires authentication, you may pass the username and password as part of the HTTP proxy URL like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - 'http://user:pass@127.0.0.1:8080', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('user:pass@127.0.0.1:8080'); ``` Note that both the username and password must be percent-encoded if they contain @@ -361,11 +343,9 @@ special characters: ```php $user = 'he:llo'; $pass = 'p@ss'; +$url = rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1:8080'; -$proxy = new Clue\React\HttpProxy\ProxyConnector( - rawurlencode($user) . ':' . rawurlencode($pass) . '@127.0.0.1:8080', - $connector -); +$proxy = new Clue\React\HttpProxy\ProxyConnector($url); ``` > 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: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector( '127.0.0.1:8080', - $connector, + null, array( 'Proxy-Authorization' => 'Bearer abc123', 'User-Agent' => 'ReactPHP' @@ -404,16 +384,10 @@ setup, because you can still establish a TLS connection between you and the destination host as above. If you want to connect to a (rather rare) HTTPS proxy, you may want use the -`https://` scheme (HTTPS default port 443) and use ReactPHP's -[`Connector`](https://github.com/reactphp/socket#connector) or the low-level -[`SecureConnector`](https://github.com/reactphp/socket#secureconnector) -instance to create a secure connection to the proxy: +`https://` scheme (HTTPS default port 443) to create a secure connection to the proxy: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - 'https://127.0.0.1:443', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('https://127.0.0.1:443'); $proxy->connect('tcp://smtp.googlemail.com:587'); ``` @@ -430,10 +404,7 @@ having to rely on explicit [authentication](#authentication). You can simply use the `http+unix://` URI scheme like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - 'http+unix:///tmp/proxy.sock', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix:///tmp/proxy.sock'); $proxy->connect('tcp://google.com:80')->then(function (React\Socket\ConnectionInterface $connection) { // connected… @@ -444,10 +415,7 @@ Similarly, you can also combine this with [authentication](#authentication) like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector( - 'http+unix://user:pass@/tmp/proxy.sock', - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix://user:pass@/tmp/proxy.sock'); ``` > Note that Unix domain sockets (UDS) are considered advanced usage and PHP only diff --git a/examples/01-http-request.php b/examples/01-http-request.php index d98ce5d..a6bc6c0 100644 --- a/examples/01-http-request.php +++ b/examples/01-http-request.php @@ -21,10 +21,7 @@ $url = 'localhost:8080'; } -$proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector($url); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, diff --git a/examples/02-optional-proxy-http-request.php b/examples/02-optional-proxy-http-request.php index 0e7beb6..d018cea 100644 --- a/examples/02-optional-proxy-http-request.php +++ b/examples/02-optional-proxy-http-request.php @@ -18,10 +18,7 @@ $connector = null; $url = getenv('http_proxy'); if ($url !== false) { - $proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - new React\Socket\Connector() - ); + $proxy = new Clue\React\HttpProxy\ProxyConnector($url); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, diff --git a/examples/11-proxy-raw-https-protocol.php b/examples/11-proxy-raw-https-protocol.php index 200b51e..1c98cc2 100644 --- a/examples/11-proxy-raw-https-protocol.php +++ b/examples/11-proxy-raw-https-protocol.php @@ -24,10 +24,7 @@ $url = 'localhost:8080'; } -$proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector($url); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, diff --git a/examples/12-optional-proxy-raw-https-protocol.php b/examples/12-optional-proxy-raw-https-protocol.php index 9dce1db..873f963 100644 --- a/examples/12-optional-proxy-raw-https-protocol.php +++ b/examples/12-optional-proxy-raw-https-protocol.php @@ -22,20 +22,17 @@ require __DIR__ . '/../vendor/autoload.php'; -$connector = new React\Socket\Connector(); - $url = getenv('http_proxy'); if ($url !== false) { - $proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - $connector - ); + $proxy = new Clue\React\HttpProxy\ProxyConnector($url); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false )); +} else { + $connector = new React\Socket\Connector(); } $connector->connect('tls://google.com:443')->then(function (React\Socket\ConnectionInterface $connection) { diff --git a/examples/13-custom-proxy-headers.php b/examples/13-custom-proxy-headers.php index 2d1463f..5314765 100644 --- a/examples/13-custom-proxy-headers.php +++ b/examples/13-custom-proxy-headers.php @@ -26,7 +26,7 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector( $url, - new React\Socket\Connector(), + null, array( 'X-Custom-Header-1' => 'Value-1', 'X-Custom-Header-2' => 'Value-2', diff --git a/examples/21-proxy-raw-smtp-protocol.php b/examples/21-proxy-raw-smtp-protocol.php index 5613b40..4feeea9 100644 --- a/examples/21-proxy-raw-smtp-protocol.php +++ b/examples/21-proxy-raw-smtp-protocol.php @@ -23,10 +23,7 @@ $url = 'localhost:8080'; } -$proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector($url); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, diff --git a/examples/22-proxy-raw-smtps-protocol.php b/examples/22-proxy-raw-smtps-protocol.php index 8338b34..296390a 100644 --- a/examples/22-proxy-raw-smtps-protocol.php +++ b/examples/22-proxy-raw-smtps-protocol.php @@ -26,10 +26,7 @@ $url = 'localhost:8080'; } -$proxy = new Clue\React\HttpProxy\ProxyConnector( - $url, - new React\Socket\Connector() -); +$proxy = new Clue\React\HttpProxy\ProxyConnector($url); $connector = new React\Socket\Connector(null, array( 'tcp' => $proxy, diff --git a/src/ProxyConnector.php b/src/ProxyConnector.php index 020e12a..0bca17e 100644 --- a/src/ProxyConnector.php +++ b/src/ProxyConnector.php @@ -9,8 +9,10 @@ use React\Promise; use React\Promise\Deferred; use React\Socket\ConnectionInterface; +use React\Socket\Connector; use React\Socket\ConnectorInterface; use React\Socket\FixedUriConnector; +use React\Socket\UnixConnector; /** * 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 * @param string $proxyUrl The proxy URL may or may not contain a scheme and * port definition. The default port will be `80` for HTTP (or `443` for * HTTPS), but many common HTTP proxy servers use custom ports. - * @param ConnectorInterface $connector In its most simple form, the given - * connector will be a \React\Socket\Connector if you want to connect to - * a given IP address. + * @param ?ConnectorInterface $connector (Optional) Connector to use. * @param array $httpHeaders Custom HTTP headers to be sent to the proxy. * @throws InvalidArgumentException if the proxy URL is invalid */ - public function __construct($proxyUrl, ConnectorInterface $connector, array $httpHeaders = array()) + public function __construct($proxyUrl, ConnectorInterface $connector = null, array $httpHeaders = array()) { // support `http+unix://` scheme for Unix domain socket (UDS) paths if (preg_match('/^http\+unix:\/\/(.*?@)?(.+?)$/', $proxyUrl, $match)) { @@ -67,7 +67,7 @@ public function __construct($proxyUrl, ConnectorInterface $connector, array $htt // connector uses Unix transport scheme and explicit path given $connector = new FixedUriConnector( 'unix://' . $match[2], - $connector + $connector ?: new UnixConnector() ); } @@ -86,7 +86,7 @@ public function __construct($proxyUrl, ConnectorInterface $connector, array $htt } $parts['scheme'] = $parts['scheme'] === 'https' ? 'tls' : 'tcp'; - $this->connector = $connector; + $this->connector = $connector ?: new Connector(); $this->proxyUri = $parts['scheme'] . '://' . $parts['host'] . ':' . $parts['port']; // prepare Proxy-Authorization header if URI contains username/password diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 321ace8..bbf60ba 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -2,19 +2,16 @@ namespace Clue\Tests\React\HttpProxy; -use React\EventLoop\Factory; -use Clue\React\HttpProxy\ProxyConnector; -use React\Socket\TcpConnector; -use React\Socket\DnsConnector; use Clue\React\Block; -use React\Socket\SecureConnector; +use Clue\React\HttpProxy\ProxyConnector; +use React\EventLoop\Factory; +use React\Socket\Connector; /** @group internet */ class FunctionalTest extends AbstractTestCase { private $loop; - private $tcpConnector; - private $dnsConnector; + private $connector; /** * @before @@ -22,18 +19,12 @@ class FunctionalTest extends AbstractTestCase public function setUpConnector() { $this->loop = Factory::create(); - - $this->tcpConnector = new TcpConnector($this->loop); - - $f = new \React\Dns\Resolver\Factory(); - $resolver = $f->create('8.8.8.8', $this->loop); - - $this->dnsConnector = new DnsConnector($this->tcpConnector, $resolver); + $this->connector = new Connector($this->loop); } public function testNonListeningSocketRejectsConnection() { - $proxy = new ProxyConnector('127.0.0.1:9999', $this->dnsConnector); + $proxy = new ProxyConnector('127.0.0.1:9999', $this->connector); $promise = $proxy->connect('google.com:80'); @@ -47,7 +38,7 @@ public function testNonListeningSocketRejectsConnection() public function testPlainGoogleDoesNotAcceptConnectMethod() { - $proxy = new ProxyConnector('google.com', $this->dnsConnector); + $proxy = new ProxyConnector('google.com', $this->connector); $promise = $proxy->connect('google.com:80'); @@ -65,8 +56,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod() $this->markTestSkipped('TLS not supported on legacy HHVM'); } - $secure = new SecureConnector($this->dnsConnector, $this->loop); - $proxy = new ProxyConnector('https://google.com:443', $secure); + $proxy = new ProxyConnector('https://google.com:443', $this->connector); $promise = $proxy->connect('google.com:80'); @@ -80,7 +70,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod() public function testSecureGoogleDoesNotAcceptPlainStream() { - $proxy = new ProxyConnector('google.com:443', $this->dnsConnector); + $proxy = new ProxyConnector('google.com:443'); $promise = $proxy->connect('google.com:80'); @@ -97,7 +87,7 @@ public function testSecureGoogleDoesNotAcceptPlainStream() */ public function testCancelWhileConnectingShouldNotCreateGarbageCycles() { - $proxy = new ProxyConnector('google.com', $this->dnsConnector); + $proxy = new ProxyConnector('google.com', $this->connector); gc_collect_cycles(); gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on diff --git a/tests/ProxyConnectorTest.php b/tests/ProxyConnectorTest.php index 654699a..a92a5c2 100644 --- a/tests/ProxyConnectorTest.php +++ b/tests/ProxyConnectorTest.php @@ -19,6 +19,17 @@ public function setUpMock() $this->connector = $this->getMockBuilder('React\Socket\ConnectorInterface')->getMock(); } + public function testConstructWithoutConnectorAssignsConnectorAutomatically() + { + $proxy = new ProxyConnector('proxy.example.com'); + + $ref = new \ReflectionProperty($proxy, 'connector'); + $ref->setAccessible(true); + $connector = $ref->getValue($proxy); + + $this->assertInstanceOf('React\Socket\ConnectorInterface', $connector); + } + public function testInvalidProxy() { $this->setExpectedException('InvalidArgumentException'); From 59f63090ddd141e7d279f4e3d08638e2ccb573ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 4 Aug 2021 19:06:48 +0200 Subject: [PATCH 04/27] Simplify usage by supporting new Socket API without nullable loop --- README.md | 18 +++++------ composer.json | 6 ++-- examples/01-http-request.php | 4 +-- examples/02-optional-proxy-http-request.php | 4 +-- examples/11-proxy-raw-https-protocol.php | 2 +- .../12-optional-proxy-raw-https-protocol.php | 2 +- examples/13-custom-proxy-headers.php | 2 +- examples/21-proxy-raw-smtp-protocol.php | 2 +- examples/22-proxy-raw-smtps-protocol.php | 2 +- tests/AbstractTestCase.php | 4 +-- tests/FunctionalTest.php | 31 ++++++------------- tests/ProxyConnectorTest.php | 2 +- 12 files changed, 32 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index ac9eb75..a4ffe14 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ secure HTTPS request to google.com through a local HTTP proxy server: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false @@ -115,7 +115,7 @@ proxy servers etc.), you can explicitly pass a custom instance of the [`ConnectorInterface`](https://github.com/reactphp/socket#connectorinterface): ```php -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'dns' => '127.0.0.1', 'tcp' => array( 'bindto' => '192.168.10.1:0' @@ -175,7 +175,7 @@ in ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector): ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -201,7 +201,7 @@ ReactPHP's [`Connector`](https://github.com/reactphp/socket#connector): ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -228,12 +228,12 @@ This allows you to send both plain HTTP and TLS-encrypted HTTPS requests like th ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); -$browser = new React\Http\Browser(null, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); @@ -263,7 +263,7 @@ underlying connection attempt if it takes too long: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false, 'timeout' => 3.0 @@ -307,7 +307,7 @@ other examples explicitly disable DNS resolution like this: ```php $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -319,7 +319,7 @@ If you want to explicitly use *local DNS resolution*, you can use the following $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); // set up Connector which uses Google's public DNS (8.8.8.8) -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => '8.8.8.8' )); diff --git a/composer.json b/composer.json index 6e427a9..13f6e0e 100644 --- a/composer.json +++ b/composer.json @@ -19,13 +19,13 @@ "require": { "php": ">=5.3", "react/promise": " ^2.1 || ^1.2.1", - "react/socket": "^1.8", + "react/socket": "^1.9", "ringcentral/psr7": "^1.2" }, "require-dev": { + "clue/block-react": "^1.1", "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8", "react/event-loop": "^1.2", - "react/http": "^1.4", - "clue/block-react": "^1.1" + "react/http": "^1.5" } } diff --git a/examples/01-http-request.php b/examples/01-http-request.php index a6bc6c0..4bbbbf4 100644 --- a/examples/01-http-request.php +++ b/examples/01-http-request.php @@ -23,12 +23,12 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); -$browser = new React\Http\Browser(null, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); diff --git a/examples/02-optional-proxy-http-request.php b/examples/02-optional-proxy-http-request.php index d018cea..be340c3 100644 --- a/examples/02-optional-proxy-http-request.php +++ b/examples/02-optional-proxy-http-request.php @@ -20,14 +20,14 @@ if ($url !== false) { $proxy = new Clue\React\HttpProxy\ProxyConnector($url); - $connector = new React\Socket\Connector(null, array( + $connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false )); } -$browser = new React\Http\Browser(null, $connector); +$browser = new React\Http\Browser($connector); $browser->get('https://example.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { var_dump($response->getHeaders(), (string) $response->getBody()); diff --git a/examples/11-proxy-raw-https-protocol.php b/examples/11-proxy-raw-https-protocol.php index 1c98cc2..1764e99 100644 --- a/examples/11-proxy-raw-https-protocol.php +++ b/examples/11-proxy-raw-https-protocol.php @@ -26,7 +26,7 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/examples/12-optional-proxy-raw-https-protocol.php b/examples/12-optional-proxy-raw-https-protocol.php index 873f963..b41246f 100644 --- a/examples/12-optional-proxy-raw-https-protocol.php +++ b/examples/12-optional-proxy-raw-https-protocol.php @@ -26,7 +26,7 @@ if ($url !== false) { $proxy = new Clue\React\HttpProxy\ProxyConnector($url); - $connector = new React\Socket\Connector(null, array( + $connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/examples/13-custom-proxy-headers.php b/examples/13-custom-proxy-headers.php index 5314765..56d3622 100644 --- a/examples/13-custom-proxy-headers.php +++ b/examples/13-custom-proxy-headers.php @@ -33,7 +33,7 @@ ) ); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false, diff --git a/examples/21-proxy-raw-smtp-protocol.php b/examples/21-proxy-raw-smtp-protocol.php index 4feeea9..98e262a 100644 --- a/examples/21-proxy-raw-smtp-protocol.php +++ b/examples/21-proxy-raw-smtp-protocol.php @@ -25,7 +25,7 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/examples/22-proxy-raw-smtps-protocol.php b/examples/22-proxy-raw-smtps-protocol.php index 296390a..321507a 100644 --- a/examples/22-proxy-raw-smtps-protocol.php +++ b/examples/22-proxy-raw-smtps-protocol.php @@ -28,7 +28,7 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector($url); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'timeout' => 3.0, 'dns' => false diff --git a/tests/AbstractTestCase.php b/tests/AbstractTestCase.php index 85eb676..72bb151 100644 --- a/tests/AbstractTestCase.php +++ b/tests/AbstractTestCase.php @@ -4,7 +4,7 @@ use PHPUnit\Framework\TestCase; -abstract class AbstractTestCase extends \PHPUnit\Framework\TestCase +abstract class AbstractTestCase extends TestCase { protected function expectCallableNever() { @@ -77,6 +77,4 @@ public function setExpectedException($exception, $exceptionMessage = '', $except parent::setExpectedException($exception, $exceptionMessage, $exceptionCode); } } - } - diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index bbf60ba..38605db 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -4,27 +4,14 @@ use Clue\React\Block; use Clue\React\HttpProxy\ProxyConnector; -use React\EventLoop\Factory; -use React\Socket\Connector; +use React\EventLoop\Loop; /** @group internet */ class FunctionalTest extends AbstractTestCase { - private $loop; - private $connector; - - /** - * @before - */ - public function setUpConnector() - { - $this->loop = Factory::create(); - $this->connector = new Connector($this->loop); - } - public function testNonListeningSocketRejectsConnection() { - $proxy = new ProxyConnector('127.0.0.1:9999', $this->connector); + $proxy = new ProxyConnector('127.0.0.1:9999'); $promise = $proxy->connect('google.com:80'); @@ -33,12 +20,12 @@ public function testNonListeningSocketRejectsConnection() 'Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, $this->loop, 3.0); + Block\await($promise, Loop::get(), 3.0); } public function testPlainGoogleDoesNotAcceptConnectMethod() { - $proxy = new ProxyConnector('google.com', $this->connector); + $proxy = new ProxyConnector('google.com'); $promise = $proxy->connect('google.com:80'); @@ -47,7 +34,7 @@ public function testPlainGoogleDoesNotAcceptConnectMethod() 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, $this->loop, 3.0); + Block\await($promise, Loop::get(), 3.0); } public function testSecureGoogleDoesNotAcceptConnectMethod() @@ -56,7 +43,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod() $this->markTestSkipped('TLS not supported on legacy HHVM'); } - $proxy = new ProxyConnector('https://google.com:443', $this->connector); + $proxy = new ProxyConnector('https://google.com:443'); $promise = $proxy->connect('google.com:80'); @@ -65,7 +52,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod() 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, $this->loop, 3.0); + Block\await($promise, Loop::get(), 3.0); } public function testSecureGoogleDoesNotAcceptPlainStream() @@ -79,7 +66,7 @@ public function testSecureGoogleDoesNotAcceptPlainStream() 'Connection to tcp://google.com:80 failed because connection to proxy was lost while waiting for response (ECONNRESET)', defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104 ); - Block\await($promise, $this->loop, 3.0); + Block\await($promise, Loop::get(), 3.0); } /** @@ -87,7 +74,7 @@ public function testSecureGoogleDoesNotAcceptPlainStream() */ public function testCancelWhileConnectingShouldNotCreateGarbageCycles() { - $proxy = new ProxyConnector('google.com', $this->connector); + $proxy = new ProxyConnector('google.com'); gc_collect_cycles(); gc_collect_cycles(); // clear twice to avoid leftovers in PHP 7.4 with ext-xdebug and code coverage turned on diff --git a/tests/ProxyConnectorTest.php b/tests/ProxyConnectorTest.php index a92a5c2..6272d6a 100644 --- a/tests/ProxyConnectorTest.php +++ b/tests/ProxyConnectorTest.php @@ -3,9 +3,9 @@ namespace Clue\Tests\React\HttpProxy; use Clue\React\HttpProxy\ProxyConnector; -use React\Promise\Promise; use React\Socket\ConnectionInterface; use React\Promise\Deferred; +use React\Promise\Promise; class ProxyConnectorTest extends AbstractTestCase { From f917b07f523b969bbf6681267b21bf4b1ac23a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 5 Aug 2021 12:57:31 +0200 Subject: [PATCH 05/27] Minor documentation improvements --- README.md | 34 +++++++++++-------- composer.json | 12 +++---- examples/01-http-request.php | 6 ++-- examples/02-optional-proxy-http-request.php | 3 +- examples/11-proxy-raw-https-protocol.php | 6 ++-- .../12-optional-proxy-raw-https-protocol.php | 2 +- examples/13-custom-proxy-headers.php | 6 ++-- examples/21-proxy-raw-smtp-protocol.php | 6 ++-- examples/22-proxy-raw-smtps-protocol.php | 6 ++-- 9 files changed, 42 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index a4ffe14..980fc50 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![installs on Packagist](https://img.shields.io/packagist/dt/clue/http-proxy-react?color=blue&label=installs%20on%20Packagist)](https://packagist.org/packages/clue/http-proxy-react) Async HTTP proxy connector, tunnel any TCP/IP-based protocol through an HTTP -CONNECT proxy server, built on top of [ReactPHP](https://reactphp.org). +CONNECT proxy server, built on top of [ReactPHP](https://reactphp.org/). HTTP CONNECT proxy servers (also commonly known as "HTTPS proxy" or "SSL proxy") are commonly used to tunnel HTTPS traffic through an intermediary ("proxy"), to @@ -71,20 +71,24 @@ The following example code demonstrates how this library can be used to send a secure HTTPS request to google.com through a local HTTP proxy server: ```php + $proxy, - 'timeout' => 3.0, 'dns' => false )); -$connector->connect('tls://google.com:443')->then(function (React\Socket\ConnectionInterface $connection) { - $connection->write("GET / HTTP/1.1\r\nHost: google.com\r\nConnection: close\r\n\r\n"); - $connection->on('data', function ($chunk) { - echo $chunk; - }); -}, 'printf'); +$browser = new React\Http\Browser($connector); + +$browser->get('https://google.com/')->then(function (Psr\Http\Message\ResponseInterface $response) { + var_dump($response->getHeaders(), (string) $response->getBody()); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +}); ``` See also the [examples](examples). @@ -334,7 +338,7 @@ If your HTTP proxy server requires authentication, you may pass the username and password as part of the HTTP proxy URL like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector('user:pass@127.0.0.1:8080'); +$proxy = new Clue\React\HttpProxy\ProxyConnector('alice:password@127.0.0.1:8080'); ``` Note that both the username and password must be percent-encoded if they contain @@ -415,7 +419,7 @@ Similarly, you can also combine this with [authentication](#authentication) like this: ```php -$proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix://user:pass@/tmp/proxy.sock'); +$proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix://alice:password@/tmp/proxy.sock'); ``` > Note that Unix domain sockets (UDS) are considered advanced usage and PHP only @@ -430,7 +434,7 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix://user:pass@/tmp/pro ## Install -The recommended way to install this library is [through Composer](https://getcomposer.org). +The recommended way to install this library is [through Composer](https://getcomposer.org/). [New to Composer?](https://getcomposer.org/doc/00-intro.md) This project follows [SemVer](https://semver.org/). @@ -445,12 +449,12 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades. This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.3 through current PHP 8+ and HHVM. -It's *highly recommended to use PHP 7+* for this project. +It's *highly recommended to use the latest supported PHP version* for this project. ## Tests To run the test suite, you first need to clone this repo and then install all -dependencies [through Composer](https://getcomposer.org): +dependencies [through Composer](https://getcomposer.org/): ```bash $ composer install @@ -459,14 +463,14 @@ $ composer install To run the test suite, go to the project root and run: ```bash -$ php vendor/bin/phpunit +$ vendor/bin/phpunit ``` The test suite contains tests that rely on a working internet connection, alternatively you can also run it like this: ```bash -$ php vendor/bin/phpunit --exclude-group internet +$ vendor/bin/phpunit --exclude-group internet ``` ## License diff --git a/composer.json b/composer.json index 13f6e0e..9840cf0 100644 --- a/composer.json +++ b/composer.json @@ -10,12 +10,6 @@ "email": "christian@clue.engineering" } ], - "autoload": { - "psr-4": { "Clue\\React\\HttpProxy\\": "src/" } - }, - "autoload-dev": { - "psr-4": { "Clue\\Tests\\React\\HttpProxy\\": "tests/" } - }, "require": { "php": ">=5.3", "react/promise": " ^2.1 || ^1.2.1", @@ -27,5 +21,11 @@ "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8", "react/event-loop": "^1.2", "react/http": "^1.5" + }, + "autoload": { + "psr-4": { "Clue\\React\\HttpProxy\\": "src/" } + }, + "autoload-dev": { + "psr-4": { "Clue\\Tests\\React\\HttpProxy\\": "tests/" } } } diff --git a/examples/01-http-request.php b/examples/01-http-request.php index 4bbbbf4..f6741d9 100644 --- a/examples/01-http-request.php +++ b/examples/01-http-request.php @@ -5,20 +5,20 @@ // // $ php leproxy.php // -// The proxy defaults to localhost:8080. +// The proxy defaults to 127.0.0.1:8080. // To run the example go to the project root and run: // // $ php examples/01-http-request.php // // 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-request.php +// $ http_proxy=127.0.0.1:8080 php examples/01-http-request.php require __DIR__ . '/../vendor/autoload.php'; $url = getenv('http_proxy'); if ($url === false) { - $url = 'localhost:8080'; + $url = '127.0.0.1:8080'; } $proxy = new Clue\React\HttpProxy\ProxyConnector($url); diff --git a/examples/02-optional-proxy-http-request.php b/examples/02-optional-proxy-http-request.php index be340c3..820aa4c 100644 --- a/examples/02-optional-proxy-http-request.php +++ b/examples/02-optional-proxy-http-request.php @@ -11,7 +11,7 @@ // // 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/02-optional-proxy-http-request.php +// $ http_proxy=127.0.0.1:8080 php examples/02-optional-proxy-http-request.php require __DIR__ . '/../vendor/autoload.php'; @@ -22,7 +22,6 @@ $connector = new React\Socket\Connector(array( 'tcp' => $proxy, - 'timeout' => 3.0, 'dns' => false )); } diff --git a/examples/11-proxy-raw-https-protocol.php b/examples/11-proxy-raw-https-protocol.php index 1764e99..dcfc7c5 100644 --- a/examples/11-proxy-raw-https-protocol.php +++ b/examples/11-proxy-raw-https-protocol.php @@ -5,14 +5,14 @@ // // $ php leproxy.php // -// The proxy defaults to localhost:8080. +// The proxy defaults to 127.0.0.1:8080. // To run the example, go to the project root and run: // // $ php examples/11-proxy-raw-https-protocol.php // // 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 +// $ http_proxy=127.0.0.1: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, example #02 and https://github.com/reactphp/http#client-usage. @@ -21,7 +21,7 @@ $url = getenv('http_proxy'); if ($url === false) { - $url = 'localhost:8080'; + $url = '127.0.0.1:8080'; } $proxy = new Clue\React\HttpProxy\ProxyConnector($url); diff --git a/examples/12-optional-proxy-raw-https-protocol.php b/examples/12-optional-proxy-raw-https-protocol.php index b41246f..1aac199 100644 --- a/examples/12-optional-proxy-raw-https-protocol.php +++ b/examples/12-optional-proxy-raw-https-protocol.php @@ -11,7 +11,7 @@ // // 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/12-optional-proxy-raw-https-protocol.php +// $ http_proxy=127.0.0.1:8080 php examples/12-optional-proxy-raw-https-protocol.php // // This example highlights how changing from direct connection to using a proxy // actually adds very little complexity and does not mess with your actual diff --git a/examples/13-custom-proxy-headers.php b/examples/13-custom-proxy-headers.php index 56d3622..d4248f5 100644 --- a/examples/13-custom-proxy-headers.php +++ b/examples/13-custom-proxy-headers.php @@ -5,14 +5,14 @@ // // $ php leproxy.php // -// The proxy defaults to localhost:8080. +// The proxy defaults to 127.0.0.1:8080. // To run the example, go to the project root and run: // // $ php examples/13-custom-proxy-headers.php // // 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 +// $ http_proxy=127.0.0.1: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, example #02 and https://github.com/reactphp/http#client-usage. @@ -21,7 +21,7 @@ $url = getenv('http_proxy'); if ($url === false) { - $url = 'localhost:8080'; + $url = '127.0.0.1:8080'; } $proxy = new Clue\React\HttpProxy\ProxyConnector( diff --git a/examples/21-proxy-raw-smtp-protocol.php b/examples/21-proxy-raw-smtp-protocol.php index 98e262a..a491d8b 100644 --- a/examples/21-proxy-raw-smtp-protocol.php +++ b/examples/21-proxy-raw-smtp-protocol.php @@ -5,14 +5,14 @@ // // $ php leproxy.php // -// The proxy defaults to localhost:8080. +// The proxy defaults to 127.0.0.1:8080. // To run the example, go to the project root and run: // // $ php examples/21-proxy-raw-smtp-protocol.php // // 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 +// $ http_proxy=127.0.0.1:8080 php examples/21-proxy-raw-smtp-protocol.php // // Please note that MANY public proxies do not allow SMTP connections, YMMV. @@ -20,7 +20,7 @@ $url = getenv('http_proxy'); if ($url === false) { - $url = 'localhost:8080'; + $url = '127.0.0.1:8080'; } $proxy = new Clue\React\HttpProxy\ProxyConnector($url); diff --git a/examples/22-proxy-raw-smtps-protocol.php b/examples/22-proxy-raw-smtps-protocol.php index 321507a..519464f 100644 --- a/examples/22-proxy-raw-smtps-protocol.php +++ b/examples/22-proxy-raw-smtps-protocol.php @@ -5,14 +5,14 @@ // // $ php leproxy.php // -// The proxy defaults to localhost:8080. +// The proxy defaults to 127.0.0.1:8080. // To run the example, go to the project root and run: // // $ php examples/22-proxy-raw-smtps-protocol.php // // 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 +// $ http_proxy=127.0.0.1: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 @@ -23,7 +23,7 @@ $url = getenv('http_proxy'); if ($url === false) { - $url = 'localhost:8080'; + $url = '127.0.0.1:8080'; } $proxy = new Clue\React\HttpProxy\ProxyConnector($url); From 8b2f18cd7bc8b2fb383ac24294d28f340a8cfcb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 6 Aug 2021 15:05:41 +0200 Subject: [PATCH 06/27] Prepare v1.7.0 release --- CHANGELOG.md | 22 ++++++++++++++++++++++ README.md | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8fa95a1..6b1f252 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,27 @@ # Changelog +## 1.7.0 (2021-08-06) + +* Feature: Simplify usage by supporting new default loop and making `Connector` optional. + (#41 and #42 by @clue) + + ```php + // old (still supported) + $proxy = new Clue\React\HttpProxy\ProxyConnector( + '127.0.0.1:8080', + new React\Socket\Connector($loop) + ); + + // new (using default loop) + $proxy = new Clue\React\HttpProxy\ProxyConnector('127.0.0.1:8080'); + ``` + +* Documentation improvements and updated examples. + (#39 and #43 by @clue and #40 by @PaulRotmann) + +* Improve test suite and use GitHub actions for continuous integration (CI). + (#38 by @SimonFrings) + ## 1.6.0 (2020-10-23) * Enhanced documentation for ReactPHP's new HTTP client. diff --git a/README.md b/README.md index 980fc50..4b06155 100644 --- a/README.md +++ b/README.md @@ -441,7 +441,7 @@ This project follows [SemVer](https://semver.org/). This will install the latest supported version: ```bash -$ composer require clue/http-proxy-react:^1.6 +$ composer require clue/http-proxy-react:^1.7 ``` See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades. From 8aa0ffc905b9306d98edf41a9d8464a29a8417bf Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Wed, 22 Jun 2022 10:07:11 +0200 Subject: [PATCH 07/27] Fix legacy HHVM build by downgrading Composer --- .github/workflows/ci.yml | 1 + README.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cf214c8..ae61063 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -42,5 +42,6 @@ jobs: - uses: azjezz/setup-hhvm@v1 with: version: lts-3.30 + - run: composer self-update --2.2 # downgrade Composer for HHVM - run: hhvm $(which composer) install - run: hhvm vendor/bin/phpunit diff --git a/README.md b/README.md index 4b06155..cdbc048 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # clue/reactphp-http-proxy -[![CI status](https://github.com/clue/reactphp-http-proxy/workflows/CI/badge.svg)](https://github.com/clue/reactphp-http-proxy/actions) +[![CI status](https://github.com/clue/reactphp-http-proxy/actions/workflows/ci.yml/badge.svg)](https://github.com/clue/reactphp-http-proxy/actions) [![installs on Packagist](https://img.shields.io/packagist/dt/clue/http-proxy-react?color=blue&label=installs%20on%20Packagist)](https://packagist.org/packages/clue/http-proxy-react) Async HTTP proxy connector, tunnel any TCP/IP-based protocol through an HTTP From d8cd8bac18d54f0f09d0b07585494d10fc81206b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 21 Jun 2022 23:18:51 +0200 Subject: [PATCH 08/27] Fix invalid references in exception stack trace --- src/ProxyConnector.php | 6 +++--- tests/ProxyConnectorTest.php | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/ProxyConnector.php b/src/ProxyConnector.php index 0bca17e..a3dc166 100644 --- a/src/ProxyConnector.php +++ b/src/ProxyConnector.php @@ -256,11 +256,11 @@ public function connect($uri) // Exception trace arguments are not available on some PHP 7.4 installs // @codeCoverageIgnoreStart - foreach ($trace as &$one) { + foreach ($trace as $ti => $one) { if (isset($one['args'])) { - foreach ($one['args'] as &$arg) { + foreach ($one['args'] as $ai => $arg) { if ($arg instanceof \Closure) { - $arg = 'Object(' . get_class($arg) . ')'; + $trace[$ti]['args'][$ai] = 'Object(' . get_class($arg) . ')'; } } } diff --git a/tests/ProxyConnectorTest.php b/tests/ProxyConnectorTest.php index 6272d6a..59fc466 100644 --- a/tests/ProxyConnectorTest.php +++ b/tests/ProxyConnectorTest.php @@ -302,15 +302,17 @@ public function testRejectsWithPreviousIfConnectorRejects() $promise = $proxy->connect('google.com:80'); - $promise->then(null, $this->expectCallableOnceWithException( - 'RuntimeException', - 'Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', - defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 - )); + $exception = null; + $promise->then(null, function ($reason) use (&$exception) { + $exception = $reason; + }); - $promise->then(null, $this->expectCallableOnceWith($this->callback(function (\Exception $e) use ($previous) { - return $e->getPrevious() === $previous; - }))); + assert($exception instanceof \RuntimeException); + $this->assertInstanceOf('RuntimeException', $exception); + $this->assertEquals('Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', $exception->getMessage()); + $this->assertEquals(defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111, $exception->getCode()); + $this->assertSame($previous, $exception->getPrevious()); + $this->assertNotEquals('', $exception->getTraceAsString()); } public function testRejectsAndClosesIfStreamWritesNonHttp() From 7a5d832d0e2f20e0b3dcdefa409b4cfd8bad8262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 21 Jun 2022 22:52:45 +0200 Subject: [PATCH 09/27] Forward compatibility with upcoming Promise v3 --- .github/workflows/ci.yml | 3 +++ composer.json | 17 +++++++++++------ tests/ProxyConnectorTest.php | 4 ++-- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae61063..081fe14 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,8 @@ jobs: with: php-version: ${{ matrix.php }} coverage: xdebug + env: + COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: composer install - run: vendor/bin/phpunit --coverage-text if: ${{ matrix.php >= 7.3 }} @@ -37,6 +39,7 @@ jobs: name: PHPUnit (HHVM) runs-on: ubuntu-18.04 continue-on-error: true + if: false # temporarily skipped until https://github.com/azjezz/setup-hhvm/issues/3 is addressed steps: - uses: actions/checkout@v2 - uses: azjezz/setup-hhvm@v1 diff --git a/composer.json b/composer.json index 9840cf0..fc23ef2 100644 --- a/composer.json +++ b/composer.json @@ -12,20 +12,25 @@ ], "require": { "php": ">=5.3", - "react/promise": " ^2.1 || ^1.2.1", - "react/socket": "^1.9", + "react/promise": "^3@dev || ^2.1 || ^1.2.1", + "react/socket": "dev-promise-3 as 1.12.0", "ringcentral/psr7": "^1.2" }, "require-dev": { - "clue/block-react": "^1.1", + "clue/block-react": "^1.5", "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8", - "react/event-loop": "^1.2", - "react/http": "^1.5" + "react/event-loop": "^1.2" }, "autoload": { "psr-4": { "Clue\\React\\HttpProxy\\": "src/" } }, "autoload-dev": { "psr-4": { "Clue\\Tests\\React\\HttpProxy\\": "tests/" } - } + }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/WyriHaximus-labs/socket" + } + ] } diff --git a/tests/ProxyConnectorTest.php b/tests/ProxyConnectorTest.php index 59fc466..0b8c155 100644 --- a/tests/ProxyConnectorTest.php +++ b/tests/ProxyConnectorTest.php @@ -137,7 +137,7 @@ public function testCancelPromiseWillCancelPendingConnection() $promise = $proxy->connect('google.com:80'); - $this->assertInstanceOf('React\Promise\CancellablePromiseInterface', $promise); + $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); $promise->cancel(); } @@ -477,7 +477,7 @@ public function testCancelPromiseWhileConnectionIsReadyWillCloseOpenConnectionAn $deferred->resolve($stream); - $this->assertInstanceOf('React\Promise\CancellablePromiseInterface', $promise); + $this->assertInstanceOf('React\Promise\PromiseInterface', $promise); $promise->cancel(); From 684f377d5b2f6fc84978e34e2ee49cb250c2876a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 22 Jun 2022 08:34:39 +0200 Subject: [PATCH 10/27] Update to stable reactphp/socket v1.12.0 --- .github/workflows/ci.yml | 3 --- composer.json | 15 +++++---------- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 081fe14..ae61063 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,8 +27,6 @@ jobs: with: php-version: ${{ matrix.php }} coverage: xdebug - env: - COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: composer install - run: vendor/bin/phpunit --coverage-text if: ${{ matrix.php >= 7.3 }} @@ -39,7 +37,6 @@ jobs: name: PHPUnit (HHVM) runs-on: ubuntu-18.04 continue-on-error: true - if: false # temporarily skipped until https://github.com/azjezz/setup-hhvm/issues/3 is addressed steps: - uses: actions/checkout@v2 - uses: azjezz/setup-hhvm@v1 diff --git a/composer.json b/composer.json index fc23ef2..4941b1a 100644 --- a/composer.json +++ b/composer.json @@ -12,25 +12,20 @@ ], "require": { "php": ">=5.3", - "react/promise": "^3@dev || ^2.1 || ^1.2.1", - "react/socket": "dev-promise-3 as 1.12.0", + "react/promise": "^3 || ^2.1 || ^1.2.1", + "react/socket": "^1.12", "ringcentral/psr7": "^1.2" }, "require-dev": { "clue/block-react": "^1.5", "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8", - "react/event-loop": "^1.2" + "react/event-loop": "^1.2", + "react/http": "^1.5" }, "autoload": { "psr-4": { "Clue\\React\\HttpProxy\\": "src/" } }, "autoload-dev": { "psr-4": { "Clue\\Tests\\React\\HttpProxy\\": "tests/" } - }, - "repositories": [ - { - "type": "vcs", - "url": "https://github.com/WyriHaximus-labs/socket" - } - ] + } } From a9f499cefb7f302abaf1890b3ed9ab7630814614 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Wed, 22 Jun 2022 13:14:47 +0200 Subject: [PATCH 11/27] Support PHP 8.1 --- .github/workflows/ci.yml | 1 + phpunit.xml.dist | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ae61063..2757783 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,7 @@ jobs: strategy: matrix: php: + - 8.1 - 8.0 - 7.4 - 7.3 diff --git a/phpunit.xml.dist b/phpunit.xml.dist index dbe6515..cd0e903 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -4,8 +4,9 @@ + convertDeprecationsToExceptions="true"> ./tests/ @@ -16,4 +17,7 @@ ./src/ + + + From 24cae3bb494b2059f4b98db07c8ee09ddb3d7dc0 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Thu, 1 Sep 2022 12:00:58 +0200 Subject: [PATCH 12/27] Test on PHP 8.2 --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2757783..0724232 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,7 @@ jobs: strategy: matrix: php: + - 8.2 - 8.1 - 8.0 - 7.4 From 90cb5a2f9bf0145326afdcd95665f333ad7f435b Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Thu, 1 Sep 2022 15:52:33 +0200 Subject: [PATCH 13/27] Mark passwords and URIs as `#[\SensitiveParameter]` (PHP 8.2+) --- src/ProxyConnector.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/ProxyConnector.php b/src/ProxyConnector.php index a3dc166..165e8ba 100644 --- a/src/ProxyConnector.php +++ b/src/ProxyConnector.php @@ -57,8 +57,12 @@ class ProxyConnector implements ConnectorInterface * @param array $httpHeaders Custom HTTP headers to be sent to the proxy. * @throws InvalidArgumentException if the proxy URL is invalid */ - public function __construct($proxyUrl, ConnectorInterface $connector = null, array $httpHeaders = array()) - { + public function __construct( + #[\SensitiveParameter] + $proxyUrl, + ConnectorInterface $connector = null, + array $httpHeaders = array() + ) { // support `http+unix://` scheme for Unix domain socket (UDS) paths if (preg_match('/^http\+unix:\/\/(.*?@)?(.+?)$/', $proxyUrl, $match)) { // rewrite URI to parse authentication from dummy host From 09366dd3e13b36b90f8e47a6acaf5a2c96b79fb5 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Thu, 1 Sep 2022 16:34:39 +0200 Subject: [PATCH 14/27] Prepare v1.8.0 release --- CHANGELOG.md | 17 +++++++++++++++++ README.md | 8 ++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b1f252..19d534d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # Changelog +## 1.8.0 (2022-09-01) + +* Feature: Full support for PHP 8.1 and PHP 8.2. + (#47 and #48 by @SimonFrings) + +* Feature: Mark passwords and URIs as `#[\SensitiveParameter]` (PHP 8.2+). + (#49 by @SimonFrings) + +* Feature: Forward compatibility with upcoming Promise v3. + (#44 by @clue) + +* Fix: Fix invalid references in exception stack trace. + (#45 by @clue) + +* Improve test suite and fix legacy HHVM build. + (#46 by @SimonFrings) + ## 1.7.0 (2021-08-06) * Feature: Simplify usage by supporting new default loop and making `Connector` optional. diff --git a/README.md b/README.md index cdbc048..b5337c1 100644 --- a/README.md +++ b/README.md @@ -441,7 +441,7 @@ This project follows [SemVer](https://semver.org/). This will install the latest supported version: ```bash -$ composer require clue/http-proxy-react:^1.7 +composer require clue/http-proxy-react:^1.8 ``` See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades. @@ -457,20 +457,20 @@ To run the test suite, you first need to clone this repo and then install all dependencies [through Composer](https://getcomposer.org/): ```bash -$ composer install +composer install ``` To run the test suite, go to the project root and run: ```bash -$ vendor/bin/phpunit +vendor/bin/phpunit ``` The test suite contains tests that rely on a working internet connection, alternatively you can also run it like this: ```bash -$ vendor/bin/phpunit --exclude-group internet +vendor/bin/phpunit --exclude-group internet ``` ## License From bbbb932a458e2874815b00cda9722ce3b0627990 Mon Sep 17 00:00:00 2001 From: Yada Clintjens Date: Mon, 13 Nov 2023 17:26:42 +0100 Subject: [PATCH 15/27] Run tests on PHP 8.3 and update test suite --- .github/workflows/ci.yml | 22 +++++++++++++--------- composer.json | 10 +++++++--- phpunit.xml.dist | 6 +++--- phpunit.xml.legacy | 5 ++++- 4 files changed, 27 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0724232..c6239b6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,10 +7,11 @@ on: jobs: PHPUnit: name: PHPUnit (PHP ${{ matrix.php }}) - runs-on: ubuntu-20.04 + runs-on: ubuntu-22.04 strategy: matrix: php: + - 8.3 - 8.2 - 8.1 - 8.0 @@ -24,7 +25,7 @@ jobs: - 5.4 - 5.3 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} @@ -37,13 +38,16 @@ jobs: PHPUnit-hhvm: name: PHPUnit (HHVM) - runs-on: ubuntu-18.04 + runs-on: ubuntu-22.04 continue-on-error: true steps: - - uses: actions/checkout@v2 - - uses: azjezz/setup-hhvm@v1 + - uses: actions/checkout@v4 + - run: cp "$(which composer)" composer.phar && ./composer.phar self-update --2.2 # downgrade Composer for HHVM + - name: Run hhvm composer.phar install + uses: docker://hhvm/hhvm:3.30-lts-latest with: - version: lts-3.30 - - run: composer self-update --2.2 # downgrade Composer for HHVM - - run: hhvm $(which composer) install - - run: hhvm vendor/bin/phpunit + args: hhvm composer.phar install + - name: Run hhvm vendor/bin/phpunit + uses: docker://hhvm/hhvm:3.30-lts-latest + with: + args: hhvm vendor/bin/phpunit diff --git a/composer.json b/composer.json index 4941b1a..0927d6b 100644 --- a/composer.json +++ b/composer.json @@ -18,14 +18,18 @@ }, "require-dev": { "clue/block-react": "^1.5", - "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8", + "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36", "react/event-loop": "^1.2", "react/http": "^1.5" }, "autoload": { - "psr-4": { "Clue\\React\\HttpProxy\\": "src/" } + "psr-4": { + "Clue\\React\\HttpProxy\\": "src/" + } }, "autoload-dev": { - "psr-4": { "Clue\\Tests\\React\\HttpProxy\\": "tests/" } + "psr-4": { + "Clue\\Tests\\React\\HttpProxy\\": "tests/" + } } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cd0e903..af249cf 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,8 +1,8 @@ - - + - + ./src/ + + + From 2073667127ee21bc55e6344b917473292da925ff Mon Sep 17 00:00:00 2001 From: Yada Clintjens Date: Mon, 13 Nov 2023 16:28:46 +0100 Subject: [PATCH 16/27] Fix typo in CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 19d534d..93fb105 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -120,7 +120,7 @@ ```php $promise = $proxy->connect('imap.example.com:143'); - $promise->then(null, function (Exeption $e) { + $promise->then(null, function (Exception $e) { if ($e->getCode() === SOCKET_EACCES) { echo 'Failed to authenticate with proxy!'; } From 81d322cb77384d8da1e844c7e18bcbbee1e512a7 Mon Sep 17 00:00:00 2001 From: Yada Clintjens Date: Fri, 9 Feb 2024 11:00:45 +0100 Subject: [PATCH 17/27] Improve documentation --- README.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b5337c1..7028a1b 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,8 @@ $proxy->connect('tcp://smtp.googlemail.com:587')->then(function (React\Socket\Co $connection->on('data', function ($chunk) use ($connection) { echo $chunk; }); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -189,6 +191,8 @@ $connector->connect('tcp://smtp.googlemail.com:587')->then(function (React\Socke $connection->on('data', function ($chunk) use ($connection) { echo $chunk; }); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -215,6 +219,8 @@ $connector->connect('tls://smtp.googlemail.com:465')->then(function (React\Socke $connection->on('data', function ($chunk) use ($connection) { echo $chunk; }); +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -247,7 +253,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) -and any of the [examples](examples) for more details. +and any of the [examples](examples/) for more details. #### Connection timeout @@ -275,6 +281,8 @@ $connector = new React\Socket\Connector(array( $connector->connect('tcp://google.com:80')->then(function ($connection) { // connection succeeded within 3.0 seconds +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -412,6 +420,8 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector('http+unix:///tmp/proxy.sock'); $proxy->connect('tcp://google.com:80')->then(function (React\Socket\ConnectionInterface $connection) { // connected… +}, function (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` From 703bcbbbf45c088d94114fa309c28800cf6deb92 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Tue, 9 Apr 2024 09:49:12 +0200 Subject: [PATCH 18/27] Update close handler to avoid unhandled promise rejections --- src/ProxyConnector.php | 2 ++ tests/ProxyConnectorTest.php | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/ProxyConnector.php b/src/ProxyConnector.php index 165e8ba..d7f2863 100644 --- a/src/ProxyConnector.php +++ b/src/ProxyConnector.php @@ -159,6 +159,8 @@ public function connect($uri) // either close active connection or cancel pending connection attempt $connecting->then(function (ConnectionInterface $stream) { $stream->close(); + }, function () { + // ignore to avoid reporting unhandled rejection }); $connecting->cancel(); }); diff --git a/tests/ProxyConnectorTest.php b/tests/ProxyConnectorTest.php index 0b8c155..f2d5795 100644 --- a/tests/ProxyConnectorTest.php +++ b/tests/ProxyConnectorTest.php @@ -542,6 +542,9 @@ public function testRejectedConnectionShouldNotCreateGarbageCycles() $proxy = new ProxyConnector('proxy.example.com', $this->connector); $promise = $proxy->connect('google.com:80'); + + $promise->then(null, $this->expectCallableOnce()); // avoid reporting unhandled rejection + unset($promise); $this->assertEquals(0, gc_collect_cycles()); From cd22f205629b33ef1c26d2af045782fa3699831d Mon Sep 17 00:00:00 2001 From: Fabian Meyer Date: Tue, 4 Oct 2022 19:42:59 +0200 Subject: [PATCH 19/27] Use reactphp/async instead of clue/reactphp-block --- composer.json | 5 +++-- tests/FunctionalTest.php | 10 ++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 0927d6b..2923c55 100644 --- a/composer.json +++ b/composer.json @@ -17,10 +17,11 @@ "ringcentral/psr7": "^1.2" }, "require-dev": { - "clue/block-react": "^1.5", "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36", + "react/async": "^4 || ^3 || ^2", "react/event-loop": "^1.2", - "react/http": "^1.5" + "react/http": "^1.5", + "react/promise-timer": "^1.10" }, "autoload": { "psr-4": { diff --git a/tests/FunctionalTest.php b/tests/FunctionalTest.php index 38605db..9bd5dbc 100644 --- a/tests/FunctionalTest.php +++ b/tests/FunctionalTest.php @@ -2,9 +2,7 @@ namespace Clue\Tests\React\HttpProxy; -use Clue\React\Block; use Clue\React\HttpProxy\ProxyConnector; -use React\EventLoop\Loop; /** @group internet */ class FunctionalTest extends AbstractTestCase @@ -20,7 +18,7 @@ public function testNonListeningSocketRejectsConnection() 'Connection to tcp://google.com:80 failed because connection to proxy failed (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, Loop::get(), 3.0); + \React\Async\await(\React\Promise\Timer\timeout($promise, 3.0)); } public function testPlainGoogleDoesNotAcceptConnectMethod() @@ -34,7 +32,7 @@ public function testPlainGoogleDoesNotAcceptConnectMethod() 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, Loop::get(), 3.0); + \React\Async\await(\React\Promise\Timer\timeout($promise, 3.0)); } public function testSecureGoogleDoesNotAcceptConnectMethod() @@ -52,7 +50,7 @@ public function testSecureGoogleDoesNotAcceptConnectMethod() 'Connection to tcp://google.com:80 failed because proxy refused connection with HTTP error code 405 (Method Not Allowed) (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 ); - Block\await($promise, Loop::get(), 3.0); + \React\Async\await(\React\Promise\Timer\timeout($promise, 3.0)); } public function testSecureGoogleDoesNotAcceptPlainStream() @@ -66,7 +64,7 @@ public function testSecureGoogleDoesNotAcceptPlainStream() 'Connection to tcp://google.com:80 failed because connection to proxy was lost while waiting for response (ECONNRESET)', defined('SOCKET_ECONNRESET') ? SOCKET_ECONNRESET : 104 ); - Block\await($promise, Loop::get(), 3.0); + \React\Async\await(\React\Promise\Timer\timeout($promise, 3.0)); } /** From f3b02835273036a9370ac1c144b55df8e2b98430 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Wed, 10 Apr 2024 16:46:11 +0200 Subject: [PATCH 20/27] Prepare v1.9.0 release --- CHANGELOG.md | 14 ++++++++++++++ README.md | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93fb105..52050cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,19 @@ # Changelog +## 1.9.0 (2024-04-10) + +* Feature / Fix: Forward compatibility with Promise v3. + (#56 by @SimonFrings) + +* Feature: Full PHP 8.3 compatibility. + (#54 by @yadaiio) + +* Minor documentation improvements. + (#53 and #55 by @yadaiio) + +* Update test suite to use new reactphp/async package instead of clue/reactphp-block. + (#50 and #57 by @dinooo13 and @SimonFrings) + ## 1.8.0 (2022-09-01) * Feature: Full support for PHP 8.1 and PHP 8.2. diff --git a/README.md b/README.md index 7028a1b..ad9126b 100644 --- a/README.md +++ b/README.md @@ -451,7 +451,7 @@ This project follows [SemVer](https://semver.org/). This will install the latest supported version: ```bash -composer require clue/http-proxy-react:^1.8 +composer require clue/http-proxy-react:^1.9 ``` See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades. From 797ec357d920a54de5a9665ff3b3d0923360799e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Wed, 5 Jun 2024 19:18:29 +0200 Subject: [PATCH 21/27] Improve PHP 8.4+ support by avoiding implicitly nullable types --- composer.json | 10 +++++----- src/ProxyConnector.php | 6 +++++- tests/ProxyConnectorTest.php | 6 ++++++ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 2923c55..7788b7d 100644 --- a/composer.json +++ b/composer.json @@ -12,16 +12,16 @@ ], "require": { "php": ">=5.3", - "react/promise": "^3 || ^2.1 || ^1.2.1", - "react/socket": "^1.12", + "react/promise": "^3.2 || ^2.1 || ^1.2.1", + "react/socket": "^1.16", "ringcentral/psr7": "^1.2" }, "require-dev": { "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36", - "react/async": "^4 || ^3 || ^2", + "react/async": "^4.3 || ^3 || ^2", "react/event-loop": "^1.2", - "react/http": "^1.5", - "react/promise-timer": "^1.10" + "react/http": "^1.11", + "react/promise-timer": "^1.11" }, "autoload": { "psr-4": { diff --git a/src/ProxyConnector.php b/src/ProxyConnector.php index d7f2863..0e7bcdb 100644 --- a/src/ProxyConnector.php +++ b/src/ProxyConnector.php @@ -60,7 +60,7 @@ class ProxyConnector implements ConnectorInterface public function __construct( #[\SensitiveParameter] $proxyUrl, - ConnectorInterface $connector = null, + $connector = null, array $httpHeaders = array() ) { // support `http+unix://` scheme for Unix domain socket (UDS) paths @@ -84,6 +84,10 @@ public function __construct( throw new InvalidArgumentException('Invalid proxy URL "' . $proxyUrl . '"'); } + if ($connector !== null && !$connector instanceof ConnectorInterface) { // manual type check to support legacy PHP < 7.1 + throw new \InvalidArgumentException('Argument #2 ($connector) expected null|React\Socket\ConnectorInterface'); + } + // apply default port and TCP/TLS transport for given scheme if (!isset($parts['port'])) { $parts['port'] = $parts['scheme'] === 'https' ? 443 : 80; diff --git a/tests/ProxyConnectorTest.php b/tests/ProxyConnectorTest.php index f2d5795..0839692 100644 --- a/tests/ProxyConnectorTest.php +++ b/tests/ProxyConnectorTest.php @@ -48,6 +48,12 @@ public function testInvalidHttpsUnixScheme() new ProxyConnector('https+unix:///tmp/proxy.sock', $this->connector); } + public function testContructorThrowsExceptionForInvalidConnector() + { + $this->setExpectedException('InvalidArgumentException', 'Argument #2 ($connector) expected null|React\Socket\ConnectorInterface'); + new ProxyConnector('proxy.example.com', 'connector'); + } + public function testCreatesConnectionToHttpPort() { $promise = new Promise(function () { }); From d0dae6f63a3288e5dba95a716c7fc7b29ebf72f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Fri, 6 Dec 2024 16:37:46 +0100 Subject: [PATCH 22/27] Run tests on PHP 8.4 and update test environment --- .github/workflows/ci.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c6239b6..98ceb2a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -7,10 +7,11 @@ on: jobs: PHPUnit: name: PHPUnit (PHP ${{ matrix.php }}) - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 strategy: matrix: php: + - 8.4 - 8.3 - 8.2 - 8.1 @@ -38,7 +39,7 @@ jobs: PHPUnit-hhvm: name: PHPUnit (HHVM) - runs-on: ubuntu-22.04 + runs-on: ubuntu-24.04 continue-on-error: true steps: - uses: actions/checkout@v4 From 2b4f466f2a7d73941baac2b9f3c86fee15698f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sat, 11 Apr 2026 12:00:22 +0200 Subject: [PATCH 23/27] Update test environment to fix tests on legacy PHP 7.2 with PHPUnit 8.5 --- .github/workflows/ci.yml | 5 +---- composer.json | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 98ceb2a..5e640b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -32,10 +32,7 @@ jobs: php-version: ${{ matrix.php }} coverage: xdebug - run: composer install - - run: vendor/bin/phpunit --coverage-text - if: ${{ matrix.php >= 7.3 }} - - run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy - if: ${{ matrix.php < 7.3 }} + - run: vendor/bin/phpunit --coverage-text ${{ matrix.php < 7.3 && '-c phpunit.xml.legacy' || '' }} PHPUnit-hhvm: name: PHPUnit (HHVM) diff --git a/composer.json b/composer.json index 7788b7d..f56cb23 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "ringcentral/psr7": "^1.2" }, "require-dev": { - "phpunit/phpunit": "^9.6 || ^5.7 || ^4.8.36", + "phpunit/phpunit": "^9.6 || ^8.5 || ^5.7 || ^4.8.36", "react/async": "^4.3 || ^3 || ^2", "react/event-loop": "^1.2", "react/http": "^1.11", From 99ec60d2ec56d795fbb13c70458904513ba85c47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 12 Apr 2026 22:04:53 +0200 Subject: [PATCH 24/27] Improve PHP 8.5+ support by avoiding deprecated `setAccessible()` calls --- .github/workflows/ci.yml | 5 +++-- composer.json | 6 +++--- src/ProxyConnector.php | 4 +++- tests/ProxyConnectorTest.php | 4 +++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5e640b2..8b912ba 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,6 +11,7 @@ jobs: strategy: matrix: php: + - 8.5 - 8.4 - 8.3 - 8.2 @@ -26,7 +27,7 @@ jobs: - 5.4 - 5.3 steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v6 - uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php }} @@ -39,7 +40,7 @@ jobs: runs-on: ubuntu-24.04 continue-on-error: true steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@v6 - run: cp "$(which composer)" composer.phar && ./composer.phar self-update --2.2 # downgrade Composer for HHVM - name: Run hhvm composer.phar install uses: docker://hhvm/hhvm:3.30-lts-latest diff --git a/composer.json b/composer.json index f56cb23..0d33cdf 100644 --- a/composer.json +++ b/composer.json @@ -12,14 +12,14 @@ ], "require": { "php": ">=5.3", - "react/promise": "^3.2 || ^2.1 || ^1.2.1", - "react/socket": "^1.16", + "react/promise": "^3.3 || ^2.1 || ^1.2.1", + "react/socket": "^1.17", "ringcentral/psr7": "^1.2" }, "require-dev": { "phpunit/phpunit": "^9.6 || ^8.5 || ^5.7 || ^4.8.36", "react/async": "^4.3 || ^3 || ^2", - "react/event-loop": "^1.2", + "react/event-loop": "^1.6", "react/http": "^1.11", "react/promise-timer": "^1.11" }, diff --git a/src/ProxyConnector.php b/src/ProxyConnector.php index 0e7bcdb..3cb6b22 100644 --- a/src/ProxyConnector.php +++ b/src/ProxyConnector.php @@ -261,7 +261,9 @@ public function connect($uri) // avoid garbage references by replacing all closures in call stack. // what a lovely piece of code! $r = new \ReflectionProperty('Exception', 'trace'); - $r->setAccessible(true); + if (PHP_VERSION_ID < 80100) { + $r->setAccessible(true); + } $trace = $r->getValue($e); // Exception trace arguments are not available on some PHP 7.4 installs diff --git a/tests/ProxyConnectorTest.php b/tests/ProxyConnectorTest.php index 0839692..bf2e292 100644 --- a/tests/ProxyConnectorTest.php +++ b/tests/ProxyConnectorTest.php @@ -24,7 +24,9 @@ public function testConstructWithoutConnectorAssignsConnectorAutomatically() $proxy = new ProxyConnector('proxy.example.com'); $ref = new \ReflectionProperty($proxy, 'connector'); - $ref->setAccessible(true); + if (PHP_VERSION_ID < 80100) { + $ref->setAccessible(true); + } $connector = $ref->getValue($proxy); $this->assertInstanceOf('React\Socket\ConnectorInterface', $connector); From b6d17a756fc36da898cb36a399bff0fa9e333c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 13 Apr 2026 01:21:18 +0200 Subject: [PATCH 25/27] Update test suite to ensure 100% code coverage --- .github/workflows/ci.yml | 8 +++++++- README.md | 9 +++++++++ src/ProxyConnector.php | 4 ++-- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8b912ba..06c449c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,13 @@ jobs: php-version: ${{ matrix.php }} coverage: xdebug - run: composer install - - run: vendor/bin/phpunit --coverage-text ${{ matrix.php < 7.3 && '-c phpunit.xml.legacy' || '' }} + - run: vendor/bin/phpunit --coverage-text --coverage-clover clover.xml ${{ matrix.php < 7.3 && '-c phpunit.xml.legacy' || '' }} + - name: Check 100% code coverage + shell: php {0} + run: | + project->metrics; + exit((int) $metrics['statements'] === (int) $metrics['coveredstatements'] ? 0 : 1); PHPUnit-hhvm: name: PHPUnit (HHVM) diff --git a/README.md b/README.md index ad9126b..0b4db17 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # clue/reactphp-http-proxy [![CI status](https://github.com/clue/reactphp-http-proxy/actions/workflows/ci.yml/badge.svg)](https://github.com/clue/reactphp-http-proxy/actions) +[![code coverage](https://img.shields.io/badge/code%20coverage-100%25-success)](#tests) [![installs on Packagist](https://img.shields.io/packagist/dt/clue/http-proxy-react?color=blue&label=installs%20on%20Packagist)](https://packagist.org/packages/clue/http-proxy-react) Async HTTP proxy connector, tunnel any TCP/IP-based protocol through an HTTP @@ -483,6 +484,14 @@ alternatively you can also run it like this: vendor/bin/phpunit --exclude-group internet ``` +The test suite is set up to always ensure 100% code coverage across all +supported environments. If you have the Xdebug extension installed, you can also +generate a code coverage report locally like this: + +```bash +XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text +``` + ## License This project is released under the permissive [MIT license](LICENSE). diff --git a/src/ProxyConnector.php b/src/ProxyConnector.php index 3cb6b22..2e3575b 100644 --- a/src/ProxyConnector.php +++ b/src/ProxyConnector.php @@ -71,7 +71,7 @@ public function __construct( // connector uses Unix transport scheme and explicit path given $connector = new FixedUriConnector( 'unix://' . $match[2], - $connector ?: new UnixConnector() + $connector ?: new UnixConnector() // @codeCoverageIgnore ); } @@ -262,7 +262,7 @@ public function connect($uri) // what a lovely piece of code! $r = new \ReflectionProperty('Exception', 'trace'); if (PHP_VERSION_ID < 80100) { - $r->setAccessible(true); + $r->setAccessible(true); // @codeCoverageIgnore } $trace = $r->getValue($e); From bb64fe031b95662e388a1e524e6a876efde59880 Mon Sep 17 00:00:00 2001 From: Alexander Barker Date: Tue, 10 Jun 2025 20:45:19 -0700 Subject: [PATCH 26/27] Remove ringcentral/psr7 because it appears to be abandoned and upstream projects would like to support psr/http-message ^2.0 --- composer.json | 3 +-- src/ProxyConnector.php | 19 +++++++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/composer.json b/composer.json index 0d33cdf..e04b32e 100644 --- a/composer.json +++ b/composer.json @@ -13,8 +13,7 @@ "require": { "php": ">=5.3", "react/promise": "^3.3 || ^2.1 || ^1.2.1", - "react/socket": "^1.17", - "ringcentral/psr7": "^1.2" + "react/socket": "^1.17" }, "require-dev": { "phpunit/phpunit": "^9.6 || ^8.5 || ^5.7 || ^4.8.36", diff --git a/src/ProxyConnector.php b/src/ProxyConnector.php index 2e3575b..c9347fe 100644 --- a/src/ProxyConnector.php +++ b/src/ProxyConnector.php @@ -5,7 +5,6 @@ use Exception; use InvalidArgumentException; use RuntimeException; -use RingCentral\Psr7; use React\Promise; use React\Promise\Deferred; use React\Socket\ConnectionInterface; @@ -183,30 +182,30 @@ public function connect($uri) $fn = null; // try to parse headers as response message - try { - $response = Psr7\parse_response(substr($buffer, 0, $pos)); - } catch (Exception $e) { + if (!preg_match('/^HTTP\/1\.[01] ([0-9]{3})( .*)?\r\n/', $buffer, $matches)) { $deferred->reject(new RuntimeException( 'Connection to ' . $uri . ' failed because proxy returned invalid response (EBADMSG)', - defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG: 71, - $e + defined('SOCKET_EBADMSG') ? SOCKET_EBADMSG : 71 )); $stream->close(); return; } - if ($response->getStatusCode() === 407) { + $statusCode = (int) $matches[1]; + $reasonPhrase = isset($matches[2]) ? trim($matches[2]) : ''; + + if ($statusCode === 407) { // map status code 407 (Proxy Authentication Required) to EACCES $deferred->reject(new RuntimeException( - 'Connection to ' . $uri . ' failed because proxy denied access with HTTP error code ' . $response->getStatusCode() . ' (' . $response->getReasonPhrase() . ') (EACCES)', + 'Connection to ' . $uri . ' failed because proxy denied access with HTTP error code ' . $statusCode . ' (' . $reasonPhrase . ') (EACCES)', defined('SOCKET_EACCES') ? SOCKET_EACCES : 13 )); $stream->close(); return; - } elseif ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { + } elseif ($statusCode < 200 || $statusCode >= 300) { // map non-2xx status code to ECONNREFUSED $deferred->reject(new RuntimeException( - 'Connection to ' . $uri . ' failed because proxy refused connection with HTTP error code ' . $response->getStatusCode() . ' (' . $response->getReasonPhrase() . ') (ECONNREFUSED)', + 'Connection to ' . $uri . ' failed because proxy refused connection with HTTP error code ' . $statusCode . ' (' . $reasonPhrase . ') (ECONNREFUSED)', defined('SOCKET_ECONNREFUSED') ? SOCKET_ECONNREFUSED : 111 )); $stream->close(); From 5bd777a2a55464aa722edde4176504a8875bdbab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Mon, 29 Jun 2026 11:27:29 +0200 Subject: [PATCH 27/27] Prepare v1.10.0 release --- CHANGELOG.md | 11 +++++++++++ README.md | 18 ++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 52050cf..46b3d82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## 1.10.0 (2026-06-29) + +* Feature: Remove abandoned `ringcentral/psr7` dependency to support PSR-7 v2 (`psr/http-message`). + (#65 by @kwhat and @clue) + +* Feature: Improve PHP 8.5+ and PHP 8.4+ support and update test environment. + (#59, #60 and #67 by @clue) + +* Improve test suite to ensure 100% code coverage and support legacy PHP 7.2 with PHPUnit 8.5. + (#66 and #68 by @clue) + ## 1.9.0 (2024-04-10) * Feature / Fix: Forward compatibility with Promise v3. diff --git a/README.md b/README.md index 0b4db17..67bf46c 100644 --- a/README.md +++ b/README.md @@ -58,13 +58,11 @@ existing higher-level protocol implementation. ## Support us -We invest a lot of time developing, maintaining and updating our awesome -open-source projects. You can help us sustain this high-quality of our work by -[becoming a sponsor on GitHub](https://github.com/sponsors/clue). Sponsors get -numerous benefits in return, see our [sponsoring page](https://github.com/sponsors/clue) -for details. - -Let's take these projects to the next level together! 🚀 +I maintain an ecosystem of open-source projects that have been downloaded +hundreds of millions of times and are actively maintained and continuously +improved. If you find any of these projects useful, please consider +[becoming a sponsor on GitHub](https://github.com/sponsors/clue). Your support +helps ensure long-term maintenance and continued development. Thank you! 🚀 ## Quickstart example @@ -452,7 +450,7 @@ This project follows [SemVer](https://semver.org/). This will install the latest supported version: ```bash -composer require clue/http-proxy-react:^1.9 +composer require clue/http-proxy-react:^1.10 ``` See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades. @@ -496,8 +494,8 @@ XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text This project is released under the permissive [MIT license](LICENSE). -> Did you know that I offer custom development services and issuing invoices for - sponsorships of releases and for contributions? Contact me (@clue) for details. +> Do you use this project in a commercial setting? Sponsoring with invoicing is + available, contact me ([@clue](https://github.com/clue)) for details. ## More