@@ -92,6 +92,8 @@ See also [`Request`](#request) and [`Response`](#response) for more details.
9292 Failing to do so will result in the server parsing the incoming request,
9393but never sending a response back to the client.
9494
95+ Checkout [ Request] ( #request ) for details about the request data body.
96+
9597The ` Server ` supports both HTTP/1.1 and HTTP/1.0 request messages.
9698If a client sends an invalid request message, uses an invalid HTTP protocol
9799version or sends an invalid ` Transfer-Encoding ` in the request header, it will
@@ -103,19 +105,7 @@ $http->on('error', function (Exception $e) {
103105});
104106```
105107
106- An ` error ` event will be emitted for the ` Request ` if the validation of the body data fails.
107- This can be e.g. invalid chunked decoded data or an unexpected ` end ` event.
108-
109- ``` php
110- $http->on('request', function (Request $request, Response $response) {
111- $request->on('error', function (\Exception $error) {
112- echo $error->getMessage();
113- });
114- });
115- ```
116-
117- Such an error will ` pause ` the connection instead of closing it. A response message
118- can still be sent.
108+ The request object can also emit an error. Checkout [ Request] ( #request ) for more details.
119109
120110### Request
121111
@@ -126,6 +116,34 @@ The `Transfer-Encoding` header will be removed.
126116
127117It implements the ` ReadableStreamInterface ` .
128118
119+ Listen on the ` data ` event and the ` end ` event of the [ Request] ( #request )
120+ to evaluate the data of the request body:
121+
122+ ``` php
123+ $http->on('request', function (Request $request, Response $response) {
124+ $contentLength = 0;
125+ $request->on('data', function ($data) use (& $contentLength) {
126+ $contentLength += strlen($data);
127+ });
128+
129+ $request->on('end', function () use ($response, & $contentLength){
130+ $response->writeHead(200, array('Content-Type' => 'text/plain'));
131+ $response->end("The length of the submitted request body is: " . $contentLength);
132+ });
133+
134+ // an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
135+ $request->on('error', function (\Exception $exception) use ($response, & $contentLength) {
136+ $response->writeHead(400, array('Content-Type' => 'text/plain'));
137+ $response->end("An error occured while reading at length: " . $contentLength);
138+ });
139+ });
140+ ```
141+
142+ An error will just ` pause ` the connection instead of closing it. A response message
143+ can still be sent.
144+
145+ A ` close ` event will be emitted after an ` error ` or ` end ` event.
146+
129147The constructor is internal, you SHOULD NOT call this yourself.
130148The ` Server ` is responsible for emitting ` Request ` and ` Response ` objects.
131149
0 commit comments