Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Response class to React\Http\Message\Response and ServerRequest class to React\Http\Message\ServerRequest #370

Merged
merged 4 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 75 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ multiple concurrent HTTP requests without blocking.
* [Request method](#request-method)
* [Cookie parameters](#cookie-parameters)
* [Invalid request](#invalid-request)
* [Response](#response)
* [Server Response](#server-response)
* [Deferred response](#deferred-response)
* [Streaming outgoing response](#streaming-outgoing-response)
* [Response length](#response-length)
Expand All @@ -68,6 +68,9 @@ multiple concurrent HTTP requests without blocking.
* [withBase()](#withbase)
* [withProtocolVersion()](#withprotocolversion)
* [withResponseBuffer()](#withresponsebuffer)
* [React\Http\Message](#reacthttpmessage)
* [Response](#response)
* [ServerRequest](#serverrequest)
* [React\Http\Middleware](#reacthttpmiddleware)
* [StreamingRequestMiddleware](#streamingrequestmiddleware)
* [LimitConcurrentRequestsMiddleware](#limitconcurrentrequestsmiddleware)
Expand Down Expand Up @@ -102,7 +105,7 @@ This is an HTTP server which responds with `Hello World!` to every request.
$loop = React\EventLoop\Factory::create();

$server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Response(
return new React\Http\Message\Response(
200,
array(
'Content-Type' => 'text/plain'
Expand Down Expand Up @@ -711,11 +714,11 @@ processing each incoming HTTP request.
When a complete HTTP request has been received, it will invoke the given
request handler function. This request handler function needs to be passed to
the constructor and will be invoked with the respective [request](#server-request)
object and expects a [response](#response) object in return:
object and expects a [response](#server-response) object in return:

```php
$server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) {
return new React\Http\Response(
return new React\Http\Message\Response(
200,
array(
'Content-Type' => 'text/plain'
Expand All @@ -731,7 +734,7 @@ see also following [request](#server-request) chapter for more details.

Each outgoing HTTP response message is always represented by the
[PSR-7 `ResponseInterface`](https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface),
see also following [response](#response) chapter for more details.
see also following [response](#server-response) chapter for more details.

In order to start listening for any incoming connections, the `Server` needs
to be attached to an instance of
Expand Down Expand Up @@ -1155,7 +1158,7 @@ $server = new React\Http\Server($loop, array(
});

$body->on('end', function () use ($resolve, &$bytes){
$resolve(new React\Http\Response(
$resolve(new React\Http\Message\Response(
200,
array(
'Content-Type' => 'text/plain'
Expand All @@ -1166,7 +1169,7 @@ $server = new React\Http\Server($loop, array(

// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
$body->on('error', function (\Exception $exception) use ($resolve, &$bytes) {
$resolve(new React\Http\Response(
$resolve(new React\Http\Message\Response(
400,
array(
'Content-Type' => 'text/plain'
Expand Down Expand Up @@ -1222,7 +1225,7 @@ $server = new React\Http\Server($loop, array(
$body = 'The request does not contain an explicit length.';
$body .= 'This example does not accept chunked transfer encoding.';

return new React\Http\Response(
return new React\Http\Message\Response(
411,
array(
'Content-Type' => 'text/plain'
Expand All @@ -1231,7 +1234,7 @@ $server = new React\Http\Server($loop, array(
);
}

return new React\Http\Response(
return new React\Http\Message\Response(
200,
array(
'Content-Type' => 'text/plain'
Expand Down Expand Up @@ -1342,25 +1345,24 @@ Note that the server will also emit an `error` event if you do not return a
valid response object from your request handler function. See also
[invalid response](#invalid-response) for more details.

### Response
### Server Response

The callback function passed to the constructor of the [`Server`](#server) is
responsible for processing the request and returning a response, which will be
delivered to the client. This function MUST return an instance implementing
[PSR-7 ResponseInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#33-psrhttpmessageresponseinterface)
delivered to the client.

This function MUST return an instance implementing
[PSR-7 `ResponseInterface`](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#33-psrhttpmessageresponseinterface)
object or a
[ReactPHP Promise](https://github.com/reactphp/promise#reactpromise)
which will resolve a `PSR-7 ResponseInterface` object.
[ReactPHP Promise](https://github.com/reactphp/promise)
which resolves with a PSR-7 `ResponseInterface` object.

You will find a `Response` class
which implements the `PSR-7 ResponseInterface` in this project.
We use instantiation of this class in our projects,
but feel free to use any implemantation of the
`PSR-7 ResponseInterface` you prefer.
This projects ships a [`Response` class](#response) which implements the PSR-7
`ResponseInterface`. In its most simple form, you can use it like this:

```php
$server = new Server($loop, function (ServerRequestInterface $request) {
return new Response(
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
return new React\Http\Message\Response(
200,
array(
'Content-Type' => 'text/plain'
Expand All @@ -1370,6 +1372,10 @@ $server = new Server($loop, function (ServerRequestInterface $request) {
});
```

We use this [`Response` class](#response) throughout our project examples, but
feel free to use any other implementation of the PSR-7 `ResponseInterface`.
See also the [`Response` class](#response) for more details.

#### Deferred response

The example above returns the response directly, because it needs
Expand Down Expand Up @@ -2324,6 +2330,53 @@ Notice that the [`Browser`](#browser) is an immutable object, i.e. this
method actually returns a *new* [`Browser`](#browser) instance with the
given setting applied.

### React\Http\Message

#### Response

The `Response` class can be used to
represent an outgoing server response message.

```php
$response = new React\Http\Message\Response(
200,
array(
'Content-Type' => 'text/html'
),
"<html>Hello world!</html>\n"
);
```

This class implements the
[PSR-7 `ResponseInterface`](https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface)
which in turn extends the
[PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface).

> Internally, this class extends the underlying `\RingCentral\Psr7\Response`
class. The only difference is that this class will accept implemenations
of ReactPHPs `ReadableStreamInterface` for the `$body` argument. This base
class is considered an implementation detail that may change in the future.

#### ServerRequest

The `ServerRequest` class can be used to
respresent an incoming server request message.

This class implements the
[PSR-7 `ServerRequestInterface`](https://www.php-fig.org/psr/psr-7/#321-psrhttpmessageserverrequestinterface)
which extends the
[PSR-7 `RequestInterface`](https://www.php-fig.org/psr/psr-7/#32-psrhttpmessagerequestinterface)
which in turn extends the
[PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface).

This is mostly used internally to represent each incoming request message.
Likewise, you can also use this class in test cases to test how your web
application reacts to certain HTTP requests.

> Internally, this implementation builds on top of an existing outgoing
request message and only adds required server methods. This base class is
considered an implementation detail that may change in the future.

### React\Http\Middleware

#### StreamingRequestMiddleware
Expand All @@ -2350,7 +2403,7 @@ $server = new React\Http\Server(array(
$bytes += \count($chunk);
});
$body->on('close', function () use (&$bytes, $resolve) {
$resolve(new React\Http\Response(
$resolve(new React\Http\Message\Response(
200,
[],
"Received $bytes bytes\n"
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 @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
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 @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
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 @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
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 @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
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 @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
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 @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;
use React\Promise\Promise;

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 @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;
use React\Promise\Promise;

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 @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;
use React\Stream\ThroughStream;

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 @@ -8,7 +8,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
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 @@ -2,7 +2,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
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 @@ -10,11 +10,11 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UploadedFileInterface;
use React\EventLoop\Factory;
use React\Http\Message\Response;
use React\Http\Middleware\LimitConcurrentRequestsMiddleware;
use React\Http\Middleware\RequestBodyBufferMiddleware;
use React\Http\Middleware\RequestBodyParserMiddleware;
use React\Http\Middleware\StreamingRequestMiddleware;
use React\Http\Response;
use React\Http\Server;

require __DIR__ . '/../vendor/autoload.php';
Expand Down
4 changes: 2 additions & 2 deletions examples/63-server-streaming-request.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function (Psr\Http\Message\ServerRequestInterface $request) {
});

$body->on('end', function () use ($resolve, &$bytes){
$resolve(new React\Http\Response(
$resolve(new React\Http\Message\Response(
200,
array(
'Content-Type' => 'text/plain'
Expand All @@ -34,7 +34,7 @@ function (Psr\Http\Message\ServerRequestInterface $request) {

// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
$body->on('error', function (\Exception $exception) use ($resolve, &$bytes) {
$resolve(new React\Http\Response(
$resolve(new React\Http\Message\Response(
400,
array(
'Content-Type' => 'text/plain'
Expand Down
2 changes: 1 addition & 1 deletion examples/71-server-http-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Psr\Http\Message\RequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;
use RingCentral\Psr7;

Expand Down
2 changes: 1 addition & 1 deletion examples/72-server-http-connect-proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;
use React\Socket\Connector;
use React\Socket\ConnectionInterface;
Expand Down
2 changes: 1 addition & 1 deletion examples/81-server-upgrade-echo.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;
use React\Stream\ThroughStream;

Expand Down
2 changes: 1 addition & 1 deletion examples/82-server-upgrade-chat.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;
use React\Stream\CompositeStream;
use React\Stream\ThroughStream;
Expand Down
2 changes: 1 addition & 1 deletion examples/99-server-benchmark-download.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Evenement\EventEmitter;
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Factory;
use React\Http\Response;
use React\Http\Message\Response;
use React\Http\Server;
use React\Stream\ReadableStreamInterface;
use React\Stream\WritableStreamInterface;
Expand Down
3 changes: 2 additions & 1 deletion src/Io/RequestHeaderParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Evenement\EventEmitter;
use Psr\Http\Message\ServerRequestInterface;
use React\Http\Message\ServerRequest;
use React\Socket\ConnectionInterface;
use Exception;

Expand Down Expand Up @@ -223,7 +224,7 @@ public function parseRequest($headers, $remoteSocketUri, $localSocketUri)
$start['method'],
$uri,
$fields,
null,
'',
$start['version'],
$serverParams
);
Expand Down
Loading