Skip to content

Commit 579d6c3

Browse files
authored
Merge pull request #282 from WyriHaximus/server-forward-errors
Server should forward the error event from StreamingServer
2 parents 337fc08 + 9cb64ff commit 579d6c3

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ calculate a sensible limit. It assumes a maximum of a quarter of the `memory_lim
6060
buffering and the other three quarter for parsing and handling the requests. The limit is
6161
division of half of `memory_limit` by `memory_limit` rounded up.
6262

63+
> Note that any errors emitted by the wrapped `StreamingServer` are forwarded by `Server`.
64+
6365
### StreamingServer
6466

6567
The `StreamingServer` class is responsible for handling incoming connections and then

src/Server.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace React\Http;
44

5+
use Evenement\EventEmitter;
56
use React\Http\Io\IniUtil;
67
use React\Http\Middleware\LimitConcurrentRequestsMiddleware;
78
use React\Http\Middleware\RequestBodyBufferMiddleware;
@@ -29,8 +30,10 @@
2930
* - file_uploads
3031
* - max_file_uploads
3132
* - enable_post_data_reading
33+
*
34+
* Forwards the error event coming from StreamingServer.
3235
*/
33-
final class Server
36+
final class Server extends EventEmitter
3437
{
3538
/**
3639
* @internal
@@ -66,6 +69,11 @@ public function __construct($callback)
6669
$middleware[] = $callback;
6770

6871
$this->streamingServer = new StreamingServer($middleware);
72+
73+
$that = $this;
74+
$this->streamingServer->on('error', function ($error) use ($that) {
75+
$that->emit('error', array($error));
76+
});
6977
}
7078

7179
/**

tests/ServerTest.php

+23-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,11 @@
44

55
use React\EventLoop\Factory;
66
use React\EventLoop\Timer\TimerInterface;
7-
use React\Http\MiddlewareRunner;
87
use React\Http\Server;
9-
use React\Http\StreamingServer;
108
use Psr\Http\Message\ServerRequestInterface;
11-
use React\Http\Response;
129
use React\Promise\Deferred;
1310
use Clue\React\Block;
14-
use React\Stream\ThroughStream;
15-
use React\Promise\Promise;
11+
use React\Promise;
1612

1713
final class ServerTest extends TestCase
1814
{
@@ -81,6 +77,28 @@ public function testPostFileUpload()
8177
$this->assertSame("hello\r\n", (string)$files['file']->getStream());
8278
}
8379

80+
public function testForwardErrors()
81+
{
82+
$exception = new \Exception();
83+
$capturedException = null;
84+
$server = new Server(function () use ($exception) {
85+
return Promise\reject($exception);
86+
});
87+
$server->on('error', function ($error) use (&$capturedException) {
88+
$capturedException = $error;
89+
});
90+
91+
$server->listen($this->socket);
92+
$this->socket->emit('connection', array($this->connection));
93+
94+
$data = $this->createPostFileUploadRequest();
95+
$this->connection->emit('data', array(implode('', $data)));
96+
97+
$this->assertInstanceOf('RuntimeException', $capturedException);
98+
$this->assertInstanceOf('Exception', $capturedException->getPrevious());
99+
$this->assertSame($exception, $capturedException->getPrevious());
100+
}
101+
84102
private function createPostFileUploadRequest()
85103
{
86104
$boundary = "---------------------------5844729766471062541057622570";

0 commit comments

Comments
 (0)