From fba10c56fb94662186cfd000f80c360d375862fb Mon Sep 17 00:00:00 2001 From: patres Date: Tue, 16 Jan 2018 17:09:26 +0100 Subject: [PATCH 1/4] Add customize http response code --- .gitignore | 3 ++- Exception/RpcException.php | 39 ++++++++++++++++++++++++++++++++++++++ JsonRpc/Implementation.php | 14 ++++++++------ Rpc/Implementation.php | 10 ++++++++-- Rpc/Server.php | 14 ++++++++++---- XmlRpc/Implementation.php | 10 ++++++---- 6 files changed, 73 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 36686ce..8a09f7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ phpunit.xml vendor -composer.lock \ No newline at end of file +composer.lock +.idea/ \ No newline at end of file diff --git a/Exception/RpcException.php b/Exception/RpcException.php index 4ab0fa5..7330ff1 100644 --- a/Exception/RpcException.php +++ b/Exception/RpcException.php @@ -18,6 +18,12 @@ class RpcException extends Exception { private $data; + /** + * @var int + */ + private $httpStatusCode; + + /** * Set additional information about the error. * @@ -37,4 +43,37 @@ public function getData() { return $this->data; } + + /** + * Get response http status code + * + * @return int + */ + public function getHttpStatusCode() + { + return $this->httpStatusCode; + } + + /** + * Set response http status code + * + * @param int $httpStatusCode + */ + public function setHttpStatusCode($httpStatusCode) + { + $this->httpStatusCode = $httpStatusCode; + } + + /** + * Throw a new exception. + * + * @param string $message Error message + * @param int $code Error code (default = 0) + * @param int $httpStatusCode HTTP status code to send (default = 400) + */ + public function __construct($message, $code = 0, $httpStatusCode = 400) + { + parent::__construct($message, $code); + $this->httpStatusCode = $httpStatusCode; + } } diff --git a/JsonRpc/Implementation.php b/JsonRpc/Implementation.php index acd3b59..4a63995 100644 --- a/JsonRpc/Implementation.php +++ b/JsonRpc/Implementation.php @@ -26,6 +26,7 @@ use Seven\RpcBundle\Rpc\Method\MethodReturn; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Seven\RpcBundle\Rpc\Server; class Implementation extends BaseImplementation { @@ -48,17 +49,17 @@ public function createMethodCall(Request $request) { $content = $request->getContent(); if (empty($content)) { - throw new InvalidJsonRpcContent('The JSON-RPC request is empty', self::ERROR_INVALID_REQUEST); + throw new InvalidJsonRpcContent('The JSON-RPC request is empty', self::ERROR_INVALID_REQUEST, self::BAD_REQUEST_STATUS_CODE); } $data = json_decode($content, true); if (is_null($data)) { - throw new InvalidJsonRpcContent('The JSON-RPC call is not valid', self::ERROR_PARSING); + throw new InvalidJsonRpcContent('The JSON-RPC call is not valid', self::ERROR_PARSING, self::ERROR_STATUS_CODE); } if (empty($data['jsonrpc']) || version_compare($data['jsonrpc'], '2.0', '<')) { - throw new InvalidJsonRpcVersion('The JSON-RPC call version is not supported', self::ERROR_SERVER_ERROR); + throw new InvalidJsonRpcVersion('The JSON-RPC call version is not supported', self::ERROR_SERVER_ERROR, self::ERROR_INVALID_REQUEST); } if (empty($data['method']) || !isset($data['params'])) { @@ -71,11 +72,12 @@ public function createMethodCall(Request $request) /** * @param MethodResponse $response * + * @param int $statusCode * @return Response * - * @throws \Seven\RpcBundle\Exception\UnknownMethodResponse + * @throws UnknownMethodResponse */ - public function createHttpResponse(MethodResponse $response) + public function createHttpResponse(MethodResponse $response, $statusCode = Server::HTTP_SUCCESS_STATUS) { $data = array( 'jsonrpc' => '2.0', @@ -116,7 +118,7 @@ public function createHttpResponse(MethodResponse $response) $data['id'] = $response->getCallId(); } - return new Response(json_encode($data), 200, array('content-type' => 'application/json')); + return new Response(json_encode($data), $statusCode, array('content-type' => 'application/json')); } /** diff --git a/Rpc/Implementation.php b/Rpc/Implementation.php index 0cfa66d..7ec549d 100644 --- a/Rpc/Implementation.php +++ b/Rpc/Implementation.php @@ -15,11 +15,16 @@ use Seven\RpcBundle\Rpc\Method\MethodResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Seven\RpcBundle\Rpc\Server; // @codeCoverageIgnoreStart abstract class Implementation { + const ERROR_STATUS_CODE = 500; + const BAD_REQUEST_STATUS_CODE = 400; + const NON_EXIST_STATUS_CODE = 404; + /** * @param Request $request * @return mixed @@ -28,7 +33,7 @@ abstract class Implementation abstract public function createMethodCall(Request $request); /** - * @param Response $response + * @param Response $response * @return MethodResponse */ @@ -36,10 +41,11 @@ abstract public function createMethodResponse(Response $response); /** * @param MethodResponse $response + * @param int $statusCode * @return Response */ - abstract public function createHttpResponse(MethodResponse $response); + abstract public function createHttpResponse(MethodResponse $response, $statusCode = Server::HTTP_SUCCESS_STATUS); /** * @param MethodCall $call diff --git a/Rpc/Server.php b/Rpc/Server.php index 2f17f85..5dac165 100644 --- a/Rpc/Server.php +++ b/Rpc/Server.php @@ -25,6 +25,8 @@ class Server implements ServerInterface { + const HTTP_SUCCESS_STATUS = 200; + protected $impl; protected $handlers; @@ -61,6 +63,7 @@ public function handle(Request $request) try { $methodCall = $this->impl->createMethodCall($request); $methodResponse = $this->_handle($methodCall); + $exceptionStatus = self::HTTP_SUCCESS_STATUS; } catch (\Exception $e) { // log exception @@ -69,15 +72,17 @@ public function handle(Request $request) } $methodResponse = new MethodFault($e); + $exceptionStatus = method_exists($e, 'getHttpStatusCode') ? $e->getHttpStatusCode() : Implementation::BAD_REQUEST_STATUS_CODE; } - - return $this->impl->createHttpResponse($methodResponse); + return $this->impl->createHttpResponse($methodResponse, $exceptionStatus); } /** * @param MethodCall $methodCall * * @return MethodResponse + * @throws MethodNotExists + * @throws InvalidParameters */ protected function _handle(MethodCall $methodCall) { @@ -97,6 +102,7 @@ protected function _handle(MethodCall $methodCall) * @throws MethodNotExists * * @return mixed + * @throws InvalidParameters */ public function call($method, $parameters) { @@ -129,7 +135,7 @@ protected function isAssociative(array $arr) * Prepare parameters. * * @param callable $callback - * @param array $parameters + * @param array $parameters * * @return array * @@ -151,7 +157,7 @@ protected function prepareParameters($callback, array $parameters) ) { throw new InvalidParameters( sprintf('Invalid number of parameters. %d given but %d are required of %d total.', - $paramCount, $refl->getNumberOfRequiredParameters(), $refl->getNumberOfParameters()) + $paramCount, $refl->getNumberOfRequiredParameters(), $refl->getNumberOfParameters()) ); } diff --git a/XmlRpc/Implementation.php b/XmlRpc/Implementation.php index 8ad914c..3ade3be 100644 --- a/XmlRpc/Implementation.php +++ b/XmlRpc/Implementation.php @@ -24,6 +24,7 @@ use Seven\RpcBundle\XmlRpc\ValueType\AbstractType; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Seven\RpcBundle\Rpc\Server; class Implementation extends BaseImplementation { @@ -116,12 +117,13 @@ protected function getSchema() } /** - * @param MethodResponse $response - * @throws \Seven\RpcBundle\Exception\UnknownMethodResponse + * @param MethodResponse $response + * @param int $statusCode * @return Response + * @throws UnknownMethodResponse */ - public function createHttpResponse(MethodResponse $response) + public function createHttpResponse(MethodResponse $response, $statusCode = Server::HTTP_SUCCESS_STATUS) { $document = new \DOMDocument("1.0", "UTF-8"); $document->appendChild($responseEl = $document->createElement("methodResponse")); @@ -140,7 +142,7 @@ public function createHttpResponse(MethodResponse $response) throw new UnknownMethodResponse("Unknown MethodResponse instance"); } - return new Response($document->saveXML(), 200, array('content-type' => 'text/xml')); + return new Response($document->saveXML(), $statusCode, array('content-type' => 'text/xml')); } /** From 0214175452e181ed7d6d015abac899df5117f206 Mon Sep 17 00:00:00 2001 From: patres Date: Tue, 16 Jan 2018 17:20:45 +0100 Subject: [PATCH 2/4] Fix server usage --- Rpc/Implementation.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Rpc/Implementation.php b/Rpc/Implementation.php index 7ec549d..0ce2ed7 100644 --- a/Rpc/Implementation.php +++ b/Rpc/Implementation.php @@ -15,7 +15,6 @@ use Seven\RpcBundle\Rpc\Method\MethodResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Seven\RpcBundle\Rpc\Server; // @codeCoverageIgnoreStart abstract class Implementation From 951d53d720b7a2dedb20baa8bcec7ae05ab3de1a Mon Sep 17 00:00:00 2001 From: patres Date: Tue, 16 Jan 2018 17:29:16 +0100 Subject: [PATCH 3/4] Fix server usage --- JsonRpc/Implementation.php | 1 - XmlRpc/Implementation.php | 1 - 2 files changed, 2 deletions(-) diff --git a/JsonRpc/Implementation.php b/JsonRpc/Implementation.php index 4a63995..f2052a3 100644 --- a/JsonRpc/Implementation.php +++ b/JsonRpc/Implementation.php @@ -26,7 +26,6 @@ use Seven\RpcBundle\Rpc\Method\MethodReturn; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Seven\RpcBundle\Rpc\Server; class Implementation extends BaseImplementation { diff --git a/XmlRpc/Implementation.php b/XmlRpc/Implementation.php index 3ade3be..142ae5d 100644 --- a/XmlRpc/Implementation.php +++ b/XmlRpc/Implementation.php @@ -24,7 +24,6 @@ use Seven\RpcBundle\XmlRpc\ValueType\AbstractType; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; -use Seven\RpcBundle\Rpc\Server; class Implementation extends BaseImplementation { From 08c98c074dbfb673ca8c80e2fa83024bab502f34 Mon Sep 17 00:00:00 2001 From: patres36 Date: Thu, 18 Jan 2018 11:06:43 +0100 Subject: [PATCH 4/4] Update implementation php createRepsonse Added status code from response to to method response exception --- JsonRpc/Implementation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JsonRpc/Implementation.php b/JsonRpc/Implementation.php index f2052a3..badc8cf 100644 --- a/JsonRpc/Implementation.php +++ b/JsonRpc/Implementation.php @@ -156,7 +156,7 @@ public function createMethodResponse(Response $response) throw new InvalidJsonRpcContent('The JSON-RPC fault code is not passed'); } - $exception = new Fault($data['error']['message'], $data['error']['code']); + $exception = new Fault($data['error']['message'], $data['error']['code'], $response->getStatusCode()); if (isset($data['error']['data'])) { $exception->setData($data['error']['data']); }