Skip to content

Commit 215c29b

Browse files
committed
Use callback function instead of request event
1 parent 62e0a52 commit 215c29b

6 files changed

Lines changed: 89 additions & 165 deletions

File tree

README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ This is an HTTP server which responds with `Hello World` to every request.
3535
$loop = React\EventLoop\Factory::create();
3636
$socket = new React\Socket\Server(8080, $loop);
3737

38-
$http = new React\Http\Server($socket);
39-
$http->on('request', function (Request $request, Response $response) {
38+
$http = new React\Http\Server($socket, function (Request $request, Response $response) {
4039
$response->writeHead(200, array('Content-Type' => 'text/plain'));
4140
$response->end("Hello World!\n");
4241
});
@@ -51,7 +50,7 @@ See also the [examples](examples).
5150
### Server
5251

5352
The `Server` class is responsible for handling incoming connections and then
54-
emit a `request` event for each incoming HTTP request.
53+
execute the execute the callback function passed to the constructor.
5554

5655
It attaches itself to an instance of `React\Socket\ServerInterface` which
5756
emits underlying streaming connections in order to then parse incoming data
@@ -76,22 +75,18 @@ $socket = new SecureServer($socket, $loop, array(
7675
$http = new React\Http\Server($socket);
7776
```
7877

79-
For each incoming connection, it emits a `request` event with the respective
78+
For each incoming connection, it executes the callback function with the respective
8079
[`Request`](#request) and [`Response`](#response) objects:
8180

8281
```php
83-
$http->on('request', function (Request $request, Response $response) {
82+
$http = new React\Http\Server($socket, function (Request $request, Response $response) {
8483
$response->writeHead(200, array('Content-Type' => 'text/plain'));
8584
$response->end("Hello World!\n");
8685
});
8786
```
8887

8988
See also [`Request`](#request) and [`Response`](#response) for more details.
9089

91-
> Note that you SHOULD always listen for the `request` event.
92-
Failing to do so will result in the server parsing the incoming request,
93-
but never sending a response back to the client.
94-
9590
Checkout [Request](#request) for details about the request data body.
9691

9792
The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages.
@@ -120,7 +115,7 @@ Listen on the `data` event and the `end` event of the [Request](#request)
120115
to evaluate the data of the request body:
121116

122117
```php
123-
$http->on('request', function (Request $request, Response $response) {
118+
$http = new React\Http\Server($socket, function (RequestInterface $request, Response $response) {
124119
$contentLength = 0;
125120
$request->on('data', function ($data) use (&$contentLength) {
126121
$contentLength += strlen($data);
@@ -247,7 +242,7 @@ This method is mostly useful in combination with the
247242
[`expectsContinue()`](#expectscontinue) method like this:
248243

249244
```php
250-
$http->on('request', function (Request $request, Response $response) {
245+
$http = new React\Http\Server($socket, function (Request $request, Response $response) {
251246
if ($request->expectsContinue()) {
252247
$response->writeContinue();
253248
}

examples/01-hello-world.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
$loop = Factory::create();
1111
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
1212

13-
$server = new \React\Http\Server($socket);
14-
$server->on('request', function (Request $request, Response $response) {
13+
$server = new \React\Http\Server($socket, function (Request $request, Response $response) {
1514
$response->writeHead(200, array('Content-Type' => 'text/plain'));
1615
$response->end("Hello world!\n");
1716
});

examples/02-hello-world-https.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
'local_cert' => isset($argv[2]) ? $argv[2] : __DIR__ . '/localhost.pem'
1515
));
1616

17-
$server = new \React\Http\Server($socket);
18-
$server->on('request', function (Request $reques, Response $response) {
17+
$server = new \React\Http\Server($socket, function (Request $request, Response $response) {
1918
$response->writeHead(200, array('Content-Type' => 'text/plain'));
2019
$response->end("Hello world!\n");
2120
});

examples/03-handling-body-data.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
$loop = Factory::create();
1111
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);
1212

13-
$server = new \React\Http\Server($socket);
14-
$server->on('request', function (Request $request, Response $response) {
13+
$server = new \React\Http\Server($socket, function (Request $request, Response $response) {
1514
$contentLength = 0;
1615
$request->on('data', function ($data) use (&$contentLength) {
1716
$contentLength += strlen($data);

src/Server.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use React\Socket\ServerInterface as SocketServerInterface;
77
use React\Socket\ConnectionInterface;
88
use Psr\Http\Message\RequestInterface;
9+
use Doctrine\Instantiator\Exception\InvalidArgumentException;
910

1011
/**
1112
* The `Server` class is responsible for handling incoming connections and then
@@ -21,18 +22,14 @@
2122
* [`Request`](#request) and [`Response`](#response) objects:
2223
*
2324
* ```php
24-
* $http->on('request', function (Request $request, Response $response) {
25+
* $http = new React\Http\Server($socket, function (Request $request, Response $response) {
2526
* $response->writeHead(200, array('Content-Type' => 'text/plain'));
2627
* $response->end("Hello World!\n");
2728
* });
2829
* ```
2930
*
3031
* See also [`Request`](#request) and [`Response`](#response) for more details.
3132
*
32-
* > Note that you SHOULD always listen for the `request` event.
33-
* Failing to do so will result in the server parsing the incoming request,
34-
* but never sending a response back to the client.
35-
*
3633
* The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages.
3734
* If a client sends an invalid request message or uses an invalid HTTP protocol
3835
* version, it will emit an `error` event, send an HTTP error response to the
@@ -49,6 +46,8 @@
4946
*/
5047
class Server extends EventEmitter
5148
{
49+
private $callback;
50+
5251
/**
5352
* Creates a HTTP server that accepts connections from the given socket.
5453
*
@@ -62,6 +61,14 @@ class Server extends EventEmitter
6261
* $http = new React\Http\Server($socket);
6362
* ```
6463
*
64+
* This also needs a callback function which will be executed on a successful request
65+
*
66+
* ```php
67+
* $http = new React\Http\Server($socket, function (Request $request, Response $response) {
68+
* $response->writeHead(200, array('Content-Type' => 'text/plain'));
69+
* $response->end("Hello World!\n");
70+
* });
71+
* ```
6572
* Similarly, you can also attach this to a
6673
* [`React\Socket\SecureServer`](https://github.com/reactphp/socket#secureserver)
6774
* in order to start a secure HTTPS server like this:
@@ -76,10 +83,16 @@ class Server extends EventEmitter
7683
* ```
7784
*
7885
* @param \React\Socket\ServerInterface $io
86+
* @param callable $callback
7987
*/
80-
public function __construct(SocketServerInterface $io)
88+
public function __construct(SocketServerInterface $io, $callback)
8189
{
90+
if (!is_callable($callback)) {
91+
throw new InvalidArgumentException();
92+
}
93+
8294
$io->on('connection', array($this, 'handleConnection'));
95+
$this->callback = $callback;
8396
}
8497

8598
/** @internal */
@@ -176,7 +189,8 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque
176189
'[]'
177190
);
178191

179-
$this->emit('request', array($request, $response));
192+
$callback = $this->callback;
193+
$callback($request, $response);
180194

181195
if ($contentLength === 0) {
182196
// If Body is empty or Content-Length is 0 and won't emit further data,

0 commit comments

Comments
 (0)