Skip to content

Commit

Permalink
Use callback function instead of request event
Browse files Browse the repository at this point in the history
  • Loading branch information
legionth committed Mar 9, 2017
1 parent 206b3e4 commit d0d122b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 180 deletions.
36 changes: 14 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ This is an HTTP server which responds with `Hello World` to every request.
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server(8080, $loop);

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

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

It attaches itself to an instance of `React\Socket\ServerInterface` which
emits underlying streaming connections in order to then parse incoming data
as HTTP:
as HTTP.

For each incoming connection, it executes the callback function with the respective
[`Request`](#request) and [`Response`](#response) objects:

```php
$socket = new React\Socket\Server(8080, $loop);

$http = new React\Http\Server($socket);
$http = new React\Http\Server($socket, function (Request $request, Response $response) {
$response->writeHead(200, array('Content-Type' => 'text/plain'));
$response->end("Hello World!\n");
});
```

Similarly, you can also attach this to a
Expand All @@ -73,26 +78,13 @@ $socket = new SecureServer($socket, $loop, array(
'local_cert' => __DIR__ . '/localhost.pem'
));

$http = new React\Http\Server($socket);
```

For each incoming connection, it emits a `request` event with the respective
[`Request`](#request) and [`Response`](#response) objects:

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

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

> Note that you SHOULD always listen for the `request` event.
Failing to do so will result in the server parsing the incoming request,
but never sending a response back to the client.

Checkout [Request](#request) for details about the request data body.
See also [`Request`](#request) and [`Response`](#response) for more details(e.g. the request data body).

The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages.
If a client sends an invalid request message, uses an invalid HTTP protocol
Expand Down Expand Up @@ -120,7 +112,7 @@ Listen on the `data` event and the `end` event of the [Request](#request)
to evaluate the data of the request body:

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

```php
$http->on('request', function (Request $request, Response $response) {
$http = new React\Http\Server($socket, function (Request $request, Response $response) {
if ($request->expectsContinue()) {
$response->writeContinue();
}
Expand Down
3 changes: 1 addition & 2 deletions examples/01-hello-world.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
$loop = Factory::create();
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);

$server = new \React\Http\Server($socket);
$server->on('request', function (Request $request, Response $response) {
$server = new \React\Http\Server($socket, function (Request $request, Response $response) {
$response->writeHead(200, array('Content-Type' => 'text/plain'));
$response->end("Hello world!\n");
});
Expand Down
3 changes: 1 addition & 2 deletions examples/02-hello-world-https.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
'local_cert' => isset($argv[2]) ? $argv[2] : __DIR__ . '/localhost.pem'
));

$server = new \React\Http\Server($socket);
$server->on('request', function (Request $reques, Response $response) {
$server = new \React\Http\Server($socket, function (Request $request, Response $response) {
$response->writeHead(200, array('Content-Type' => 'text/plain'));
$response->end("Hello world!\n");
});
Expand Down
3 changes: 1 addition & 2 deletions examples/03-handling-body-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
$loop = Factory::create();
$socket = new Server(isset($argv[1]) ? $argv[1] : '0.0.0.0:0', $loop);

$server = new \React\Http\Server($socket);
$server->on('request', function (Request $request, Response $response) {
$server = new \React\Http\Server($socket, function (Request $request, Response $response) {
$contentLength = 0;
$request->on('data', function ($data) use (&$contentLength) {
$contentLength += strlen($data);
Expand Down
37 changes: 26 additions & 11 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use React\Socket\ServerInterface as SocketServerInterface;
use React\Socket\ConnectionInterface;
use Psr\Http\Message\RequestInterface;
use Doctrine\Instantiator\Exception\InvalidArgumentException;

/**
* The `Server` class is responsible for handling incoming connections and then
Expand All @@ -21,18 +22,14 @@
* [`Request`](#request) and [`Response`](#response) objects:
*
* ```php
* $http->on('request', function (Request $request, Response $response) {
* $http = new React\Http\Server($socket, function (Request $request, Response $response) {
* $response->writeHead(200, array('Content-Type' => 'text/plain'));
* $response->end("Hello World!\n");
* });
* ```
*
* See also [`Request`](#request) and [`Response`](#response) for more details.
*
* > Note that you SHOULD always listen for the `request` event.
* Failing to do so will result in the server parsing the incoming request,
* but never sending a response back to the client.
*
* The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages.
* If a client sends an invalid request message or uses an invalid HTTP protocol
* version, it will emit an `error` event, send an HTTP error response to the
Expand All @@ -49,17 +46,25 @@
*/
class Server extends EventEmitter
{
private $callback;

/**
* Creates a HTTP server that accepts connections from the given socket.
*
* It attaches itself to an instance of `React\Socket\ServerInterface` which
* emits underlying streaming connections in order to then parse incoming data
* as HTTP:
* as HTTP.
*
* For each incoming connection, it executes the callback function with the respective
* [`Request`](#request) and [`Response`](#response) objects:
*
* ```php
* $socket = new React\Socket\Server(8080, $loop);
*
* $http = new React\Http\Server($socket);
* $http = new React\Http\Server($socket, function (Request $request, Response $response) {
* $response->writeHead(200, array('Content-Type' => 'text/plain'));
* $response->end("Hello World!\n");
* });
* ```
*
* Similarly, you can also attach this to a
Expand All @@ -72,14 +77,23 @@ class Server extends EventEmitter
* 'local_cert' => __DIR__ . '/localhost.pem'
* ));
*
* $http = new React\Http\Server($socket);
* ```
* $http = new React\Http\Server($socket, function (Request $request, Response $response) {
* $response->writeHead(200, array('Content-Type' => 'text/plain'));
* $response->end("Hello World!\n");
* });
*```
*
* @param \React\Socket\ServerInterface $io
* @param callable $callback
*/
public function __construct(SocketServerInterface $io)
public function __construct(SocketServerInterface $io, $callback)
{
if (!is_callable($callback)) {
throw new InvalidArgumentException();
}

$io->on('connection', array($this, 'handleConnection'));
$this->callback = $callback;
}

/** @internal */
Expand Down Expand Up @@ -176,7 +190,8 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque
'[]'
);

$this->emit('request', array($request, $response));
$callback = $this->callback;
$callback($request, $response);

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

0 comments on commit d0d122b

Please sign in to comment.