Skip to content

Commit 41bcdb0

Browse files
committed
Handle events of simple requests
1 parent 9969b67 commit 41bcdb0

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/Server.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque
170170

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

173+
if ($stream instanceof CloseProtectionStream) {
174+
$request->emit('end');
175+
$request->close();
176+
return;
177+
}
178+
173179
if ($stream instanceof LengthLimitedStream && $contentLength === 0) {
174180
// Content-Length is 0 and won't emit further data,
175181
// 'handleData' from LengthLimitedStream won't be called anymore

tests/ServerTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ public function testRequestPauseWillbeForwardedToConnection()
100100
$this->connection->expects($this->once())->method('pause');
101101
$this->socket->emit('connection', array($this->connection));
102102

103-
$data = $this->createGetRequest();
103+
$data = "GET / HTTP/1.1\r\n";
104+
$data .= "Host: example.com:80\r\n";
105+
$data .= "Connection: close\r\n";
106+
$data .= "Content-Length: 5\r\n";
107+
$data .= "\r\n";
108+
104109
$this->connection->emit('data', array($data));
105110
}
106111

@@ -1175,6 +1180,31 @@ public function testCloseRequestWillPauseConnection()
11751180
$this->connection->emit('data', array($data));
11761181
}
11771182

1183+
public function testEndEventWillBeEmittedOnSimpleRequest()
1184+
{
1185+
$dataEvent = $this->expectCallableNever();
1186+
$closeEvent = $this->expectCallableOnce();
1187+
$endEvent = $this->expectCallableOnce();
1188+
$errorEvent = $this->expectCallableNever();
1189+
1190+
$server = new Server($this->socket);
1191+
$server->on('request', function ($request, $response) use ($dataEvent, $closeEvent, $endEvent, $errorEvent){
1192+
$request->on('data', $dataEvent);
1193+
$request->on('close', $closeEvent);
1194+
$request->on('end', $endEvent);
1195+
$request->on('error', $errorEvent);
1196+
});
1197+
1198+
$this->connection->expects($this->once())->method('pause');
1199+
$this->connection->expects($this->never())->method('close');
1200+
1201+
$this->socket->emit('connection', array($this->connection));
1202+
1203+
$data = $this->createGetRequest();
1204+
1205+
$this->connection->emit('data', array($data));
1206+
}
1207+
11781208
private function createGetRequest()
11791209
{
11801210
$data = "GET / HTTP/1.1\r\n";

0 commit comments

Comments
 (0)