Proxy.php 8.6 KB

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