introduce negative debug level
authorgggeek <giunta.gaetano@gmail.com>
Tue, 24 Jan 2023 09:44:10 +0000 (09:44 +0000)
committergggeek <giunta.gaetano@gmail.com>
Tue, 24 Jan 2023 09:44:10 +0000 (09:44 +0000)
src/Client.php
src/Helper/Http.php
src/Request.php
src/Response.php
src/Server.php

index 372beff..2430f74 100644 (file)
@@ -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
index d4c902f..829ad50 100644 (file)
@@ -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)
     {
index 153d731..f5ae68b 100644 (file)
@@ -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, '<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
             if ($start) {
                 $start += strlen('<!-- SERVER DEBUG INFO (BASE64 ENCODED):');
@@ -314,7 +321,7 @@ class Request
 
         // if the user wants back raw xml, give it to her
         if ($returnType == 'xml') {
-            return new Response($data, 0, '', 'xml', $this->httpResponse);
+            return new Response($data, 0, '', 'xml', $httpResponse);
         }
 
         /// @todo move this block of code into the XMLParser
@@ -357,10 +364,10 @@ class Request
 
             $r = new Response(0, PhpXmlRpc::$xmlrpcerr['invalid_xml'],
                 PhpXmlRpc::$xmlrpcstr['invalid_xml'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '',
-                $this->httpResponse
+                $httpResponse
             );
 
-            if ($this->debug) {
+            if ($this->debug > 0) {
                 $this->getLogger()->debugMessage($xmlRpcParser->_xh['isf_reason']);
             }
         }
@@ -368,20 +375,23 @@ class Request
         elseif ($xmlRpcParser->_xh['isf'] == 2) {
             $r = new Response(0, PhpXmlRpc::$xmlrpcerr['xml_not_compliant'],
                 PhpXmlRpc::$xmlrpcstr['xml_not_compliant'] . ' ' . $xmlRpcParser->_xh['isf_reason'], '',
-                $this->httpResponse
+                $httpResponse
             );
 
-            if ($this->debug) {
-                /// @todo echo something for user?
-            }
+            /// @todo echo something for the user? check if this was already done by the parser...
+            //if ($this->debug > 0) {
+            //    $this->getLogger()->debugMessage($xmlRpcParser->_xh['isf_reason']);
+            //}
         }
         // third error check: parsing of the response has somehow gone boink.
         /// @todo shall we omit this check, since we trust the parsing code?
         elseif ($xmlRpcParser->_xh['isf'] > 3 || $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['xml_parsing_error'], PhpXmlRpc::$xmlrpcstr['xml_parsing_error'],
-                '', $this->httpResponse
+                '', $httpResponse
             );
+
+            /// @todo echo something for the user?
         } else {
             if ($this->debug > 1) {
                 $this->getLogger()->debugMessage(
@@ -409,9 +419,9 @@ class Request
                     $errNo = -1;
                 }
 
-                $r = new Response(0, $errNo, $errStr, '', $this->httpResponse);
+                $r = new Response(0, $errNo, $errStr, '', $httpResponse);
             } else {
-                $r = new Response($v, 0, '', $returnType, $this->httpResponse);
+                $r = new Response($v, 0, '', $returnType, $httpResponse);
             }
         }
 
@@ -431,7 +441,7 @@ class Request
     /**
      * Enables/disables the echoing to screen of the xml-rpc responses received.
      *
-     * @param integer $level values 0, 1, 2 are supported
+     * @param integer $level values <0, 0, 1, >1 are supported
      * @return $this
      */
     public function setDebug($level)
index 6b5b111..c20f9af 100644 (file)
@@ -56,11 +56,13 @@ class Response
      * @param string $fString the error string, in case of an error response
      * @param string $valType The type of $val passed in. Either 'xmlrpcvals', 'phpvals' or 'xml'. Leave empty to let
      *                        the code guess the correct type.
-     * @param array|null $httpResponse
+     * @param array|null $httpResponse this should be set when the response is being built out of data received from
+     *                                 http (i.e. not when programmatically building a Response server-side). Array
+     *                                 keys should include, if known: headers, cookies, raw_data, status_code
      *
      * @todo add check that $val / $fCode / $fString is of correct type???
      *       NB: as of now we do not do it, since it might be either an xml-rpc value or a plain php val, or a complete
-     *       xml chunk, depending on usage of Client::send() inside which creator is called...
+     *       xml chunk, depending on usage of Client::send() inside which the constructor is called...
      */
     public function __construct($val, $fCode = 0, $fString = '', $valType = '', $httpResponse = null)
     {
@@ -129,7 +131,8 @@ class Response
      * with attributes being e.g. 'expires', 'path', domain'.
      * NB: cookies sent as 'expired' by the server (i.e. with an expiry date in the past) are still present in the array.
      * It is up to the user-defined code to decide how to use the received cookies, and whether they have to be sent back
-     * with the next request to the server (using Client::setCookie) or not.
+     * with the next request to the server (using $client->setCookie) or not.
+     * The values are filled in at constructor time, and might not be set for specific debug values used.
      *
      * @return array[] array of cookies received from the server
      */
@@ -139,7 +142,10 @@ class Response
     }
 
     /**
-     * @return array array with keys 'headers', 'cookies', 'raw_data' and 'status_code'
+     * Returns an array with info about the http response received from the server.
+     * The values are filled in at constructor time, and might not be set for specific debug values used.
+     *
+     * @return array array with keys 'headers', 'cookies', 'raw_data' and 'status_code'.
      */
     public function httpResponse()
     {
index d681364..f8137a4 100644 (file)
@@ -221,7 +221,7 @@ class Server
      *                    3 = add also all processing warnings happened during method processing
      *                    (NB: this involves setting a custom error handler, and might interfere
      *                    with the standard processing of the php function exposed as method. In
-     *                    particular, triggering an USER_ERROR level error will not halt script
+     *                    particular, triggering a USER_ERROR level error will not halt script
      *                    execution anymore, but just end up logged in the xml-rpc response)
      *                    Note that info added at level 2 and 3 will be base64 encoded
      * @return $this