diff --git a/src/Server/Error.php b/src/Server/Error.php index c2d6227c2..87104364e 100644 --- a/src/Server/Error.php +++ b/src/Server/Error.php @@ -11,7 +11,7 @@ class Error { - const ERROR_PARSE = -32768; + const ERROR_PARSE = -32700; const ERROR_INVALID_REQUEST = -32600; const ERROR_INVALID_METHOD = -32601; const ERROR_INVALID_PARAMS = -32602; diff --git a/src/Server/Request.php b/src/Server/Request.php index 29682862f..21b455f1b 100644 --- a/src/Server/Request.php +++ b/src/Server/Request.php @@ -28,6 +28,12 @@ class Request */ protected $isMethodError = false; + /** + * Flag + * @var bool + */ + protected $isParseError = false; + /** * Requested method * @var string @@ -178,6 +184,16 @@ public function isMethodError() return $this->isMethodError; } + /** + * Was a malformed JSON provided? + * + * @return bool + */ + public function isParseError() + { + return $this->isParseError; + } + /** * Set request identifier * @@ -234,8 +250,12 @@ public function getVersion() */ public function loadJson($json) { - $options = Json\Json::decode($json, Json\Json::TYPE_ARRAY); - $this->setOptions($options); + try { + $options = Json\Json::decode($json, Json\Json::TYPE_ARRAY); + $this->setOptions($options); + } catch(\Exception $e) { + $this->isParseError = true; + } } /** diff --git a/src/Server/Server.php b/src/Server/Server.php index 6597dcc69..200c6acd5 100644 --- a/src/Server/Server.php +++ b/src/Server/Server.php @@ -483,6 +483,10 @@ protected function _handle() { $request = $this->getRequest(); + if($request->isParseError()){ + return $this->fault('Parse error', Error::ERROR_PARSE); + } + if (!$request->isMethodError() && (null === $request->getMethod())) { return $this->fault('Invalid Request', Error::ERROR_INVALID_REQUEST); } diff --git a/test/Server/ErrorTest.php b/test/Server/ErrorTest.php index b604ff1f7..57597a9df 100644 --- a/test/Server/ErrorTest.php +++ b/test/Server/ErrorTest.php @@ -52,8 +52,8 @@ public function testCodeShouldBeErrOtherByDefault() public function testSetCodeShouldCastToInteger() { - $this->error->setCode('-32768'); - $this->assertEquals(-32768, $this->error->getCode()); + $this->error->setCode('-32700'); + $this->assertEquals(-32700, $this->error->getCode()); } public function testCodeShouldBeLimitedToStandardIntegers() diff --git a/test/Server/RequestTest.php b/test/Server/RequestTest.php index 07471c0eb..dd6f7fa04 100644 --- a/test/Server/RequestTest.php +++ b/test/Server/RequestTest.php @@ -233,6 +233,13 @@ public function testMethodNamesShouldAllowDotNamespacing() $this->assertEquals('foo.bar', $this->request->getMethod()); } + public function testIsParseErrorSetOnMalformedJson() + { + $testJson = '{"id":1, "method": "test", "params:"[1,2,3]}'; + $this->request->loadJson($testJson); + $this->assertTrue($this->request->isParseError()); + } + public function getOptions() { return array(