]> git.mxchange.org Git - core.git/commitdiff
Continued:
authorRoland Häder <roland@mxchange.org>
Fri, 17 Feb 2023 21:03:03 +0000 (22:03 +0100)
committerRoland Häder <roland@mxchange.org>
Fri, 17 Feb 2023 21:03:03 +0000 (22:03 +0100)
- added more debug lines
- added checks on parameter

framework/main/classes/client/http/class_HttpClient.php
framework/main/classes/commands/class_BaseCommand.php

index f8a68c76a855ef15d0478a546a2e16256930b292..f05acd99d07c9fa9eec69342ec7ef74c830ecb25 100644 (file)
@@ -6,6 +6,11 @@ namespace Org\Mxchange\CoreFramework\Client\Http;
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
 use Org\Mxchange\CoreFramework\Client\BaseClient;
 use Org\Mxchange\CoreFramework\Client\Client;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
+
+// Import SPL stuff
+use \InvalidArgumentException;
+use \UnexpectedValueException;
 
 /**
  * A HTTP client class
@@ -55,12 +60,14 @@ class HttpClient extends BaseClient implements Client {
         */
        public final static function createHttpClient ($socketResouce = FALSE) {
                // Get a new instance
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: socketResource[%s]=%s - CALLED!', gettype($socketResource), $socketResource));
                $clientInstance = new HttpClient();
 
                // Set socket resource
                $clientInstance->setSocketResource($socketResource);
 
                // Return the prepared instance
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: clientInstance=%s - EXIT!', $clientInstance->__toString()));
                return $clientInstance;
        }
 
@@ -71,12 +78,14 @@ class HttpClient extends BaseClient implements Client {
         */
        protected function isProxyUsed () {
                // Do we have cache?
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HTTP-CLIENT: CALLED!');
                if (!isset($GLOBALS[__METHOD__])) {
                        // Determine it
                        $GLOBALS[__METHOD__] = ((FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_host') != '') && (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_port') > 0));
                }
 
                // Return cache
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: isProxyUsed=%d - EXIT!', $GLOBALS[__METHOD__]));
                return $GLOBALS[__METHOD__];
        }
 
