Skip to content

Commit 158fc23

Browse files
authored
Merge pull request #141 from legionth/add-example
Add example to handle body data
2 parents 01f0583 + 6769334 commit 158fc23

2 files changed

Lines changed: 65 additions & 13 deletions

File tree

README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
9393
but never sending a response back to the client.
9494

95+
Checkout [Request](#request) for details about the request data body.
96+
9597
The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages.
9698
If a client sends an invalid request message, uses an invalid HTTP protocol
9799
version 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

127117
It 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+
129147
The constructor is internal, you SHOULD NOT call this yourself.
130148
The `Server` is responsible for emitting `Request` and `Response` objects.
131149

examples/03-handling-body-data.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
use React\Socket\Server;
5+
use React\Http\Request;
6+
use React\Http\Response;
7+
8+
require __DIR__ . '/../vendor/autoload.php';
9+
10+
$loop = Factory::create();
11+
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
12+
13+
$server = new \React\Http\Server($socket);
14+
$server->on('request', function (Request $request, Response $response) {
15+
$contentLength = 0;
16+
$request->on('data', function ($data) use (&$contentLength) {
17+
$contentLength += strlen($data);
18+
});
19+
20+
$request->on('end', function () use ($response, &$contentLength){
21+
$response->writeHead(200, array('Content-Type' => 'text/plain'));
22+
$response->end("The length of the submitted request body is: " . $contentLength);
23+
});
24+
25+
// an error occures e.g. on invalid chunked encoded data or an unexpected 'end' event
26+
$request->on('error', function (\Exception $exception) use ($response, &$contentLength) {
27+
$response->writeHead(400, array('Content-Type' => 'text/plain'));
28+
$response->end("An error occured while reading at length: " . $contentLength);
29+
});
30+
});
31+
32+
echo 'Listening on http://' . $socket->getAddress() . PHP_EOL;
33+
34+
$loop->run();

0 commit comments

Comments
 (0)