Skip to content

Commit 19b03ff

Browse files
committed
Move Response class to React\Http\Message\Response
1 parent bc883e0 commit 19b03ff

30 files changed

Lines changed: 143 additions & 91 deletions

README.md

Lines changed: 54 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,8 @@ multiple concurrent HTTP requests without blocking.
6868
* [withBase()](#withbase)
6969
* [withProtocolVersion()](#withprotocolversion)
7070
* [withResponseBuffer()](#withresponsebuffer)
71+
* [React\Http\Message](#reacthttpmessage)
72+
* [Response](#response)
7173
* [React\Http\Middleware](#reacthttpmiddleware)
7274
* [StreamingRequestMiddleware](#streamingrequestmiddleware)
7375
* [LimitConcurrentRequestsMiddleware](#limitconcurrentrequestsmiddleware)
@@ -102,7 +104,7 @@ This is an HTTP server which responds with `Hello World!` to every request.
102104
$loop = React\EventLoop\Factory::create();
103105

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

716718
```php
717719
$server = new React\Http\Server($loop, function (Psr\Http\Message\ServerRequestInterface $request) {
718-
return new React\Http\Response(
720+
return new React\Http\Message\Response(
719721
200,
720722
array(
721723
'Content-Type' => 'text/plain'
@@ -731,7 +733,7 @@ see also following [request](#server-request) chapter for more details.
731733

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

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

11571159
$body->on('end', function () use ($resolve, &$bytes){
1158-
$resolve(new React\Http\Response(
1160+
$resolve(new React\Http\Message\Response(
11591161
200,
11601162
array(
11611163
'Content-Type' => 'text/plain'
@@ -1166,7 +1168,7 @@ $server = new React\Http\Server($loop, array(
11661168

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

1225-
return new React\Http\Response(
1227+
return new React\Http\Message\Response(
12261228
411,
12271229
array(
12281230
'Content-Type' => 'text/plain'
@@ -1231,7 +1233,7 @@ $server = new React\Http\Server($loop, array(
12311233
);
12321234
}
12331235

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

1345-
### Response
1347+
### Server Response
13461348

13471349
The callback function passed to the constructor of the [`Server`](#server) is
13481350
responsible for processing the request and returning a response, which will be
1349-
delivered to the client. This function MUST return an instance implementing
1350-
[PSR-7 ResponseInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#33-psrhttpmessageresponseinterface)
1351+
delivered to the client.
1352+
1353+
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)
13511355
object or a
1352-
[ReactPHP Promise](https://github.com/reactphp/promise#reactpromise)
1353-
which will resolve a `PSR-7 ResponseInterface` object.
1356+
[ReactPHP Promise](https://github.com/reactphp/promise)
1357+
which resolves with a PSR-7 `ResponseInterface` object.
13541358

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

13611362
```php
1362-
$server = new Server($loop, function (ServerRequestInterface $request) {
1363-
return new Response(
1363+
$server = new React\Http\Server($loop, function (ServerRequestInterface $request) {
1364+
return new React\Http\Message\Response(
13641365
200,
13651366
array(
13661367
'Content-Type' => 'text/plain'
@@ -1370,6 +1371,10 @@ $server = new Server($loop, function (ServerRequestInterface $request) {
13701371
});
13711372
```
13721373

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

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

2332+
### React\Http\Message
2333+
2334+
#### Response
2335+
2336+
The `Response` class can be used to
2337+
represent an outgoing server response message.
2338+
2339+
```php
2340+
$response = new React\Http\Message\Response(
2341+
200,
2342+
array(
2343+
'Content-Type' => 'text/html'
2344+
),
2345+
"<html>Hello world!</html>\n"
2346+
);
2347+
```
2348+
2349+
This class implements the
2350+
[PSR-7 `ResponseInterface`](https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface)
2351+
which in turn extends the
2352+
[PSR-7 `MessageInterface`](https://www.php-fig.org/psr/psr-7/#31-psrhttpmessagemessageinterface).
2353+
2354+
> Internally, this class extends the underlying `\RingCentral\Psr7\Response`
2355+
class. The only difference is that this class will accept implemenations
2356+
of ReactPHPs `ReadableStreamInterface` for the `$body` argument. This base
2357+
class is considered an implementation detail that may change in the future.
2358+
23272359
### React\Http\Middleware
23282360

23292361
#### StreamingRequestMiddleware
@@ -2350,7 +2382,7 @@ $server = new React\Http\Server(array(
23502382
$bytes += \count($chunk);
23512383
});
23522384
$body->on('close', function () use (&$bytes, $resolve) {
2353-
$resolve(new React\Http\Response(
2385+
$resolve(new React\Http\Message\Response(
23542386
200,
23552387
[],
23562388
"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)