diff options
author | Frédéric Guillot <fred@kanboard.net> | 2014-09-17 14:47:41 +0200 |
---|---|---|
committer | Frédéric Guillot <fred@kanboard.net> | 2014-09-17 14:47:41 +0200 |
commit | 243e72474bd0c87b8647efe1edeb1116a0a5fcd0 (patch) | |
tree | 11630e7879c012f64c8b457e37110ada67c10e83 /vendor | |
parent | e7a20b9d8f8d7c47173c59782c5bd24a0ba6cac9 (diff) |
Improve board API calls
Diffstat (limited to 'vendor')
-rw-r--r-- | vendor/JsonRPC/Client.php | 20 | ||||
-rw-r--r-- | vendor/JsonRPC/Server.php | 137 |
2 files changed, 112 insertions, 45 deletions
diff --git a/vendor/JsonRPC/Client.php b/vendor/JsonRPC/Client.php index dda78094..65aed223 100644 --- a/vendor/JsonRPC/Client.php +++ b/vendor/JsonRPC/Client.php @@ -2,11 +2,13 @@ namespace JsonRPC; +use BadFunctionCallException; + /** * JsonRPC client class * * @package JsonRPC - * @author Frderic Guillot + * @author Frederic Guillot * @license Unlicense http://unlicense.org/ */ class Client @@ -44,6 +46,14 @@ class Client private $password; /** + * Enable debug output to the php error log + * + * @access public + * @var boolean + */ + public $debug = false; + + /** * Default HTTP headers to send to the server * * @access private @@ -100,6 +110,7 @@ class Client * Execute * * @access public + * @throws BadFunctionCallException Exception thrown when a bad request is made (missing argument/procedure) * @param string $procedure Procedure name * @param array $params Procedure arguments * @return mixed @@ -124,7 +135,7 @@ class Client return $result['result']; } - return null; + throw new BadFunctionCallException('Bad Request'); } /** @@ -154,6 +165,11 @@ class Client $result = curl_exec($ch); $response = json_decode($result, true); + if ($this->debug) { + error_log('==> Request: '.PHP_EOL.json_encode($payload, JSON_PRETTY_PRINT)); + error_log('==> Response: '.PHP_EOL.json_encode($response, JSON_PRETTY_PRINT)); + } + curl_close($ch); return is_array($response) ? $response : array(); diff --git a/vendor/JsonRPC/Server.php b/vendor/JsonRPC/Server.php index f80531fd..72d4e27e 100644 --- a/vendor/JsonRPC/Server.php +++ b/vendor/JsonRPC/Server.php @@ -9,7 +9,7 @@ use Closure; * JsonRPC server class * * @package JsonRPC - * @author Frderic Guillot + * @author Frederic Guillot * @license Unlicense http://unlicense.org/ */ class Server @@ -155,16 +155,18 @@ class Server * @param array $request_params Incoming arguments * @param array $method_params Procedure arguments * @param array $params Arguments to pass to the callback + * @param integer $nb_required_params Number of required parameters * @return bool */ - public function mapParameters(array $request_params, array $method_params, array &$params) + public function mapParameters(array $request_params, array $method_params, array &$params, $nb_required_params) { + if (count($request_params) < $nb_required_params) { + return false; + } + // Positional parameters if (array_keys($request_params) === range(0, count($request_params) - 1)) { - - if (count($request_params) !== count($method_params)) return false; $params = $request_params; - return true; } @@ -188,14 +190,13 @@ class Server } /** - * Parse incoming requests + * Parse the payload and test if the parsed JSON is ok * * @access public - * @return string + * @return boolean */ - public function execute() + public function isValidJsonFormat() { - // Parse payload if (empty($this->payload)) { $this->payload = file_get_contents('php://input'); } @@ -204,53 +205,102 @@ class Server $this->payload = json_decode($this->payload, true); } - // Check JSON format - if (! is_array($this->payload)) { + return is_array($this->payload); + } - return $this->getResponse(array( - 'error' => array( - 'code' => -32700, - 'message' => 'Parse error' - )), - array('id' => null) - ); + /** + * Test if all required JSON-RPC parameters are here + * + * @access public + * @return boolean + */ + public function isValidJsonRpcFormat() + { + if (! isset($this->payload['jsonrpc']) || + ! isset($this->payload['method']) || + ! is_string($this->payload['method']) || + $this->payload['jsonrpc'] !== '2.0' || + (isset($this->payload['params']) && ! is_array($this->payload['params']))) { + + return false; } - // Handle batch request - if (array_keys($this->payload) === range(0, count($this->payload) - 1)) { + return true; + } - $responses = array(); + /** + * Return true if we have a batch request + * + * @access public + * @return boolean + */ + private function isBatchRequest() + { + return array_keys($this->payload) === range(0, count($this->payload) - 1); + } + + /** + * Handle batch request + * + * @access private + * @return string + */ + private function handleBatchRequest() + { + $responses = array(); - foreach ($this->payload as $payload) { + foreach ($this->payload as $payload) { - if (! is_array($payload)) { + if (! is_array($payload)) { - $responses[] = $this->getResponse(array( - 'error' => array( - 'code' => -32600, - 'message' => 'Invalid Request' - )), - array('id' => null) - ); - } - else { + $responses[] = $this->getResponse(array( + 'error' => array( + 'code' => -32600, + 'message' => 'Invalid Request' + )), + array('id' => null) + ); + } + else { - $server = new Server($payload); - $response = $server->execute(); + $server = new Server($payload); + $response = $server->execute(); - if ($response) $responses[] = $response; + if ($response) { + $responses[] = $response; } } + } - return empty($responses) ? '' : '['.implode(',', $responses).']'; + return empty($responses) ? '' : '['.implode(',', $responses).']'; + } + + /** + * Parse incoming requests + * + * @access public + * @return string + */ + public function execute() + { + // Invalid Json + if (! $this->isValidJsonFormat()) { + return $this->getResponse(array( + 'error' => array( + 'code' => -32700, + 'message' => 'Parse error' + )), + array('id' => null) + ); } - // Check JSON-RPC format - if (! isset($this->payload['jsonrpc']) || - ! isset($this->payload['method']) || - ! is_string($this->payload['method']) || - $this->payload['jsonrpc'] !== '2.0' || - (isset($this->payload['params']) && ! is_array($this->payload['params']))) { + // Handle batch request + if ($this->isBatchRequest()){ + return $this->handleBatchRequest(); + } + + // Invalid JSON-RPC format + if (! $this->isValidJsonRpcFormat()) { return $this->getResponse(array( 'error' => array( @@ -273,6 +323,7 @@ class Server ); } + // Execute the procedure $callback = self::$procedures[$this->payload['method']]; $params = array(); @@ -282,7 +333,7 @@ class Server $parameters = $reflection->getParameters(); - if (! $this->mapParameters($this->payload['params'], $parameters, $params)) { + if (! $this->mapParameters($this->payload['params'], $parameters, $params, $reflection->getNumberOfRequiredParameters())) { return $this->getResponse(array( 'error' => array( |