Skip to content

Commit

Permalink
Add ChunkedDecoder to Server
Browse files Browse the repository at this point in the history
Add ServerTest
  • Loading branch information
legionth committed Feb 17, 2017
1 parent df49dc5 commit 17d5df8
Show file tree
Hide file tree
Showing 2 changed files with 195 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ public function __construct(SocketServerInterface $io)
// TODO: multipart parsing

$parser = new RequestHeaderParser();
$parser->on('headers', function (Request $request, $bodyBuffer) use ($conn, $parser, $that) {
$listener = array($parser, 'feed');

$parser->on('headers', function (Request $request, $bodyBuffer) use ($conn, $parser, $that, $listener) {
// attach remote ip to the request as metadata
$request->remoteAddress = trim(
parse_url('tcp://' . $conn->getRemoteAddress(), PHP_URL_HOST),
Expand All @@ -84,19 +86,11 @@ public function __construct(SocketServerInterface $io)
// forward pause/resume calls to underlying connection
$request->on('pause', array($conn, 'pause'));
$request->on('resume', array($conn, 'resume'));
$conn->removeListener('data', $listener);

$that->handleRequest($conn, $request, $bodyBuffer);

$conn->removeListener('data', array($parser, 'feed'));
$conn->on('end', function () use ($request) {
$request->emit('end');
});
$conn->on('data', function ($data) use ($request) {
$request->emit('data', array($data));
});
});

$listener = array($parser, 'feed');
$conn->on('data', $listener);
$parser->on('error', function() use ($conn, $listener, $that) {
// TODO: return 400 response
Expand All @@ -118,10 +112,27 @@ public function handleRequest(ConnectionInterface $conn, Request $request, $body
return;
}

$stream = $conn;
if ($request->hasHeader('Transfer-Encoding')) {
$transferEncodingHeader = $request->getHeader('Transfer-Encoding');
// 'chunked' must always be the final value of 'Transfer-Encoding' according to: https://tools.ietf.org/html/rfc7230#section-3.3.1
if (strtolower(end($transferEncodingHeader)) === 'chunked') {
$stream = new ChunkedDecoder($conn);
}
}

$stream->on('data', function ($data) use ($request) {
$request->emit('data', array($data));
});

$stream->on('end', function () use ($request) {
$request->emit('end', array());
});

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

if ($bodyBuffer !== '') {
$request->emit('data', array($bodyBuffer));
$conn->emit('data', array($bodyBuffer));
}
}
}
173 changes: 173 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,179 @@ public function testParserErrorEmitted()
$this->connection->expects($this->never())->method('write');
}

public function testBodyDataWillBeSendViaRequestEvent()
{
$server = new Server($this->socket);

$dataEvent = $this->expectCallableOnceWith('hello');
$endEvent = $this->expectCallableNever();
$closeEvent = $this->expectCallableNever();
$errorEvent = $this->expectCallableNever();

$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) {
$request->on('data', $dataEvent);
$request->on('end', $endEvent);
$request->on('close', $closeEvent);
$request->on('error', $errorEvent);
});

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

$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";
$data .= "hello";

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

public function testChunkedEncodedRequestWillBeParsedForRequestEvent()
{
$server = new Server($this->socket);

$dataEvent = $this->expectCallableOnceWith('hello');
$endEvent = $this->expectCallableOnce();
$closeEvent = $this->expectCallableNever();
$errorEvent = $this->expectCallableNever();

$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) {
$request->on('data', $dataEvent);
$request->on('end', $endEvent);
$request->on('close', $closeEvent);
$request->on('error', $errorEvent);
});

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

$data = "GET / HTTP/1.1\r\n";
$data .= "Host: example.com:80\r\n";
$data .= "Connection: close\r\n";
$data .= "Transfer-Encoding: chunked\r\n";
$data .= "\r\n";
$data .= "5\r\nhello\r\n";
$data .= "0\r\n\r\n";

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

public function testChunkedEncodedRequestAdditionalDataWontBeEmitted()
{
$server = new Server($this->socket);

$dataEvent = $this->expectCallableOnceWith('hello');
$endEvent = $this->expectCallableOnce();
$closeEvent = $this->expectCallableNever();
$errorEvent = $this->expectCallableNever();

$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) {
$request->on('data', $dataEvent);
$request->on('end', $endEvent);
$request->on('close', $closeEvent);
$request->on('error', $errorEvent);
});

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

$data = "GET / HTTP/1.1\r\n";
$data .= "Host: example.com:80\r\n";
$data .= "Connection: close\r\n";
$data .= "Transfer-Encoding: chunked\r\n";
$data .= "\r\n";
$data .= "5\r\nhello\r\n";
$data .= "0\r\n\r\n";
$data .= "2\r\nhi\r\n";

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

public function testEmptyChunkedEncodedRequest()
{
$server = new Server($this->socket);

$dataEvent = $this->expectCallableNever();
$endEvent = $this->expectCallableOnce();
$closeEvent = $this->expectCallableNever();
$errorEvent = $this->expectCallableNever();

$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) {
$request->on('data', $dataEvent);
$request->on('end', $endEvent);
$request->on('close', $closeEvent);
$request->on('error', $errorEvent);
});

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

$data = "GET / HTTP/1.1\r\n";
$data .= "Host: example.com:80\r\n";
$data .= "Connection: close\r\n";
$data .= "Transfer-Encoding: chunked\r\n";
$data .= "\r\n";
$data .= "0\r\n\r\n";

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

public function testChunkedIsUpperCase()
{
$server = new Server($this->socket);

$dataEvent = $this->expectCallableOnceWith('hello');
$endEvent = $this->expectCallableOnce();
$closeEvent = $this->expectCallableNever();
$errorEvent = $this->expectCallableNever();

$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) {
$request->on('data', $dataEvent);
$request->on('end', $endEvent);
$request->on('close', $closeEvent);
$request->on('error', $errorEvent);
});

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

$data = "GET / HTTP/1.1\r\n";
$data .= "Host: example.com:80\r\n";
$data .= "Connection: close\r\n";
$data .= "Transfer-Encoding: CHUNKED\r\n";
$data .= "\r\n";
$data .= "5\r\nhello\r\n";
$data .= "0\r\n\r\n";

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

public function testChunkedIsMixedUpperAndLowerCase()
{
$server = new Server($this->socket);

$dataEvent = $this->expectCallableOnceWith('hello');
$endEvent = $this->expectCallableOnce();
$closeEvent = $this->expectCallableNever();
$errorEvent = $this->expectCallableNever();

$server->on('request', function (Request $request, Response $response) use ($dataEvent, $endEvent, $closeEvent, $errorEvent) {
$request->on('data', $dataEvent);
$request->on('end', $endEvent);
$request->on('close', $closeEvent);
$request->on('error', $errorEvent);
});

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

$data = "GET / HTTP/1.1\r\n";
$data .= "Host: example.com:80\r\n";
$data .= "Connection: close\r\n";
$data .= "Transfer-Encoding: CHunKeD\r\n";
$data .= "\r\n";
$data .= "5\r\nhello\r\n";
$data .= "0\r\n\r\n";

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

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

0 comments on commit 17d5df8

Please sign in to comment.