From a83a4e460a3bf46334b4cf3b8e30d324448a6104 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Mon, 9 Oct 2017 13:18:18 -0400 Subject: [PATCH 1/3] Create a noop controller for impatient clients --- src/Ratchet/Http/NoOpHttpServerController.php | 18 ++++++++++++++++++ src/Ratchet/Http/Router.php | 11 ++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 src/Ratchet/Http/NoOpHttpServerController.php diff --git a/src/Ratchet/Http/NoOpHttpServerController.php b/src/Ratchet/Http/NoOpHttpServerController.php new file mode 100644 index 0000000..4f72e66 --- /dev/null +++ b/src/Ratchet/Http/NoOpHttpServerController.php @@ -0,0 +1,18 @@ +_matcher = $matcher; + $this->_noopController = new NoOpHttpServerController; } /** @@ -28,6 +31,8 @@ class Router implements HttpServerInterface { throw new \UnexpectedValueException('$request can not be null'); } + $conn->controller = $this->_noopController; + $uri = $request->getUri(); $context = $this->_matcher->getContext(); @@ -67,14 +72,14 @@ class Router implements HttpServerInterface { /** * {@inheritdoc} */ - function onMessage(ConnectionInterface $from, $msg) { + public function onMessage(ConnectionInterface $from, $msg) { $from->controller->onMessage($from, $msg); } /** * {@inheritdoc} */ - function onClose(ConnectionInterface $conn) { + public function onClose(ConnectionInterface $conn) { if (isset($conn->controller)) { $conn->controller->onClose($conn); } @@ -83,7 +88,7 @@ class Router implements HttpServerInterface { /** * {@inheritdoc} */ - function onError(ConnectionInterface $conn, \Exception $e) { + public function onError(ConnectionInterface $conn, \Exception $e) { if (isset($conn->controller)) { $conn->controller->onError($conn, $e); } From 444d4b5b55d9cf34ad05574c766970963a11bdd1 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sat, 14 Oct 2017 23:03:09 -0400 Subject: [PATCH 2/3] Test impatient client sending body --- tests/unit/Http/RouterTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/unit/Http/RouterTest.php b/tests/unit/Http/RouterTest.php index e924aa1..e6ecb7c 100644 --- a/tests/unit/Http/RouterTest.php +++ b/tests/unit/Http/RouterTest.php @@ -3,6 +3,10 @@ namespace Ratchet\Http; use Ratchet\WebSocket\WsServerInterface; use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Symfony\Component\Routing\Matcher\UrlMatcherInterface; +use Symfony\Component\Routing\RequestContext; +use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\Matcher\UrlMatcher; + /** * @covers Ratchet\Http\Router @@ -142,4 +146,18 @@ class RouterTest extends \PHPUnit_Framework_TestCase { $this->assertEquals('ws', $request->getUri()->getScheme()); $this->assertEquals('doesnt.matter', $request->getUri()->getHost()); } + + public function testImpatientClientOverflow() { + $header = "GET /nope HTTP/1.1 +Upgrade: websocket +Connection: upgrade +Host: localhost +Origin: http://localhost +Sec-WebSocket-Version: 13\r\n\r\n"; + + $app = new HttpServer(new Router(new UrlMatcher(new RouteCollection, new RequestContext))); + $app->onOpen($this->_conn); + $app->onMessage($this->_conn, $header); + $app->onMessage($this->_conn, 'Silly body'); + } } From 1aafdac3ff742434df141a21c986f1cb3d397c7b Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sat, 14 Oct 2017 23:12:27 -0400 Subject: [PATCH 3/3] Let's add an assertion to the test for fun :) --- tests/unit/Http/RouterTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit/Http/RouterTest.php b/tests/unit/Http/RouterTest.php index e6ecb7c..1ca4cbc 100644 --- a/tests/unit/Http/RouterTest.php +++ b/tests/unit/Http/RouterTest.php @@ -148,6 +148,8 @@ class RouterTest extends \PHPUnit_Framework_TestCase { } public function testImpatientClientOverflow() { + $this->_conn->expects($this->once())->method('close'); + $header = "GET /nope HTTP/1.1 Upgrade: websocket Connection: upgrade