Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque

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

if ($stream instanceof CloseProtectionStream) {
$request->emit('end');
$request->close();
return;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Emitting events on another instance should generally be avoided. Also, this looks somewhat similar to the code below, perhaps this can/should be unified?


if ($stream instanceof LengthLimitedStream && $contentLength === 0) {
// Content-Length is 0 and won't emit further data,
// 'handleData' from LengthLimitedStream won't be called anymore
Expand Down
32 changes: 31 additions & 1 deletion tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,12 @@ public function testRequestPauseWillbeForwardedToConnection()
$this->connection->expects($this->once())->method('pause');
$this->socket->emit('connection', array($this->connection));

$data = $this->createGetRequest();
$data = "GET / HTTP/1.1\r\n";
$data .= "Host: example.com:80\r\n";
$data .= "Connection: close\r\n";
$data .= "Content-Length: 5\r\n";
$data .= "\r\n";

$this->connection->emit('data', array($data));
}

Expand Down Expand Up @@ -1175,6 +1180,31 @@ public function testCloseRequestWillPauseConnection()
$this->connection->emit('data', array($data));
}

public function testEndEventWillBeEmittedOnSimpleRequest()
{
$dataEvent = $this->expectCallableNever();
$closeEvent = $this->expectCallableOnce();
$endEvent = $this->expectCallableOnce();
$errorEvent = $this->expectCallableNever();

$server = new Server($this->socket);
$server->on('request', function ($request, $response) use ($dataEvent, $closeEvent, $endEvent, $errorEvent){
$request->on('data', $dataEvent);
$request->on('close', $closeEvent);
$request->on('end', $endEvent);
$request->on('error', $errorEvent);
});

$this->connection->expects($this->once())->method('pause');
$this->connection->expects($this->never())->method('close');

$this->socket->emit('connection', array($this->connection));

$data = $this->createGetRequest();

$this->connection->emit('data', array($data));
}

private function createGetRequest()
{
$data = "GET / HTTP/1.1\r\n";
Expand Down