X-Git-Url: https://git.mxchange.org/?p=core.git;a=blobdiff_plain;f=inc%2Fclasses%2Fmain%2Fclient%2Fhttp%2Fclass_HttpClient.php;h=095292437aa14161c3d23cbddf27dd18e8a8f30b;hp=ec4da778a5e1dd9931e98d087b09d41e48c2f47b;hb=87dacf0cd1569f9ef85ce1f61003d3169feec6d2;hpb=849ce32873a9bdd7c9eec70fdae588e025b85651 diff --git a/inc/classes/main/client/http/class_HttpClient.php b/inc/classes/main/client/http/class_HttpClient.php index ec4da778..09529243 100644 --- a/inc/classes/main/client/http/class_HttpClient.php +++ b/inc/classes/main/client/http/class_HttpClient.php @@ -22,12 +22,19 @@ * along with this program. If not, see . */ 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]