]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/client/http/class_HttpClient.php
Continued:
[core.git] / framework / main / classes / client / http / class_HttpClient.php
index 20c4c78786bdddca625d582fa5395be18cf31320..fc955edab11c7442ec9c251b3c689288d78b8267 100644 (file)
@@ -3,15 +3,21 @@
 namespace Org\Mxchange\CoreFramework\Client\Http;
 
 // Import framework stuff
+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
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
  * @version            0.0.0
- * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -38,7 +44,7 @@ class HttpClient extends BaseClient implements Client {
         *
         * @return      void
         */
-       protected function __construct () {
+       private function __construct () {
                // Set default user agent string (to allow other classes to override this)
                $this->setUserAgent(self::HTTP_USER_AGENT);
 
@@ -54,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;
        }
 
@@ -70,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__] = (($this->getConfigInstance()->getConfigEntry('proxy_host') != '') && ($this->getConfigInstance()->getConfigEntry('proxy_port') > 0));
-               } // END - if
+                       $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__];
        }
 
@@ -85,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 ($host, $port) {
+       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 = 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)), FrameworkInterface::EXCEPTION_UNEXPECTED_VALUE);
+               }
 
                // 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;
-               } // END - if
+               }
 
                // All fine!
-               return $respArray;
+               /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('HTTP-CLIENT: responseArray()=%d - EXIT!', count($responseArray)));
+               return $responseArray;
        }
 
        /**
@@ -111,38 +142,45 @@ class HttpClient extends BaseClient implements Client {
         * @param       $port                   Port number to connect to
         * @return      $responseArray  Array with raw response
         */
-       private function sendRawHttpRequest ($method, $host, $port, array $header = array()) {
+       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;
 
                // Use login data to proxy? (username at least)
-               if ($this->getConfigInstance()->getConfigEntry('proxy_username') != '') {
+               if (FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_username') != '') {
                        // Add it as well
-                       $encodedAuth = base64_encode($this->getConfigInstance()->getConfigEntry('proxy_username') . ':' . $this->getConfigInstance()->getConfigEntry('proxy_password'));
+                       $encodedAuth = base64_encode(FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_username') . ':' . FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('proxy_password'));
                        $rawRequest .= 'Proxy-Authorization: Basic ' . $encodedAuth . self::HTTP_EOL;
-               } // END - if
+               }
 
                // Add last new-line
                $rawRequest .= self::HTTP_EOL;
-               //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('HTTP-CLIENT[' . __METHOD__ . ':' . __LINE__ . ']: rawRequest=' . $rawRequest);
+               //* 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;
-               } // END - if
+               }
 
                // 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;
        }
 
@@ -152,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 ($host, $port) {
+       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 = array(
+               $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;
        }