Skip to content

Commit 0df5dc8

Browse files
committed
Use PSR-7 approach to handle bodiless data
1 parent 41bcdb0 commit 0df5dc8

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

src/Server.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,14 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque
140140

141141
$response = new Response($conn, $request->getProtocolVersion());
142142

143+
$contentLength = 0;
143144
$stream = new CloseProtectionStream($conn);
144145
if ($request->hasHeader('Transfer-Encoding')) {
145146
$transferEncodingHeader = $request->getHeader('Transfer-Encoding');
146147
// 'chunked' must always be the final value of 'Transfer-Encoding' according to: https://tools.ietf.org/html/rfc7230#section-3.3.1
147148
if (strtolower(end($transferEncodingHeader)) === 'chunked') {
148149
$stream = new ChunkedDecoder($stream);
150+
$contentLength = null;
149151
}
150152
} elseif ($request->hasHeader('Content-Length')) {
151153
$string = $request->getHeaderLine('Content-Length');
@@ -170,15 +172,9 @@ public function handleRequest(ConnectionInterface $conn, RequestInterface $reque
170172

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

173-
if ($stream instanceof CloseProtectionStream) {
174-
$request->emit('end');
175-
$request->close();
176-
return;
177-
}
178-
179-
if ($stream instanceof LengthLimitedStream && $contentLength === 0) {
180-
// Content-Length is 0 and won't emit further data,
181-
// 'handleData' from LengthLimitedStream won't be called anymore
175+
if ($contentLength === 0) {
176+
// If Body is empty or Content-Length is 0 and won't emit further data,
177+
// 'data' events from other streams won't be called anymore
182178
$stream->emit('end');
183179
$stream->close();
184180
}

tests/ServerTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,42 @@ public function testEndEventWillBeEmittedOnSimpleRequest()
12051205
$this->connection->emit('data', array($data));
12061206
}
12071207

1208+
public function testRequestWithIncompleteHeaderWontEmitRequestEvent()
1209+
{
1210+
$server = new Server($this->socket);
1211+
$server->on('request', $this->expectCallableNever());
1212+
1213+
$this->socket->emit('connection', array($this->connection));
1214+
1215+
$data = "GET / HTTP/1.1\r\n";
1216+
1217+
$this->connection->emit('data', array($data));
1218+
}
1219+
1220+
public function testRequestWithoutDefinedLengthWillIgnoreDataEvent()
1221+
{
1222+
$server = new Server($this->socket);
1223+
1224+
$dataEvent = $this->expectCallableNever();
1225+
$endEvent = $this->expectCallableOnce();
1226+
$closeEvent = $this->expectCallableOnce();
1227+
$errorEvent = $this->expectCallableNever();
1228+
1229+
$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) {
1230+
$request->on('data', $dataEvent);
1231+
$request->on('end', $endEvent);
1232+
$request->on('close', $closeEvent);
1233+
$request->on('error', $errorEvent);
1234+
});
1235+
1236+
$this->socket->emit('connection', array($this->connection));
1237+
1238+
$data = $this->createGetRequest();
1239+
$data .= "hello world";
1240+
1241+
$this->connection->emit('data', array($data));
1242+
}
1243+
12081244
private function createGetRequest()
12091245
{
12101246
$data = "GET / HTTP/1.1\r\n";

0 commit comments

Comments
 (0)