Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closing request should stop reading from connection #123

Merged
merged 2 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
87 changes: 46 additions & 41 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@
*/
class Server extends EventEmitter
{
private $io;

/**
* Creates a HTTP server that accepts connections from the given socket.
*
Expand Down Expand Up @@ -65,49 +63,36 @@ class Server extends EventEmitter
*/
public function __construct(SocketServerInterface $io)
{
$this->io = $io;
$that = $this;

$this->io->on('connection', function (ConnectionInterface $conn) use ($that) {
// TODO: http 1.1 keep-alive
// TODO: chunked transfer encoding (also for outgoing data)
// TODO: multipart parsing

$parser = new RequestHeaderParser();
$parser->on('headers', function (Request $request, $bodyBuffer) use ($conn, $parser, $that) {
// attach remote ip to the request as metadata
$request->remoteAddress = trim(
parse_url('tcp://' . $conn->getRemoteAddress(), PHP_URL_HOST),
'[]'
);
$io->on('connection', array($this, 'handleConnection'));
}

// forward pause/resume calls to underlying connection
$request->on('pause', array($conn, 'pause'));
$request->on('resume', array($conn, 'resume'));
/** @internal */
public function handleConnection(ConnectionInterface $conn)
{
$that = $this;
$parser = new RequestHeaderParser();
$listener = array($parser, 'feed');
$parser->on('headers', function (Request $request, $bodyBuffer) use ($conn, $listener, $parser, $that) {
// parsing request completed => stop feeding parser
$conn->removeListener('data', $listener);

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

$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));
});
});
if ($bodyBuffer !== '') {
$request->emit('data', array($bodyBuffer));
}
});

$listener = array($parser, 'feed');
$conn->on('data', $listener);
$parser->on('error', function() use ($conn, $listener, $that) {
// TODO: return 400 response
$conn->removeListener('data', $listener);
$that->emit('error', func_get_args());
});
$conn->on('data', $listener);
$parser->on('error', function() use ($conn, $listener, $that) {
// TODO: return 400 response
$conn->removeListener('data', $listener);
$that->emit('error', func_get_args());
});
}

/** @internal */
public function handleRequest(ConnectionInterface $conn, Request $request, $bodyBuffer)
public function handleRequest(ConnectionInterface $conn, Request $request)
{
$response = new Response($conn);
$response->on('close', array($request, 'close'));
Expand All @@ -118,10 +103,30 @@ public function handleRequest(ConnectionInterface $conn, Request $request, $body
return;
}

$this->emit('request', array($request, $response));
// attach remote ip to the request as metadata
$request->remoteAddress = trim(
parse_url('tcp://' . $conn->getRemoteAddress(), PHP_URL_HOST),
'[]'
);

if ($bodyBuffer !== '') {
$request->emit('data', array($bodyBuffer));
}
// forward pause/resume calls to underlying connection
$request->on('pause', array($conn, 'pause'));
$request->on('resume', array($conn, 'resume'));

// closing the request currently emits an "end" event
// stop reading from the connection by pausing it
$request->on('end', function () use ($conn) {
$conn->pause();
});

// forward connection events to request
$conn->on('end', function () use ($request) {
$request->emit('end');
});
$conn->on('data', function ($data) use ($request) {
$request->emit('data', array($data));
});

$this->emit('request', array($request, $response));
}
}
14 changes: 14 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,20 @@ public function testRequestResumeWillbeForwardedToConnection()
$this->connection->emit('data', array($data));
}

public function testRequestCloseWillPauseConnection()
{
$server = new Server($this->socket);
$server->on('request', function (Request $request) {
$request->close();
});

$this->connection->expects($this->once())->method('pause');
$this->socket->emit('connection', array($this->connection));

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

public function testRequestEventWithoutBodyWillNotEmitData()
{
$never = $this->expectCallableNever();
Expand Down