From 9b38d863b8dbac73298a9e495677f71598d600c7 Mon Sep 17 00:00:00 2001 From: Gerrit Drost Date: Mon, 14 Apr 2014 17:24:36 +0200 Subject: [PATCH 1/9] Fix app for flash usage --- src/Ratchet/App.php | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index da634af..7cb4363 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -49,12 +49,16 @@ class App { protected $_routeCounter = 0; /** - * @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');` - * @param int $port Port to listen on. If 80, assuming production, Flash on 843 otherwise expecting Flash to be proxied through 8843 - * @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine. + * @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');` + * @param int $port Port to listen on. If 80, assuming production, Flash on 843 otherwise expecting Flash to be proxied through 8843 + * @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine. + * @param array $flashAllowedHosts associative array with ports as key and hostnames as value. These domains are the domains the fhlash websocket fallback may connect from + * @param int $flashPort the port the flash cross-domain-policy file will be hosted on + * @param string $flashHost the host the flash cross-domain-policy file will be hosted on * @param LoopInterface $loop Specific React\EventLoop to bind the application to. null will create one for you. */ - public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', LoopInterface $loop = null) { + public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', $flashAllowedHosts = false, $flashPort = 8843, $flashHost = '0.0.0.0', 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); } @@ -75,17 +79,20 @@ class App { $this->routes = new RouteCollection; $this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher($this->routes, new RequestContext))), $socket, $loop); - $policy = new FlashPolicy; - $policy->addAllowedAccess($httpHost, 80); - $policy->addAllowedAccess($httpHost, $port); - $flashSock = new Reactor($loop); - $this->flashServer = new IoServer($policy, $flashSock); - - if (80 == $port) { - $flashSock->listen(843, '0.0.0.0'); - } else { - $flashSock->listen(8843); + if ($flashAllowedHosts === false) { + $flashAllowedHosts = [ 80 => $httpHost ]; } + + $policy = new FlashPolicy(); + + foreach ($flashAllowedHosts as $flashAllowedPort => $flashAllowedHost) { + $policy->addAllowedAccess($flashAllowedHost, $flashAllowedPort); + } + + $flashSock = new Reactor($loop); + + $this->flashServer = new IoServer($policy, $flashSock); + $flashSock->listen($flashPort, $flashHost); } /** From 97e01225700147acfbd202a693d7a26216fb8be9 Mon Sep 17 00:00:00 2001 From: Gerrit Drost Date: Tue, 15 Apr 2014 09:29:17 +0200 Subject: [PATCH 2/9] Improved comments and changed some parameter definitions/names. --- src/Ratchet/App.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index 7cb4363..1d87915 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -49,15 +49,15 @@ class App { protected $_routeCounter = 0; /** - * @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost');` - * @param int $port Port to listen on. If 80, assuming production, Flash on 843 otherwise expecting Flash to be proxied through 8843 - * @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine. - * @param array $flashAllowedHosts associative array with ports as key and hostnames as value. These domains are the domains the fhlash websocket fallback may connect from + * @param string $httpHost HTTP hostname clients intend to connect to. MUST match JS `new WebSocket('ws://$httpHost') + * @param int $port Port to listen on + * @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine + * @param array $flashAllowedHosts associative array with hostnames as key and ports as value. These domains are the domains the flash websocket fallback may connect from * @param int $flashPort the port the flash cross-domain-policy file will be hosted on - * @param string $flashHost the host the flash cross-domain-policy file will be hosted on + * @param string $flashAddress the IP address the flash cross-domain-policy server will bind to * @param LoopInterface $loop Specific React\EventLoop to bind the application to. null will create one for you. */ - public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', $flashAllowedHosts = false, $flashPort = 8843, $flashHost = '0.0.0.0', LoopInterface $loop = null) { + public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', $flashAllowedHosts = false, $flashPort = 8843, $flashAddress = '0.0.0.0', 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); @@ -85,14 +85,14 @@ class App { $policy = new FlashPolicy(); - foreach ($flashAllowedHosts as $flashAllowedPort => $flashAllowedHost) { + foreach ($flashAllowedHosts as $flashAllowedHost => $flashAllowedPort) { $policy->addAllowedAccess($flashAllowedHost, $flashAllowedPort); } $flashSock = new Reactor($loop); $this->flashServer = new IoServer($policy, $flashSock); - $flashSock->listen($flashPort, $flashHost); + $flashSock->listen($flashPort, $flashAddress); } /** From ae5e383c02e4600f345a450552a34f0dc672e768 Mon Sep 17 00:00:00 2001 From: Gerrit Drost Date: Mon, 19 May 2014 09:28:43 +0200 Subject: [PATCH 3/9] small change for PHP 5.3 compatibility --- src/Ratchet/App.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index 1d87915..d9baff0 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -80,7 +80,7 @@ class App { $this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher($this->routes, new RequestContext))), $socket, $loop); if ($flashAllowedHosts === false) { - $flashAllowedHosts = [ 80 => $httpHost ]; + $flashAllowedHosts = array( 80 => $httpHost ); } $policy = new FlashPolicy(); From 06c4c3ddd8b8c54c56739af1b36e14d2ee8192a8 Mon Sep 17 00:00:00 2001 From: Gerrit Drost Date: Mon, 19 May 2014 09:36:28 +0200 Subject: [PATCH 4/9] PHPDoc formatting fix --- 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 d9baff0..229a448 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -54,8 +54,8 @@ class App { * @param string $address IP address to bind to. Default is localhost/proxy only. '0.0.0.0' for any machine * @param array $flashAllowedHosts associative array with hostnames as key and ports as value. These domains are the domains the flash websocket fallback may connect from * @param int $flashPort the port the flash cross-domain-policy file will be hosted on - * @param string $flashAddress the IP address the flash cross-domain-policy server will bind to - * @param LoopInterface $loop Specific React\EventLoop to bind the application to. null will create one for you. + * @param string $flashAddress the IP address the flash cross-domain-policy server will bind to + * @param LoopInterface $loop Specific React\EventLoop to bind the application to. null will create one for you. */ public function __construct($httpHost = 'localhost', $port = 8080, $address = '127.0.0.1', $flashAllowedHosts = false, $flashPort = 8843, $flashAddress = '0.0.0.0', LoopInterface $loop = null) { From a456c50df437616648d45a73c711d78804ded21d Mon Sep 17 00:00:00 2001 From: Gerrit Drost Date: Fri, 23 May 2014 16:20:27 +0200 Subject: [PATCH 5/9] Reverted the changes previously made in favor of a different approach as requested in https://github.com/cboden/Ratchet/pull/188 --- src/Ratchet/App.php | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index 229a448..5cafeae 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -1,5 +1,7 @@ routes = new RouteCollection; $this->_server = new IoServer(new HttpServer(new Router(new UrlMatcher($this->routes, new RequestContext))), $socket, $loop); - if ($flashAllowedHosts === false) { - $flashAllowedHosts = array( 80 => $httpHost ); - } - $policy = new FlashPolicy(); - - foreach ($flashAllowedHosts as $flashAllowedHost => $flashAllowedPort) { - $policy->addAllowedAccess($flashAllowedHost, $flashAllowedPort); - } - + $policy->addAllowedAccess($httpHost, 80); + $policy->addAllowedAccess($httpHost, $port); $flashSock = new Reactor($loop); - $this->flashServer = new IoServer($policy, $flashSock); - $flashSock->listen($flashPort, $flashAddress); + + if (80 == $port) { + $flashSock->listen(843, '0.0.0.0'); + } else { + $flashSock->listen(8843); + } + } + + /** + * Returns the FlashPolicy running in the FlashServer. Modifications of this object take effect immediately! + * + * @return FlashPolicy + */ + public function getFlashPolicy() { + return $this->flashServer->getApp(); } /** @@ -136,3 +140,4 @@ class App { $this->_server->run(); } } + From a1c27ac91b7a07326785acf4ad0bfeb03d0488ef Mon Sep 17 00:00:00 2001 From: Gerrit Drost Date: Fri, 23 May 2014 16:21:13 +0200 Subject: [PATCH 6/9] Added the property socket to the IOServer class and exposed the app and socket properties through getters. --- src/Ratchet/Server/IoServer.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Ratchet/Server/IoServer.php b/src/Ratchet/Server/IoServer.php index b27ee93..587dcd5 100644 --- a/src/Ratchet/Server/IoServer.php +++ b/src/Ratchet/Server/IoServer.php @@ -1,5 +1,7 @@ loop = $loop; $this->app = $app; + $this->socket = $socket; $socket->on('connection', array($this, 'handleConnect')); @@ -50,6 +59,24 @@ class IoServer { $this->handlers[1] = array($this, 'handleEnd'); $this->handlers[2] = array($this, 'handleError'); } + + /** + * Returns the Ratchet App + * + * @return \Ratchet\MessageComponentInterface + */ + public function getApp() { + return $this->app; + } + + /** + * Returns the Socket + * + * @return \React\Socket\ServerInterface + */ + public function getSocket() { + return $this->socket; + } /** * @param \Ratchet\MessageComponentInterface $component The application that I/O will call when events are received From 5ad295e02ad35625e4ee3eea904f794ea7140de1 Mon Sep 17 00:00:00 2001 From: Gerrit Drost Date: Fri, 23 May 2014 16:22:00 +0200 Subject: [PATCH 7/9] Added a method to clear the allowedAccess array. --- src/Ratchet/Server/FlashPolicy.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Ratchet/Server/FlashPolicy.php b/src/Ratchet/Server/FlashPolicy.php index 4997362..4a1b8bd 100644 --- a/src/Ratchet/Server/FlashPolicy.php +++ b/src/Ratchet/Server/FlashPolicy.php @@ -71,6 +71,18 @@ class FlashPolicy implements MessageComponentInterface { return $this; } + + /** + * Removes all domains from the allowed access list. + * + * @return \Ratchet\Server\FlashPolicy + */ + public function clearAllowedAccess() { + $this->_access = array(); + $this->_cacheValid = false; + + return $this; + } /** * site-control defines the meta-policy for the current domain. A meta-policy specifies acceptable From 478bdc10c52023dfddfb30c2512a047a731a072f Mon Sep 17 00:00:00 2001 From: Gerrit Drost Date: Fri, 23 May 2014 16:31:08 +0200 Subject: [PATCH 8/9] Added a method to expose the flashsocket --- src/Ratchet/App.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index 03bf1bb..e59c074 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -91,13 +91,22 @@ class App { } /** - * Returns the FlashPolicy running in the FlashServer. Modifications of this object take effect immediately! + * Returns the FlashPolicy running in the flash server. Modifications of this object take effect immediately! * * @return FlashPolicy */ public function getFlashPolicy() { return $this->flashServer->getApp(); } + + /** + * Returns the FlashSocket of the flash server. + * + * @return \React\Socket\ServerInterface + */ + public function getFlashSocket() { + return $this->flashServer->getSocket(); + } /** * Add an endpiont/application to the server From 77c6d53a4dad77d082cbd13a4d405403f7e54946 Mon Sep 17 00:00:00 2001 From: Gerrit Drost Date: Wed, 4 Jun 2014 16:59:30 +0200 Subject: [PATCH 9/9] Made changes as suggested in pull request. Getters have been removed from IoServer and desired properties have been made public. --- src/Ratchet/App.php | 4 ++-- src/Ratchet/Server/IoServer.php | 20 +------------------- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/src/Ratchet/App.php b/src/Ratchet/App.php index e59c074..cc724fc 100644 --- a/src/Ratchet/App.php +++ b/src/Ratchet/App.php @@ -96,7 +96,7 @@ class App { * @return FlashPolicy */ public function getFlashPolicy() { - return $this->flashServer->getApp(); + return $this->flashServer->app; } /** @@ -105,7 +105,7 @@ class App { * @return \React\Socket\ServerInterface */ public function getFlashSocket() { - return $this->flashServer->getSocket(); + return $this->flashServer->socket; } /** diff --git a/src/Ratchet/Server/IoServer.php b/src/Ratchet/Server/IoServer.php index 587dcd5..160a1fc 100644 --- a/src/Ratchet/Server/IoServer.php +++ b/src/Ratchet/Server/IoServer.php @@ -33,7 +33,7 @@ class IoServer { * The socket server the Ratchet Application is run off of * @var \React\Socket\ServerInterface */ - protected $socket; + public $socket; /** * @param \Ratchet\MessageComponentInterface $app The Ratchet application stack to host @@ -59,24 +59,6 @@ class IoServer { $this->handlers[1] = array($this, 'handleEnd'); $this->handlers[2] = array($this, 'handleError'); } - - /** - * Returns the Ratchet App - * - * @return \Ratchet\MessageComponentInterface - */ - public function getApp() { - return $this->app; - } - - /** - * Returns the Socket - * - * @return \React\Socket\ServerInterface - */ - public function getSocket() { - return $this->socket; - } /** * @param \Ratchet\MessageComponentInterface $component The application that I/O will call when events are received