PageRenderTime 23ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/phpQuery/Zend/Http/Client/Adapter/Proxy.php

https://github.com/wzs/Scrappr
PHP | 268 lines | 129 code | 34 blank | 105 comment | 35 complexity | 4d8e65dbef069716a7086709c222da10 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http
  17. * @subpackage Client_Adapter
  18. * @version $Id: Proxy.php 9868 2008-07-02 06:47:38Z shahar $
  19. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. require_once 'Zend/Uri/Http.php';
  23. require_once 'Zend/Http/Client.php';
  24. require_once 'Zend/Http/Client/Adapter/Socket.php';
  25. /**
  26. * HTTP Proxy-supporting Zend_Http_Client adapter class, based on the default
  27. * socket based adapter.
  28. *
  29. * Should be used if proxy HTTP access is required. If no proxy is set, will
  30. * fall back to Zend_Http_Client_Adapter_Socket behavior. Just like the
  31. * default Socket adapter, this adapter does not require any special extensions
  32. * installed.
  33. *
  34. * @category Zend
  35. * @package Zend_Http
  36. * @subpackage Client_Adapter
  37. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Http_Client_Adapter_Proxy extends Zend_Http_Client_Adapter_Socket
  41. {
  42. /**
  43. * Parameters array
  44. *
  45. * @var array
  46. */
  47. protected $config = array(
  48. 'ssltransport' => 'ssl',
  49. 'proxy_host' => '',
  50. 'proxy_port' => 8080,
  51. 'proxy_user' => '',
  52. 'proxy_pass' => '',
  53. 'proxy_auth' => Zend_Http_Client::AUTH_BASIC,
  54. 'persistent' => false
  55. );
  56. /**
  57. * Whether HTTPS CONNECT was already negotiated with the proxy or not
  58. *
  59. * @var boolean
  60. */
  61. protected $negotiated = false;
  62. /**
  63. * Connect to the remote server
  64. *
  65. * Will try to connect to the proxy server. If no proxy was set, will
  66. * fall back to the target server (behave like regular Socket adapter)
  67. *
  68. * @param string $host
  69. * @param int $port
  70. * @param boolean $secure
  71. * @param int $timeout
  72. */
  73. public function connect($host, $port = 80, $secure = false)
  74. {
  75. // If no proxy is set, fall back to Socket adapter
  76. if (! $this->config['proxy_host']) return parent::connect($host, $port, $secure);
  77. // Go through a proxy - the connection is actually to the proxy server
  78. $host = $this->config['proxy_host'];
  79. $port = $this->config['proxy_port'];
  80. // If we are connected to the wrong proxy, disconnect first
  81. if (($this->connected_to[0] != $host || $this->connected_to[1] != $port)) {
  82. if (is_resource($this->socket)) $this->close();
  83. }
  84. // Now, if we are not connected, connect
  85. if (! is_resource($this->socket) || ! $this->config['keepalive']) {
  86. $this->socket = @fsockopen($host, $port, $errno, $errstr, (int) $this->config['timeout']);
  87. if (! $this->socket) {
  88. $this->close();
  89. require_once 'Zend/Http/Client/Adapter/Exception.php';
  90. throw new Zend_Http_Client_Adapter_Exception(
  91. 'Unable to Connect to proxy server ' . $host . ':' . $port . '. Error #' . $errno . ': ' . $errstr);
  92. }
  93. // Set the stream timeout
  94. if (!stream_set_timeout($this->socket, (int) $this->config['timeout'])) {
  95. require_once 'Zend/Http/Client/Adapter/Exception.php';
  96. throw new Zend_Http_Client_Adapter_Exception('Unable to set the connection timeout');
  97. }
  98. // Update connected_to
  99. $this->connected_to = array($host, $port);
  100. }
  101. }
  102. /**
  103. * Send request to the proxy server
  104. *
  105. * @param string $method
  106. * @param Zend_Uri_Http $uri
  107. * @param string $http_ver
  108. * @param array $headers
  109. * @param string $body
  110. * @return string Request as string
  111. */
  112. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  113. {
  114. // If no proxy is set, fall back to default Socket adapter
  115. if (! $this->config['proxy_host']) return parent::write($method, $uri, $http_ver, $headers, $body);
  116. // Make sure we're properly connected
  117. if (! $this->socket) {
  118. require_once 'Zend/Http/Client/Adapter/Exception.php';
  119. throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are not connected");
  120. }
  121. $host = $this->config['proxy_host'];
  122. $port = $this->config['proxy_port'];
  123. if ($this->connected_to[0] != $host || $this->connected_to[1] != $port) {
  124. require_once 'Zend/Http/Client/Adapter/Exception.php';
  125. throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong proxy server");
  126. }
  127. // Add Proxy-Authorization header
  128. if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization']))
  129. $headers['proxy-authorization'] = Zend_Http_Client::encodeAuthHeader(
  130. $this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
  131. );
  132. // if we are proxying HTTPS, preform CONNECT handshake with the proxy
  133. if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
  134. $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
  135. $this->negotiated = true;
  136. }
  137. // Save request method for later
  138. $this->method = $method;
  139. // Build request headers
  140. $request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
  141. // Add all headers to the request string
  142. foreach ($headers as $k => $v) {
  143. if (is_string($k)) $v = "$k: $v";
  144. $request .= "$v\r\n";
  145. }
  146. // Add the request body
  147. $request .= "\r\n" . $body;
  148. // Send the request
  149. if (! @fwrite($this->socket, $request)) {
  150. require_once 'Zend/Http/Client/Adapter/Exception.php';
  151. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  152. }
  153. return $request;
  154. }
  155. /**
  156. * Preform handshaking with HTTPS proxy using CONNECT method
  157. *
  158. * @param string $host
  159. * @param integer $port
  160. * @param string $http_ver
  161. * @param array $headers
  162. */
  163. protected function connectHandshake($host, $port = 443, $http_ver = '1.1', array &$headers = array())
  164. {
  165. $request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
  166. "Host: " . $this->config['proxy_host'] . "\r\n";
  167. // Add the user-agent header
  168. if (isset($this->config['useragent'])) {
  169. $request .= "User-agent: " . $this->config['useragent'] . "\r\n";
  170. }
  171. // If the proxy-authorization header is set, send it to proxy but remove
  172. // it from headers sent to target host
  173. if (isset($headers['proxy-authorization'])) {
  174. $request .= "Proxy-authorization: " . $headers['proxy-authorization'] . "\r\n";
  175. unset($headers['proxy-authorization']);
  176. }
  177. $request .= "\r\n";
  178. // Send the request
  179. if (! @fwrite($this->socket, $request)) {
  180. require_once 'Zend/Http/Client/Adapter/Exception.php';
  181. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  182. }
  183. // Read response headers only
  184. $response = '';
  185. $gotStatus = false;
  186. while ($line = @fgets($this->socket)) {
  187. $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
  188. if ($gotStatus) {
  189. $response .= $line;
  190. if (!chop($line)) break;
  191. }
  192. }
  193. // Check that the response from the proxy is 200
  194. if (Zend_Http_Response::extractCode($response) != 200) {
  195. require_once 'Zend/Http/Client/Adapter/Exception.php';
  196. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to HTTPS proxy. Server response: " . $response);
  197. }
  198. // If all is good, switch socket to secure mode. We have to fall back
  199. // through the different modes
  200. $modes = array(
  201. STREAM_CRYPTO_METHOD_TLS_CLIENT,
  202. STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
  203. STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
  204. STREAM_CRYPTO_METHOD_SSLv2_CLIENT
  205. );
  206. $success = false;
  207. foreach($modes as $mode) {
  208. $success = stream_socket_enable_crypto($this->socket, true, $mode);
  209. if ($success) break;
  210. }
  211. if (! $success) {
  212. require_once 'Zend/Http/Client/Adapter/Exception.php';
  213. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to" .
  214. " HTTPS server through proxy: could not negotiate secure connection.");
  215. }
  216. }
  217. /**
  218. * Close the connection to the server
  219. *
  220. */
  221. public function close()
  222. {
  223. parent::close();
  224. $this->negotiated = false;
  225. }
  226. /**
  227. * Destructor: make sure the socket is disconnected
  228. *
  229. */
  230. public function __destruct()
  231. {
  232. if ($this->socket) $this->close();
  233. }
  234. }