From 35e83702ccc561b92cb3b3fbf73bbc7e1b35b931 Mon Sep 17 00:00:00 2001 From: Nepomuk Fraedrich Date: Thu, 28 Jan 2016 17:42:03 +0100 Subject: [PATCH 1/6] Added more error handling --- src/WampPost/WampPost.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/WampPost/WampPost.php b/src/WampPost/WampPost.php index 064753b..69161fa 100644 --- a/src/WampPost/WampPost.php +++ b/src/WampPost/WampPost.php @@ -63,7 +63,9 @@ public function handleRequest($request, $response) { return; } - if (isset($json->topic) && isset($json->args) + if ( + isset($json->topic) + && isset($json->args) && Utils::uriIsValid($json->topic) && is_array($json->args) && ($this->getPublisher() !== null) @@ -71,6 +73,30 @@ public function handleRequest($request, $response) { $argsKw = isset($json->argsKw) && is_object($json->argsKw) ? $json->argsKw : null; $options = isset($json->options) && is_object($json->opitons) ? $json->options : null; $this->getSession()->publish($json->topic, $json->args, $argsKw, $options); + } else { + + $errors = []; + if (!isset($json->topic)) { + $errors[] = 'Topic not set'; + } + if (!isset($json->args)) { + $errors[] = 'Args not set'; + } + if (!is_array($json->args)) { + $errors[] = 'Args is not an array, got ' . gettype($json->args); + } + if (!Utils::uriIsValid($json->topic)) { + $errors[] = 'Topic is not a valid URI'; + } + if (!($this->getPublisher() !== null)) { + $errors[] = 'Publisher is not set'; + } + + $response->writeHead(400, ['Content-Type' => 'text/plain', 'Connection' => 'close']); + $response->end( + "The following errors occurred:" . PHP_EOL . PHP_EOL . implode(PHP_EOL, $errors) + ); + return; } } catch (\Exception $e) { // should shut down everything From 596bbca0b5267095a216c3cd5e93e450cc960869 Mon Sep 17 00:00:00 2001 From: Nepomuk Fraedrich Date: Fri, 29 Jan 2016 09:15:41 +0100 Subject: [PATCH 2/6] Rebase to version 0.1.2 --- src/WampPost/WampPost.php | 149 +++++++++++++++++++++----------------- 1 file changed, 83 insertions(+), 66 deletions(-) diff --git a/src/WampPost/WampPost.php b/src/WampPost/WampPost.php index 69161fa..3cd38f1 100644 --- a/src/WampPost/WampPost.php +++ b/src/WampPost/WampPost.php @@ -8,16 +8,19 @@ use React\Socket\Server; use Thruway\CallResult; use Thruway\Common\Utils; +use Thruway\Message\ErrorMessage; use Thruway\Peer\Client; class WampPost extends Client { private $bindAddress; private $port; private $realmName; + + /** @var Server */ private $socket; private $http; - function __construct($realmName, $loop = null, $bindAddress = '127.0.0.1', $port = 8181) + public function __construct($realmName, $loop = null, $bindAddress = '127.0.0.1', $port = 8181) { if ($loop === null) { $loop = Factory::create(); @@ -45,76 +48,86 @@ public function onSessionStart($session, $transport) { } + /** + * @inheritDoc + */ + public function onClose($reason) + { + $this->socket->shutdown(); + + parent::onClose($reason); + } + /** * @param Request $request * @param Response $response */ - public function handleRequest($request, $response) { + public function handleRequest(Request $request, Response $response) { if ($request->getPath() == '/pub' && $request->getMethod() == 'POST') { - $bodySnatcher = new BodySnatcher($request); - $bodySnatcher->promise()->then(function ($body) use ($request, $response) { - try { - //{"topic": "com.myapp.topic1", "args": ["Hello, world"]} - $json = json_decode($body); - - if ($json === null) { - $response->writeHead(400, ['Content-Type' => 'text/plain', 'Connection' => 'close']); - $response->end("JSON decoding failed: " . json_last_error_msg()); - return; - } + $this->handlePublishHttpPost($request, $response); + } else if ($request->getPath() == '/call' && $request->getMethod() == 'POST') { + $this->handleCallHttpRequest($request, $response); + } else { + $response->writeHead(404, ['Content-Type' => 'text/plain', 'Connection' => 'close']); + $response->end("Not found"); + } + } - if ( - isset($json->topic) - && isset($json->args) - && Utils::uriIsValid($json->topic) - && is_array($json->args) - && ($this->getPublisher() !== null) - ) { - $argsKw = isset($json->argsKw) && is_object($json->argsKw) ? $json->argsKw : null; - $options = isset($json->options) && is_object($json->opitons) ? $json->options : null; - $this->getSession()->publish($json->topic, $json->args, $argsKw, $options); - } else { - - $errors = []; - if (!isset($json->topic)) { - $errors[] = 'Topic not set'; - } - if (!isset($json->args)) { - $errors[] = 'Args not set'; - } - if (!is_array($json->args)) { - $errors[] = 'Args is not an array, got ' . gettype($json->args); - } - if (!Utils::uriIsValid($json->topic)) { - $errors[] = 'Topic is not a valid URI'; - } - if (!($this->getPublisher() !== null)) { - $errors[] = 'Publisher is not set'; - } + private function handlePublishHttpPost(Request $request, Response $response) { + $bodySnatcher = new BodySnatcher($request); + $bodySnatcher->promise()->then(function ($body) use ($request, $response) { + try { + //{"topic": "com.myapp.topic1", "args": ["Hello, world"]} + $json = json_decode($body); + + if ($json === null) { + $response->writeHead(400, ['Content-Type' => 'text/plain', 'Connection' => 'close']); + $response->end("JSON decoding failed: " . json_last_error_msg()); + return; + } - $response->writeHead(400, ['Content-Type' => 'text/plain', 'Connection' => 'close']); - $response->end( - "The following errors occurred:" . PHP_EOL . PHP_EOL . implode(PHP_EOL, $errors) - ); - return; + if ( + isset($json->topic) + && isset($json->args) + && Utils::uriIsValid($json->topic) + && is_array($json->args) + && ($this->getPublisher() !== null) + ) { + $argsKw = isset($json->argsKw) && is_object($json->argsKw) ? $json->argsKw : null; + $options = isset($json->options) && is_object($json->opitons) ? $json->options : null; + $this->getSession()->publish($json->topic, $json->args, $argsKw, $options); + } else { + $errors = []; + if (!isset($json->topic)) { + $errors[] = 'Topic not set'; + } + if (!isset($json->args)) { + $errors[] = 'Args not set'; + } + if (!is_array($json->args)) { + $errors[] = 'Args is not an array, got ' . gettype($json->args); + } + if (!Utils::uriIsValid($json->topic)) { + $errors[] = 'Topic is not a valid URI'; + } + if (!($this->getPublisher() !== null)) { + $errors[] = 'Publisher is not set'; } - } catch (\Exception $e) { - // should shut down everything $response->writeHead(400, ['Content-Type' => 'text/plain', 'Connection' => 'close']); $response->end( - "An exception was thrown: " . $e->getMessage() . PHP_EOL . $e->getTraceAsString() + "The following errors occurred:" . PHP_EOL . PHP_EOL . implode(PHP_EOL, $errors) ); return; } - $response->writeHead(200, ['Content-Type' => 'text/plain', 'Connection' => 'close']); - $response->end("pub"); - }); - } else if ($request->getPath() == '/call' && $request->getMethod() == 'POST') { - $this->handleCallHttpRequest($request, $response); - } else { - $response->writeHead(404, ['Content-Type' => 'text/plain', 'Connection' => 'close']); - $response->end("Not found"); - } + } catch (\Exception $e) { + // should shut down everything + $response->writeHead(400, ['Content-Type' => 'text/plain', 'Connection' => 'close']); + $response->end("Bad Request: " . $e->getMessage()); + return; + } + $response->writeHead(200, ['Content-Type' => 'text/plain', 'Connection' => 'close']); + $response->end("pub"); + }); } private function handleCallHttpRequest($request, $response) { @@ -133,20 +146,24 @@ private function handleCallHttpRequest($request, $response) { $options = isset($json->options) && is_object($json->opitons) ? $json->options : null; $this->getSession()->call($json->procedure, $args, $argsKw, $options)->then( - /** @param CallResult $result */ + /** @param CallResult $result */ function (CallResult $result) use ($response) { - $responseObj = new \stdClass(); - $responseObj->result = "SUCCESS"; - $responseObj->args = $result->getArguments(); - $responseObj->argsKw = $result->getArgumentsKw(); + $responseObj = new \stdClass(); + $responseObj->result = "SUCCESS"; + $responseObj->args = $result->getArguments(); + $responseObj->argsKw = $result->getArgumentsKw(); $responseObj->details = $result->getDetails(); $response->writeHead(200, ['Content-Type' => 'application/json', 'Connection' => 'close']); $response->end(json_encode($responseObj)); }, - function ($result) use ($response) { - $responseObj = new \stdClass(); - $responseObj->result = "ERROR"; + function (ErrorMessage $msg) use ($response) { + $responseObj = new \stdClass(); + $responseObj->result = "ERROR"; + $responseObj->error_uri = $msg->getErrorURI(); + $responseObj->error_args = $msg->getArguments(); + $responseObj->error_argskw = $msg->getArgumentsKw(); + $responseObj->error_details = $msg->getDetails(); // maybe return an error code here $response->writeHead(200, ['Content-Type' => 'application/json', 'Connection' => 'close']); From 8e3eaf91141639eb896757353f353d1958aa8754 Mon Sep 17 00:00:00 2001 From: Nepomuk Fraedrich Date: Thu, 26 Oct 2017 13:26:22 +0200 Subject: [PATCH 3/6] Try to create my own package --- composer.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0b1f094..34ca577 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "voryx/wamppost", + "name": "nepda/wamppost", "description": "HTTP to WAMP Publishing Proxy", "keywords": [ "WebSockets", @@ -25,6 +25,9 @@ "WampPost": "src" } }, + "provide": { + "voryx/wamppost": "0.1.*" + }, "require": { "react/http": "~0.4", "voryx/thruway": "~0.3|~0.4" From 2f703872e087404e545d15ade193990e04de54ab Mon Sep 17 00:00:00 2001 From: Nepomuk Fraedrich Date: Thu, 26 Oct 2017 13:41:06 +0200 Subject: [PATCH 4/6] Rename URLs --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 51406fd..3b97665 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ -[![Build Status](https://travis-ci.org/voryx/WampPost.svg?branch=master)](https://travis-ci.org/voryx/WampPost) +[![Build Status](https://travis-ci.org/nepda/WampPost.svg?branch=master)](https://travis-ci.org/nepda/WampPost) WampPost =========== WampPost is a [WAMP v2](http://wamp.ws/) (Web Application Messaging Protocol) Client built with -[Thruway](https://github.com/voryx/Thruway) that allows publishing events and making RPC calls to a realm via HTTP Post. +[Thruway](https://github.com/nepda/Thruway) that allows publishing events and making RPC calls to a realm via HTTP Post. WampPost is designed to be compatible with the [crossbar HTTP pusher service](http://crossbar.io/docs/HTTP-Pusher-Service/). @@ -29,11 +29,11 @@ Download Composer [more info](https://getcomposer.org/doc/00-intro.md#downloadin Download WampPost and dependencies - $ php composer.phar require "voryx/wamppost":"dev-master" + $ php composer.phar require "nepda/wamppost":"dev-master" If you need a WAMP router to test with, then start the sample with: - $ php vendor/voryx/thruway/Examples/SimpleWsServer.php + $ php vendor/nepda/thruway/Examples/SimpleWsServer.php Thruway is now running on 127.0.0.1 port 9090. From 5a5d19a9ee1bb9d3388ad4c794a598315ec79848 Mon Sep 17 00:00:00 2001 From: Nepomuk Fraedrich Date: Thu, 26 Oct 2017 13:46:26 +0200 Subject: [PATCH 5/6] Change version of voryx/thruway to ~0.4 --- composer.json | 73 +++++++++++++++++++++++++++------------------------ 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/composer.json b/composer.json index 34ca577..2cab266 100644 --- a/composer.json +++ b/composer.json @@ -1,39 +1,42 @@ { - "name": "nepda/wamppost", - "description": "HTTP to WAMP Publishing Proxy", - "keywords": [ - "WebSockets", - "PubSub", - "Publish", - "WAMP", - "WAMP2", - "HTTP", - "Proxy" - ], - "license": "MIT", - "authors": [ - { - "name": "Matt Bonneau", "email": "matt@bonneau.net", "role": "Developer" - }, - - { - "name": "David Dan", "email": "davidwdan@gmail.com", "role": "Developer" - } - ], - "autoload": { - "psr-0": { - "WampPost": "src" - } + "name": "nepda/wamppost", + "description": "HTTP to WAMP Publishing Proxy", + "keywords": [ + "WebSockets", + "PubSub", + "Publish", + "WAMP", + "WAMP2", + "HTTP", + "Proxy" + ], + "license": "MIT", + "authors": [ + { + "name": "Matt Bonneau", + "email": "matt@bonneau.net", + "role": "Developer" }, - "provide": { - "voryx/wamppost": "0.1.*" - }, - "require": { - "react/http": "~0.4", - "voryx/thruway": "~0.3|~0.4" - }, - "require-dev": { - "react/http-client": "^0.4.8", - "voryx/event-loop": "^0.2.0" + { + "name": "David Dan", + "email": "davidwdan@gmail.com", + "role": "Developer" + } + ], + "autoload": { + "psr-0": { + "WampPost": "src" } + }, + "provide": { + "voryx/wamppost": "0.1.*" + }, + "require": { + "react/http": "~0.4", + "voryx/thruway": "~0.4" + }, + "require-dev": { + "react/http-client": "^0.4.8", + "voryx/event-loop": "^0.2.0" + } } From 2b039247e268aa4bbcdf3cb7c701e8c74ac88554 Mon Sep 17 00:00:00 2001 From: Nepomuk Fraedrich Date: Thu, 26 Oct 2017 16:35:51 +0200 Subject: [PATCH 6/6] Removed provide part... just testing --- composer.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/composer.json b/composer.json index 2cab266..fb87d9a 100644 --- a/composer.json +++ b/composer.json @@ -28,9 +28,6 @@ "WampPost": "src" } }, - "provide": { - "voryx/wamppost": "0.1.*" - }, "require": { "react/http": "~0.4", "voryx/thruway": "~0.4"