add a couple of extra checks parsing for non-compliant responses
authorgggeek <giunta.gaetano@gmail.com>
Sun, 14 Apr 2024 19:35:02 +0000 (19:35 +0000)
committergggeek <giunta.gaetano@gmail.com>
Sun, 14 Apr 2024 19:35:02 +0000 (19:35 +0000)
src/Helper/XMLParser.php

index 7676c20..2a2094a 100644 (file)
@@ -162,7 +162,7 @@ class XMLParser
             'isf_reason' => '',
             'value' => null,
             'method' => false, // so we can check later if we got a methodname or not
-            'params' => array(),
+            'params' => false, // so we can check later if we got a params tag or not
             'pt' => array(),
             'rt' => '',
         );
@@ -290,6 +290,11 @@ class XMLParser
         xml_parser_free($parser);
         $this->current_parsing_options = array();
 
+        // BC
+        if ($this->_xh['params'] === false) {
+            $this->_xh['params'] = array();
+        }
+
         return $this->_xh;
     }
 
@@ -419,10 +424,13 @@ class XMLParser
 
             case 'METHODCALL':
             case 'METHODRESPONSE':
-            case 'PARAMS':
                 // valid elements that add little to processing
                 break;
 
+            case 'PARAMS':
+                $this->_xh['params'] = array();
+                break;
+
             case 'METHODNAME':
             case 'NAME':
                 /// @todo we could check for 2 NAME elements inside a MEMBER element
@@ -751,13 +759,28 @@ class XMLParser
                 break;
 
             /// @todo add extra checking:
-            ///       - METHODRESPONSE should contain either a PARAMS with a single PARAM, or a FAULT
             ///       - FAULT should contain a single struct with the 2 expected members (check their name and type)
-            ///       - METHODCALL should contain a methodname
             case 'PARAMS':
             case 'FAULT':
+                break;
+
             case 'METHODCALL':
+                /// @todo should we allow to accept this case via a call to handleParsingError ?
+                if ($this->_xh['method'] === false) {
+                    $this->_xh['isf'] = 2;
+                    $this->_xh['isf_reason'] = "missing METHODNAME element inside METHODCALL";
+                }
+                break;
+
             case 'METHODRESPONSE':
+                /// @todo should we allow to accept these cases via a call to handleParsingError ?
+                if ($this->_xh['isf'] != 1 && $this->_xh['params'] === false) {
+                    $this->_xh['isf'] = 2;
+                    $this->_xh['isf_reason'] = "missing both FAULT and PARAMS elements inside METHODRESPONSE";
+                } elseif ($this->_xh['isf'] == 0 && count($this->_xh['params']) !== 1) {
+                    $this->_xh['isf'] = 2;
+                    $this->_xh['isf_reason'] = "PARAMS element inside METHODRESPONSE should have exactly 1 PARAM";
+                }
                 break;
 
             default: