make server swallow php warnings when not in debug mode
authorgggeek <giunta.gaetano@gmail.com>
Sun, 26 Feb 2023 21:38:44 +0000 (21:38 +0000)
committergggeek <giunta.gaetano@gmail.com>
Sun, 26 Feb 2023 21:38:44 +0000 (21:38 +0000)
NEWS.md
demo/server/server.php
src/Server.php
tests/08ServerTest.php

diff --git a/NEWS.md b/NEWS.md
index ea22992..0e7b797 100644 (file)
--- a/NEWS.md
+++ b/NEWS.md
@@ -1,3 +1,11 @@
+## XML-RPC for PHP version 4.XX - unreleased
+
+* fixed: allow `Server` subclasses to use their own Parser to determine the Request's charset
+
+* fixed: the Server would not swallow and log php warnings generated from end user's method handler functions unless
+  debug mode was set to 2 or higher. It now does that always.
+
+
 ## XML-RPC for PHP version 4.10.1 - 2023/02/22
 
 * fixed: class autoloading got broken in rel 4.10.0 for users of the legacy API (issue #111)
index 893bf9e..9c76461 100644 (file)
@@ -61,6 +61,9 @@ $s->setOption(Server::OPT_DEBUG, 3);
 // Out-of-band information: let the client manipulate the server operations.
 // We do this to help the testsuite script: *** do not reproduce in production or public environments! ***
 if (defined('TESTMODE')) {
+    if (isset($_GET['FORCE_DEBUG'])) {
+        $s->setOption(Server::OPT_DEBUG, $_GET['FORCE_DEBUG']);
+    }
     if (isset($_GET['RESPONSE_ENCODING'])) {
         $s->setOption(Server::OPT_RESPONSE_CHARSET_ENCODING, $_GET['RESPONSE_ENCODING']);
     }
index 8c24d5e..b583a60 100644 (file)
@@ -847,11 +847,11 @@ class Server
             $exception_handling = $this->exception_handling;
         }
 
-        // If debug level is 3, we should catch all errors generated during processing of user function, and log them
-        // as part of response
-        if ($this->debug > 2) {
-            self::$_xmlrpcs_prev_ehandler = set_error_handler(array('\PhpXmlRpc\Server', '_xmlrpcs_errorHandler'));
-        }
+        // We always catch all errors generated during processing of user function, and log them as part of response;
+        // if debug level is 3 or above, we also serialize them in the response as comments
+        self::$_xmlrpcs_prev_ehandler = set_error_handler(array('\PhpXmlRpc\Server', '_xmlrpcs_errorHandler'));
+
+        /// @todo what about using output-buffering as well, in case user code echoes anything to screen?
 
         try {
             // Allow mixed-convention servers
@@ -910,12 +910,11 @@ class Server
             // proper error-response
             switch ($exception_handling) {
                 case 2:
-                    if ($this->debug > 2) {
-                        if (self::$_xmlrpcs_prev_ehandler) {
-                            set_error_handler(self::$_xmlrpcs_prev_ehandler);
-                        } else {
-                            restore_error_handler();
-                        }
+                    if (self::$_xmlrpcs_prev_ehandler) {
+                        set_error_handler(self::$_xmlrpcs_prev_ehandler);
+                        self::$_xmlrpcs_prev_ehandler = null;
+                    } else {
+                        restore_error_handler();
                     }
                     throw $e;
                 case 1:
@@ -933,12 +932,11 @@ class Server
             // proper error-response
             switch ($exception_handling) {
                 case 2:
-                    if ($this->debug > 2) {
-                        if (self::$_xmlrpcs_prev_ehandler) {
-                            set_error_handler(self::$_xmlrpcs_prev_ehandler);
-                        } else {
-                            restore_error_handler();
-                        }
+                    if (self::$_xmlrpcs_prev_ehandler) {
+                        set_error_handler(self::$_xmlrpcs_prev_ehandler);
+                        self::$_xmlrpcs_prev_ehandler = null;
+                    } else {
+                        restore_error_handler();
                     }
                     throw $e;
                 case 1:
@@ -953,14 +951,13 @@ class Server
             }
         }
 
-        if ($this->debug > 2) {
-            // note: restore the error handler we found before calling the user func, even if it has been changed
-            // inside the func itself
-            if (self::$_xmlrpcs_prev_ehandler) {
-                set_error_handler(self::$_xmlrpcs_prev_ehandler);
-            } else {
-                restore_error_handler();
-            }
+        // note: restore the error handler we found before calling the user func, even if it has been changed
+        // inside the func itself
+        if (self::$_xmlrpcs_prev_ehandler) {
+            set_error_handler(self::$_xmlrpcs_prev_ehandler);
+            self::$_xmlrpcs_prev_ehandler = null;
+        } else {
+            restore_error_handler();
         }
 
         return $r;
@@ -974,7 +971,7 @@ class Server
      * @internal
      * @param $methodName
      * @param XMLParser $xmlParser
-     * @param resource $parser
+     * @param null|resource $parser
      * @return void
      * @throws NoSuchMethodException
      *
@@ -982,7 +979,7 @@ class Server
      *       dirtying a lot the logic, as we would have back to both parseRequest() and execute() methods the info
      *       about the matched method handler, in order to avoid doing the work twice...
      */
-    public function methodNameCallback($methodName, $xmlParser, $parser)
+    public function methodNameCallback($methodName, $xmlParser, $parser = null)
     {
         $sysCall = $this->isSyscall($methodName);
         $dmap = $sysCall ? $this->getSystemDispatchMap() : $this->dmap;
index 9571219..074e249 100644 (file)
@@ -597,6 +597,7 @@ And turned it into nylon';
         $m = new xmlrpcmsg('tests.generatePHPWarning', array(
             new xmlrpcval('whatever', 'string'),
         ));
+        $this->addQueryParams(array('FORCE_DEBUG' => 0));
         $v = $this->send($m);
         if ($v) {
             $this->assertEquals(true, $v->scalarval());