Skip to content

Commit b90e080

Browse files
authored
Merge pull request #122 from clue-labs/no-listener
The Server should always have a `request` listener
2 parents eb4fffe + 11841b0 commit b90e080

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
The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages.
9296
If a client sends an invalid request message or uses an invalid HTTP protocol
9397
version, it will emit an `error` event, send an HTTP error response to the

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
* The `Server` supports both HTTP/1.1 and HTTP/1.0 request messages.
3236
* If a client sends an invalid request message or uses an invalid HTTP protocol
3337
* version, it will emit an `error` event, send an HTTP error response to the
@@ -118,12 +122,6 @@ public function handleRequest(ConnectionInterface $conn, Request $request)
118122
$response = new Response($conn, $request->getProtocolVersion());
119123
$response->on('close', array($request, 'close'));
120124

121-
if (!$this->listeners('request')) {
122-
$response->end();
123-
124-
return;
125-
}
126-
127125
// attach remote ip to the request as metadata
128126
$request->remoteAddress = trim(
129127
parse_url('tcp://' . $conn->getRemoteAddress(), PHP_URL_HOST),

tests/ServerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,28 @@ function ($data) use (&$buffer) {
339339
$this->assertContains("\r\n\r\nError 505: HTTP Version Not Supported", $buffer);
340340
}
341341

342+
public function testServerWithNoRequestListenerDoesNotSendAnythingToConnection()
343+
{
344+
$server = new Server($this->socket);
345+
346+
$this->connection
347+
->expects($this->never())
348+
->method('write');
349+
350+
$this->connection
351+
->expects($this->never())
352+
->method('end');
353+
354+
$this->connection
355+
->expects($this->never())
356+
->method('close');
357+
358+
$this->socket->emit('connection', array($this->connection));
359+
360+
$data = $this->createGetRequest();
361+
$this->connection->emit('data', array($data));
362+
}
363+
342364
public function testParserErrorEmitted()
343365
{
344366
$error = null;

0 commit comments

Comments
 (0)