From: gggeek Date: Thu, 2 Feb 2023 12:38:55 +0000 (+0000) Subject: refactor access to req/resp payload X-Git-Tag: 4.10.0~34 X-Git-Url: http://git.onelab.eu/?a=commitdiff_plain;h=881e0c72f7260a762defb0bac6cf1626c853fbb0;p=plcapi.git refactor access to req/resp payload --- diff --git a/debugger/action.php b/debugger/action.php index 83205c4f..7b6534e5 100644 --- a/debugger/action.php +++ b/debugger/action.php @@ -214,25 +214,28 @@ if ($action) { $msg[0] = new $requestClass($method, array(), $id); // hack! build payload by hand if ($wstype == 1) { - $msg[0]->payload = "{\n" . + $payload = "{\n" . '"method": "' . $method . "\",\n\"params\": [" . $payload . "\n],\n\"id\": "; // fix: if user gave an empty string, use NULL, or we'll break json syntax if ($id == "") { - $msg[0]->payload .= "null\n}"; + $payload .= "null\n}"; } else { if (is_numeric($id) || $id == 'false' || $id == 'true' || $id == 'null') { - $msg[0]->payload .= "$id\n}"; + $payload .= "$id\n}"; } else { - $msg[0]->payload .= "\"$id\"\n}"; + $payload .= "\"$id\"\n}"; } } + $msg[0]->setPayload($payload); } else { - $msg[0]->payload = $msg[0]->xml_header($inputcharset) . + $msg[0]->setPayload( + $msg[0]->xml_header($inputcharset) . '' . $method . "\n" . $payload . - "\n" . $msg[0]->xml_footer(); + "\n" . $msg[0]->xml_footer() + ); } $actionname = 'Execution of method ' . $method; break; diff --git a/src/Client.php b/src/Client.php index d10938b1..38a913c7 100644 --- a/src/Client.php +++ b/src/Client.php @@ -998,7 +998,7 @@ class Client return $this->multicall($req, $timeout, $method); } elseif (is_string($req)) { $n = new static::$requestClass(''); - $n->payload = $req; + $n->setPayload($req); $req = $n; } @@ -1080,10 +1080,10 @@ class Client // Only create the payload if it was not created previously /// @todo what if the request's payload was created with a different encoding? /// Also, if we do not call serialize(), the request will not set its content-type to have the charset declared - if (empty($req->payload)) { - $req->serialize($opts['request_charset_encoding']); + $payload = $req->getPayload(); + if (empty($payload)) { + $payload = $req->serialize($opts['request_charset_encoding']); } - $payload = $req->payload; // Deflate request body and set appropriate request headers $encodingHdr = ''; @@ -1360,12 +1360,12 @@ class Client } // Only create the payload if it was not created previously - if (empty($req->payload)) { - $req->serialize($opts['request_charset_encoding']); + $payload = $req->getPayload(); + if (empty($payload)) { + $payload = $req->serialize($opts['request_charset_encoding']); } // Deflate request body and set appropriate request headers - $payload = $req->payload; $encodingHdr = ''; /// @todo test for existence of proper function, in case of polyfills if (function_exists('gzdeflate') && ($opts['request_compression'] == 'gzip' || $opts['request_compression'] == 'deflate')) { diff --git a/src/Request.php b/src/Request.php index 6a99d92e..2d9fd1f3 100644 --- a/src/Request.php +++ b/src/Request.php @@ -8,6 +8,7 @@ use PhpXmlRpc\Helper\XMLParser; use PhpXmlRpc\Traits\CharsetEncoderAware; use PhpXmlRpc\Traits\DeprecationLogger; use PhpXmlRpc\Traits\ParserAware; +use PhpXmlRpc\Traits\PayloadBearer; /** * This class provides the representation of a request to an XML-RPC server. @@ -20,17 +21,15 @@ class Request use CharsetEncoderAware; use DeprecationLogger; use ParserAware; + use PayloadBearer; /// @todo: do these need to be public? - public $payload; /** @internal */ public $methodname; /** @internal */ public $params = array(); /** @var int */ public $debug = 0; - /** @var string */ - public $content_type = 'text/xml'; // holds data while parsing the response. NB: Not a full Response object /** @deprecated will be removed in a future release */ diff --git a/src/Response.php b/src/Response.php index ad7e2f1b..f891d553 100644 --- a/src/Response.php +++ b/src/Response.php @@ -5,6 +5,7 @@ namespace PhpXmlRpc; use PhpXmlRpc\Exception\StateErrorException; use PhpXmlRpc\Traits\CharsetEncoderAware; use PhpXmlRpc\Traits\DeprecationLogger; +use PhpXmlRpc\Traits\PayloadBearer; /** * This class provides the representation of the response of an XML-RPC server. @@ -19,6 +20,7 @@ class Response { use CharsetEncoderAware; use DeprecationLogger; + use PayloadBearer; /// @todo: do these need to be public? /** @internal */ @@ -29,9 +31,6 @@ class Response public $errno = 0; /** @internal */ public $errstr = ''; - public $payload; - /** @var string */ - public $content_type = 'text/xml'; protected $httpResponse = array('headers' => array(), 'cookies' => array(), 'raw_data' => '', 'status_code' => null); @@ -224,7 +223,8 @@ class Response /// @todo throw instead? There are very few other places where the lib trigger errors which can potentially reach stdout... $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1); trigger_error('Undefined property via __get(): ' . $name . ' in ' . $trace[0]['file'] . ' on line ' . $trace[0]['line'], E_USER_WARNING); - return null; + $result = null; + return $result; } } diff --git a/src/Server.php b/src/Server.php index 33428f65..2da4258b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -399,18 +399,19 @@ class Server static::$_xmlrpcs_occurred_errors . "+++END+++"); } - $payload = $resp->xml_header($respCharset); + $header = $resp->xml_header($respCharset); if ($this->debug > 0) { - $payload = $payload . $this->serializeDebug($respCharset); + $header .= $this->serializeDebug($respCharset); } // Do not create response serialization if it has already happened. Helps to build json magic /// @todo what if the payload was created targeting a different charset than $respCharset? /// Also, if we do not call serialize(), the request will not set its content-type to have the charset declared - if (empty($resp->payload)) { - $resp->serialize($respCharset); + $payload = $resp->getPayload(); + if (empty($payload)) { + $payload = $resp->serialize($respCharset); } - $payload = $payload . $resp->payload; + $payload = $header . $payload; if ($returnPayload) { return $payload; diff --git a/src/Traits/PayloadBearer.php b/src/Traits/PayloadBearer.php new file mode 100644 index 00000000..555baf81 --- /dev/null +++ b/src/Traits/PayloadBearer.php @@ -0,0 +1,37 @@ +payload = $payload; + + if ($contentType != '') { + $this->content_type = $contentType; + } + + return $this; + } + + /** + * @return string + */ + public function getPayload() + { + return $this->payload; + } +}