From 72b1a44e382f8cd263e388e6be71fbdbd82acc7f Mon Sep 17 00:00:00 2001 From: Ben Connito Date: Thu, 7 Aug 2014 11:56:13 -0400 Subject: [PATCH 1/7] add port property to App allow origins in flash policy server dont start a flash policy server if one is already running better CURIE support on CALL URI should be un prefixed WampConnection callResult should allow an object to be encoded and sent --- .gitignore | 12 ++++++----- src/Ratchet/App.php | 32 +++++++++++++++++++++++++---- src/Ratchet/Wamp/ServerProtocol.php | 3 ++- src/Ratchet/Wamp/WampConnection.php | 18 +++++++++++++--- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 2e580df..32db066 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ -phpunit.xml -reports -sandbox -vendor -composer.lock +phpunit.xml +reports +sandbox +vendor +composer.lock +/nbproject/private/ +/nbproject/ \ No newline at end of file diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index c6d9ceb..f7d5e52 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -42,6 +42,12 @@ class App { * @var string */ protected $httpHost; + + /*** + * The port the socket is listening + * @var int + */ + protected $port; /** * @var int @@ -68,6 +74,7 @@ class App { } $this->httpHost = $httpHost; + $this->port = $port; $socket = new Reactor($loop); $socket->listen($port, $address); @@ -81,10 +88,20 @@ class App { $flashSock = new Reactor($loop); $this->flashServer = new IoServer($policy, $flashSock); - if (80 == $port) { - $flashSock->listen(843, '0.0.0.0'); - } else { - $flashSock->listen(8843); + //check if another App is already running a flash policy server on 843 + $test = @fsockopen('127.0.0.1', 843, $errno, $errstr, 5); + + //if not start a flash policy serever + if(is_resource($test) === false){ + $policy = new FlashPolicy; + $policy->addAllowedAccess($httpHost, 80); + $policy->addAllowedAccess($httpHost, $port); + $flashSock = new Reactor($loop); + $this->flashServer = new IoServer($policy, $flashSock); + + $flashSock->listen(843, '0.0.0.0'); + }else{ + fclose($test); } } @@ -118,6 +135,13 @@ class App { if ('*' !== $allowedOrigins[0]) { $decorated = new OriginCheck($decorated, $allowedOrigins); } + + //allow origins in flash policy server + if(empty($this->flashServer) === false){ + foreach($allowedOrigins as $allowedOrgin){ + $this->flashServer->app->addAllowedAccess($allowedOrgin, $this->port); + } + } $this->routes->add('rr-' . ++$this->_routeCounter, new Route($path, array('_controller' => $decorated), array('Origin' => $this->httpHost), array(), $httpHost)); diff --git a/src/Ratchet/Wamp/ServerProtocol.php b/src/Ratchet/Wamp/ServerProtocol.php index 92dbd85..0bddc83 100644 --- a/src/Ratchet/Wamp/ServerProtocol.php +++ b/src/Ratchet/Wamp/ServerProtocol.php @@ -107,7 +107,8 @@ class ServerProtocol implements MessageComponentInterface, WsServerInterface { $json = $json[0]; } - $this->_decorating->onCall($from, $callID, $procURI, $json); + //procURI should be un prefixed + $this->_decorating->onCall($from, $callID, $from->getUri($procURI), $json); break; case static::MSG_SUBSCRIBE: diff --git a/src/Ratchet/Wamp/WampConnection.php b/src/Ratchet/Wamp/WampConnection.php index 95e1969..6e46eba 100644 --- a/src/Ratchet/Wamp/WampConnection.php +++ b/src/Ratchet/Wamp/WampConnection.php @@ -26,10 +26,10 @@ class WampConnection extends AbstractConnectionDecorator { /** * Successfully respond to a call made by the client * @param string $id The unique ID given by the client to respond to - * @param array $data An array of data to return to the client + * @param array $data an object or array * @return WampConnection */ - public function callResult($id, array $data = array()) { + public function callResult($id, $data = array()) { return $this->send(json_encode(array(WAMP::MSG_CALL_RESULT, $id, $data))); } @@ -77,11 +77,23 @@ class WampConnection extends AbstractConnectionDecorator { /** * Get the full request URI from the connection object if a prefix has been established for it + * Compliant with WAMP Spec for curie URIs * @param string $uri * @return string */ public function getUri($uri) { - return (array_key_exists($uri, $this->WAMP->prefixes) ? $this->WAMP->prefixes[$uri] : $uri); + $seperator = ':'; + + if(preg_match('/http(s*)\:\/\//', $uri) === false){ + if(strpos($uri, $seperator) !== false){ + list($prefix, $action) = explode(':', $uri); + $expandedPrefix = isset($this->WAMP->prefixes[$prefix]) ? $this->WAMP->prefixes[$prefix] : $prefix; + + return $expandedPrefix . '#' . $action; + } + } + + return $uri; } /** From 29e22a0a2e5d6a73990e4e5393e264dc2856d0b7 Mon Sep 17 00:00:00 2001 From: Ben Connito Date: Thu, 7 Aug 2014 12:34:00 -0400 Subject: [PATCH 2/7] testPrefix passing --- src/Ratchet/Wamp/WampConnection.php | 11 ++++++----- tests/unit/Wamp/ServerProtocolTest.php | 11 ++++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/Ratchet/Wamp/WampConnection.php b/src/Ratchet/Wamp/WampConnection.php index 6e46eba..a0bf523 100644 --- a/src/Ratchet/Wamp/WampConnection.php +++ b/src/Ratchet/Wamp/WampConnection.php @@ -82,14 +82,15 @@ class WampConnection extends AbstractConnectionDecorator { * @return string */ public function getUri($uri) { - $seperator = ':'; + $curieSeperator = ':'; + $fullSeperator = '#'; - if(preg_match('/http(s*)\:\/\//', $uri) === false){ - if(strpos($uri, $seperator) !== false){ - list($prefix, $action) = explode(':', $uri); + if(preg_match('/http(s*)\:\/\//', $uri) == false){ + if(strpos($uri, $curieSeperator) !== false){ + list($prefix, $action) = explode($curieSeperator, $uri); $expandedPrefix = isset($this->WAMP->prefixes[$prefix]) ? $this->WAMP->prefixes[$prefix] : $prefix; - return $expandedPrefix . '#' . $action; + return $expandedPrefix . $fullSeperator . $action; } } diff --git a/tests/unit/Wamp/ServerProtocolTest.php b/tests/unit/Wamp/ServerProtocolTest.php index 1b423d2..082a3f5 100644 --- a/tests/unit/Wamp/ServerProtocolTest.php +++ b/tests/unit/Wamp/ServerProtocolTest.php @@ -211,13 +211,14 @@ class ServerProtocolTest extends \PHPUnit_Framework_TestCase { $conn = new WampConnection($this->newConn()); $this->_comp->onOpen($conn); - $shortIn = 'incoming'; - $longIn = 'http://example.com/incoming/'; + $prefix = 'incoming'; + $fullURI = "http://example.com/$prefix"; + $method = 'call'; - $this->_comp->onMessage($conn, json_encode(array(1, $shortIn, $longIn))); + $this->_comp->onMessage($conn, json_encode(array(1, $prefix, $fullURI))); - $this->assertEquals($longIn, $conn->WAMP->prefixes[$shortIn]); - $this->assertEquals($longIn, $conn->getUri($shortIn)); + $this->assertEquals($fullURI, $conn->WAMP->prefixes[$prefix]); + $this->assertEquals("$fullURI#$method", $conn->getUri("$prefix:$method")); } public function testMessageMustBeJson() { From 705423e0b3beabf9628206ffb0c1c828fcdb11ef Mon Sep 17 00:00:00 2001 From: Ben Connito Date: Thu, 7 Aug 2014 13:35:01 -0400 Subject: [PATCH 3/7] more entropy on session id all tests passing --- src/Ratchet/Wamp/WampConnection.php | 2 +- tests/unit/Wamp/TopicTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Ratchet/Wamp/WampConnection.php b/src/Ratchet/Wamp/WampConnection.php index a0bf523..a12b162 100644 --- a/src/Ratchet/Wamp/WampConnection.php +++ b/src/Ratchet/Wamp/WampConnection.php @@ -17,7 +17,7 @@ class WampConnection extends AbstractConnectionDecorator { parent::__construct($conn); $this->WAMP = new \StdClass; - $this->WAMP->sessionId = uniqid(); + $this->WAMP->sessionId = md5(uniqid(mt_rand(), true)); $this->WAMP->prefixes = array(); $this->send(json_encode(array(WAMP::MSG_WELCOME, $this->WAMP->sessionId, 1, \Ratchet\VERSION))); diff --git a/tests/unit/Wamp/TopicTest.php b/tests/unit/Wamp/TopicTest.php index b8685b7..26db0d3 100644 --- a/tests/unit/Wamp/TopicTest.php +++ b/tests/unit/Wamp/TopicTest.php @@ -81,7 +81,7 @@ class TopicTest extends \PHPUnit_Framework_TestCase { $topic->add($first); $topic->add($second); $topic->add($third); - + $topic->broadcast($msg, array($second->WAMP->sessionId)); } From 417017b66735c43b96710cddf22f2e7ce7ce526a Mon Sep 17 00:00:00 2001 From: Ben Connito Date: Fri, 12 Sep 2014 11:46:33 -0400 Subject: [PATCH 4/7] remove local project folder from gitignore in App::__construct use echo instead of trigger_error --- .gitignore | 4 +--- src/Ratchet/App.php | 3 ++- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 32db066..793ef58 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,4 @@ phpunit.xml reports sandbox vendor -composer.lock -/nbproject/private/ -/nbproject/ \ No newline at end of file +composer.lock \ No newline at end of file diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index f7d5e52..484129a 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -62,7 +62,7 @@ class App { */ public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null) { if (extension_loaded('xdebug')) { - trigger_error("XDebug extension detected. Remember to disable this if performance testing or going live!", E_USER_WARNING); + echo("XDebug extension detected. Remember to disable this if performance testing or going live!" . PHP_EOL); } if (3 !== strlen('✓')) { @@ -102,6 +102,7 @@ class App { $flashSock->listen(843, '0.0.0.0'); }else{ fclose($test); + echo('flash socket server already running on 843' . PHP_EOL); } } From a6e7952671b5287f2478309f839b37656b8aff9c Mon Sep 17 00:00:00 2001 From: Ben Connito Date: Sun, 14 Sep 2014 11:59:46 -0400 Subject: [PATCH 5/7] changed echos back to trigger_error --- src/Ratchet/App.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index 484129a..4755560 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -62,7 +62,7 @@ class App { */ public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null) { if (extension_loaded('xdebug')) { - echo("XDebug extension detected. Remember to disable this if performance testing or going live!" . PHP_EOL); + trigger_error('XDebug extension detected. Remember to disable this if performance testing or going live!', E_USER_WARNING); } if (3 !== strlen('✓')) { @@ -102,7 +102,7 @@ class App { $flashSock->listen(843, '0.0.0.0'); }else{ fclose($test); - echo('flash socket server already running on 843' . PHP_EOL); + trigger_error('flash socket server already running on 843', E_USER_NOTICE); } } From f5d148cdc426e7209ee2bbd44fa4a9b9df54a81c Mon Sep 17 00:00:00 2001 From: Ben Connito Date: Mon, 15 Sep 2014 17:01:24 -0400 Subject: [PATCH 6/7] just strip the '.' from uniqid with added entropy. this way we dont run into any md5 collisions. --- src/Ratchet/Wamp/WampConnection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ratchet/Wamp/WampConnection.php b/src/Ratchet/Wamp/WampConnection.php index a12b162..8364efd 100644 --- a/src/Ratchet/Wamp/WampConnection.php +++ b/src/Ratchet/Wamp/WampConnection.php @@ -17,7 +17,7 @@ class WampConnection extends AbstractConnectionDecorator { parent::__construct($conn); $this->WAMP = new \StdClass; - $this->WAMP->sessionId = md5(uniqid(mt_rand(), true)); + $this->WAMP->sessionId = str_replace('.','',uniqid(mt_rand(), true)); $this->WAMP->prefixes = array(); $this->send(json_encode(array(WAMP::MSG_WELCOME, $this->WAMP->sessionId, 1, \Ratchet\VERSION))); From c179d60e77a9f370d278285c95eb34464687c862 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sun, 23 Nov 2014 11:40:06 -0500 Subject: [PATCH 7/7] Formatting, removed flash check --- src/Ratchet/App.php | 34 ++++++++++------------------- src/Ratchet/Wamp/ServerProtocol.php | 1 - src/Ratchet/Wamp/WampConnection.php | 25 ++++++++++----------- tests/unit/Wamp/TopicTest.php | 2 +- 4 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index 4755560..4e44663 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -42,12 +42,12 @@ class App { * @var string */ protected $httpHost; - + /*** * The port the socket is listening * @var int */ - protected $port; + protected $port; /** * @var int @@ -87,22 +87,10 @@ class App { $policy->addAllowedAccess($httpHost, $port); $flashSock = new Reactor($loop); $this->flashServer = new IoServer($policy, $flashSock); - - //check if another App is already running a flash policy server on 843 - $test = @fsockopen('127.0.0.1', 843, $errno, $errstr, 5); - - //if not start a flash policy serever - if(is_resource($test) === false){ - $policy = new FlashPolicy; - $policy->addAllowedAccess($httpHost, 80); - $policy->addAllowedAccess($httpHost, $port); - $flashSock = new Reactor($loop); - $this->flashServer = new IoServer($policy, $flashSock); - - $flashSock->listen(843, '0.0.0.0'); - }else{ - fclose($test); - trigger_error('flash socket server already running on 843', E_USER_NOTICE); + if (80 == $port) { + $flashSock->listen(843, '0.0.0.0'); + } else { + $flashSock->listen(8843); } } @@ -136,12 +124,12 @@ class App { if ('*' !== $allowedOrigins[0]) { $decorated = new OriginCheck($decorated, $allowedOrigins); } - + //allow origins in flash policy server - if(empty($this->flashServer) === false){ - foreach($allowedOrigins as $allowedOrgin){ - $this->flashServer->app->addAllowedAccess($allowedOrgin, $this->port); - } + if(empty($this->flashServer) === false) { + foreach($allowedOrigins as $allowedOrgin) { + $this->flashServer->app->addAllowedAccess($allowedOrgin, $this->port); + } } $this->routes->add('rr-' . ++$this->_routeCounter, new Route($path, array('_controller' => $decorated), array('Origin' => $this->httpHost), array(), $httpHost)); diff --git a/src/Ratchet/Wamp/ServerProtocol.php b/src/Ratchet/Wamp/ServerProtocol.php index 0bddc83..28badd3 100644 --- a/src/Ratchet/Wamp/ServerProtocol.php +++ b/src/Ratchet/Wamp/ServerProtocol.php @@ -107,7 +107,6 @@ class ServerProtocol implements MessageComponentInterface, WsServerInterface { $json = $json[0]; } - //procURI should be un prefixed $this->_decorating->onCall($from, $callID, $from->getUri($procURI), $json); break; diff --git a/src/Ratchet/Wamp/WampConnection.php b/src/Ratchet/Wamp/WampConnection.php index 8364efd..64590a0 100644 --- a/src/Ratchet/Wamp/WampConnection.php +++ b/src/Ratchet/Wamp/WampConnection.php @@ -17,7 +17,7 @@ class WampConnection extends AbstractConnectionDecorator { parent::__construct($conn); $this->WAMP = new \StdClass; - $this->WAMP->sessionId = str_replace('.','',uniqid(mt_rand(), true)); + $this->WAMP->sessionId = str_replace('.', '', uniqid(mt_rand(), true)); $this->WAMP->prefixes = array(); $this->send(json_encode(array(WAMP::MSG_WELCOME, $this->WAMP->sessionId, 1, \Ratchet\VERSION))); @@ -77,24 +77,23 @@ class WampConnection extends AbstractConnectionDecorator { /** * Get the full request URI from the connection object if a prefix has been established for it - * Compliant with WAMP Spec for curie URIs * @param string $uri * @return string */ public function getUri($uri) { - $curieSeperator = ':'; - $fullSeperator = '#'; - - if(preg_match('/http(s*)\:\/\//', $uri) == false){ - if(strpos($uri, $curieSeperator) !== false){ - list($prefix, $action) = explode($curieSeperator, $uri); - $expandedPrefix = isset($this->WAMP->prefixes[$prefix]) ? $this->WAMP->prefixes[$prefix] : $prefix; + $curieSeperator = ':'; + $fullSeperator = '#'; - return $expandedPrefix . $fullSeperator . $action; + if (preg_match('/http(s*)\:\/\//', $uri) == false) { + if (strpos($uri, $curieSeperator) !== false) { + list($prefix, $action) = explode($curieSeperator, $uri); + $expandedPrefix = isset($this->WAMP->prefixes[$prefix]) ? $this->WAMP->prefixes[$prefix] : $prefix; + + return $expandedPrefix . $fullSeperator . $action; + } } - } - - return $uri; + + return $uri; } /** diff --git a/tests/unit/Wamp/TopicTest.php b/tests/unit/Wamp/TopicTest.php index 26db0d3..b8685b7 100644 --- a/tests/unit/Wamp/TopicTest.php +++ b/tests/unit/Wamp/TopicTest.php @@ -81,7 +81,7 @@ class TopicTest extends \PHPUnit_Framework_TestCase { $topic->add($first); $topic->add($second); $topic->add($third); - + $topic->broadcast($msg, array($second->WAMP->sessionId)); }