Skip to content

Commit 524fe9d

Browse files
committed
Adapt README
1 parent b27a34b commit 524fe9d

File tree

1 file changed

+33
-18
lines changed

1 file changed

+33
-18
lines changed

README.md

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,40 @@ 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+
Because of the `ReactPHP ReadableStreamInterface` implemantation
129+
you have opportunities like receiving large amount of data without
130+
the need to buffer, deny the request if the body is too big or
131+
start processing parts of the body without waiting for
132+
the complete body data.
133+
Use this to receive the full potential out of such scenarios and
134+
create a fast application.
118135
However, most of the `PSR-7 StreamInterface` methods have been
119136
designed under the assumption of being in control of the request body.
120-
Given that this does not apply to this server, the following
137+
This does not apply to this server, the following
121138
`PSR-7 StreamInterface` methods are not used and SHOULD NOT be called:
122139
`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
140+
The stream you access to the incoming request body as the individual chunks
126141
arrive:
127142

128143
```php
@@ -161,11 +176,11 @@ advance because the request message uses chunked transfer encoding.
161176
```php
162177
$http = new Server($socket, function (RequestInterface $request, Response $response) {
163178
$response->writeHead(200, array('Content-Type' => 'text/plain'));
164-
$response->write("Request method: " . $request->getMethod() . "\n");
165179
if ($request->getBody()->getSize() !== null) {
166-
$response->write("Request body size: " . $request->getBody()->getSize() . "\n");
180+
$response->end("Request body size: " . $request->getBody()->getSize() . "\n");
181+
return;
167182
}
168-
$response->end("The requested path is: " . $request->getUri()->getPath());
183+
$response->end("No size given.");
169184
});
170185
```
171186

0 commit comments

Comments
 (0)