Skip to content

Commit 40cac8b

Browse files
committed
Adapt README
1 parent b27a34b commit 40cac8b

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

README.md

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,42 @@ for more details.
104104
### Request
105105

106106
A HTTP request will be sent by a client to the [Server](#server).
107-
The `Server` will receive this request on the `data` event and
108-
is responsible to create a request object from this data.
109-
This object will be passed to the callback function.
110-
111-
The request is an instance of [PSR-7 RequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface).
112-
113-
As defined in PSR-7, the `getBody()` method returns a stream instance
114-
which implements the [PSR-7 StreamInterface](http://www.php-fig.org/psr/psr-7/#psrhttpmessagestreaminterface).
115-
Note that the incoming request will be processed once the request headers have
116-
been received, which implies that the (potentially much larger) request body
117-
may still be outstanding (in a streaming state).
107+
The `Server` will receive this request and
108+
is responsible for creating a request object from this data.
109+
The request will be processed once the request headers have
110+
been received, irrespective of the (potentially much larger) request body (see also below).
111+
This object implements the
112+
[PSR-7 RequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface).
113+
and will be passed to the callback function.
114+
115+
```php
116+
$http = new Server($socket, function (RequestInterface $request, Response $response) {
117+
$response->writeHead(200, array('Content-Type' => 'text/plain'));
118+
$response->write("The method of the request is: " . $request->getMethod());
119+
$response->end("The requested path is: " . $request->getUri()->getPath());
120+
});
121+
```
122+
123+
For more details about the request object, check out the [PSR-7 RequestInterface](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-7-http-message.md#32-psrhttpmessagerequestinterface) documentation.
124+
125+
If you want to access the request body you can use `getBody()`.
126+
This method returns an instance that implements both the [PSR-7 StreamInterface](http://www.php-fig.org/psr/psr-7/#psrhttpmessagestreaminterface)
127+
and the [ReactPHP ReadableStreamInterface](https://github.com/reactphp/stream#readablestreaminterface).
128+
The `ReactPHP ReadableStreamInterface` implemantation
129+
gives you advantages like:
130+
131+
* Receiving large amount of data without the need to buffer
132+
* Deny the request if the body is too big
133+
* Start processing parts of the body without waiting for the complete body data.
134+
135+
Use this to receive the full potential from these scenarios and
136+
create a fast application.
118137
However, most of the `PSR-7 StreamInterface` methods have been
119138
designed under the assumption of being in control of the request body.
120-
Given that this does not apply to this server, the following
139+
This does not apply to this server, the following
121140
`PSR-7 StreamInterface` methods are not used and SHOULD NOT be called:
122141
`tell()`, `eof()`, `seek()`, `rewind()`, `write()` and `read()`.
123-
Instead, the returned stream instance *also* implements the
124-
[ReactPHP ReadableStreamInterface](https://github.com/reactphp/stream#readablestreaminterface)
125-
which gives you access to the incoming request body as the individual chunks
142+
The stream you access to the incoming request body as the individual chunks
126143
arrive:
127144

128145
```php
@@ -161,11 +178,11 @@ advance because the request message uses chunked transfer encoding.
161178
```php
162179
$http = new Server($socket, function (RequestInterface $request, Response $response) {
163180
$response->writeHead(200, array('Content-Type' => 'text/plain'));
164-
$response->write("Request method: " . $request->getMethod() . "\n");
165181
if ($request->getBody()->getSize() !== null) {
166-
$response->write("Request body size: " . $request->getBody()->getSize() . "\n");
182+
$response->end("Request body size: " . $request->getBody()->getSize() . "\n");
183+
return;
167184
}
168-
$response->end("The requested path is: " . $request->getUri()->getPath());
185+
$response->end("No size given.");
169186
});
170187
```
171188

0 commit comments

Comments
 (0)