5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class HttpClient extends BaseClient implements Client {
26 const HTTP_EOL = "\r\n";
27 const HTTP_USER_AGENT = 'HttpClient-Core/1.0';
30 * Protected constructor
34 protected function __construct () {
35 // Set default user agent string (to allow other classes to override this)
36 $this->setUserAgent(self::HTTP_USER_AGENT);
38 // Call parent constructor
39 parent::__construct(__CLASS__);
43 * Creates an instance of this Client class and prepares it for usage
45 * @param $socketResource Resource of a socket (optional)
46 * @return $clientInstance An instance of a Client class
48 public final static function createHttpClient ($socketResouce = FALSE) {
50 $clientInstance = new HttpClient();
52 // Set socket resource
53 $clientInstance->setSocketResource($socketResource);
55 // Return the prepared instance
56 return $clientInstance;
60 * Checks wether proxy configuration is used
62 * @return $isUsed Wether proxy is used
64 protected function isProxyUsed () {
66 if (!isset($GLOBALS[__METHOD__])) {
68 $GLOBALS[__METHOD__] = (($this->getConfigInstance()->getConfigEntry('proxy_host') != '') && ($this->getConfigInstance()->getConfigEntry('proxy_port') > 0));
72 return $GLOBALS[__METHOD__];
76 * Sets up a proxy tunnel for given hostname and through resource
78 * @param $host Host to connect to
79 * @param $port Port number to connect to
80 * @return $response Response array
82 protected function setupProxyTunnel ($host, $port) {
84 $response = array('', '', '');
87 $respArray = $this->doConnectRequest($host, $port);
89 // Analyze first header line
90 if (((strtolower($respArray[0]) !== 'http/1.0') && (strtolower($respArray[0]) !== 'http/1.1')) || ($respArray[1] != '200')) {
91 // Response code is not 200
100 * Sends a raw HTTP request out to given IP/host and port number
102 * @param $method Request method (GET, POST, HEAD, CONNECT, ...)
103 * @param $host Host to connect to
104 * @param $port Port number to connect to
105 * @return $responseArray Array with raw response
107 private function sendRawHttpRequest ($method, $host, $port, array $header = array()) {
108 // Minimum raw HTTP/1.1 request
109 $rawRequest = $method . ' ' . $host . ':' . $port . ' HTTP/1.1' . self::HTTP_EOL;
110 $rawRequest .= 'Host: ' . $host . ':' . $port . self::HTTP_EOL;
112 // Use login data to proxy? (username at least)
113 if ($this->getConfigInstance()->getConfigEntry('proxy_username') != '') {
115 $encodedAuth = base64_encode($this->getConfigInstance()->getConfigEntry('proxy_username') . ':' . $this->getConfigInstance()->getConfigEntry('proxy_password'));
116 $rawRequest .= 'Proxy-Authorization: Basic ' . $encodedAuth . self::HTTP_EOL;
120 $rawRequest .= self::HTTP_EOL;
121 //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('HTTP-CLIENT[' . __METHOD__ . ':' . __LINE__ . ']: rawRequest=' . $rawRequest);
124 fwrite($this->getSocketResource(), $rawRequest);
127 if (feof($this->getSocketResource())) {
128 // No response received
132 // Read the first line
133 $resp = trim(fgets($this->getSocketResource(), 10240));
135 // "Explode" the string to an array
136 $responseArray = explode(' ', $resp);
139 return $responseArray;
143 * A HTTP/1.1 CONNECT request
145 * @param $host Host to connect to
146 * @param $port Port number to connect to
147 * @return $responseArray An array with the read response
149 public function doConnectRequest ($host, $port) {
150 // Prepare extra header(s)
152 'Proxy-Connection' => 'Keep-Alive'
155 // Prepare raw request
156 $responseArray = $this->sendRawHttpRequest('CONNECT', $host, $port, $headers);
158 // Return response array
159 return $responseArray;