From b2f7761e9dfa26083e482d130dbfdfd063b963ad Mon Sep 17 00:00:00 2001 From: gggeek Date: Tue, 24 Jan 2023 09:44:10 +0000 Subject: [PATCH] introduce negative debug level --- src/Client.php | 8 +++++--- src/Helper/Http.php | 6 +++--- src/Request.php | 42 ++++++++++++++++++++++++++---------------- src/Response.php | 14 ++++++++++---- src/Server.php | 2 +- 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/Client.php b/src/Client.php index 372beff0..2430f743 100644 --- a/src/Client.php +++ b/src/Client.php @@ -353,12 +353,14 @@ class Client * The debugging information at level 1 includes the raw data returned from the XML-RPC server it was querying * (including bot HTTP headers and the full XML payload), and the PHP value the client attempts to create to * represent the value returned by the server. - * At level2, the complete payload of the xml-rpc request is also printed, before being sent to the server. + * At level 2, the complete payload of the xml-rpc request is also printed, before being sent to the server. + * At level -1, the Response objects returned by send() calls will not carry information about the http response's + * cookies, headers and body, which might save some memory * * This option can be very useful when debugging servers as it allows you to see exactly what the client sends and * the server returns. Never leave it enabled for production! * - * @param integer $level values 0, 1 and 2 are supported (2 = echo sent msg too, before received response) + * @param integer $level values -1, 0, 1 and 2 are supported * @return $this */ public function setDebug($level) @@ -1206,7 +1208,7 @@ class Client if ($this->debug > 1) { curl_setopt($curl, CURLOPT_VERBOSE, true); - /// @todo allow callers to redirect curlopt_stderr to some stream which can be buffered + /// @todo redirect curlopt_stderr to some stream which can be piped to the logger } curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent); // required for XMLRPC: post the data diff --git a/src/Helper/Http.php b/src/Helper/Http.php index d4c902fa..829ad50e 100644 --- a/src/Helper/Http.php +++ b/src/Helper/Http.php @@ -87,12 +87,12 @@ class Http * * @param string $data the http response, headers and body. It will be stripped of headers * @param bool $headersProcessed when true, we assume that response inflating and dechunking has been already carried out - * @param int $debug when != 0, logs to screen messages detailing info about the parsed data + * @param int $debug when > 0, logs to screen messages detailing info about the parsed data * @return array with keys 'headers', 'cookies', 'raw_data' and 'status_code' * @throws HttpException * - * @todo if $debug is 0, we could avoid populating 'raw_data' and 'headers' in the returned value - even better, have - * 2 debug levels + * @todo if $debug is < 0, we could avoid populating 'raw_data' and 'headers' in the returned value - but that would + * be a weird API... */ public function parseResponseHeaders(&$data, $headersProcessed = false, $debug = 0) { diff --git a/src/Request.php b/src/Request.php index 153d7310..f5ae68b5 100644 --- a/src/Request.php +++ b/src/Request.php @@ -258,11 +258,12 @@ class Request */ public function parseResponse($data = '', $headersProcessed = false, $returnType = XMLParser::RETURN_XMLRPCVALS) { - if ($this->debug) { + if ($this->debug > 0) { $this->getLogger()->debugMessage("---GOT---\n$data\n---END---"); } - $this->httpResponse = array('raw_data' => $data, 'headers' => array(), 'cookies' => array()); + $httpResponse = array('raw_data' => $data, 'headers' => array(), 'cookies' => array()); + $this->httpResponse = $httpResponse; if ($data == '') { $this->getLogger()->errorLog('XML-RPC: ' . __METHOD__ . ': no response received from server.'); @@ -273,7 +274,7 @@ class Request if (substr($data, 0, 4) == 'HTTP') { $httpParser = new Http(); try { - $this->httpResponse = $httpParser->parseResponseHeaders($data, $headersProcessed, $this->debug); + $httpResponse = $httpParser->parseResponseHeaders($data, $headersProcessed, $this->debug > 0); } catch (HttpException $e) { // failed processing of HTTP response headers // save into response obj the full payload received, for debugging @@ -297,11 +298,17 @@ class Request // try to 'guestimate' the character encoding of the received response $respEncoding = XMLParser::guessEncoding( - isset($this->httpResponse['headers']['content-type']) ? $this->httpResponse['headers']['content-type'] : '', + isset($httpResponse['headers']['content-type']) ? $httpResponse['headers']['content-type'] : '', $data ); - if ($this->debug) { + if ($this->debug >= 0) { + $this->httpResponse = $httpResponse; + } else { + $httpResponse = null; + } + + if ($this->debug > 0) { $start = strpos($data, '