From 18906cbcf1f5ca49d903ec1bbe0e474a506e2497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 27 Jul 2021 17:34:50 +0200 Subject: [PATCH] Simplify usage by supporting new Socket API without nullable loop args --- README.md | 31 ++--- composer.json | 2 +- examples/11-client-http-connect-proxy.php | 2 +- examples/12-client-socks-proxy.php | 2 +- examples/13-client-ssh-proxy.php | 2 +- examples/51-server-hello-world.php | 2 +- examples/52-server-count-visitors.php | 2 +- examples/53-server-whatsmyip.php | 2 +- examples/54-server-query-parameter.php | 2 +- examples/55-server-cookie-handling.php | 2 +- examples/56-server-sleep.php | 2 +- examples/57-server-error-handling.php | 2 +- examples/58-server-stream-response.php | 2 +- examples/59-server-json-api.php | 2 +- examples/61-server-hello-world-https.php | 10 +- examples/62-server-form-upload.php | 2 +- examples/63-server-streaming-request.php | 2 +- examples/71-server-http-proxy.php | 2 +- examples/72-server-http-connect-proxy.php | 2 +- examples/81-server-upgrade-echo.php | 2 +- examples/82-server-upgrade-chat.php | 2 +- examples/99-server-benchmark-download.php | 2 +- src/Browser.php | 2 +- src/Client/Client.php | 2 +- src/HttpServer.php | 20 +-- src/Io/Sender.php | 2 +- src/Io/StreamingServer.php | 6 +- tests/Client/FunctionalIntegrationTest.php | 20 +-- tests/FunctionalBrowserTest.php | 21 ++-- tests/FunctionalHttpServerTest.php | 136 ++++++++++----------- 30 files changed, 146 insertions(+), 144 deletions(-) diff --git a/README.md b/README.md index 0cfd0307..49d8ecbc 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterf ); }); -$socket = new React\Socket\Server(8080); +$socket = new React\Socket\SocketServer('127.0.0.1:8080'); $http->listen($socket); ``` @@ -260,7 +260,6 @@ like this: ```php $browser = new React\Http\Browser( new React\Socket\Connector( - null, array( 'timeout' => 5 ) @@ -604,7 +603,7 @@ $proxy = new Clue\React\HttpProxy\ProxyConnector( new React\Socket\Connector() ); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -631,7 +630,7 @@ $proxy = new Clue\React\Socks\Client( new React\Socket\Connector() ); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -660,7 +659,7 @@ plain HTTP and TLS-encrypted HTTPS. ```php $proxy = new Clue\React\SshProxy\SshSocksConnector('me@localhost:22', Loop::get()); -$connector = new React\Socket\Connector(null, array( +$connector = new React\Socket\Connector(array( 'tcp' => $proxy, 'dns' => false )); @@ -741,13 +740,13 @@ to be attached to an instance of [`React\Socket\ServerInterface`](https://github.com/reactphp/socket#serverinterface) through the [`listen()`](#listen) method as described in the following chapter. In its most simple form, you can attach this to a -[`React\Socket\Server`](https://github.com/reactphp/socket#server) in order -to start a plaintext HTTP server like this: +[`React\Socket\SocketServer`](https://github.com/reactphp/socket#socketserver) +in order to start a plaintext HTTP server like this: ```php $http = new React\Http\HttpServer($handler); -$socket = new React\Socket\Server('0.0.0.0:8080'); +$socket = new React\Socket\SocketServer('0.0.0.0:8080'); $http->listen($socket); ``` @@ -869,13 +868,13 @@ is responsible for emitting the underlying streaming connections. This HTTP server needs to be attached to it in order to process any connections and pase incoming streaming data as incoming HTTP request messages. In its most common form, you can attach this to a -[`React\Socket\Server`](https://github.com/reactphp/socket#server) in -order to start a plaintext HTTP server like this: +[`React\Socket\SocketServer`](https://github.com/reactphp/socket#socketserver) +in order to start a plaintext HTTP server like this: ```php $http = new React\Http\HttpServer($handler); -$socket = new React\Socket\Server('0.0.0.0:8080'); +$socket = new React\Socket\SocketServer('0.0.0.0:8080'); $http->listen($socket); ``` @@ -894,15 +893,17 @@ Likewise, it's usually recommended to use a reverse proxy setup to accept secure HTTPS requests on default HTTPS port `443` (TLS termination) and only route plaintext requests to this HTTP server. As an alternative, you can also accept secure HTTPS requests with this HTTP server by attaching -this to a [`React\Socket\Server`](https://github.com/reactphp/socket#server) +this to a [`React\Socket\SocketServer`](https://github.com/reactphp/socket#socketserver) using a secure TLS listen address, a certificate file and optional `passphrase` like this: ```php $http = new React\Http\HttpServer($handler); -$socket = new React\Socket\Server('tls://0.0.0.0:8443', null, array( - 'local_cert' => __DIR__ . '/localhost.pem' +$socket = new React\Socket\SocketServer('tls://0.0.0.0:8443', array( + 'tls' => array( + 'local_cert' => __DIR__ . '/localhost.pem' + ) )); $http->listen($socket); ``` @@ -1889,7 +1890,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' diff --git a/composer.json b/composer.json index 6673b232..44387e53 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "react/event-loop": "^1.2", "react/promise": "^2.3 || ^1.2.1", "react/promise-stream": "^1.1", - "react/socket": "^1.8", + "react/socket": "^1.9", "react/stream": "^1.2", "ringcentral/psr7": "^1.2" }, diff --git a/examples/11-client-http-connect-proxy.php b/examples/11-client-http-connect-proxy.php index ca7104aa..39b0cbcb 100644 --- a/examples/11-client-http-connect-proxy.php +++ b/examples/11-client-http-connect-proxy.php @@ -17,7 +17,7 @@ $proxy = new HttpConnectClient('127.0.0.1:8080', new Connector()); // create a Browser object that uses the HTTP CONNECT proxy client for connections -$connector = new Connector(null, array( +$connector = new Connector(array( 'tcp' => $proxy, 'dns' => false )); diff --git a/examples/12-client-socks-proxy.php b/examples/12-client-socks-proxy.php index 5801b138..ce020ad8 100644 --- a/examples/12-client-socks-proxy.php +++ b/examples/12-client-socks-proxy.php @@ -14,7 +14,7 @@ $proxy = new SocksClient('127.0.0.1:1080', new Connector()); // create a Browser object that uses the SOCKS proxy client for connections -$connector = new Connector(null, array( +$connector = new Connector(array( 'tcp' => $proxy, 'dns' => false )); diff --git a/examples/13-client-ssh-proxy.php b/examples/13-client-ssh-proxy.php index 764fb30f..d4acaba0 100644 --- a/examples/13-client-ssh-proxy.php +++ b/examples/13-client-ssh-proxy.php @@ -13,7 +13,7 @@ $proxy = new SshSocksConnector(isset($argv[1]) ? $argv[1] : 'localhost:22', Loop::get()); // create a Browser object that uses the SSH proxy client for connections -$connector = new Connector(null, array( +$connector = new Connector(array( 'tcp' => $proxy, 'dns' => false )); diff --git a/examples/51-server-hello-world.php b/examples/51-server-hello-world.php index 50c82396..f549ece8 100644 --- a/examples/51-server-hello-world.php +++ b/examples/51-server-hello-world.php @@ -15,7 +15,7 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/52-server-count-visitors.php b/examples/52-server-count-visitors.php index 1884acb0..d52285d0 100644 --- a/examples/52-server-count-visitors.php +++ b/examples/52-server-count-visitors.php @@ -16,7 +16,7 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/53-server-whatsmyip.php b/examples/53-server-whatsmyip.php index 89dad25c..5df1050d 100644 --- a/examples/53-server-whatsmyip.php +++ b/examples/53-server-whatsmyip.php @@ -17,7 +17,7 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/54-server-query-parameter.php b/examples/54-server-query-parameter.php index 2ab16647..22be7566 100644 --- a/examples/54-server-query-parameter.php +++ b/examples/54-server-query-parameter.php @@ -24,7 +24,7 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/55-server-cookie-handling.php b/examples/55-server-cookie-handling.php index ff89fe1e..a6858061 100644 --- a/examples/55-server-cookie-handling.php +++ b/examples/55-server-cookie-handling.php @@ -30,7 +30,7 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/56-server-sleep.php b/examples/56-server-sleep.php index bd2ea694..caa22644 100644 --- a/examples/56-server-sleep.php +++ b/examples/56-server-sleep.php @@ -22,7 +22,7 @@ }); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/57-server-error-handling.php b/examples/57-server-error-handling.php index f5281c53..4a1b6757 100644 --- a/examples/57-server-error-handling.php +++ b/examples/57-server-error-handling.php @@ -27,7 +27,7 @@ }); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/58-server-stream-response.php b/examples/58-server-stream-response.php index d965e306..2069b7a8 100644 --- a/examples/58-server-stream-response.php +++ b/examples/58-server-stream-response.php @@ -38,7 +38,7 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/59-server-json-api.php b/examples/59-server-json-api.php index c06a9702..7fa8cc66 100644 --- a/examples/59-server-json-api.php +++ b/examples/59-server-json-api.php @@ -52,7 +52,7 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/61-server-hello-world-https.php b/examples/61-server-hello-world-https.php index 34f1a8bd..e5e0ed84 100644 --- a/examples/61-server-hello-world-https.php +++ b/examples/61-server-hello-world-https.php @@ -15,12 +15,14 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$socket = new React\Socket\SecureServer($socket, null, array( - 'local_cert' => isset($argv[2]) ? $argv[2] : __DIR__ . '/localhost.pem' +$uri = 'tls://' . (isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer($uri, array( + 'tls' => array( + 'local_cert' => isset($argv[2]) ? $argv[2] : __DIR__ . '/localhost.pem' + ) )); $http->listen($socket); -//$socket->on('error', 'printf'); +$socket->on('error', 'printf'); echo 'Listening on ' . str_replace('tls:', 'https:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/62-server-form-upload.php b/examples/62-server-form-upload.php index 5a0fdace..6984b4e3 100644 --- a/examples/62-server-form-upload.php +++ b/examples/62-server-form-upload.php @@ -128,7 +128,7 @@ $handler ); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/63-server-streaming-request.php b/examples/63-server-streaming-request.php index 693cf2c0..2b5f8a6c 100644 --- a/examples/63-server-streaming-request.php +++ b/examples/63-server-streaming-request.php @@ -44,7 +44,7 @@ function (Psr\Http\Message\ServerRequestInterface $request) { $http->on('error', 'printf'); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/71-server-http-proxy.php b/examples/71-server-http-proxy.php index f6ab5ff5..c4fe244e 100644 --- a/examples/71-server-http-proxy.php +++ b/examples/71-server-http-proxy.php @@ -44,7 +44,7 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/72-server-http-connect-proxy.php b/examples/72-server-http-connect-proxy.php index 4399614b..205dd1da 100644 --- a/examples/72-server-http-connect-proxy.php +++ b/examples/72-server-http-connect-proxy.php @@ -50,7 +50,7 @@ function ($e) { ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/81-server-upgrade-echo.php b/examples/81-server-upgrade-echo.php index cb5d0b0d..2f77172f 100644 --- a/examples/81-server-upgrade-echo.php +++ b/examples/81-server-upgrade-echo.php @@ -56,7 +56,7 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/82-server-upgrade-chat.php b/examples/82-server-upgrade-chat.php index 22fed927..42635e8c 100644 --- a/examples/82-server-upgrade-chat.php +++ b/examples/82-server-upgrade-chat.php @@ -84,7 +84,7 @@ ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/examples/99-server-benchmark-download.php b/examples/99-server-benchmark-download.php index bdc6735a..df0e69e7 100644 --- a/examples/99-server-benchmark-download.php +++ b/examples/99-server-benchmark-download.php @@ -122,7 +122,7 @@ public function getSize() ); }); -$socket = new React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); +$socket = new React\Socket\SocketServer(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); $http->listen($socket); echo 'Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()) . PHP_EOL; diff --git a/src/Browser.php b/src/Browser.php index 11519094..5879b977 100644 --- a/src/Browser.php +++ b/src/Browser.php @@ -47,7 +47,7 @@ class Browser * [`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' diff --git a/src/Client/Client.php b/src/Client/Client.php index f28ec289..7a97349c 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -16,7 +16,7 @@ class Client public function __construct(LoopInterface $loop, ConnectorInterface $connector = null) { if ($connector === null) { - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); } $this->connector = $connector; diff --git a/src/HttpServer.php b/src/HttpServer.php index 582ea380..d5f947d5 100644 --- a/src/HttpServer.php +++ b/src/HttpServer.php @@ -54,13 +54,13 @@ * [`React\Socket\ServerInterface`](https://github.com/reactphp/socket#serverinterface) * through the [`listen()`](#listen) method as described in the following * chapter. In its most simple form, you can attach this to a - * [`React\Socket\Server`](https://github.com/reactphp/socket#server) in order - * to start a plaintext HTTP server like this: + * [`React\Socket\SocketServer`](https://github.com/reactphp/socket#socketserver) + * in order to start a plaintext HTTP server like this: * * ```php * $http = new React\Http\HttpServer($handler); * - * $socket = new React\Socket\Server('0.0.0.0:8080'); + * $socket = new React\Socket\SocketServer('0.0.0.0:8080'); * $http->listen($socket); * ``` * @@ -263,13 +263,13 @@ public function __construct($requestHandlerOrLoop) * HTTP server needs to be attached to it in order to process any * connections and pase incoming streaming data as incoming HTTP request * messages. In its most common form, you can attach this to a - * [`React\Socket\Server`](https://github.com/reactphp/socket#server) in - * order to start a plaintext HTTP server like this: + * [`React\Socket\SocketServer`](https://github.com/reactphp/socket#socketserver) + * in order to start a plaintext HTTP server like this: * * ```php * $http = new React\Http\HttpServer($handler); * - * $socket = new React\Socket\Server(8080); + * $socket = new React\Socket\SocketServer('0.0.0.0:8080'); * $http->listen($socket); * ``` * @@ -288,15 +288,17 @@ public function __construct($requestHandlerOrLoop) * secure HTTPS requests on default HTTPS port `443` (TLS termination) and * only route plaintext requests to this HTTP server. As an alternative, you * can also accept secure HTTPS requests with this HTTP server by attaching - * this to a [`React\Socket\Server`](https://github.com/reactphp/socket#server) + * this to a [`React\Socket\SocketServer`](https://github.com/reactphp/socket#socketserver) * using a secure TLS listen address, a certificate file and optional * `passphrase` like this: * * ```php * $http = new React\Http\HttpServer($handler); * - * $socket = new React\Socket\Server('tls://0.0.0.0:8443', null, array( - * 'local_cert' => __DIR__ . '/localhost.pem' + * $socket = new React\Socket\SocketServer('tls://0.0.0.0:8443', array( + * 'tls' => array( + * 'local_cert' => __DIR__ . '/localhost.pem' + * ) * )); * $http->listen($socket); * ``` diff --git a/src/Io/Sender.php b/src/Io/Sender.php index 1eb098c6..c1bbab42 100644 --- a/src/Io/Sender.php +++ b/src/Io/Sender.php @@ -39,7 +39,7 @@ class Sender * settings. You can use this method manually like this: * * ```php - * $connector = new \React\Socket\Connector($loop); + * $connector = new \React\Socket\Connector(array(), $loop); * $sender = \React\Http\Io\Sender::createFromLoop($loop, $connector); * ``` * diff --git a/src/Io/StreamingServer.php b/src/Io/StreamingServer.php index 95e0e4c6..2c912dfa 100644 --- a/src/Io/StreamingServer.php +++ b/src/Io/StreamingServer.php @@ -20,7 +20,7 @@ * The internal `StreamingServer` class is responsible for handling incoming connections and then * processing each incoming HTTP request. * - * Unlike the [`HttpServer`](#server) class, it does not buffer and parse the incoming + * Unlike the [`HttpServer`](#httpserver) class, it does not buffer and parse the incoming * HTTP request body by default. This means that the request handler will be * invoked with a streaming request body. Once the request headers have been * received, it will invoke the request handler function. This request handler @@ -50,13 +50,13 @@ * In order to process any connections, the server needs to be attached to an * instance of `React\Socket\ServerInterface` through the [`listen()`](#listen) method * as described in the following chapter. In its most simple form, you can attach - * this to a [`React\Socket\Server`](https://github.com/reactphp/socket#server) + * this to a [`React\Socket\SocketServer`](https://github.com/reactphp/socket#socketserver) * in order to start a plaintext HTTP server like this: * * ```php * $server = new StreamingServer($loop, $handler); * - * $socket = new React\Socket\Server('0.0.0.0:8080', $loop); + * $socket = new React\Socket\SocketServer('0.0.0.0:8080', array(), $loop); * $server->listen($socket); * ``` * diff --git a/tests/Client/FunctionalIntegrationTest.php b/tests/Client/FunctionalIntegrationTest.php index 3e07803a..d3219426 100644 --- a/tests/Client/FunctionalIntegrationTest.php +++ b/tests/Client/FunctionalIntegrationTest.php @@ -8,9 +8,9 @@ use React\Http\Client\Client; use React\Promise\Deferred; use React\Promise\Stream; -use React\Socket\Server; use React\Socket\ConnectionInterface; use React\Stream\ReadableStreamInterface; +use React\Socket\SocketServer; use React\Tests\Http\TestCase; class FunctionalIntegrationTest extends TestCase @@ -39,13 +39,13 @@ public function testRequestToLocalhostEmitsSingleRemoteConnection() { $loop = Factory::create(); - $server = new Server(0, $loop); - $server->on('connection', $this->expectCallableOnce()); - $server->on('connection', function (ConnectionInterface $conn) use ($server) { + $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket->on('connection', $this->expectCallableOnce()); + $socket->on('connection', function (ConnectionInterface $conn) use ($socket) { $conn->end("HTTP/1.1 200 OK\r\n\r\nOk"); - $server->close(); + $socket->close(); }); - $port = parse_url($server->getAddress(), PHP_URL_PORT); + $port = parse_url($socket->getAddress(), PHP_URL_PORT); $client = new Client($loop); $request = $client->request('GET', 'http://localhost:' . $port); @@ -60,14 +60,14 @@ public function testRequestLegacyHttpServerWithOnlyLineFeedReturnsSuccessfulResp { $loop = Factory::create(); - $server = new Server(0, $loop); - $server->on('connection', function (ConnectionInterface $conn) use ($server) { + $socket = new SocketServer('127.0.0.1:0', array(), $loop); + $socket->on('connection', function (ConnectionInterface $conn) use ($socket) { $conn->end("HTTP/1.0 200 OK\n\nbody"); - $server->close(); + $socket->close(); }); $client = new Client($loop); - $request = $client->request('GET', str_replace('tcp:', 'http:', $server->getAddress())); + $request = $client->request('GET', str_replace('tcp:', 'http:', $socket->getAddress())); $once = $this->expectCallableOnceWith('body'); $request->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($once) { diff --git a/tests/FunctionalBrowserTest.php b/tests/FunctionalBrowserTest.php index a46e74a6..5db62003 100644 --- a/tests/FunctionalBrowserTest.php +++ b/tests/FunctionalBrowserTest.php @@ -14,6 +14,7 @@ use React\Promise\Promise; use React\Promise\Stream; use React\Socket\Connector; +use React\Socket\SocketServer; use React\Stream\ReadableStreamInterface; use React\Stream\ThroughStream; use RingCentral\Psr7\Request; @@ -141,7 +142,7 @@ public function setUpBrowserAndServer() var_dump($path); }); - $socket = new \React\Socket\Server(0, $this->loop); + $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; @@ -392,11 +393,11 @@ public function testVerifyPeerEnabledForBadSslRejects() $this->markTestSkipped('Not supported on HHVM'); } - $connector = new Connector($this->loop, array( + $connector = new Connector(array( 'tls' => array( 'verify_peer' => true ) - )); + ), $this->loop); $browser = new Browser($connector, $this->loop); @@ -414,11 +415,11 @@ public function testVerifyPeerDisabledForBadSslResolves() $this->markTestSkipped('Not supported on HHVM'); } - $connector = new Connector($this->loop, array( + $connector = new Connector(array( 'tls' => array( 'verify_peer' => false ) - )); + ), $this->loop); $browser = new Browser($connector, $this->loop); @@ -497,7 +498,7 @@ public function testRequestStreamWithHeadRequestReturnsEmptyResponseBodWithTrans public function testRequestStreamReturnsResponseWithResponseBodyUndecodedWhenResponseHasDoubleTransferEncoding() { - $socket = new \React\Socket\Server(0, $this->loop); + $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); $socket->on('connection', function (\React\Socket\ConnectionInterface $connection) { $connection->on('data', function () use ($connection) { $connection->end("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked, chunked\r\nConnection: close\r\n\r\nhello"); @@ -517,7 +518,7 @@ public function testRequestStreamReturnsResponseWithResponseBodyUndecodedWhenRes public function testReceiveStreamAndExplicitlyCloseConnectionEvenWhenServerKeepsConnectionOpen() { $closed = new \React\Promise\Deferred(); - $socket = new \React\Socket\Server(0, $this->loop); + $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); $socket->on('connection', function (\React\Socket\ConnectionInterface $connection) use ($closed) { $connection->on('data', function () use ($connection) { $connection->write("HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello"); @@ -576,7 +577,7 @@ public function testPostStreamWillStartSendingRequestEvenWhenBodyDoesNotEmitData $http = new HttpServer($this->loop, new StreamingRequestMiddleware(), function (ServerRequestInterface $request) { return new Response(200); }); - $socket = new \React\Socket\Server(0, $this->loop); + $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; @@ -607,7 +608,7 @@ public function testSendsHttp11ByDefault() $request->getProtocolVersion() ); }); - $socket = new \React\Socket\Server(0, $this->loop); + $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; @@ -627,7 +628,7 @@ public function testSendsExplicitHttp10Request() $request->getProtocolVersion() ); }); - $socket = new \React\Socket\Server(0, $this->loop); + $socket = new SocketServer('127.0.0.1:0', array(), $this->loop); $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; diff --git a/tests/FunctionalHttpServerTest.php b/tests/FunctionalHttpServerTest.php index f7aa8392..fe0e1936 100644 --- a/tests/FunctionalHttpServerTest.php +++ b/tests/FunctionalHttpServerTest.php @@ -11,10 +11,9 @@ use React\Http\Middleware\LimitConcurrentRequestsMiddleware; use React\Http\Middleware\RequestBodyBufferMiddleware; use React\Http\Middleware\StreamingRequestMiddleware; -use React\Socket\Server as Socket; -use React\Socket\Connector; use React\Socket\ConnectionInterface; -use React\Socket\SecureServer; +use React\Socket\Connector; +use React\Socket\SocketServer; use React\Promise; use React\Promise\Stream; use React\Stream\ThroughStream; @@ -24,13 +23,13 @@ class FunctionalHttpServerTest extends TestCase public function testPlainHttpOnRandomPort() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -50,7 +49,7 @@ public function testPlainHttpOnRandomPort() public function testPlainHttpOnRandomPortWithSingleRequestHandlerArray() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer( $loop, @@ -59,7 +58,7 @@ function () { } ); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -78,13 +77,13 @@ function () { public function testPlainHttpOnRandomPortWithoutHostHeaderUsesSocketUri() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -104,13 +103,13 @@ public function testPlainHttpOnRandomPortWithoutHostHeaderUsesSocketUri() public function testPlainHttpOnRandomPortWithOtherHostHeaderTakesPrecedence() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -134,18 +133,17 @@ public function testSecureHttpsOnRandomPort() } $loop = Factory::create(); - $connector = new Connector($loop, array( + $connector = new Connector(array( 'tls' => array('verify_peer' => false) - )); + ), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $socket = new Socket(0, $loop); - $socket = new SecureServer($socket, $loop, array( + $socket = new SocketServer('tls://127.0.0.1:0', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )); + )), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -178,15 +176,14 @@ public function testSecureHttpsReturnsData() ); }); - $socket = new Socket(0, $loop); - $socket = new SecureServer($socket, $loop, array( + $socket = new SocketServer('tls://127.0.0.1:0', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )); + )), $loop); $http->listen($socket); - $connector = new Connector($loop, array( + $connector = new Connector(array( 'tls' => array('verify_peer' => false) - )); + ), $loop); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); @@ -210,18 +207,17 @@ public function testSecureHttpsOnRandomPortWithoutHostHeaderUsesSocketUri() } $loop = Factory::create(); - $connector = new Connector($loop, array( + $connector = new Connector(array( 'tls' => array('verify_peer' => false) - )); + ), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $socket = new Socket(0, $loop); - $socket = new SecureServer($socket, $loop, array( + $socket = new SocketServer('tls://127.0.0.1:0', array('tls' => array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )); + )), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -242,11 +238,11 @@ public function testPlainHttpOnStandardPortReturnsUriWithNoPort() { $loop = Factory::create(); try { - $socket = new Socket(80, $loop); + $socket = new SocketServer('127.0.0.1:80', array(), $loop); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 80 failed (root and unused?)'); } - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); @@ -272,11 +268,11 @@ public function testPlainHttpOnStandardPortWithoutHostHeaderReturnsUriWithNoPort { $loop = Factory::create(); try { - $socket = new Socket(80, $loop); + $socket = new SocketServer('127.0.0.1:80', array(), $loop); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 80 failed (root and unused?)'); } - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); @@ -306,16 +302,16 @@ public function testSecureHttpsOnStandardPortReturnsUriWithNoPort() $loop = Factory::create(); try { - $socket = new Socket(443, $loop); + $socket = new SocketServer('127.0.0.1:443', array('tls' => array( + 'local_cert' => __DIR__ . '/../examples/localhost.pem' + )), $loop); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 443 failed (root and unused?)'); } - $socket = new SecureServer($socket, $loop, array( - 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )); - $connector = new Connector($loop, array( + + $connector = new Connector(array( 'tls' => array('verify_peer' => false) - )); + ), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); @@ -345,16 +341,16 @@ public function testSecureHttpsOnStandardPortWithoutHostHeaderUsesSocketUri() $loop = Factory::create(); try { - $socket = new Socket(443, $loop); + $socket = new SocketServer('127.0.0.1:443', array('tls' => array( + 'local_cert' => __DIR__ . '/../examples/localhost.pem' + )), $loop); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 443 failed (root and unused?)'); } - $socket = new SecureServer($socket, $loop, array( - 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )); - $connector = new Connector($loop, array( + + $connector = new Connector(array( 'tls' => array('verify_peer' => false) - )); + ), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); @@ -380,11 +376,11 @@ public function testPlainHttpOnHttpsStandardPortReturnsUriWithPort() { $loop = Factory::create(); try { - $socket = new Socket(443, $loop); + $socket = new SocketServer('127.0.0.1:443', array(), $loop); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 443 failed (root and unused?)'); } - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); @@ -414,16 +410,16 @@ public function testSecureHttpsOnHttpStandardPortReturnsUriWithPort() $loop = Factory::create(); try { - $socket = new Socket(80, $loop); + $socket = new SocketServer('127.0.0.1:80', array('tls' => array( + 'local_cert' => __DIR__ . '/../examples/localhost.pem' + )), $loop); } catch (\RuntimeException $e) { $this->markTestSkipped('Listening on port 80 failed (root and unused?)'); } - $socket = new SecureServer($socket, $loop, array( - 'local_cert' => __DIR__ . '/../examples/localhost.pem' - )); - $connector = new Connector($loop, array( + + $connector = new Connector(array( 'tls' => array('verify_peer' => false) - )); + ), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri() . 'x' . $request->getHeaderLine('Host')); @@ -448,7 +444,7 @@ public function testSecureHttpsOnHttpStandardPortReturnsUriWithPort() public function testClosedStreamFromRequestHandlerWillSendEmptyBody() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $stream = new ThroughStream(); $stream->close(); @@ -457,7 +453,7 @@ public function testClosedStreamFromRequestHandlerWillSendEmptyBody() return new Response(200, array(), $stream); }); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -477,7 +473,7 @@ public function testClosedStreamFromRequestHandlerWillSendEmptyBody() public function testRequestHandlerWithStreamingRequestWillReceiveCloseEventIfConnectionClosesWhileSendingBody() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $once = $this->expectCallableOnce(); $http = new HttpServer( @@ -488,7 +484,7 @@ function (RequestInterface $request) use ($once) { } ); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) { @@ -507,7 +503,7 @@ function (RequestInterface $request) use ($once) { public function testStreamFromRequestHandlerWillBeClosedIfConnectionClosesWhileSendingStreamingRequestBody() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $stream = new ThroughStream(); @@ -519,7 +515,7 @@ function (RequestInterface $request) use ($stream) { } ); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) { @@ -541,7 +537,7 @@ function (RequestInterface $request) use ($stream) { public function testStreamFromRequestHandlerWillBeClosedIfConnectionCloses() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $stream = new ThroughStream(); @@ -549,7 +545,7 @@ public function testStreamFromRequestHandlerWillBeClosedIfConnectionCloses() return new Response(200, array(), $stream); }); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) { @@ -571,7 +567,7 @@ public function testStreamFromRequestHandlerWillBeClosedIfConnectionCloses() public function testUpgradeWithThroughStreamReturnsDataAsGiven() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { $stream = new ThroughStream(); @@ -583,7 +579,7 @@ public function testUpgradeWithThroughStreamReturnsDataAsGiven() return new Response(101, array('Upgrade' => 'echo'), $stream); }); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -608,7 +604,7 @@ public function testUpgradeWithThroughStreamReturnsDataAsGiven() public function testUpgradeWithRequestBodyAndThroughStreamReturnsDataAsGiven() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { $stream = new ThroughStream(); @@ -620,7 +616,7 @@ public function testUpgradeWithRequestBodyAndThroughStreamReturnsDataAsGiven() return new Response(101, array('Upgrade' => 'echo'), $stream); }); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -646,7 +642,7 @@ public function testUpgradeWithRequestBodyAndThroughStreamReturnsDataAsGiven() public function testConnectWithThroughStreamReturnsDataAsGiven() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { $stream = new ThroughStream(); @@ -658,7 +654,7 @@ public function testConnectWithThroughStreamReturnsDataAsGiven() return new Response(200, array(), $stream); }); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -683,7 +679,7 @@ public function testConnectWithThroughStreamReturnsDataAsGiven() public function testConnectWithThroughStreamReturnedFromPromiseReturnsDataAsGiven() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { $stream = new ThroughStream(); @@ -699,7 +695,7 @@ public function testConnectWithThroughStreamReturnedFromPromiseReturnsDataAsGive }); }); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -724,7 +720,7 @@ public function testConnectWithThroughStreamReturnedFromPromiseReturnsDataAsGive public function testConnectWithClosedThroughStreamReturnsNoData() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer($loop, function (RequestInterface $request) { $stream = new ThroughStream(); @@ -733,7 +729,7 @@ public function testConnectWithClosedThroughStreamReturnsNoData() return new Response(200, array(), $stream); }); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { @@ -758,7 +754,7 @@ public function testConnectWithClosedThroughStreamReturnsNoData() public function testLimitConcurrentRequestsMiddlewareRequestStreamPausing() { $loop = Factory::create(); - $connector = new Connector($loop); + $connector = new Connector(array(), $loop); $http = new HttpServer( $loop, @@ -776,7 +772,7 @@ function (ServerRequestInterface $request) { } ); - $socket = new Socket(0, $loop); + $socket = new SocketServer('127.0.0.1:0', array(), $loop); $http->listen($socket); $result = array();