From 89ae23b8660e13a595f3528cae3a7cf9f2d80719 Mon Sep 17 00:00:00 2001 From: gggeek Date: Thu, 24 Nov 2022 17:13:02 +0000 Subject: [PATCH] allow library users to override fine-grained response parsing errors --- NEWS | 4 ++++ src/PhpXmlRpc.php | 12 ++++++++++++ src/Request.php | 14 ++++++-------- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 53787fdb..7b1831a7 100644 --- a/NEWS +++ b/NEWS @@ -4,6 +4,10 @@ XML-RPC for PHP version xxx - unreleased * fixed: DateTimeInterface is not present in php 5.4 (error introduced in ver. 4.8.1) +* new: allow users of the library to get more fine-grained information about errors in parsing received responses by + overriding the integer value of `PhpXmlRpc::$xmlrpcerr['invalid_xml']`, `PhpXmlRpc::$xmlrpcerr['xml_not_compliant']`, + `PhpXmlRpc::$xmlrpcerr['xml_parsing_error']` and the equivalent `PhpXmlRpc::$xmlrpcstr` strings. Feature req. #101 + * improved: CI tests now run on php versions 5.4, 5.5 besides all more recent ones * improved: the test container for local testing now defaults to php 7.4 on ubuntu 20 focal diff --git a/src/PhpXmlRpc.php b/src/PhpXmlRpc.php index c7c39eaa..b3931978 100644 --- a/src/PhpXmlRpc.php +++ b/src/PhpXmlRpc.php @@ -9,6 +9,7 @@ class PhpXmlRpc { static public $xmlrpcerr = array( 'unknown_method' => 1, + /// @deprecated. left in for BC 'invalid_return' => 2, 'incorrect_params' => 3, 'introspect_unknown' => 4, @@ -27,6 +28,11 @@ class PhpXmlRpc 'multicall_noparams' => 13, 'multicall_notarray' => 14, 'no_http2' => 15, + // the following 3 are meant to give greater insight than 'invalid_return'. They use the same code for BC, + // but you can override their value in your own code + 'invalid_xml' => 2, + 'xml_not_compliant' => 2, + 'xml_parsing_error' => 2, 'cannot_decompress' => 103, 'decompress_fail' => 104, @@ -37,6 +43,7 @@ class PhpXmlRpc static public $xmlrpcstr = array( 'unknown_method' => 'Unknown method', + /// @deprecated. left in for BC 'invalid_return' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)', 'incorrect_params' => 'Incorrect parameters passed to method', 'introspect_unknown' => "Can't introspect: method unknown", @@ -55,6 +62,11 @@ class PhpXmlRpc 'multicall_noparams' => 'Missing params', 'multicall_notarray' => 'params is not an array', 'no_http2' => 'No HTTP/2 support compiled in', + // the following 3 are meant to give greater insight than 'invalid_return'. They use the same string for BC, + // but you can override their value in your own code + 'invalid_xml' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)', + 'xml_not_compliant' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)', + 'xml_parsing_error' => 'Invalid response payload (you can use the setDebug method to allow analysis of the response)', 'cannot_decompress' => 'Received from server compressed HTTP and cannot decompress', 'decompress_fail' => 'Received from server invalid compressed HTTP', diff --git a/src/Request.php b/src/Request.php index 9841c015..5af3e418 100644 --- a/src/Request.php +++ b/src/Request.php @@ -336,8 +336,8 @@ class Request // BC break: in the past for some cases we used the error message: 'XML error at line 1, check URL' - $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'], - PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '', + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_xml'], + PhpXmlRpc::$xmlrpcstr['invalid_xml'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '', $this->httpResponse ); @@ -347,8 +347,8 @@ class Request } // second error check: xml well formed but not xml-rpc compliant elseif ($xmlRpcParser->_xh['isf'] == 2) { - $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'], - PhpXmlRpc::$xmlrpcstr['invalid_return'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '', + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['xml_not_compliant'], + PhpXmlRpc::$xmlrpcstr['xml_not_compliant'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '', $this->httpResponse ); @@ -359,10 +359,8 @@ class Request // third error check: parsing of the response has somehow gone boink. /// @todo shall we omit this check, since we trust the parsing code? elseif ($returnType == XMLParser::RETURN_XMLRPCVALS && !is_object($xmlRpcParser->_xh['value'])) { - // something odd has happened - // and it's time to generate a client side error - // indicating something odd went on - $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_return'], PhpXmlRpc::$xmlrpcstr['invalid_return'], + // something odd has happened and it's time to generate a client side error indicating something odd went on + $r = new Response(0, PhpXmlRpc::$xmlrpcerr['xml_parsing_error'], PhpXmlRpc::$xmlrpcstr['xml_parsing_error'], '', $this->httpResponse ); } else { -- 2.47.0