Skip to content

Commit bfefb7e

Browse files
committed
The Server should always have a request listener
1 parent 1373acb commit bfefb7e

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ $http->on('request', function (Request $request, Response $response) {
8888

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

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+
9195
### Request
9296

9397
The `Request` class is responsible for streaming the incoming request body

src/Server.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
*
2929
* See also [`Request`](#request) and [`Response`](#response) for more details.
3030
*
31+
* Note that you SHOULD always listen for the `request` event.
32+
* Failing to do so will result in the server parsing the incoming request,
33+
* but never sending a response back to the client.
34+
*
3135
* @see Request
3236
* @see Response
3337
*/
@@ -112,12 +116,6 @@ public function handleRequest(ConnectionInterface $conn, Request $request, $body
112116
$response = new Response($conn);
113117
$response->on('close', array($request, 'close'));
114118

115-
if (!$this->listeners('request')) {
116-
$response->end();
117-
118-
return;
119-
}
120-
121119
$this->emit('request', array($request, $response));
122120

123121
if ($bodyBuffer !== '') {

tests/ServerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,28 @@ function ($data) use (&$buffer) {
202202
$this->assertContains("\r\nX-Powered-By: React/alpha\r\n", $buffer);
203203
}
204204

205+
public function testServerWithNoRequestListenerDoesNotSendAnythingToConnection()
206+
{
207+
$server = new Server($this->socket);
208+
209+
$this->connection
210+
->expects($this->never())
211+
->method('write');
212+
213+
$this->connection
214+
->expects($this->never())
215+
->method('end');
216+
217+
$this->connection
218+
->expects($this->never())
219+
->method('close');
220+
221+
$this->socket->emit('connection', array($this->connection));
222+
223+
$data = $this->createGetRequest();
224+
$this->connection->emit('data', array($data));
225+
}
226+
205227
public function testParserErrorEmitted()
206228
{
207229
$error = null;

0 commit comments

Comments
 (0)