Rewrites for client factory, added methods for HttpClient (mostly from ConsoleClient).
authorRoland Haeder <roland@mxchange.org>
Mon, 12 May 2014 20:47:31 +0000 (22:47 +0200)
committerRoland Haeder <roland@mxchange.org>
Mon, 12 May 2014 20:47:31 +0000 (22:47 +0200)
Signed-off-by: Roland H├Ąder <roland@mxchange.org>
inc/classes/main/client/http/class_HttpClient.php
inc/classes/main/factories/client/class_ClientFactory.php
inc/classes/main/factories/objects/class_ObjectFactory.php

index ec4da77..0952924 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class HttpClient extends BaseClient implements Client {
+       // Constants
+       const HTTP_EOL = "\r\n";
+       const HTTP_USER_AGENT = 'HttpClient-Core/1.0';
+
        /**
         * Protected constructor
         *
         * @return      void
         */
        protected function __construct () {
+               // Set default user agent string (to allow other classes to override this)
+               $this->setUserAgent(self::HTTP_USER_AGENT);
+
                // Call parent constructor
                parent::__construct(__CLASS__);
        }
@@ -35,15 +42,122 @@ class HttpClient extends BaseClient implements Client {
        /**
         * Creates an instance of this Client class and prepares it for usage
         *
+        * @param       $socketResource         Resource of a socket (optional)
         * @return      $clientInstance         An instance of a Client class
         */
-       public final static function createHttpClient () {
+       public final static function createHttpClient ($socketResouce = FALSE) {
                // Get a new instance
                $clientInstance = new HttpClient();
 
+               // Set socket resource
+               $clientInstance->setSocketResource($socketResource);
+
                // Return the prepared instance
                return $clientInstance;
        }
+
+       /**
+        * Checks wether proxy configuration is used
+        *
+        * @return      $isUsed         Wether proxy is used
+        */
+       protected function isProxyUsed () {
+               // Do we have cache?
+               if (!isset($GLOBALS[__METHOD__])) {
+                       // Determine it
+                       $GLOBALS[__METHOD__] = (($this->getConfigInstance()->getConfigEntry('proxy_host') != '') && ($this->getConfigInstance()->getConfigEntry('proxy_port') > 0));
+               } // END - if
+
+               // Return cache
+               return $GLOBALS[__METHOD__];
+       }
+
+       /**
+        * Sets up a proxy tunnel for given hostname and through resource
+        *
+        * @param       $host           Host to connect to
+        * @param       $port           Port number to connect to
+        * @return      $response       Response array
+        */
+       protected function setupProxyTunnel ($host, $port) {
+               // Initialize array
+               $response = array('', '', '');
+
+               // Do the connect
+               $respArray = $this->doConnectRequest($host, $port);
+
+               // Analyze first header line
+               if (((strtolower($respArray[0]) !== 'http/1.0') && (strtolower($respArray[0]) !== 'http/1.1')) || ($respArray[1] != '200')) {
+                       // Response code is not 200
+                       return $response;
+               } // END - if
+
+               // All fine!
+               return $respArray;
+       }
+
+       /**
+        * Sends a raw HTTP request out to given host/port
+        *
+        * @param       $method                 Request method (GET, POST, HEAD, CONNECT, ...)
+        * @param       $host                   Host to connect to
+        * @param       $port                   Port number to connect to
+        * @return      $responseArray  Array with raw response
+        */
+       private function sendRawHttpRequest ($method, $host, $port, array $header = array()) {
+               // Minimum raw HTTP/1.1 request
+               $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') != '') {
+                       // Add it as well
+                       $encodedAuth = base64_encode($this->getConfigInstance()->getConfigEntry('proxy_username') . ':' . $this->getConfigInstance()->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);
+
+               // Write request
+               fwrite($this->getSocketResource(), $rawRequest);
+
+               // Got response?
+               if (feof($this->getSocketResource())) {
+                       // No response received
+                       return $response;
+               } // END - if
+
+               // Read the first line
+               $resp = trim(fgets($this->getSocketResource(), 10240));
+
+               // "Explode" the string to an array
+               $responseArray = explode(' ', $resp);
+
+               // And return it
+               return $responseArray;
+       }
+
+       /**
+        * A HTTP/1.1 CONNECT request
+        *
+        * @param       $host   Host to connect to
+        * @param       $port   Port number to connect to
+        * @return      $responseArray  An array with the read response
+        */
+       public function doConnectRequest ($host, $port) {
+               // Prepare extra header(s)
+               $headers = array(
+                       'Proxy-Connection' => 'Keep-Alive'
+               );
+
+               // Prepare raw request
+               $responseArray = $this->sendRawHttpRequest('CONNECT', $host, $port, $headers);
+
+               // Return response array
+               return $responseArray;
+       }
 }
 
 // [EOF]
index a46749b..880f5ce 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * An general object factory
+ * An object factory for clients
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class ObjectFactory extends BaseFactory {
+class ClientFactory extends ObjectFactory {
        /**
         * Protected constructor
         *
-        * @param       $className      Name of this class
         * @return      void
         */
-       protected function __construct ($className = __CLASS__) {
+       protected function __construct () {
                // Call parent constructor
-               parent::__construct($className);
+               parent::__construct(__CLASS__);
        }
 
        /**
-        * Creates a new object given by the name or throws an exception if
-        * the class was not found. No parameters for the object are currently
-        * supported.
+        * Creates a client object for given protocol. This method uses the
+        * registry pattern to cache those instances.
         *
-        * @param       $className                      Name of the class we shall construct
-        * @param       $args                           Arguments in an indexed array
-        * @return      $objectInstance         An instance of the requested object
-        * @throws      NoClassException        If the requested class was not found
+        * @param       $protocol                       Protocol to create a client object for (e.g. 'http' for a HTTP/1.1 client)
+        * @param       $socketResource         A valid socket resource (optional)
+        * @return      $clientInstance         An instance of the requested client
         * @throws      EmptyVariableException  If a variable is empty unexpectly
         */
-       public static final function createObjectByName ($className, array $args=array()) {
-               // First get an instance of this factory
-               $factoryInstance = new ObjectFactory();
-
+       public static final function createClientByProtokol ($protocol, $socketResource = FALSE) {
                // Is the class name valid and is the class there?
-               if (empty($className)) {
+               if (empty($protocol)) {
+                       // Get an instance of this factory
+                       $factoryInstance = new ClientFactory();
+
                        // Throw an exception here
-                       throw new EmptyVariableException(array($factoryInstance, 'className'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
-               } elseif (!class_exists($className)) {
-                       // Then throw an exception
-                       throw new NoClassException(array($factoryInstance, $className), self::EXCEPTION_CLASS_NOT_FOUND);
-               }
+                       throw new EmptyVariableException(array($factoryInstance, 'protocol'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
+               } // END - if
 
-               // Create method name
-               $methodName = sprintf("create%s", $className);
+               // Default is NULL (to initialize variable)
+               $clientInstance = NULL;
 
-               // Run the user function
-               $objectInstance = call_user_func_array(array($className, $methodName), $args);
+               // Generate registry key
+               $registryKey = $protocol . '_client';
 
-               // Count this one up
-               self::countObject($className);
+               // Is the key already in registry?
+               if (Registry::getRegistry()->instanceExists($registryKey)) {
+                       // Then use that instance
+                       $clientInstance = Registry::getRegistry()->getInstance($registryKey);
 
-               // Return the prepared instance
-               return $objectInstance;
-       }
+                       // Set socket resource
+                       $clientInstance->setSocketResource($socketResource);
+               } else {
+                       // Generate object instance
+                       $clientInstance = self::createObjectByConfiguredName($registryKey, array($socketResource));
 
-       /**
-        * Creates an object by it's configured name
-        *
-        * @param       $configEnttry           Configuration entry to read
-        * @param       $args                           Arguments in an indexed array
-        * @return      $objectInstance         An instance of the requested object
-        */
-       public static final function createObjectByConfiguredName ($configEntry, array $args=array()) {
-               // Read the configuration entry
-               $className = FrameworkConfiguration::getSelfInstance()->getConfigEntry($configEntry);
-
-               // Send this to the other factory...
-               $objectInstance = self::createObjectByName($className, $args);
+                       // Set it in registry for later re-use
+                       Registry::getRegistry()->addInstance($registryKey, $clientInstance);
+               }
 
-               // Return the instance
-               return $objectInstance;
+               // Return the prepared instance
+               return $clientInstance;
        }
 }
 
index a46749b..a323b32 100644 (file)
@@ -58,7 +58,7 @@ class ObjectFactory extends BaseFactory {
                }
 
                // Create method name
-               $methodName = sprintf("create%s", $className);
+               $methodName = sprintf('create%s', $className);
 
                // Run the user function
                $objectInstance = call_user_func_array(array($className, $methodName), $args);