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. diff --git a/composer.json b/composer.json index 0b1f094..fb87d9a 100644 --- a/composer.json +++ b/composer.json @@ -1,36 +1,39 @@ { - "name": "voryx/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" }, - "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" } + }, + "require": { + "react/http": "~0.4", + "voryx/thruway": "~0.4" + }, + "require-dev": { + "react/http-client": "^0.4.8", + "voryx/event-loop": "^0.2.0" + } } diff --git a/src/WampPost/WampPost.php b/src/WampPost/WampPost.php index 85c88b3..235e52e 100644 --- a/src/WampPost/WampPost.php +++ b/src/WampPost/WampPost.php @@ -20,7 +20,7 @@ class WampPost extends Client { 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(); @@ -81,25 +81,43 @@ private function handlePublishHttpPost(Request $request, Response $response) { $json = json_decode($body); if ($json === null) { - throw new \Exception("JSON decoding failed: " . json_last_error_msg()); + $response->writeHead(400, ['Content-Type' => 'text/plain', 'Connection' => 'close']); + $response->end("JSON decoding failed: " . json_last_error_msg()); + return; } - if (isset($json->topic) - && is_scalar($json->topic) + if ( + isset($json->topic) && isset($json->args) + && Utils::uriIsValid($json->topic) && is_array($json->args) && ($this->getPublisher() !== null) ) { - $json->topic = strtolower($json->topic); - if (!Utils::uriIsValid($json->topic)) { - throw new \Exception("Invalid URI: " . $json->topic); - } - $argsKw = isset($json->argsKw) && is_object($json->argsKw) ? $json->argsKw : null; $options = isset($json->options) && is_object($json->options) ? $json->options : null; $this->getSession()->publish($json->topic, $json->args, $argsKw, $options); } else { - throw new \Exception("Invalid request: " . json_encode($json)); + $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 @@ -128,7 +146,7 @@ private function handleCallHttpRequest($request, $response) { $options = isset($json->options) && is_object($json->options) ? $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";