From f14fec7fd3d356dce5ef95051297477672998bf3 Mon Sep 17 00:00:00 2001 From: barrylb Date: Tue, 29 Jul 2014 23:13:05 +1000 Subject: [PATCH 1/4] Support http connection keep-alive --- src/RequestHeaderParser.php | 3 +-- src/Response.php | 7 +++++-- src/Server.php | 12 ++++++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/RequestHeaderParser.php b/src/RequestHeaderParser.php index 39bc127a..26f652ae 100644 --- a/src/RequestHeaderParser.php +++ b/src/RequestHeaderParser.php @@ -26,9 +26,8 @@ public function feed($data) if (false !== strpos($this->buffer, "\r\n\r\n")) { list($request, $bodyBuffer) = $this->parseRequest($this->buffer); - + $this->buffer = ''; $this->emit('headers', array($request, $bodyBuffer)); - $this->removeAllListeners(); } } diff --git a/src/Response.php b/src/Response.php index 13b22211..412ef5b2 100644 --- a/src/Response.php +++ b/src/Response.php @@ -107,7 +107,7 @@ public function write($data) return $flushed; } - public function end($data = null) + public function end($data = null, $keepAlive = false) { if (null !== $data) { $this->write($data); @@ -119,7 +119,10 @@ public function end($data = null) $this->emit('end'); $this->removeAllListeners(); - $this->conn->end(); + + if (!$keepAlive) { + $this->conn->end(); + } } public function close() diff --git a/src/Server.php b/src/Server.php index 4bf6cc28..0f6383b1 100644 --- a/src/Server.php +++ b/src/Server.php @@ -16,7 +16,6 @@ public function __construct(SocketServerInterface $io) $this->io = $io; $this->io->on('connection', function ($conn) { - // TODO: http 1.1 keep-alive // TODO: chunked transfer encoding (also for outgoing data) // TODO: multipart parsing @@ -25,8 +24,6 @@ public function __construct(SocketServerInterface $io) // attach remote ip to the request as metadata $request->remoteAddress = $conn->getRemoteAddress(); - $this->handleRequest($conn, $request, $bodyBuffer); - $conn->removeListener('data', array($parser, 'feed')); $conn->on('end', function () use ($request) { $request->emit('end'); @@ -40,13 +37,15 @@ public function __construct(SocketServerInterface $io) $request->on('resume', function () use ($conn) { $conn->emit('resume'); }); + + $this->handleRequest($conn, $parser, $request, $bodyBuffer); }); $conn->on('data', array($parser, 'feed')); }); } - public function handleRequest(ConnectionInterface $conn, Request $request, $bodyBuffer) + public function handleRequest(ConnectionInterface $conn, RequestHeaderParser $parser, Request $request, $bodyBuffer) { $response = new Response($conn); $response->on('close', array($request, 'close')); @@ -57,6 +56,11 @@ public function handleRequest(ConnectionInterface $conn, Request $request, $body return; } + $response->on('end', function () use ($conn, $parser) { + $conn->removeAllListeners(); // stop data being sent to this Request instance + $conn->on('data', array($parser, 'feed')); // resume sending data to RequestHeaderParser + }); + $this->emit('request', array($request, $response)); $request->emit('data', array($bodyBuffer)); } From f4685149beefd9485ebd9619e72a53f0e43e92e0 Mon Sep 17 00:00:00 2001 From: barrylb Date: Thu, 14 Aug 2014 22:16:54 +1000 Subject: [PATCH 2/4] fix indent --- src/Response.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Response.php b/src/Response.php index 412ef5b2..e3b414e4 100644 --- a/src/Response.php +++ b/src/Response.php @@ -121,7 +121,7 @@ public function end($data = null, $keepAlive = false) $this->removeAllListeners(); if (!$keepAlive) { - $this->conn->end(); + $this->conn->end(); } } From 6e2087fd7cb4f5f7599c1abf23f55ac49c3a04da Mon Sep 17 00:00:00 2001 From: barrylb Date: Fri, 15 Aug 2014 00:05:16 +1000 Subject: [PATCH 3/4] fix indent --- src/Server.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Server.php b/src/Server.php index 0f6383b1..12fa898f 100644 --- a/src/Server.php +++ b/src/Server.php @@ -57,8 +57,8 @@ public function handleRequest(ConnectionInterface $conn, RequestHeaderParser $pa } $response->on('end', function () use ($conn, $parser) { - $conn->removeAllListeners(); // stop data being sent to this Request instance - $conn->on('data', array($parser, 'feed')); // resume sending data to RequestHeaderParser + $conn->removeAllListeners(); // stop data being sent to this Request instance + $conn->on('data', array($parser, 'feed')); // resume sending data to RequestHeaderParser }); $this->emit('request', array($request, $response)); From 09cfa234320d28e74963419df349819d585deadd Mon Sep 17 00:00:00 2001 From: barrylb Date: Wed, 24 Sep 2014 01:35:26 +1000 Subject: [PATCH 4/4] Removal of listeners from RequestHeaderParser When connection ends or if we do not want to keepAlive --- src/Response.php | 2 +- src/Server.php | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Response.php b/src/Response.php index e3b414e4..1d954952 100644 --- a/src/Response.php +++ b/src/Response.php @@ -117,7 +117,7 @@ public function end($data = null, $keepAlive = false) $this->conn->write("0\r\n\r\n"); } - $this->emit('end'); + $this->emit('end', array($keepAlive)); $this->removeAllListeners(); if (!$keepAlive) { diff --git a/src/Server.php b/src/Server.php index 12fa898f..77a51bfd 100644 --- a/src/Server.php +++ b/src/Server.php @@ -42,6 +42,9 @@ public function __construct(SocketServerInterface $io) }); $conn->on('data', array($parser, 'feed')); + $conn->on('end', function () use ($parser) { + $parser->removeAllListeners(); + }); }); } @@ -56,9 +59,17 @@ public function handleRequest(ConnectionInterface $conn, RequestHeaderParser $pa return; } - $response->on('end', function () use ($conn, $parser) { + $response->on('end', function ($keepAlive) use ($conn, $parser) { $conn->removeAllListeners(); // stop data being sent to this Request instance - $conn->on('data', array($parser, 'feed')); // resume sending data to RequestHeaderParser + if ($keepAlive) { + $conn->on('data', array($parser, 'feed')); // resume sending data to RequestHeaderParser + $conn->on('end', function () use ($parser) { + $parser->removeAllListeners(); + }); + } + else { + $parser->removeAllListeners(); + } }); $this->emit('request', array($request, $response));