Proxy.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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']) {
  86. return parent::connect($host, $port, $secure);
  87. }
  88. // Connect (a non-secure connection) to the proxy server
  89. return parent::connect(
  90. $this->config['proxy_host'],
  91. $this->config['proxy_port'],
  92. false
  93. );
  94. }
  95. /**
  96. * Send request to the proxy server
  97. *
  98. * @param string $method
  99. * @param Zend_Uri_Http $uri
  100. * @param string $http_ver
  101. * @param array $headers
  102. * @param string $body
  103. * @return string Request as string
  104. */
  105. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  106. {
  107. // If no proxy is set, fall back to default Socket adapter
  108. if (! $this->config['proxy_host']) return parent::write($method, $uri, $http_ver, $headers, $body);
  109. // Make sure we're properly connected
  110. if (! $this->socket) {
  111. require_once 'Zend/Http/Client/Adapter/Exception.php';
  112. throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are not connected");
  113. }
  114. $host = $this->config['proxy_host'];
  115. $port = $this->config['proxy_port'];
  116. if ($this->connected_to[0] != "tcp://$host" || $this->connected_to[1] != $port) {
  117. require_once 'Zend/Http/Client/Adapter/Exception.php';
  118. throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong proxy server");
  119. }
  120. // Add Proxy-Authorization header
  121. if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) {
  122. $headers['proxy-authorization'] = Zend_Http_Client::encodeAuthHeader(
  123. $this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
  124. );
  125. }
  126. // if we are proxying HTTPS, preform CONNECT handshake with the proxy
  127. if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
  128. $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
  129. $this->negotiated = true;
  130. }
  131. // Save request method for later
  132. $this->method = $method;
  133. // Build request headers
  134. $request = "{$method} {$uri->__toString()} HTTP/{$http_ver}\r\n";
  135. // Add all headers to the request string
  136. foreach ($headers as $k => $v) {
  137. if (is_string($k)) $v = "$k: $v";
  138. $request .= "$v\r\n";
  139. }
  140. // Add the request body
  141. $request .= "\r\n" . $body;
  142. // Send the request
  143. if (! @fwrite($this->socket, $request)) {
  144. require_once 'Zend/Http/Client/Adapter/Exception.php';
  145. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  146. }
  147. return $request;
  148. }
  149. /**
  150. * Preform handshaking with HTTPS proxy using CONNECT method
  151. *
  152. * @param string $host
  153. * @param integer $port
  154. * @param string $http_ver
  155. * @param array $headers
  156. */
  157. protected function connectHandshake($host, $port = 443, $http_ver = '1.1', array &$headers = array())
  158. {
  159. $request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
  160. "Host: " . $this->config['proxy_host'] . "\r\n";
  161. // Add the user-agent header
  162. if (isset($this->config['useragent'])) {
  163. $request .= "User-agent: " . $this->config['useragent'] . "\r\n";
  164. }
  165. // If the proxy-authorization header is set, send it to proxy but remove
  166. // it from headers sent to target host
  167. if (isset($headers['proxy-authorization'])) {
  168. $request .= "Proxy-authorization: " . $headers['proxy-authorization'] . "\r\n";
  169. unset($headers['proxy-authorization']);
  170. }
  171. $request .= "\r\n";
  172. // Send the request
  173. if (! @fwrite($this->socket, $request)) {
  174. require_once 'Zend/Http/Client/Adapter/Exception.php';
  175. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  176. }
  177. // Read response headers only
  178. $response = '';
  179. $gotStatus = false;
  180. while ($line = @fgets($this->socket)) {
  181. $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
  182. if ($gotStatus) {
  183. $response .= $line;
  184. if (!chop($line)) break;
  185. }
  186. }
  187. // Check that the response from the proxy is 200
  188. if (Zend_Http_Response::extractCode($response) != 200) {
  189. require_once 'Zend/Http/Client/Adapter/Exception.php';
  190. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to HTTPS proxy. Server response: " . $response);
  191. }
  192. // If all is good, switch socket to secure mode. We have to fall back
  193. // through the different modes
  194. $modes = array(
  195. STREAM_CRYPTO_METHOD_TLS_CLIENT,
  196. STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
  197. STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
  198. STREAM_CRYPTO_METHOD_SSLv2_CLIENT
  199. );
  200. $success = false;
  201. foreach($modes as $mode) {
  202. $success = stream_socket_enable_crypto($this->socket, true, $mode);
  203. if ($success) break;
  204. }
  205. if (! $success) {
  206. require_once 'Zend/Http/Client/Adapter/Exception.php';
  207. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to" .
  208. " HTTPS server through proxy: could not negotiate secure connection.");
  209. }
  210. }
  211. /**
  212. * Close the connection to the server
  213. *
  214. */
  215. public function close()
  216. {
  217. parent::close();
  218. $this->negotiated = false;
  219. }
  220. /**
  221. * Destructor: make sure the socket is disconnected
  222. *
  223. */
  224. public function __destruct()
  225. {
  226. if ($this->socket) $this->close();
  227. }
  228. }