Skip to content

Commit

Permalink
Merge pull request #361 from WyriHaximus-labs/add-loop-to-servers-con…
Browse files Browse the repository at this point in the history
…structors

Add the event loop as required constructor argument
  • Loading branch information
jsor authored Jul 8, 2020
2 parents 4c65e2b + c0fb86e commit bc883e0
Show file tree
Hide file tree
Showing 23 changed files with 196 additions and 188 deletions.
66 changes: 33 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ This is an HTTP server which responds with `Hello World!` to every request.
```php
$loop = React\EventLoop\Factory::create();

$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) {
$server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Response(
200,
array(
Expand Down Expand Up @@ -714,7 +714,7 @@ the constructor and will be invoked with the respective [request](#server-reques
object and expects a [response](#response) object in return:

```php
$server = new React\Http\Server(function (Psr\Http\Message\ServerRequestInterface $request) {
$server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Response(
200,
array(
Expand Down Expand Up @@ -742,7 +742,7 @@ 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);
$server = new React\Http\Server($loop, $handler);

$socket = new React\Socket\Server('0.0.0.0:8080', $loop);
$server->listen($socket);
Expand Down Expand Up @@ -803,7 +803,7 @@ to explicitly configure the total number of requests that can be handled at
once like this:

```php
$server = new React\Http\Server(array(
$server = new React\Http\Server($loop, array(
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
Expand All @@ -822,7 +822,7 @@ also use a streaming approach where only small chunks of data have to be kept
in memory:

```php
$server = new React\Http\Server(array(
$server = new React\Http\Server($loop, array(
new React\Http\Middleware\StreamingRequestMiddleware(),
$handler
));
Expand Down Expand Up @@ -851,7 +851,7 @@ 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);
$server = new React\Http\Server($loop, $handler);

$socket = new React\Socket\Server('0.0.0.0:8080', $loop);
$server->listen($socket);
Expand All @@ -877,7 +877,7 @@ using a secure TLS listen address, a certificate file and optional
`passphrase` like this:

```php
$server = new React\Http\Server($handler);
$server = new React\Http\Server($loop, $handler);

$socket = new React\Socket\Server('tls://0.0.0.0:8443', $loop, array(
'local_cert' => __DIR__ . '/localhost.pem'
Expand All @@ -902,7 +902,7 @@ which in turn extends the
and will be passed to the callback function like this.

```php
$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
$body = "The method of the request is: " . $request->getMethod();
$body .= "The requested path is: " . $request->getUri()->getPath();

Expand Down Expand Up @@ -945,7 +945,7 @@ The following parameters are currently available:
Set to 'on' if the request used HTTPS, otherwise it won't be set

```php
$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];

return new Response(
Expand All @@ -970,7 +970,7 @@ The `getQueryParams(): array` method can be used to get the query parameters
similiar to the `$_GET` variable.

```php
$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
$queryParams = $request->getQueryParams();

$body = 'The query parameter "foo" is not set. Click the following link ';
Expand Down Expand Up @@ -1024,7 +1024,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 Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
$name = $request->getParsedBody()['name'] ?? 'anonymous';

return new Response(
Expand All @@ -1048,7 +1048,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 Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
$data = json_decode((string)$request->getBody());
$name = $data->name ?? 'anonymous';

Expand All @@ -1071,7 +1071,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 Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
$files = $request->getUploadedFiles();
$name = isset($files['avatar']) ? $files['avatar']->getClientFilename() : 'nothing';

Expand Down Expand Up @@ -1141,7 +1141,7 @@ The ReactPHP `ReadableStreamInterface` gives you access to the incoming
request body as the individual chunks arrive:

```php
$server = new React\Http\Server(array(
$server = new React\Http\Server($loop, array(
new React\Http\Middleware\StreamingRequestMiddleware(),
function (Psr\Http\Message\ServerRequestInterface $request) {
$body = $request->getBody();
Expand Down Expand Up @@ -1214,7 +1214,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(array(
$server = new React\Http\Server($loop, array(
new React\Http\Middleware\StreamingRequestMiddleware(),
function (Psr\Http\Message\ServerRequestInterface $request) {
$size = $request->getBody()->getSize();
Expand Down Expand Up @@ -1287,7 +1287,7 @@ The `getCookieParams(): string[]` method can be used to
get all cookies sent with the current request.

```php
$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
$key = 'react\php';

if (isset($request->getCookieParams()[$key])) {
Expand Down Expand Up @@ -1359,7 +1359,7 @@ but feel free to use any implemantation of the
`PSR-7 ResponseInterface` you prefer.

```php
$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
200,
array(
Expand All @@ -1382,7 +1382,7 @@ To prevent this you SHOULD use a
This example shows how such a long-term action could look like:

```php
$server = new Server(function (ServerRequestInterface $request) use ($loop) {
$server = new Server($loop, function (ServerRequestInterface $request) use ($loop) {
return new Promise(function ($resolve, $reject) use ($loop) {
$loop->addTimer(1.5, function() use ($resolve) {
$response = new Response(
Expand Down Expand Up @@ -1419,7 +1419,7 @@ Note that other implementations of the `PSR-7 ResponseInterface` likely
only support strings.

```php
$server = new Server(function (ServerRequestInterface $request) use ($loop) {
$server = new Server($loop, function (ServerRequestInterface $request) use ($loop) {
$stream = new ThroughStream();

$timer = $loop->addPeriodicTimer(0.5, function () use ($stream) {
Expand Down Expand Up @@ -1510,7 +1510,7 @@ added automatically. This is the most common use case, for example when using
a `string` response body like this:

```php
$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
200,
array(
Expand All @@ -1529,7 +1529,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 Server(function (ServerRequestInterface $request) use ($loop) {
$server = new Server($loop, function (ServerRequestInterface $request) use ($loop) {
$stream = new ThroughStream();

$loop->addTimer(2.0, function () use ($stream) {
Expand Down Expand Up @@ -1608,7 +1608,7 @@ A `Date` header will be automatically added with the system date and time if non
You can add a custom `Date` header yourself like this:

```php
$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
200,
array(
Expand All @@ -1622,7 +1622,7 @@ If you don't have a appropriate clock to rely on, you should
unset this header with an empty string:

```php
$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
200,
array(
Expand All @@ -1636,7 +1636,7 @@ Note that it will automatically assume a `X-Powered-By: react/alpha` header
unless your specify a custom `X-Powered-By` header yourself:

```php
$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
200,
array(
Expand All @@ -1650,7 +1650,7 @@ If you do not want to send this header at all, you can use an empty string as
value like this:

```php
$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
200,
array(
Expand Down Expand Up @@ -1723,7 +1723,7 @@ The following example adds a middleware request handler that adds the current ti
header (`Request-Time`) and a final request handler that always returns a 200 code without a body:

```php
$server = new Server(array(
$server = new Server($loop, array(
function (ServerRequestInterface $request, callable $next) {
$request = $request->withHeader('Request-Time', time());
return $next($request);
Expand All @@ -1747,7 +1747,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 Server(array(
$server = new Server($loop, array(
function (ServerRequestInterface $request, callable $next) {
$promise = React\Promise\resolve($next($request));
return $promise->then(function (ResponseInterface $response) {
Expand All @@ -1769,7 +1769,7 @@ handling logic (or logging etc.) by wrapping this in a
[`Promise`](https://reactphp.org/promise/#promise) like this:

```php
$server = new Server(array(
$server = new Server($loop, array(
function (ServerRequestInterface $request, callable $next) {
$promise = new React\Promise\Promise(function ($resolve) use ($next, $request) {
$resolve($next($request));
Expand Down Expand Up @@ -2404,7 +2404,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 Server(array(
$server = new Server($loop, array(
new LimitConcurrentRequestsMiddleware(10),
$handler
));
Expand All @@ -2415,7 +2415,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 Server(array(
$server = new Server($loop, array(
new StreamingRequestMiddleware(),
new LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers
new RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request
Expand All @@ -2429,7 +2429,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 Server(array(
$server = new Server($loop, array(
new StreamingRequestMiddleware(),
new LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers
new RequestBodyBufferMiddleware(2 * 1024 * 1024), // 2 MiB per request
Expand Down Expand Up @@ -2482,7 +2482,7 @@ the total number of concurrent requests.
Usage:

```php
$server = new Server(array(
$server = new Server($loop, array(
new StreamingRequestMiddleware(),
new LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers
new RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB
Expand Down Expand Up @@ -2542,7 +2542,7 @@ $handler = function (ServerRequestInterface $request) {
);
};

$server = new Server(array(
$server = new Server($loop, array(
new StreamingRequestMiddleware(),
new LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers
new RequestBodyBufferMiddleware(16 * 1024 * 1024), // 16 MiB
Expand Down
2 changes: 1 addition & 1 deletion examples/51-server-hello-world.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

$loop = Factory::create();

$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
200,
array(
Expand Down
2 changes: 1 addition & 1 deletion examples/52-server-count-visitors.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
$loop = Factory::create();

$counter = 0;
$server = new Server(function (ServerRequestInterface $request) use (&$counter) {
$server = new Server($loop, function (ServerRequestInterface $request) use (&$counter) {
return new Response(
200,
array(
Expand Down
2 changes: 1 addition & 1 deletion examples/53-server-whatsmyip.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

$loop = Factory::create();

$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
$body = "Your IP is: " . $request->getServerParams()['REMOTE_ADDR'];

return new Response(
Expand Down
2 changes: 1 addition & 1 deletion examples/54-server-query-parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

$loop = Factory::create();

$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
$queryParams = $request->getQueryParams();

$body = 'The query parameter "foo" is not set. Click the following link ';
Expand Down
2 changes: 1 addition & 1 deletion examples/55-server-cookie-handling.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

$loop = Factory::create();

$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
$key = 'react\php';

if (isset($request->getCookieParams()[$key])) {
Expand Down
2 changes: 1 addition & 1 deletion examples/56-server-sleep.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

$loop = Factory::create();

$server = new Server(function (ServerRequestInterface $request) use ($loop) {
$server = new Server($loop, function (ServerRequestInterface $request) use ($loop) {
return new Promise(function ($resolve, $reject) use ($loop) {
$loop->addTimer(1.5, function() use ($resolve) {
$response = new Response(
Expand Down
2 changes: 1 addition & 1 deletion examples/57-server-error-handling.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
$loop = Factory::create();

$count = 0;
$server = new Server(function (ServerRequestInterface $request) use (&$count) {
$server = new Server($loop, function (ServerRequestInterface $request) use (&$count) {
return new Promise(function ($resolve, $reject) use (&$count) {
$count++;

Expand Down
2 changes: 1 addition & 1 deletion examples/58-server-stream-response.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

$loop = Factory::create();

$server = new Server(function (ServerRequestInterface $request) use ($loop) {
$server = new Server($loop, function (ServerRequestInterface $request) use ($loop) {
if ($request->getMethod() !== 'GET' || $request->getUri()->getPath() !== '/') {
return new Response(404);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/59-server-json-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

$loop = Factory::create();

$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
if ($request->getHeaderLine('Content-Type') !== 'application/json') {
return new Response(
415, // Unsupported Media Type
Expand Down
2 changes: 1 addition & 1 deletion examples/61-server-hello-world-https.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

$loop = Factory::create();

$server = new Server(function (ServerRequestInterface $request) {
$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
200,
array(
Expand Down
2 changes: 1 addition & 1 deletion examples/62-server-form-upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,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(array(
$server = new Server($loop, array(
new StreamingRequestMiddleware(),
new LimitConcurrentRequestsMiddleware(100), // 100 concurrent buffering handlers, queue otherwise
new RequestBodyBufferMiddleware(8 * 1024 * 1024), // 8 MiB max, ignore body otherwise
Expand Down
Loading

0 comments on commit bc883e0

Please sign in to comment.