Proxy.php 9.5 KB

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