Proxy.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. 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-2009 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. }
  133. // if we are proxying HTTPS, preform CONNECT handshake with the proxy
  134. if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
  135. $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
  136. $this->negotiated = true;
  137. }
  138. // Save request method for later
  139. $this->method = $method;
  140. // Build request headers
  141. $request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
  142. // Add all headers to the request string
  143. foreach ($headers as $k => $v) {
  144. if (is_string($k)) $v = "$k: $v";
  145. $request .= "$v\r\n";
  146. }
  147. // Add the request body
  148. $request .= "\r\n" . $body;
  149. // Send the request
  150. if (! @fwrite($this->socket, $request)) {
  151. require_once 'Zend/Http/Client/Adapter/Exception.php';
  152. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  153. }
  154. return $request;
  155. }
  156. /**
  157. * Preform handshaking with HTTPS proxy using CONNECT method
  158. *
  159. * @param string $host
  160. * @param integer $port
  161. * @param string $http_ver
  162. * @param array $headers
  163. */
  164. protected function connectHandshake($host, $port = 443, $http_ver = '1.1', array &$headers = array())
  165. {
  166. $request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
  167. "Host: " . $this->config['proxy_host'] . "\r\n";
  168. // Add the user-agent header
  169. if (isset($this->config['useragent'])) {
  170. $request .= "User-agent: " . $this->config['useragent'] . "\r\n";
  171. }
  172. // If the proxy-authorization header is set, send it to proxy but remove
  173. // it from headers sent to target host
  174. if (isset($headers['proxy-authorization'])) {
  175. $request .= "Proxy-authorization: " . $headers['proxy-authorization'] . "\r\n";
  176. unset($headers['proxy-authorization']);
  177. }
  178. $request .= "\r\n";
  179. // Send the request
  180. if (! @fwrite($this->socket, $request)) {
  181. require_once 'Zend/Http/Client/Adapter/Exception.php';
  182. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  183. }
  184. // Read response headers only
  185. $response = '';
  186. $gotStatus = false;
  187. while ($line = @fgets($this->socket)) {
  188. $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
  189. if ($gotStatus) {
  190. $response .= $line;
  191. if (!chop($line)) break;
  192. }
  193. }
  194. // Check that the response from the proxy is 200
  195. if (Zend_Http_Response::extractCode($response) != 200) {
  196. require_once 'Zend/Http/Client/Adapter/Exception.php';
  197. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to HTTPS proxy. Server response: " . $response);
  198. }
  199. // If all is good, switch socket to secure mode. We have to fall back
  200. // through the different modes
  201. $modes = array(
  202. STREAM_CRYPTO_METHOD_TLS_CLIENT,
  203. STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
  204. STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
  205. STREAM_CRYPTO_METHOD_SSLv2_CLIENT
  206. );
  207. $success = false;
  208. foreach($modes as $mode) {
  209. $success = stream_socket_enable_crypto($this->socket, true, $mode);
  210. if ($success) break;
  211. }
  212. if (! $success) {
  213. require_once 'Zend/Http/Client/Adapter/Exception.php';
  214. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to" .
  215. " HTTPS server through proxy: could not negotiate secure connection.");
  216. }
  217. }
  218. /**
  219. * Close the connection to the server
  220. *
  221. */
  222. public function close()
  223. {
  224. parent::close();
  225. $this->negotiated = false;
  226. }
  227. /**
  228. * Destructor: make sure the socket is disconnected
  229. *
  230. */
  231. public function __destruct()
  232. {
  233. if ($this->socket) $this->close();
  234. }
  235. }