Skip to content

Commit 4fbbeb0

Browse files
authored
Merge pull request #370 from clue-labs/message
Move `Response` class to `React\Http\Message\Response` and `ServerRequest` class to `React\Http\Message\ServerRequest`
2 parents 214475d + 0ada5ec commit 4fbbeb0

37 files changed

+383
-150
lines changed

README.md

Lines changed: 75 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ multiple concurrent HTTP requests without blocking.
4343
* [Request method](#request-method)
4444
* [Cookie parameters](#cookie-parameters)
4545
* [Invalid request](#invalid-request)
46-
* [Response](#response)
46+
* [Server Response](#server-response)
4747
* [Deferred response](#deferred-response)
4848
* [Streaming outgoing response](#streaming-outgoing-response)
4949
* [Response length](#response-length)
@@ -68,6 +68,9 @@ multiple concurrent HTTP requests without blocking.
6868
* [withBase()](#withbase)
6969
* [withProtocolVersion()](#withprotocolversion)
7070
* [withResponseBuffer()](#withresponsebuffer)
71+
* [React\Http\Message](#reacthttpmessage)
72+
* [Response](#response)
73+
* [ServerRequest](#serverrequest)
7174
* [React\Http\Middleware](#reacthttpmiddleware)
7275
* [StreamingRequestMiddleware](#streamingrequestmiddleware)
7376
* [LimitConcurrentRequestsMiddleware](#limitconcurrentrequestsmiddleware)
@@ -102,7 +105,7 @@ This is an HTTP server which responds with `Hello World!` to every request.
102105
$loop = React\EventLoop\Factory::create();
103106

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

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

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

736739
In order to start listening for any incoming connections, the `Server` needs
737740
to be attached to an instance of
@@ -1158,7 +1161,7 @@ $server = new React\Http\Server(
11581161
});
11591162

11601163
$body->on('end', function () use ($resolve, &$bytes){
1161-
$resolve(new React\Http\Response(
1164+
$resolve(new React\Http\Message\Response(
11621165
200,
11631166
array(
11641167
'Content-Type' => 'text/plain'
@@ -1169,7 +1172,7 @@ $server = new React\Http\Server(
11691172

11701173
// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
11711174
$body->on('error', function (\Exception $exception) use ($resolve, &$bytes) {
1172-
$resolve(new React\Http\Response(
1175+
$resolve(new React\Http\Message\Response(
11731176
400,
11741177
array(
11751178
'Content-Type' => 'text/plain'
@@ -1226,7 +1229,7 @@ $server = new React\Http\Server(
12261229
$body = 'The request does not contain an explicit length.';
12271230
$body .= 'This example does not accept chunked transfer encoding.';
12281231

1229-
return new React\Http\Response(
1232+
return new React\Http\Message\Response(
12301233
411,
12311234
array(
12321235
'Content-Type' => 'text/plain'
@@ -1235,7 +1238,7 @@ $server = new React\Http\Server(
12351238
);
12361239
}
12371240

1238-
return new React\Http\Response(
1241+
return new React\Http\Message\Response(
12391242
200,
12401243
array(
12411244
'Content-Type' => 'text/plain'
@@ -1346,25 +1349,24 @@ Note that the server will also emit an `error` event if you do not return a
13461349
valid response object from your request handler function. See also
13471350
[invalid response](#invalid-response) for more details.
13481351

1349-
### Response
1352+
### Server Response
13501353

13511354
The callback function passed to the constructor of the [`Server`](#server) is
13521355
responsible for processing the request and returning a response, which will be
1353-
delivered to the client. This function MUST return an instance implementing
1354-
[PSR-7 ResponseInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#33-psrhttpmessageresponseinterface)
1356+
delivered to the client.
1357+
1358+
This function MUST return an instance implementing
1359+
[PSR-7 `ResponseInterface`](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#33-psrhttpmessageresponseinterface)
13551360
object or a
1356-
[ReactPHP Promise](https://github.com/reactphp/promise#reactpromise)
1357-
which will resolve a `PSR-7 ResponseInterface` object.
1361+
[ReactPHP Promise](https://github.com/reactphp/promise)
1362+
which resolves with a PSR-7 `ResponseInterface` object.
13581363

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

13651367
```php
1366-
$server = new Server($loop, function (ServerRequestInterface $request) {
1367-
return new Response(
1368+
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
1369+
return new React\Http\Message\Response(
13681370
200,
13691371
array(
13701372
'Content-Type' => 'text/plain'
@@ -1374,6 +1376,10 @@ $server = new Server($loop, function (ServerRequestInterface $request) {
13741376
});
13751377
```
13761378

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

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

2340+
### React\Http\Message
2341+
2342+
#### Response
2343+
2344+
The `Response` class can be used to
2345+
represent an outgoing server response message.
2346+
2347+
```php
2348+
$response = new React\Http\Message\Response(
2349+
200,
2350+
array(
2351+
'Content-Type' => 'text/html'
2352+
),
2353+
"<html>Hello world!</html>\n"
2354+
);
2355+
```
2356+
2357+
This class implements the
2358+
[PSR-7 `ResponseInterface`](https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface)
2359+
which in turn extends the
2360+
[PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface).
2361+
2362+
> Internally, this class extends the underlying `\RingCentral\Psr7\Response`
2363+
class. The only difference is that this class will accept implemenations
2364+
of ReactPHPs `ReadableStreamInterface` for the `$body` argument. This base
2365+
class is considered an implementation detail that may change in the future.
2366+
2367+
#### ServerRequest
2368+
2369+
The `ServerRequest` class can be used to
2370+
respresent an incoming server request message.
2371+
2372+
This class implements the
2373+
[PSR-7 `ServerRequestInterface`](https://www.php-fig.org/psr/psr-7/#321-psrhttpmessageserverrequestinterface)
2374+
which extends the
2375+
[PSR-7 `RequestInterface`](https://www.php-fig.org/psr/psr-7/#32-psrhttpmessagerequestinterface)
2376+
which in turn extends the
2377+
[PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface).
2378+
2379+
This is mostly used internally to represent each incoming request message.
2380+
Likewise, you can also use this class in test cases to test how your web
2381+
application reacts to certain HTTP requests.
2382+
2383+
> Internally, this implementation builds on top of an existing outgoing
2384+
request message and only adds required server methods. This base class is
2385+
considered an implementation detail that may change in the future.
2386+
23342387
### React\Http\Middleware
23352388

23362389
#### StreamingRequestMiddleware
@@ -2357,7 +2410,7 @@ $server = new React\Http\Server(array(
23572410
$bytes += \count($chunk);
23582411
});
23592412
$body->on('close', function () use (&$bytes, $resolve) {
2360-
$resolve(new React\Http\Response(
2413+
$resolve(new React\Http\Message\Response(
23612414
200,
23622415
[],
23632416
"Received $bytes bytes\n"

examples/51-server-hello-world.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
5-
use React\Http\Response;
5+
use React\Http\Message\Response;
66
use React\Http\Server;
77

88
require __DIR__ . '/../vendor/autoload.php';

examples/52-server-count-visitors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
5-
use React\Http\Response;
5+
use React\Http\Message\Response;
66
use React\Http\Server;
77

88
require __DIR__ . '/../vendor/autoload.php';

examples/53-server-whatsmyip.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
5-
use React\Http\Response;
5+
use React\Http\Message\Response;
66
use React\Http\Server;
77

88
require __DIR__ . '/../vendor/autoload.php';

examples/54-server-query-parameter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
5-
use React\Http\Response;
5+
use React\Http\Message\Response;
66
use React\Http\Server;
77

88
require __DIR__ . '/../vendor/autoload.php';

examples/55-server-cookie-handling.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
5-
use React\Http\Response;
5+
use React\Http\Message\Response;
66
use React\Http\Server;
77

88
require __DIR__ . '/../vendor/autoload.php';

examples/56-server-sleep.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
5-
use React\Http\Response;
5+
use React\Http\Message\Response;
66
use React\Http\Server;
77
use React\Promise\Promise;
88

examples/57-server-error-handling.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
5-
use React\Http\Response;
5+
use React\Http\Message\Response;
66
use React\Http\Server;
77
use React\Promise\Promise;
88

examples/58-server-stream-response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Psr\Http\Message\ServerRequestInterface;
44
use React\EventLoop\Factory;
5-
use React\Http\Response;
5+
use React\Http\Message\Response;
66
use React\Http\Server;
77
use React\Stream\ThroughStream;
88

examples/59-server-json-api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
use Psr\Http\Message\ServerRequestInterface;
1010
use React\EventLoop\Factory;
11-
use React\Http\Response;
11+
use React\Http\Message\Response;
1212
use React\Http\Server;
1313

1414
require __DIR__ . '/../vendor/autoload.php';

0 commit comments

Comments
 (0)