@@ -86,22 +95,43 @@ class HttpClient extends BaseClient implements Client {
         * @param       $host           Host to connect to
         * @param       $port           Port number to connect to
         * @return      $response       Response array
+        * @throws      InvalidArgumentException        If a paramter has an invalid value
+        * @throws      UnexpectedValueException        If an unexpected value was found
         */
        protected function setupProxyTunnel (string $host, int $port) {
+               // Check paramters
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: host=%s,port=%d - CALLED!', $host, $port));
+               if (empty($host)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "host" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               } elseif ($port < 1) {
+                       // Throw IAE
+                       throw new InvalidArgumentException(sprintf('port=%d is not a valid port number', $port), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               }
+
                // Initialize array
                $response = ['', '', ''];
 
                // Do the connect
-               $respArray = $this->doConnectRequest($host, $port);
+               $responseArray = $this->doConnectRequest($host, $port);
+
+               // Check array
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: responseArray()=%d', count($responseArray)));
+               if (count($responseArray) < 2) {
+                       // Not expected count
+                       throw new UnexpectedValueException(sprintf('responseArray()=%d must have at least two elements', count($responseArray)));
+               }
 
                // Analyze first header line
-               if (((strtolower($respArray[0]) !== 'http/1.0') && (strtolower($respArray[0]) !== 'http/1.1')) || ($respArray[1] != '200')) {
+               if (((strtolower($responseArray[0]) !== 'http/1.0') && (strtolower($responseArray[0]) !== 'http/1.1')) || ($responseArray[1] != '200')) {
                        // Response code is not 200
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HTTP-CLIENT: Returning empty response array - EXIT!');
                        return $response;
                }
 
                // All fine!
-               return $respArray;
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: responseArray()=%d - EXIT!', count($responseArray)));
+               return $responseArray;
        }
 
        /**
@@ -114,6 +144,7 @@ class HttpClient extends BaseClient implements Client {
         */
        private function sendRawHttpRequest (string $method, string $host, int $port, array $header = []) {
                // Minimum raw HTTP/1.1 request
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: method=%s,host=%s,port=%d,header()=%d - CALLED!', $method, $host, $port, count($header)));
                $rawRequest  = $method . ' ' . $host . ':' . $port . ' HTTP/1.1' . self::HTTP_EOL;
                $rawRequest .= 'Host: ' . $host . ':' . $port . self::HTTP_EOL;
 
@@ -129,21 +160,27 @@ class HttpClient extends BaseClient implements Client {
                //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('HTTP-CLIENT: rawRequest=' . $rawRequest);
 
                // Write request
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: Sending %d bytes to this->socketResource=%s ...', strlen($rawRequest), $this->getSocketResource()));
                fwrite($this->getSocketResource(), $rawRequest);
 
                // Got response?
-               if (feof($this->getSocketResource())) {
+               $feof = feof($this->getSocketResource());
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: this->socketResource=%s,feof=%d', $this->getSocketResource(), intval($feof)));
+               if ($feof) {
                        // No response received
+                       /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: this->socketResource=%s has reached EOF - EXIT!', $this->getSocketResource()));
                        return $response;
                }
 
                // Read the first line
-               $resp = trim(fgets($this->getSocketResource(), 10240));
+               $rawResponse = trim(fgets($this->getSocketResource(), 10240));
 
                // "Explode" the string to an array
-               $responseArray = explode(' ', $resp);
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: Received %d bytes back from this->socketResource=%s ...', strlen($rawResponse), $this->getSocketResource()));
+               $responseArray = explode(' ', $rawResponse);
 
                // And return it
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: responseArray()=%d - EXIT!', count($responseArray)));
                return $responseArray;
        }
 
@@ -153,17 +190,30 @@ class HttpClient extends BaseClient implements Client {
         * @param       $host   Host to connect to
         * @param       $port   Port number to connect to
         * @return      $responseArray  An array with the read response
+        * @throws      InvalidArgumentException        If a paramter has an invalid value
         */
        public function doConnectRequest (string $host, int $port) {
+               // Check paramters
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: host=%s,port=%d - CALLED!', $host, $port));
+               if (empty($host)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "host" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               } elseif ($port < 1) {
+                       // Throw IAE
+                       throw new InvalidArgumentException(sprintf('port=%d is not a valid port number', $port), FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               }
+
                // Prepare extra header(s)
                $headers = [
                        'Proxy-Connection' => 'Keep-Alive'
                ];
 
                // Prepare raw request
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: Invoking this->sendRawHttpRequest(CONNECT,%s,%d,headers()=%d) ...', $host, $port, count($headers)));
                $responseArray = $this->sendRawHttpRequest('CONNECT', $host, $port, $headers);
 
                // Return response array
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: responseArray()=%d - EXIT!', count($responseArray)));
                return $responseArray;
        }
 
index dc94d64a8d2ceaf31a44767aea4427e1f05e5ed9..39065b4d57f311f2db3795ccdf50f203559b29f4 100644 (file)
@@ -6,6 +6,7 @@ namespace Org\Mxchange\CoreFramework\Command;
 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
 use Org\Mxchange\CoreFramework\Helper\Application\ApplicationHelper;
+use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
 use Org\Mxchange\CoreFramework\Request\Requestable;
@@ -13,6 +14,9 @@ use Org\Mxchange\CoreFramework\Response\Responseable;
 use Org\Mxchange\CoreFramework\Traits\Resolver\ResolverTrait;
 use Org\Mxchange\CoreFramework\Traits\Template\CompileableTemplateTrait;
 
+// Import SPL stuff
+use \InvalidArgumentException;
+
 /**
  * A general (base) command
  *
@@ -58,11 +62,21 @@ abstract class BaseCommand extends BaseFrameworkSystem {
         * @return      void
         */
        public final function initTemplateEngine (string $templateType) {
+               // Check paramter
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-COMMAND: templateType=%s - CALLED!', $templateType));
+               if (empty($templateType)) {
+                       // Throw IAE
+                       throw new InvalidArgumentException('Parameter "templateType" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
+               }
+
                // Prepare a template instance
                $templateInstance = ObjectFactory::createObjectByConfiguredName(sprintf('%s_template_class', $templateType));
 
                // Set it here
                $this->setTemplateInstance($templateInstance);
+
+               // Trace message
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-COMMAND: EXIT!');
        }
 
        /**