Skip to content

Commit 11841b0

Browse files
committed
The Server should always have a request listener
1 parent 8dfc3a3 commit 11841b0

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
@@ -308,6 +308,28 @@ function ($data) use (&$buffer) {
308308
$this->assertContains("\r\n\r\nError 505: HTTP Version Not Supported", $buffer);
309309
}
310310

311+
public function testServerWithNoRequestListenerDoesNotSendAnythingToConnection()
312+
{
313+
$server = new Server($this->socket);
314+
315+
$this->connection
316+
->expects($this->never())
317+
->method('write');
318+
319+
$this->connection
320+
->expects($this->never())
321+
->method('end');
322+
323+
$this->connection
324+
->expects($this->never())
325+
->method('close');
326+
327+
$this->socket->emit('connection', array($this->connection));
328+
329+
$data = $this->createGetRequest();
330+
$this->connection->emit('data', array($data));
331+
}
332+
311333
public function testParserErrorEmitted()
312334
{
313335
$error = null;

0 commit comments

Comments
 (0)