From e654f59a267e72a157916a8adeb48c6f961bfea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sat, 24 Jul 2021 14:46:19 +0200 Subject: [PATCH 1/2] Rename `Server` to `HttpServer` to avoid class name collisions --- README.md | 128 +++++++++--------- examples/51-server-hello-world.php | 7 +- examples/52-server-count-visitors.php | 7 +- examples/53-server-whatsmyip.php | 7 +- examples/54-server-query-parameter.php | 7 +- examples/55-server-cookie-handling.php | 7 +- examples/56-server-sleep.php | 7 +- examples/57-server-error-handling.php | 7 +- examples/58-server-stream-response.php | 7 +- examples/59-server-json-api.php | 7 +- examples/61-server-hello-world-https.php | 9 +- examples/62-server-form-upload.php | 7 +- examples/63-server-streaming-request.php | 8 +- examples/71-server-http-proxy.php | 10 +- examples/72-server-http-connect-proxy.php | 9 +- examples/81-server-upgrade-echo.php | 9 +- examples/82-server-upgrade-chat.php | 9 +- examples/99-server-benchmark-download.php | 7 +- src/{Server.php => HttpServer.php} | 32 +++-- src/Io/StreamingServer.php | 8 +- .../LimitConcurrentRequestsMiddleware.php | 6 +- src/Middleware/StreamingRequestMiddleware.php | 10 +- tests/FunctionalBrowserTest.php | 18 +-- ...rTest.php => FunctionalHttpServerTest.php} | 106 +++++++-------- tests/{ServerTest.php => HttpServerTest.php} | 98 +++++++------- 25 files changed, 264 insertions(+), 273 deletions(-) rename src/{Server.php => HttpServer.php} (93%) rename tests/{FunctionalServerTest.php => FunctionalHttpServerTest.php} (90%) rename tests/{ServerTest.php => HttpServerTest.php} (81%) diff --git a/README.md b/README.md index 126fdcaf..d0fe6961 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ multiple concurrent HTTP requests without blocking. * [SSH proxy](#ssh-proxy) * [Unix domain sockets](#unix-domain-sockets) * [Server Usage](#server-usage) - * [Server](#server) + * [HttpServer](#httpserver) * [listen()](#listen) * [Server Request](#server-request) * [Request parameters](#request-parameters) @@ -97,7 +97,7 @@ $client->get('http://www.google.com/')->then(function (Psr\Http\Message\Response This is an HTTP server which responds with `Hello World!` to every request. ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { return new React\Http\Message\Response( 200, array( @@ -108,7 +108,7 @@ $server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterfac }); $socket = new React\Socket\Server(8080); -$server->listen($socket); +$http->listen($socket); ``` See also the [examples](examples/). @@ -699,9 +699,11 @@ See also the [Unix Domain Sockets (UDS) example](examples/14-client-unix-domain- ## Server Usage -### Server +### HttpServer -The `React\Http\Server` class is responsible for handling incoming connections and then + + +The `React\Http\HttpServer` class is responsible for handling incoming connections and then processing each incoming HTTP request. When a complete HTTP request has been received, it will invoke the given @@ -710,7 +712,7 @@ the constructor and will be invoked with the respective [request](#server-reques object and expects a [response](#server-response) object in return: ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { return new React\Http\Message\Response( 200, array( @@ -735,7 +737,7 @@ here in order to use the [default loop](https://github.com/reactphp/event-loop#l This value SHOULD NOT be given unless you're sure you want to explicitly use a given event loop instance. -In order to start listening for any incoming connections, the `Server` needs +In order to start listening for any incoming connections, the `HttpServer` needs 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 @@ -744,17 +746,17 @@ chapter. In its most simple form, you can attach this to a to start a plaintext HTTP server like this: ```php -$server = new React\Http\Server($handler); +$http = new React\Http\HttpServer($handler); $socket = new React\Socket\Server('0.0.0.0:8080'); -$server->listen($socket); +$http->listen($socket); ``` See also the [`listen()`](#listen) method and the [hello world server example](examples/51-server-hello-world.php) for more details. -By default, the `Server` buffers and parses the complete incoming HTTP +By default, the `HttpServer` buffers and parses the complete incoming HTTP request in memory. It will invoke the given request handler function when the complete request headers and request body has been received. This means the [request](#server-request) object passed to your request handler function will be @@ -807,14 +809,14 @@ limit to allow for more concurrent requests (set `memory_limit 512M` or more) or explicitly limit concurrency. In order to override the above buffering defaults, you can configure the -`Server` explicitly. You can use the +`HttpServer` explicitly. You can use the [`LimitConcurrentRequestsMiddleware`](#limitconcurrentrequestsmiddleware) and [`RequestBodyBufferMiddleware`](#requestbodybuffermiddleware) (see below) to explicitly configure the total number of requests that can be handled at once like this: ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers new React\Http\Middleware\RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request @@ -839,7 +841,7 @@ also use a streaming approach where only small chunks of data have to be kept in memory: ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), $handler ); @@ -854,6 +856,10 @@ have full control over consuming the incoming HTTP request body and concurrency settings. See also [streaming incoming request](#streaming-incoming-request) below for more details. +> Changelog v1.5.0: This class has been renamed to `HttpServer` from the + previous `Server` class in order to avoid any ambiguities. + The previous name has been deprecated and should not be used anymore. + ### listen() The `listen(React\Socket\ServerInterface $socket): void` method can be used to @@ -868,10 +874,10 @@ messages. In its most common form, you can attach this to a order to start a plaintext HTTP server like this: ```php -$server = new React\Http\Server($handler); +$http = new React\Http\HttpServer($handler); $socket = new React\Socket\Server('0.0.0.0:8080'); -$server->listen($socket); +$http->listen($socket); ``` See also [hello world server example](examples/51-server-hello-world.php) @@ -894,12 +900,12 @@ using a secure TLS listen address, a certificate file and optional `passphrase` like this: ```php -$server = new React\Http\Server($handler); +$http = new React\Http\HttpServer($handler); $socket = new React\Socket\Server('tls://0.0.0.0:8443', null, array( 'local_cert' => __DIR__ . '/localhost.pem' )); -$server->listen($socket); +$http->listen($socket); ``` See also [hello world HTTPS example](examples/61-server-hello-world-https.php) @@ -907,7 +913,7 @@ for more details. ### Server Request -As seen above, the [`Server`](#server) class is responsible for handling +As seen above, the [`HttpServer`](#httpserver) class is responsible for handling incoming connections and then processing each incoming HTTP request. The request object will be processed once the request has @@ -919,7 +925,7 @@ which in turn extends the and will be passed to the callback function like this. ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { $body = "The method of the request is: " . $request->getMethod(); $body .= "The requested path is: " . $request->getUri()->getPath(); @@ -962,7 +968,7 @@ The following parameters are currently available: Set to 'on' if the request used HTTPS, otherwise it won't be set ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { $body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR']; return new React\Http\Message\Response( @@ -987,7 +993,7 @@ The `getQueryParams(): array` method can be used to get the query parameters similiar to the `$_GET` variable. ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { $queryParams = $request->getQueryParams(); $body = 'The query parameter "foo" is not set. Click the following link '; @@ -1017,7 +1023,7 @@ See also [server query parameters example](examples/54-server-query-parameter.ph #### Request body -By default, the [`Server`](#server) will buffer and parse the full request body +By default, the [`Server`](#httpserver) will buffer and parse the full request body in memory. This means the given request object includes the parsed request body and any file uploads. @@ -1041,7 +1047,7 @@ By default, this method will only return parsed data for requests using request headers (commonly used for `POST` requests for HTML form submission data). ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { $name = $request->getParsedBody()['name'] ?? 'anonymous'; return new React\Http\Message\Response( @@ -1065,7 +1071,7 @@ an XML (`Content-Type: application/xml`) request body (which is commonly used fo `POST`, `PUT` or `PATCH` requests in JSON-based or RESTful/RESTish APIs). ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { $data = json_decode((string)$request->getBody()); $name = $data->name ?? 'anonymous'; @@ -1088,7 +1094,7 @@ This array will only be filled when using the `Content-Type: multipart/form-data request header (commonly used for `POST` requests for HTML file uploads). ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { $files = $request->getUploadedFiles(); $name = isset($files['avatar']) ? $files['avatar']->getClientFilename() : 'nothing'; @@ -1112,7 +1118,7 @@ This method operates on the buffered request body, i.e. the request body size is always known, even when the request does not specify a `Content-Length` request header or when using `Transfer-Encoding: chunked` for HTTP/1.1 requests. -> Note: The `Server` automatically takes care of handling requests with the +> Note: The `HttpServer` automatically takes care of handling requests with the additional `Expect: 100-continue` request header. When HTTP/1.1 clients want to send a bigger request body, they MAY send only the request headers with an additional `Expect: 100-continue` request header and wait before sending the actual @@ -1161,7 +1167,7 @@ The [ReactPHP `ReadableStreamInterface`](https://github.com/reactphp/stream#read gives you access to the incoming request body as the individual chunks arrive: ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), function (Psr\Http\Message\ServerRequestInterface $request) { $body = $request->getBody(); @@ -1234,7 +1240,7 @@ This method operates on the streaming request body, i.e. the request body size may be unknown (`null`) when using `Transfer-Encoding: chunked` for HTTP/1.1 requests. ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), function (Psr\Http\Message\ServerRequestInterface $request) { $size = $request->getBody()->getSize(); @@ -1262,7 +1268,7 @@ $server = new React\Http\Server( ); ``` -> Note: The `Server` automatically takes care of handling requests with the +> Note: The `HttpServer` automatically takes care of handling requests with the additional `Expect: 100-continue` request header. When HTTP/1.1 clients want to send a bigger request body, they MAY send only the request headers with an additional `Expect: 100-continue` request header and wait before sending the actual @@ -1307,7 +1313,7 @@ The `getCookieParams(): string[]` method can be used to get all cookies sent with the current request. ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { $key = 'react\php'; if (isset($request->getCookieParams()[$key])) { @@ -1344,7 +1350,7 @@ See also [cookie server example](examples/55-server-cookie-handling.php) for mor #### Invalid request -The `Server` class supports both HTTP/1.1 and HTTP/1.0 request messages. +The `HttpServer` class supports both HTTP/1.1 and HTTP/1.0 request messages. If a client sends an invalid request message, uses an invalid HTTP protocol version or sends an invalid `Transfer-Encoding` request header value, the server will automatically send a `400` (Bad Request) HTTP error response @@ -1353,7 +1359,7 @@ On top of this, it will emit an `error` event that can be used for logging purposes like this: ```php -$server->on('error', function (Exception $e) { +$http->on('error', function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; }); ``` @@ -1364,7 +1370,7 @@ valid response object from your request handler function. See also ### Server Response -The callback function passed to the constructor of the [`Server`](#server) is +The callback function passed to the constructor of the [`HttpServer`](#httpserver) is responsible for processing the request and returning a response, which will be delivered to the client. @@ -1379,7 +1385,7 @@ This projects ships a [`Response` class](#response) which implements the In its most simple form, you can use it like this: ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { return new React\Http\Message\Response( 200, array( @@ -1407,7 +1413,7 @@ To prevent this you SHOULD use a This example shows how such a long-term action could look like: ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { return new Promise(function ($resolve, $reject) { Loop::addTimer(1.5, function() use ($resolve) { $response = new React\Http\Message\Response( @@ -1445,7 +1451,7 @@ Note that other implementations of the may only support strings. ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { $stream = new ThroughStream(); $timer = Loop::addPeriodicTimer(0.5, function () use ($stream) { @@ -1537,7 +1543,7 @@ added automatically. This is the most common use case, for example when using a `string` response body like this: ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { return new React\Http\Message\Response( 200, array( @@ -1556,7 +1562,7 @@ response messages will contain the plain response body. If you know the length of your streaming response body, you MAY want to specify it explicitly like this: ```php -$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { $stream = new ThroughStream(); Loop::addTimer(2.0, function () use ($stream) { @@ -1600,7 +1606,7 @@ On top of this, it will emit an `error` event that can be used for logging purposes like this: ```php -$server->on('error', function (Exception $e) { +$http->on('error', function (Exception $e) { echo 'Error: ' . $e->getMessage() . PHP_EOL; if ($e->getPrevious() !== null) { echo 'Previous: ' . $e->getPrevious()->getMessage() . PHP_EOL; @@ -1626,13 +1632,13 @@ create your own HTTP response message instead. #### Default response headers When a response is returned from the request handler function, it will be -processed by the [`Server`](#server) and then sent back to the client. +processed by the [`HttpServer`](#httpserver) and then sent back to the client. A `Server: ReactPHP/1` response header will be added automatically. You can add a custom `Server` response header like this: ```php -$server = new React\Http\Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { return new React\Http\Message\Response( 200, array( @@ -1647,7 +1653,7 @@ don't want to expose the underlying server software), you can use an empty string value like this: ```php -$server = new React\Http\Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { return new React\Http\Message\Response( 200, array( @@ -1662,7 +1668,7 @@ date and time if none is given. You can add a custom `Date` response header like this: ```php -$server = new React\Http\Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { return new React\Http\Message\Response( 200, array( @@ -1677,7 +1683,7 @@ don't have an appropriate clock to rely on), you can use an empty string value like this: ```php -$server = new React\Http\Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { return new React\Http\Message\Response( 200, array( @@ -1687,7 +1693,7 @@ $server = new React\Http\Server(function (ServerRequestInterface $request) { }); ``` -The `Server` class will automatically add the protocol version of the request, +The `HttpServer` class will automatically add the protocol version of the request, so you don't have to. For instance, if the client sends the request using the HTTP/1.1 protocol version, the response message will also use the same protocol version, no matter what version is returned from the request handler function. @@ -1701,7 +1707,7 @@ choice with a `Connection: close` response header. ### Middleware -As documented above, the [`Server`](#server) accepts a single request handler +As documented above, the [`HttpServer`](#httpserver) accepts a single request handler argument that is responsible for processing an incoming HTTP request and then creating and returning an outgoing HTTP response. @@ -1755,12 +1761,12 @@ required to match PHP's request behavior (see below) and otherwise actively encourages [Third-Party Middleware](#third-party-middleware) implementations. In order to use middleware request handlers, simply pass an array with all -callables as defined above to the [`Server`](#server). +callables as defined above to the [`HttpServer`](#httpserver). The following example adds a middleware request handler that adds the current time to the request as a header (`Request-Time`) and a final request handler that always returns a 200 code without a body: ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( function (Psr\Http\Message\ServerRequestInterface $request, callable $next) { $request = $request->withHeader('Request-Time', time()); return $next($request); @@ -1784,7 +1790,7 @@ In order to simplify handling both paths, you can simply wrap this in a [`Promise\resolve()`](https://reactphp.org/promise/#resolve) call like this: ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( function (Psr\Http\Message\ServerRequestInterface $request, callable $next) { $promise = React\Promise\resolve($next($request)); return $promise->then(function (ResponseInterface $response) { @@ -1800,13 +1806,13 @@ $server = new React\Http\Server( Note that the `$next` middleware request handler may also throw an `Exception` (or return a rejected promise) as described above. The previous example does not catch any exceptions and would thus signal an -error condition to the `Server`. +error condition to the `HttpServer`. Alternatively, you can also catch any `Exception` to implement custom error handling logic (or logging etc.) by wrapping this in a [`Promise`](https://reactphp.org/promise/#promise) like this: ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( function (Psr\Http\Message\ServerRequestInterface $request, callable $next) { $promise = new React\Promise\Promise(function ($resolve) use ($next, $request) { $resolve($next($request)); @@ -2439,7 +2445,7 @@ body in memory. Instead, it will represent the request body as a that emit chunks of incoming data as it is received: ```php -$server = new React\Http\Server(array( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), function (Psr\Http\Message\ServerRequestInterface $request) { $body = $request->getBody(); @@ -2460,7 +2466,7 @@ $server = new React\Http\Server(array( }); }); } -)); +); ``` See also [streaming incoming request](#streaming-incoming-request) @@ -2473,17 +2479,17 @@ to explicitly configure the total number of requests that can be handled at once: ```php -$server = new React\Http\Server(array( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers new React\Http\Middleware\RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request new React\Http\Middleware\RequestBodyParserMiddleware(), $handler -)); +); ``` > Internally, this class is used as a "marker" to not trigger the default - request buffering behavior in the `Server`. It does not implement any logic + request buffering behavior in the `HttpServer`. It does not implement any logic on its own. #### LimitConcurrentRequestsMiddleware @@ -2506,7 +2512,7 @@ The following example shows how this middleware can be used to ensure no more than 10 handlers will be invoked at once: ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( new React\Http\Middleware\LimitConcurrentRequestsMiddleware(10), $handler ); @@ -2517,7 +2523,7 @@ Similarly, this middleware is often used in combination with the to limit the total number of requests that can be buffered at once: ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers new React\Http\Middleware\RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request @@ -2531,7 +2537,7 @@ that can be buffered at once and then ensure the actual request handler only processes one request after another without any concurrency: ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers new React\Http\Middleware\RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request @@ -2584,7 +2590,7 @@ the total number of concurrent requests. Usage: ```php -$server = new React\Http\Server( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers new React\Http\Middleware\RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB @@ -2645,7 +2651,7 @@ $handler = function (Psr\Http\Message\ServerRequestInterface $request) { ); }; -$server = new React\Http\Server( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers new React\Http\Middleware\RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB diff --git a/examples/51-server-hello-world.php b/examples/51-server-hello-world.php index 2cfd5649..50c82396 100644 --- a/examples/51-server-hello-world.php +++ b/examples/51-server-hello-world.php @@ -2,11 +2,10 @@ use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; -use React\Http\Server; require __DIR__ . '/../vendor/autoload.php'; -$server = new Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { return new Response( 200, array( @@ -16,7 +15,7 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 ae173bbd..1884acb0 100644 --- a/examples/52-server-count-visitors.php +++ b/examples/52-server-count-visitors.php @@ -2,12 +2,11 @@ use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; -use React\Http\Server; require __DIR__ . '/../vendor/autoload.php'; $counter = 0; -$server = new Server(function (ServerRequestInterface $request) use (&$counter) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use (&$counter) { return new Response( 200, array( @@ -17,7 +16,7 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 82f34742..89dad25c 100644 --- a/examples/53-server-whatsmyip.php +++ b/examples/53-server-whatsmyip.php @@ -2,11 +2,10 @@ use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; -use React\Http\Server; require __DIR__ . '/../vendor/autoload.php'; -$server = new Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { $body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR']; return new Response( @@ -18,7 +17,7 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 dbddfbc0..2ab16647 100644 --- a/examples/54-server-query-parameter.php +++ b/examples/54-server-query-parameter.php @@ -2,11 +2,10 @@ use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; -use React\Http\Server; require __DIR__ . '/../vendor/autoload.php'; -$server = new Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { $queryParams = $request->getQueryParams(); $body = 'The query parameter "foo" is not set. Click the following link '; @@ -25,7 +24,7 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 55370c3b..ff89fe1e 100644 --- a/examples/55-server-cookie-handling.php +++ b/examples/55-server-cookie-handling.php @@ -2,11 +2,10 @@ use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; -use React\Http\Server; require __DIR__ . '/../vendor/autoload.php'; -$server = new Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { $key = 'react\php'; if (isset($request->getCookieParams()[$key])) { @@ -31,7 +30,7 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 9f149c4c..bd2ea694 100644 --- a/examples/56-server-sleep.php +++ b/examples/56-server-sleep.php @@ -3,12 +3,11 @@ use Psr\Http\Message\ServerRequestInterface; use React\EventLoop\Loop; use React\Http\Message\Response; -use React\Http\Server; use React\Promise\Promise; require __DIR__ . '/../vendor/autoload.php'; -$server = new Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { return new Promise(function ($resolve, $reject) { Loop::addTimer(1.5, function() use ($resolve) { $response = new Response( @@ -23,7 +22,7 @@ }); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 6952a559..f5281c53 100644 --- a/examples/57-server-error-handling.php +++ b/examples/57-server-error-handling.php @@ -2,13 +2,12 @@ use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; -use React\Http\Server; use React\Promise\Promise; require __DIR__ . '/../vendor/autoload.php'; $count = 0; -$server = new Server(function (ServerRequestInterface $request) use (&$count) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use (&$count) { return new Promise(function ($resolve, $reject) use (&$count) { $count++; @@ -28,7 +27,7 @@ }); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 941ded1f..d965e306 100644 --- a/examples/58-server-stream-response.php +++ b/examples/58-server-stream-response.php @@ -3,12 +3,11 @@ use Psr\Http\Message\ServerRequestInterface; use React\EventLoop\Loop; use React\Http\Message\Response; -use React\Http\Server; use React\Stream\ThroughStream; require __DIR__ . '/../vendor/autoload.php'; -$server = new Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { if ($request->getMethod() !== 'GET' || $request->getUri()->getPath() !== '/') { return new Response(404); } @@ -39,7 +38,7 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 c1b4d305..c06a9702 100644 --- a/examples/59-server-json-api.php +++ b/examples/59-server-json-api.php @@ -8,11 +8,10 @@ use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; -use React\Http\Server; require __DIR__ . '/../vendor/autoload.php'; -$server = new Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { if ($request->getHeaderLine('Content-Type') !== 'application/json') { return new Response( 415, // Unsupported Media Type @@ -53,7 +52,7 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 5b671618..34f1a8bd 100644 --- a/examples/61-server-hello-world-https.php +++ b/examples/61-server-hello-world-https.php @@ -2,11 +2,10 @@ use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; -use React\Http\Server; require __DIR__ . '/../vendor/autoload.php'; -$server = new Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { return new Response( 200, array( @@ -16,11 +15,11 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$socket = new \React\Socket\SecureServer($socket, null, array( +$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' )); -$server->listen($socket); +$http->listen($socket); //$socket->on('error', 'printf'); diff --git a/examples/62-server-form-upload.php b/examples/62-server-form-upload.php index b1f0d8ee..5a0fdace 100644 --- a/examples/62-server-form-upload.php +++ b/examples/62-server-form-upload.php @@ -14,7 +14,6 @@ use React\Http\Middleware\RequestBodyBufferMiddleware; use React\Http\Middleware\RequestBodyParserMiddleware; use React\Http\Middleware\StreamingRequestMiddleware; -use React\Http\Server; require __DIR__ . '/../vendor/autoload.php'; @@ -121,7 +120,7 @@ // Note how this example explicitly uses the advanced `StreamingRequestMiddleware` to apply // custom request buffering limits below before running our request handler. -$server = new Server( +$http = new React\Http\HttpServer( new StreamingRequestMiddleware(), new LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers, queue otherwise new RequestBodyBufferMiddleware(8 * 1024 * 1024), // 8 MiB max, ignore body otherwise @@ -129,7 +128,7 @@ $handler ); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', null); -$server->listen($socket); +$socket = new React\Socket\Server(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 c2416e15..693cf2c0 100644 --- a/examples/63-server-streaming-request.php +++ b/examples/63-server-streaming-request.php @@ -5,7 +5,7 @@ // Note how this example uses the advanced `StreamingRequestMiddleware` to allow streaming // the incoming HTTP request. This very simple example merely counts the size // of the streaming body, it does not otherwise buffer its contents in memory. -$server = new React\Http\Server( +$http = new React\Http\HttpServer( new React\Http\Middleware\StreamingRequestMiddleware(), function (Psr\Http\Message\ServerRequestInterface $request) { $body = $request->getBody(); @@ -42,9 +42,9 @@ function (Psr\Http\Message\ServerRequestInterface $request) { } ); -$server->on('error', 'printf'); +$http->on('error', 'printf'); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 95c2f411..f6ab5ff5 100644 --- a/examples/71-server-http-proxy.php +++ b/examples/71-server-http-proxy.php @@ -4,18 +4,16 @@ // $ curl -v --proxy http://localhost:8080 http://reactphp.org/ use Psr\Http\Message\RequestInterface; -use React\EventLoop\Factory; use React\Http\Message\Response; -use React\Http\Server; use RingCentral\Psr7; require __DIR__ . '/../vendor/autoload.php'; -// Note how this example uses the `Server` without the `StreamingRequestMiddleware`. +// Note how this example uses the `HttpServer` without the `StreamingRequestMiddleware`. // This means that this proxy buffers the whole request before "processing" it. // As such, this is store-and-forward proxy. This could also use the advanced // `StreamingRequestMiddleware` to forward the incoming request as it comes in. -$server = new Server(function (RequestInterface $request) { +$http = new React\Http\HttpServer(function (RequestInterface $request) { if (strpos($request->getRequestTarget(), '://') === false) { return new Response( 400, @@ -46,7 +44,7 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 a1f33983..4399614b 100644 --- a/examples/72-server-http-connect-proxy.php +++ b/examples/72-server-http-connect-proxy.php @@ -5,7 +5,6 @@ use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; -use React\Http\Server; use React\Socket\Connector; use React\Socket\ConnectionInterface; @@ -13,11 +12,11 @@ $connector = new Connector(); -// Note how this example uses the `Server` without the `StreamingRequestMiddleware`. +// Note how this example uses the `HttpServer` without the `StreamingRequestMiddleware`. // Unlike the plain HTTP proxy, the CONNECT method does not contain a body // and we establish an end-to-end connection over the stream object, so this // doesn't have to store any payload data in memory at all. -$server = new Server(function (ServerRequestInterface $request) use ($connector) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use ($connector) { if ($request->getMethod() !== 'CONNECT') { return new Response( 405, @@ -51,7 +50,7 @@ function ($e) { ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 6a20181a..cb5d0b0d 100644 --- a/examples/81-server-upgrade-echo.php +++ b/examples/81-server-upgrade-echo.php @@ -20,15 +20,14 @@ use Psr\Http\Message\ServerRequestInterface; use React\EventLoop\Loop; use React\Http\Message\Response; -use React\Http\Server; use React\Stream\ThroughStream; require __DIR__ . '/../vendor/autoload.php'; -// Note how this example uses the `Server` without the `StreamingRequestMiddleware`. +// Note how this example uses the `HttpServer` without the `StreamingRequestMiddleware`. // The initial incoming request does not contain a body and we upgrade to a // stream object below. -$server = new Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { if ($request->getHeaderLine('Upgrade') !== 'echo' || $request->getProtocolVersion() === '1.0') { return new Response( 426, @@ -57,7 +56,7 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 2c6f08a8..22fed927 100644 --- a/examples/82-server-upgrade-chat.php +++ b/examples/82-server-upgrade-chat.php @@ -22,7 +22,6 @@ use Psr\Http\Message\ServerRequestInterface; use React\EventLoop\Loop; use React\Http\Message\Response; -use React\Http\Server; use React\Stream\CompositeStream; use React\Stream\ThroughStream; @@ -33,10 +32,10 @@ // this means that any Upgraded data will simply be sent back to the client $chat = new ThroughStream(); -// Note how this example uses the `Server` without the `StreamingRequestMiddleware`. +// Note how this example uses the `HttpServer` without the `StreamingRequestMiddleware`. // The initial incoming request does not contain a body and we upgrade to a // stream object below. -$server = new Server(function (ServerRequestInterface $request) use ($chat) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) use ($chat) { if ($request->getHeaderLine('Upgrade') !== 'chat' || $request->getProtocolVersion() === '1.0') { return new Response( 426, @@ -85,7 +84,7 @@ ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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 a6b4e9c1..bdc6735a 100644 --- a/examples/99-server-benchmark-download.php +++ b/examples/99-server-benchmark-download.php @@ -17,7 +17,6 @@ use Evenement\EventEmitter; use Psr\Http\Message\ServerRequestInterface; use React\Http\Message\Response; -use React\Http\Server; use React\Stream\ReadableStreamInterface; use React\Stream\WritableStreamInterface; @@ -91,7 +90,7 @@ public function getSize() } } -$server = new Server(function (ServerRequestInterface $request) { +$http = new React\Http\HttpServer(function (ServerRequestInterface $request) { switch ($request->getUri()->getPath()) { case '/': return new Response( @@ -123,7 +122,7 @@ public function getSize() ); }); -$socket = new \React\Socket\Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0'); -$server->listen($socket); +$socket = new React\Socket\Server(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/Server.php b/src/HttpServer.php similarity index 93% rename from src/Server.php rename to src/HttpServer.php index c2e3f5a6..582ea380 100644 --- a/src/Server.php +++ b/src/HttpServer.php @@ -15,7 +15,7 @@ use React\Socket\ServerInterface; /** - * The `React\Http\Server` class is responsible for handling incoming connections and then + * The `React\Http\HttpServer` class is responsible for handling incoming connections and then * processing each incoming HTTP request. * * When a complete HTTP request has been received, it will invoke the given @@ -24,7 +24,7 @@ * object and expects a [response](#server-response) object in return: * * ```php - * $server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) { + * $http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) { * return new React\Http\Message\Response( * 200, * array( @@ -49,7 +49,7 @@ * This value SHOULD NOT be given unless you're sure you want to explicitly use a * given event loop instance. * - * In order to start listening for any incoming connections, the `Server` needs + * In order to start listening for any incoming connections, the `HttpServer` needs * 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 @@ -58,17 +58,17 @@ * to start a plaintext HTTP server like this: * * ```php - * $server = new React\Http\Server($handler); + * $http = new React\Http\HttpServer($handler); * * $socket = new React\Socket\Server('0.0.0.0:8080'); - * $server->listen($socket); + * $http->listen($socket); * ``` * * See also the [`listen()`](#listen) method and * [hello world server example](../examples/51-server-hello-world.php) * for more details. * - * By default, the `Server` buffers and parses the complete incoming HTTP + * By default, the `HttpServer` buffers and parses the complete incoming HTTP * request in memory. It will invoke the given request handler function when the * complete request headers and request body has been received. This means the * [request](#server-request) object passed to your request handler function will be @@ -121,14 +121,14 @@ * or explicitly limit concurrency. * * In order to override the above buffering defaults, you can configure the - * `Server` explicitly. You can use the + * `HttpServer` explicitly. You can use the * [`LimitConcurrentRequestsMiddleware`](#limitconcurrentrequestsmiddleware) and * [`RequestBodyBufferMiddleware`](#requestbodybuffermiddleware) (see below) * to explicitly configure the total number of requests that can be handled at * once like this: * * ```php - * $server = new React\Http\Server( + * $http = new React\Http\HttpServer( * new React\Http\Middleware\StreamingRequestMiddleware(), * new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers * new React\Http\Middleware\RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request @@ -153,7 +153,7 @@ * in memory: * * ```php - * $server = new React\Http\Server( + * $http = new React\Http\HttpServer( * new React\Http\Middleware\StreamingRequestMiddleware(), * $handler * ); @@ -167,8 +167,12 @@ * have full control over consuming the incoming HTTP request body and * concurrency settings. See also [streaming incoming request](#streaming-incoming-request) * below for more details. + * + * > Changelog v1.5.0: This class has been renamed to `HttpServer` from the + * previous `Server` class in order to avoid any ambiguities. + * The previous name has been deprecated and should not be used anymore. */ -final class Server extends EventEmitter +final class HttpServer extends EventEmitter { /** * The maximum buffer size used for each request. @@ -263,10 +267,10 @@ public function __construct($requestHandlerOrLoop) * order to start a plaintext HTTP server like this: * * ```php - * $server = new React\Http\Server($handler); + * $http = new React\Http\HttpServer($handler); * * $socket = new React\Socket\Server(8080); - * $server->listen($socket); + * $http->listen($socket); * ``` * * See also [hello world server example](../examples/51-server-hello-world.php) @@ -289,12 +293,12 @@ public function __construct($requestHandlerOrLoop) * `passphrase` like this: * * ```php - * $server = new React\Http\Server($handler); + * $http = new React\Http\HttpServer($handler); * * $socket = new React\Socket\Server('tls://0.0.0.0:8443', null, array( * 'local_cert' => __DIR__ . '/localhost.pem' * )); - * $server->listen($socket); + * $http->listen($socket); * ``` * * See also [hello world HTTPS example](../examples/61-server-hello-world-https.php) diff --git a/src/Io/StreamingServer.php b/src/Io/StreamingServer.php index e20ddf48..95e0e4c6 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 [`Server`](#server) class, it does not buffer and parse the incoming + * Unlike the [`HttpServer`](#server) 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 @@ -63,7 +63,7 @@ * See also the [`listen()`](#listen) method and the [first example](examples) for more details. * * The `StreamingServer` class is considered advanced usage and unless you know - * what you're doing, you're recommended to use the [`Server`](#server) class + * what you're doing, you're recommended to use the [`HttpServer`](#httpserver) class * instead. The `StreamingServer` class is specifically designed to help with * more advanced use cases where you want to have full control over consuming * the incoming HTTP request body and concurrency settings. @@ -75,7 +75,7 @@ * handler function may not be fully compatible with PSR-7. See also * [streaming request](#streaming-request) below for more details. * - * @see \React\Http\Server + * @see \React\Http\HttpServer * @see \React\Http\Message\Response * @see self::listen() * @internal @@ -130,7 +130,7 @@ public function __construct(LoopInterface $loop, $requestHandler) * Starts listening for HTTP requests on the given socket server instance * * @param ServerInterface $socket - * @see \React\Http\Server::listen() + * @see \React\Http\HttpServer::listen() */ public function listen(ServerInterface $socket) { diff --git a/src/Middleware/LimitConcurrentRequestsMiddleware.php b/src/Middleware/LimitConcurrentRequestsMiddleware.php index cc1dfc42..53338100 100644 --- a/src/Middleware/LimitConcurrentRequestsMiddleware.php +++ b/src/Middleware/LimitConcurrentRequestsMiddleware.php @@ -29,7 +29,7 @@ * than 10 handlers will be invoked at once: * * ```php - * $server = new React\Http\Server( + * $http = new React\Http\HttpServer( * new React\Http\Middleware\StreamingRequestMiddleware(), * new React\Http\Middleware\LimitConcurrentRequestsMiddleware(10), * $handler @@ -41,7 +41,7 @@ * to limit the total number of requests that can be buffered at once: * * ```php - * $server = new React\Http\Server( + * $http = new React\Http\HttpServer( * new React\Http\Middleware\StreamingRequestMiddleware(), * new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers * new React\Http\Middleware\RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request @@ -55,7 +55,7 @@ * processes one request after another without any concurrency: * * ```php - * $server = new React\Http\Server( + * $http = new React\Http\HttpServer( * new React\Http\Middleware\StreamingRequestMiddleware(), * new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers * new React\Http\Middleware\RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request diff --git a/src/Middleware/StreamingRequestMiddleware.php b/src/Middleware/StreamingRequestMiddleware.php index a68454f6..6ab74b71 100644 --- a/src/Middleware/StreamingRequestMiddleware.php +++ b/src/Middleware/StreamingRequestMiddleware.php @@ -13,7 +13,7 @@ * that emit chunks of incoming data as it is received: * * ```php - * $server = new React\Http\Server(array( + * $http = new React\Http\HttpServer( * new React\Http\Middleware\StreamingRequestMiddleware(), * function (Psr\Http\Message\ServerRequestInterface $request) { * $body = $request->getBody(); @@ -34,7 +34,7 @@ * }); * }); * } - * )); + * ); * ``` * * See also [streaming incoming request](../../README.md#streaming-incoming-request) @@ -47,17 +47,17 @@ * once: * * ```php - * $server = new React\Http\Server(array( + * $http = new React\Http\HttpServer( * new React\Http\Middleware\StreamingRequestMiddleware(), * new React\Http\Middleware\LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers * new React\Http\Middleware\RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request * new React\Http\Middleware\RequestBodyParserMiddleware(), * $handler - * )); + * ); * ``` * * > Internally, this class is used as a "marker" to not trigger the default - * request buffering behavior in the `Server`. It does not implement any logic + * request buffering behavior in the `HttpServer`. It does not implement any logic * on its own. */ final class StreamingRequestMiddleware diff --git a/tests/FunctionalBrowserTest.php b/tests/FunctionalBrowserTest.php index f5b7f324..edb7f40b 100644 --- a/tests/FunctionalBrowserTest.php +++ b/tests/FunctionalBrowserTest.php @@ -7,10 +7,10 @@ use Psr\Http\Message\ServerRequestInterface; use React\EventLoop\Factory; use React\Http\Browser; +use React\Http\HttpServer; use React\Http\Message\ResponseException; use React\Http\Middleware\StreamingRequestMiddleware; use React\Http\Message\Response; -use React\Http\Server; use React\Promise\Promise; use React\Promise\Stream; use React\Socket\Connector; @@ -32,7 +32,7 @@ public function setUpBrowserAndServer() $this->loop = $loop = Factory::create(); $this->browser = new Browser($this->loop); - $server = new Server($this->loop, new StreamingRequestMiddleware(), function (ServerRequestInterface $request) use ($loop) { + $http = new HttpServer($this->loop, new StreamingRequestMiddleware(), function (ServerRequestInterface $request) use ($loop) { $path = $request->getUri()->getPath(); $headers = array(); @@ -142,7 +142,7 @@ public function setUpBrowserAndServer() var_dump($path); }); $socket = new \React\Socket\Server(0, $this->loop); - $server->listen($socket); + $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; } @@ -573,11 +573,11 @@ public function testPostStreamKnownLength() */ public function testPostStreamWillStartSendingRequestEvenWhenBodyDoesNotEmitData() { - $server = new Server($this->loop, new StreamingRequestMiddleware(), function (ServerRequestInterface $request) { + $http = new HttpServer($this->loop, new StreamingRequestMiddleware(), function (ServerRequestInterface $request) { return new Response(200); }); $socket = new \React\Socket\Server(0, $this->loop); - $server->listen($socket); + $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; @@ -600,7 +600,7 @@ public function testPostStreamClosed() public function testSendsHttp11ByDefault() { - $server = new Server($this->loop, function (ServerRequestInterface $request) { + $http = new HttpServer($this->loop, function (ServerRequestInterface $request) { return new Response( 200, array(), @@ -608,7 +608,7 @@ public function testSendsHttp11ByDefault() ); }); $socket = new \React\Socket\Server(0, $this->loop); - $server->listen($socket); + $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; @@ -620,7 +620,7 @@ public function testSendsHttp11ByDefault() public function testSendsExplicitHttp10Request() { - $server = new Server($this->loop, function (ServerRequestInterface $request) { + $http = new HttpServer($this->loop, function (ServerRequestInterface $request) { return new Response( 200, array(), @@ -628,7 +628,7 @@ public function testSendsExplicitHttp10Request() ); }); $socket = new \React\Socket\Server(0, $this->loop); - $server->listen($socket); + $http->listen($socket); $this->base = str_replace('tcp:', 'http:', $socket->getAddress()) . '/'; diff --git a/tests/FunctionalServerTest.php b/tests/FunctionalHttpServerTest.php similarity index 90% rename from tests/FunctionalServerTest.php rename to tests/FunctionalHttpServerTest.php index 41cf31db..f7aa8392 100644 --- a/tests/FunctionalServerTest.php +++ b/tests/FunctionalHttpServerTest.php @@ -2,36 +2,36 @@ namespace React\Tests\Http; +use Clue\React\Block; +use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ServerRequestInterface; +use React\EventLoop\Factory; +use React\Http\HttpServer; +use React\Http\Message\Response; use React\Http\Middleware\LimitConcurrentRequestsMiddleware; use React\Http\Middleware\RequestBodyBufferMiddleware; -use React\Http\Message\Response; -use React\Http\Server; +use React\Http\Middleware\StreamingRequestMiddleware; use React\Socket\Server as Socket; -use React\EventLoop\Factory; -use Psr\Http\Message\RequestInterface; use React\Socket\Connector; use React\Socket\ConnectionInterface; -use Clue\React\Block; use React\Socket\SecureServer; use React\Promise; use React\Promise\Stream; use React\Stream\ThroughStream; -use React\Http\Middleware\StreamingRequestMiddleware; -class FunctionalServerTest extends TestCase +class FunctionalHttpServerTest extends TestCase { public function testPlainHttpOnRandomPort() { $loop = Factory::create(); $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); @@ -52,7 +52,7 @@ public function testPlainHttpOnRandomPortWithSingleRequestHandlerArray() $loop = Factory::create(); $connector = new Connector($loop); - $server = new Server( + $http = new HttpServer( $loop, function () { return new Response(404); @@ -60,7 +60,7 @@ function () { ); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); @@ -80,12 +80,12 @@ public function testPlainHttpOnRandomPortWithoutHostHeaderUsesSocketUri() $loop = Factory::create(); $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\n\r\n"); @@ -106,12 +106,12 @@ public function testPlainHttpOnRandomPortWithOtherHostHeaderTakesPrecedence() $loop = Factory::create(); $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: localhost:1000\r\n\r\n"); @@ -138,7 +138,7 @@ public function testSecureHttpsOnRandomPort() 'tls' => array('verify_peer' => false) )); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); @@ -146,7 +146,7 @@ public function testSecureHttpsOnRandomPort() $socket = new SecureServer($socket, $loop, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); @@ -170,7 +170,7 @@ public function testSecureHttpsReturnsData() $loop = Factory::create(); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response( 200, array(), @@ -182,7 +182,7 @@ public function testSecureHttpsReturnsData() $socket = new SecureServer($socket, $loop, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); - $server->listen($socket); + $http->listen($socket); $connector = new Connector($loop, array( 'tls' => array('verify_peer' => false) @@ -214,7 +214,7 @@ public function testSecureHttpsOnRandomPortWithoutHostHeaderUsesSocketUri() 'tls' => array('verify_peer' => false) )); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); @@ -222,7 +222,7 @@ public function testSecureHttpsOnRandomPortWithoutHostHeaderUsesSocketUri() $socket = new SecureServer($socket, $loop, array( 'local_cert' => __DIR__ . '/../examples/localhost.pem' )); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\n\r\n"); @@ -248,11 +248,11 @@ public function testPlainHttpOnStandardPortReturnsUriWithNoPort() } $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n"); @@ -278,11 +278,11 @@ public function testPlainHttpOnStandardPortWithoutHostHeaderReturnsUriWithNoPort } $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\n\r\n"); @@ -317,11 +317,11 @@ public function testSecureHttpsOnStandardPortReturnsUriWithNoPort() 'tls' => array('verify_peer' => false) )); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: 127.0.0.1\r\n\r\n"); @@ -356,11 +356,11 @@ public function testSecureHttpsOnStandardPortWithoutHostHeaderUsesSocketUri() 'tls' => array('verify_peer' => false) )); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\n\r\n"); @@ -386,11 +386,11 @@ public function testPlainHttpOnHttpsStandardPortReturnsUriWithPort() } $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri()); }); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); @@ -425,11 +425,11 @@ public function testSecureHttpsOnHttpStandardPortReturnsUriWithPort() 'tls' => array('verify_peer' => false) )); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { return new Response(200, array(), (string)$request->getUri() . 'x' . $request->getHeaderLine('Host')); }); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\nHost: " . noScheme($conn->getRemoteAddress()) . "\r\n\r\n"); @@ -453,12 +453,12 @@ public function testClosedStreamFromRequestHandlerWillSendEmptyBody() $stream = new ThroughStream(); $stream->close(); - $server = new Server($loop, function (RequestInterface $request) use ($stream) { + $http = new HttpServer($loop, function (RequestInterface $request) use ($stream) { return new Response(200, array(), $stream); }); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.0\r\n\r\n"); @@ -480,7 +480,7 @@ public function testRequestHandlerWithStreamingRequestWillReceiveCloseEventIfCon $connector = new Connector($loop); $once = $this->expectCallableOnce(); - $server = new Server( + $http = new HttpServer( $loop, new StreamingRequestMiddleware(), function (RequestInterface $request) use ($once) { @@ -489,7 +489,7 @@ function (RequestInterface $request) use ($once) { ); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) { $conn->write("GET / HTTP/1.0\r\nContent-Length: 100\r\n\r\n"); @@ -511,7 +511,7 @@ public function testStreamFromRequestHandlerWillBeClosedIfConnectionClosesWhileS $stream = new ThroughStream(); - $server = new Server( + $http = new HttpServer( $loop, new StreamingRequestMiddleware(), function (RequestInterface $request) use ($stream) { @@ -520,7 +520,7 @@ function (RequestInterface $request) use ($stream) { ); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) { $conn->write("GET / HTTP/1.0\r\nContent-Length: 100\r\n\r\n"); @@ -545,12 +545,12 @@ public function testStreamFromRequestHandlerWillBeClosedIfConnectionCloses() $stream = new ThroughStream(); - $server = new Server($loop, function (RequestInterface $request) use ($stream) { + $http = new HttpServer($loop, function (RequestInterface $request) use ($stream) { return new Response(200, array(), $stream); }); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) use ($loop) { $conn->write("GET / HTTP/1.0\r\n\r\n"); @@ -573,7 +573,7 @@ public function testUpgradeWithThroughStreamReturnsDataAsGiven() $loop = Factory::create(); $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) use ($loop) { + $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { $stream = new ThroughStream(); $loop->addTimer(0.1, function () use ($stream) { @@ -584,7 +584,7 @@ public function testUpgradeWithThroughStreamReturnsDataAsGiven() }); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("GET / HTTP/1.1\r\nHost: example.com:80\r\nUpgrade: echo\r\n\r\n"); @@ -610,7 +610,7 @@ public function testUpgradeWithRequestBodyAndThroughStreamReturnsDataAsGiven() $loop = Factory::create(); $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) use ($loop) { + $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { $stream = new ThroughStream(); $loop->addTimer(0.1, function () use ($stream) { @@ -621,7 +621,7 @@ public function testUpgradeWithRequestBodyAndThroughStreamReturnsDataAsGiven() }); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("POST / HTTP/1.1\r\nHost: example.com:80\r\nUpgrade: echo\r\nContent-Length: 3\r\n\r\n"); @@ -648,7 +648,7 @@ public function testConnectWithThroughStreamReturnsDataAsGiven() $loop = Factory::create(); $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) use ($loop) { + $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { $stream = new ThroughStream(); $loop->addTimer(0.1, function () use ($stream) { @@ -659,7 +659,7 @@ public function testConnectWithThroughStreamReturnsDataAsGiven() }); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("CONNECT example.com:80 HTTP/1.1\r\nHost: example.com:80\r\nConnection: close\r\n\r\n"); @@ -685,7 +685,7 @@ public function testConnectWithThroughStreamReturnedFromPromiseReturnsDataAsGive $loop = Factory::create(); $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) use ($loop) { + $http = new HttpServer($loop, function (RequestInterface $request) use ($loop) { $stream = new ThroughStream(); $loop->addTimer(0.1, function () use ($stream) { @@ -700,7 +700,7 @@ public function testConnectWithThroughStreamReturnedFromPromiseReturnsDataAsGive }); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("CONNECT example.com:80 HTTP/1.1\r\nHost: example.com:80\r\nConnection: close\r\n\r\n"); @@ -726,7 +726,7 @@ public function testConnectWithClosedThroughStreamReturnsNoData() $loop = Factory::create(); $connector = new Connector($loop); - $server = new Server($loop, function (RequestInterface $request) { + $http = new HttpServer($loop, function (RequestInterface $request) { $stream = new ThroughStream(); $stream->close(); @@ -734,7 +734,7 @@ public function testConnectWithClosedThroughStreamReturnsNoData() }); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = $connector->connect($socket->getAddress())->then(function (ConnectionInterface $conn) { $conn->write("CONNECT example.com:80 HTTP/1.1\r\nHost: example.com:80\r\nConnection: close\r\n\r\n"); @@ -760,7 +760,7 @@ public function testLimitConcurrentRequestsMiddlewareRequestStreamPausing() $loop = Factory::create(); $connector = new Connector($loop); - $server = new Server( + $http = new HttpServer( $loop, new LimitConcurrentRequestsMiddleware(5), new RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB @@ -777,7 +777,7 @@ function (ServerRequestInterface $request) { ); $socket = new Socket(0, $loop); - $server->listen($socket); + $http->listen($socket); $result = array(); for ($i = 0; $i < 6; $i++) { diff --git a/tests/ServerTest.php b/tests/HttpServerTest.php similarity index 81% rename from tests/ServerTest.php rename to tests/HttpServerTest.php index 6cb1b9b5..9a0a789a 100644 --- a/tests/ServerTest.php +++ b/tests/HttpServerTest.php @@ -2,17 +2,17 @@ namespace React\Tests\Http; -use React\EventLoop\Factory; -use React\Http\Server; -use Psr\Http\Message\ServerRequestInterface; -use React\Promise\Deferred; use Clue\React\Block; -use React\Promise; +use Psr\Http\Message\ServerRequestInterface; +use React\EventLoop\Factory; +use React\Http\HttpServer; +use React\Http\Io\IniUtil; use React\Http\Middleware\StreamingRequestMiddleware; +use React\Promise; +use React\Promise\Deferred; use React\Stream\ReadableStreamInterface; -use React\Http\Io\IniUtil; -final class ServerTest extends TestCase +final class HttpServerTest extends TestCase { private $connection; private $socket; @@ -48,11 +48,11 @@ public function setUpConnectionMockAndSocket() public function testConstructWithoutLoopAssignsLoopAutomatically() { - $server = new Server(function () { }); + $http = new HttpServer(function () { }); - $ref = new \ReflectionProperty($server, 'streamingServer'); + $ref = new \ReflectionProperty($http, 'streamingServer'); $ref->setAccessible(true); - $streamingServer = $ref->getValue($server); + $streamingServer = $ref->getValue($http); $ref = new \ReflectionProperty($streamingServer, 'loop'); $ref->setAccessible(true); @@ -64,17 +64,17 @@ public function testConstructWithoutLoopAssignsLoopAutomatically() public function testInvalidCallbackFunctionLeadsToException() { $this->setExpectedException('InvalidArgumentException'); - new Server(Factory::create(), 'invalid'); + new HttpServer('invalid'); } public function testSimpleRequestCallsRequestHandlerOnce() { $called = null; - $server = new Server(Factory::create(), function (ServerRequestInterface $request) use (&$called) { + $http = new HttpServer(function (ServerRequestInterface $request) use (&$called) { ++$called; }); - $server->listen($this->socket); + $http->listen($this->socket); $this->socket->emit('connection', array($this->connection)); $this->connection->emit('data', array("GET / HTTP/1.0\r\n\r\n")); @@ -87,9 +87,9 @@ public function testSimpleRequestCallsRequestHandlerOnce() public function testSimpleRequestCallsArrayRequestHandlerOnce() { $this->called = null; - $server = new Server(Factory::create(), array($this, 'helperCallableOnce')); + $http = new HttpServer(array($this, 'helperCallableOnce')); - $server->listen($this->socket); + $http->listen($this->socket); $this->socket->emit('connection', array($this->connection)); $this->connection->emit('data', array("GET / HTTP/1.0\r\n\r\n")); @@ -104,8 +104,7 @@ public function helperCallableOnce() public function testSimpleRequestWithMiddlewareArrayProcessesMiddlewareStack() { $called = null; - $server = new Server( - Factory::create(), + $http = new HttpServer( function (ServerRequestInterface $request, $next) use (&$called) { $called = 'before'; $ret = $next($request->withHeader('Demo', 'ok')); @@ -118,7 +117,7 @@ function (ServerRequestInterface $request) use (&$called) { } ); - $server->listen($this->socket); + $http->listen($this->socket); $this->socket->emit('connection', array($this->connection)); $this->connection->emit('data', array("GET / HTTP/1.0\r\n\r\n")); @@ -129,11 +128,11 @@ public function testPostFormData() { $loop = Factory::create(); $deferred = new Deferred(); - $server = new Server($loop, function (ServerRequestInterface $request) use ($deferred) { + $http = new HttpServer($loop, function (ServerRequestInterface $request) use ($deferred) { $deferred->resolve($request); }); - $server->listen($this->socket); + $http->listen($this->socket); $this->socket->emit('connection', array($this->connection)); $this->connection->emit('data', array("POST / HTTP/1.0\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 7\r\n\r\nfoo=bar")); @@ -158,11 +157,11 @@ public function testPostFileUpload() { $loop = Factory::create(); $deferred = new Deferred(); - $server = new Server($loop, function (ServerRequestInterface $request) use ($deferred) { + $http = new HttpServer($loop, function (ServerRequestInterface $request) use ($deferred) { $deferred->resolve($request); }); - $server->listen($this->socket); + $http->listen($this->socket); $this->socket->emit('connection', array($this->connection)); $connection = $this->connection; @@ -202,11 +201,11 @@ public function testPostJsonWillNotBeParsedByDefault() { $loop = Factory::create(); $deferred = new Deferred(); - $server = new Server($loop, function (ServerRequestInterface $request) use ($deferred) { + $http = new HttpServer($loop, function (ServerRequestInterface $request) use ($deferred) { $deferred->resolve($request); }); - $server->listen($this->socket); + $http->listen($this->socket); $this->socket->emit('connection', array($this->connection)); $this->connection->emit('data', array("POST / HTTP/1.0\r\nContent-Type: application/json\r\nContent-Length: 6\r\n\r\n[true]")); @@ -227,11 +226,11 @@ public function testPostJsonWillNotBeParsedByDefault() public function testServerReceivesBufferedRequestByDefault() { $streaming = null; - $server = new Server(Factory::create(), function (ServerRequestInterface $request) use (&$streaming) { + $http = new HttpServer(function (ServerRequestInterface $request) use (&$streaming) { $streaming = $request->getBody() instanceof ReadableStreamInterface; }); - $server->listen($this->socket); + $http->listen($this->socket); $this->socket->emit('connection', array($this->connection)); $this->connection->emit('data', array("GET / HTTP/1.0\r\n\r\n")); @@ -241,15 +240,14 @@ public function testServerReceivesBufferedRequestByDefault() public function testServerWithStreamingRequestMiddlewareReceivesStreamingRequest() { $streaming = null; - $server = new Server( - Factory::create(), + $http = new HttpServer( new StreamingRequestMiddleware(), function (ServerRequestInterface $request) use (&$streaming) { $streaming = $request->getBody() instanceof ReadableStreamInterface; } ); - $server->listen($this->socket); + $http->listen($this->socket); $this->socket->emit('connection', array($this->connection)); $this->connection->emit('data', array("GET / HTTP/1.0\r\n\r\n")); @@ -260,14 +258,14 @@ public function testForwardErrors() { $exception = new \Exception(); $capturedException = null; - $server = new Server(Factory::create(), function () use ($exception) { + $http = new HttpServer(function () use ($exception) { return Promise\reject($exception); }); - $server->on('error', function ($error) use (&$capturedException) { + $http->on('error', function ($error) use (&$capturedException) { $capturedException = $error; }); - $server->listen($this->socket); + $http->listen($this->socket); $this->socket->emit('connection', array($this->connection)); $data = $this->createPostFileUploadRequest(); @@ -328,36 +326,36 @@ public function provideIniSettingsForConcurrency() */ public function testServerConcurrency($memory_limit, $post_max_size, $expectedConcurrency) { - $server = new Server(Factory::create(), function () { }); + $http = new HttpServer(function () { }); - $ref = new \ReflectionMethod($server, 'getConcurrentRequestsLimit'); + $ref = new \ReflectionMethod($http, 'getConcurrentRequestsLimit'); $ref->setAccessible(true); - $value = $ref->invoke($server, $memory_limit, $post_max_size); + $value = $ref->invoke($http, $memory_limit, $post_max_size); $this->assertEquals($expectedConcurrency, $value); } public function testServerGetPostMaxSizeReturnsSizeFromGivenIniSetting() { - $server = new Server(Factory::create(), function () { }); + $http = new HttpServer(function () { }); - $ref = new \ReflectionMethod($server, 'getMaxRequestSize'); + $ref = new \ReflectionMethod($http, 'getMaxRequestSize'); $ref->setAccessible(true); - $value = $ref->invoke($server, '1k'); + $value = $ref->invoke($http, '1k'); $this->assertEquals(1024, $value); } public function testServerGetPostMaxSizeReturnsSizeCappedFromGivenIniSetting() { - $server = new Server(Factory::create(), function () { }); + $http = new HttpServer(function () { }); - $ref = new \ReflectionMethod($server, 'getMaxRequestSize'); + $ref = new \ReflectionMethod($http, 'getMaxRequestSize'); $ref->setAccessible(true); - $value = $ref->invoke($server, '1M'); + $value = $ref->invoke($http, '1M'); $this->assertEquals(64 * 1024, $value); } @@ -368,12 +366,12 @@ public function testServerGetPostMaxSizeFromIniIsCapped() $this->markTestSkipped(); } - $server = new Server(Factory::create(), function () { }); + $http = new HttpServer(function () { }); - $ref = new \ReflectionMethod($server, 'getMaxRequestSize'); + $ref = new \ReflectionMethod($http, 'getMaxRequestSize'); $ref->setAccessible(true); - $value = $ref->invoke($server); + $value = $ref->invoke($http); $this->assertEquals(64 * 1024, $value); } @@ -383,14 +381,14 @@ public function testConstructServerWithUnlimitedMemoryLimitDoesNotLimitConcurren $old = ini_get('memory_limit'); ini_set('memory_limit', '-1'); - $server = new Server(Factory::create(), function () { }); + $http = new HttpServer(function () { }); ini_set('memory_limit', $old); - $ref = new \ReflectionProperty($server, 'streamingServer'); + $ref = new \ReflectionProperty($http, 'streamingServer'); $ref->setAccessible(true); - $streamingServer = $ref->getValue($server); + $streamingServer = $ref->getValue($http); $ref = new \ReflectionProperty($streamingServer, 'callback'); $ref->setAccessible(true); @@ -411,14 +409,14 @@ public function testConstructServerWithMemoryLimitDoesLimitConcurrency() $old = ini_get('memory_limit'); ini_set('memory_limit', '100M'); - $server = new Server(Factory::create(), function () { }); + $http = new HttpServer(function () { }); ini_set('memory_limit', $old); - $ref = new \ReflectionProperty($server, 'streamingServer'); + $ref = new \ReflectionProperty($http, 'streamingServer'); $ref->setAccessible(true); - $streamingServer = $ref->getValue($server); + $streamingServer = $ref->getValue($http); $ref = new \ReflectionProperty($streamingServer, 'callback'); $ref->setAccessible(true); From 255e89173a6a1636fbcd19d472ea7d492cbbc859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Sun, 25 Jul 2021 20:13:37 +0200 Subject: [PATCH 2/2] Add deprecated `Server` alias for new `HttpServer` class to ensure BC --- src/Server.php | 18 ++++++++++++++++++ tests/ServerTest.php | 15 +++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/Server.php create mode 100644 tests/ServerTest.php diff --git a/src/Server.php b/src/Server.php new file mode 100644 index 00000000..9bb9cf7f --- /dev/null +++ b/src/Server.php @@ -0,0 +1,18 @@ +assertInstanceOf('React\Http\HttpServer', $http); + } +}