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

https://bitbucket.org/simukti/zf1 · PHP · 304 lines · 156 code · 32 blank · 116 comment · 35 complexity · 6e88ece804b0321f2c7680dbe0cd775d 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 24818 2012-05-28 18:49:53Z rob $
  19. * @copyright Copyright (c) 2005-2012 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-2012 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. 'sslcert' => null,
  59. 'sslpassphrase' => null,
  60. 'sslusecontext' => false,
  61. 'proxy_host' => '',
  62. 'proxy_port' => 8080,
  63. 'proxy_user' => '',
  64. 'proxy_pass' => '',
  65. 'proxy_auth' => Zend_Http_Client::AUTH_BASIC,
  66. 'persistent' => false
  67. );
  68. /**
  69. * Whether HTTPS CONNECT was already negotiated with the proxy or not
  70. *
  71. * @var boolean
  72. */
  73. protected $negotiated = false;
  74. /**
  75. * Stores the last CONNECT handshake request
  76. *
  77. * @var string
  78. */
  79. protected $connectHandshakeRequest;
  80. /**
  81. * Connect to the remote server
  82. *
  83. * Will try to connect to the proxy server. If no proxy was set, will
  84. * fall back to the target server (behave like regular Socket adapter)
  85. *
  86. * @param string $host
  87. * @param int $port
  88. * @param boolean $secure
  89. */
  90. public function connect($host, $port = 80, $secure = false)
  91. {
  92. // If no proxy is set, fall back to Socket adapter
  93. if (! $this->config['proxy_host']) {
  94. return parent::connect($host, $port, $secure);
  95. }
  96. /* Url might require stream context even if proxy connection doesn't */
  97. if ($secure) {
  98. $this->config['sslusecontext'] = true;
  99. }
  100. // Connect (a non-secure connection) to the proxy server
  101. return parent::connect(
  102. $this->config['proxy_host'],
  103. $this->config['proxy_port'],
  104. false
  105. );
  106. }
  107. /**
  108. * Send request to the proxy server
  109. *
  110. * @param string $method
  111. * @param Zend_Uri_Http $uri
  112. * @param string $http_ver
  113. * @param array $headers
  114. * @param string $body
  115. * @return string Request as string
  116. */
  117. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  118. {
  119. // If no proxy is set, fall back to default Socket adapter
  120. if (! $this->config['proxy_host']) return parent::write($method, $uri, $http_ver, $headers, $body);
  121. // Make sure we're properly connected
  122. if (! $this->socket) {
  123. require_once 'Zend/Http/Client/Adapter/Exception.php';
  124. throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are not connected");
  125. }
  126. $host = $this->config['proxy_host'];
  127. $port = $this->config['proxy_port'];
  128. if ($this->connected_to[0] != "tcp://$host" || $this->connected_to[1] != $port) {
  129. require_once 'Zend/Http/Client/Adapter/Exception.php';
  130. throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong proxy server");
  131. }
  132. // Add Proxy-Authorization header
  133. if ($this->config['proxy_user']) {
  134. // Check to see if one already exists
  135. $hasProxyAuthHeader = false;
  136. foreach ($headers as $k => $v) {
  137. if ($k == 'proxy-authorization' || preg_match("/^proxy-authorization:/i", $v) ) {
  138. $hasProxyAuthHeader = true;
  139. break;
  140. }
  141. }
  142. if (!$hasProxyAuthHeader) {
  143. $headers[] = 'Proxy-authorization: ' . Zend_Http_Client::encodeAuthHeader(
  144. $this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
  145. );
  146. }
  147. }
  148. // if we are proxying HTTPS, preform CONNECT handshake with the proxy
  149. if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
  150. $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
  151. $this->negotiated = true;
  152. }
  153. // Save request method for later
  154. $this->method = $method;
  155. // Build request headers
  156. if ($this->negotiated) {
  157. $path = $uri->getPath();
  158. if ($uri->getQuery()) {
  159. $path .= '?' . $uri->getQuery();
  160. }
  161. $request = "$method $path HTTP/$http_ver\r\n";
  162. } else {
  163. $request = "$method $uri HTTP/$http_ver\r\n";
  164. }
  165. // Add all headers to the request string
  166. foreach ($headers as $k => $v) {
  167. if (is_string($k)) $v = "$k: $v";
  168. $request .= "$v\r\n";
  169. }
  170. if(is_resource($body)) {
  171. $request .= "\r\n";
  172. } else {
  173. // Add the request body
  174. $request .= "\r\n" . $body;
  175. }
  176. // Send the request
  177. if (! @fwrite($this->socket, $request)) {
  178. require_once 'Zend/Http/Client/Adapter/Exception.php';
  179. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  180. }
  181. if(is_resource($body)) {
  182. if(stream_copy_to_stream($body, $this->socket) == 0) {
  183. require_once 'Zend/Http/Client/Adapter/Exception.php';
  184. throw new Zend_Http_Client_Adapter_Exception('Error writing request to server');
  185. }
  186. }
  187. return $request;
  188. }
  189. /**
  190. * Preform handshaking with HTTPS proxy using CONNECT method
  191. *
  192. * @param string $host
  193. * @param integer $port
  194. * @param string $http_ver
  195. * @param array $headers
  196. */
  197. protected function connectHandshake($host, $port = 443, $http_ver = '1.1', array &$headers = array())
  198. {
  199. $request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
  200. "Host: " . $this->config['proxy_host'] . "\r\n";
  201. // Process provided headers, including important ones to CONNECT request
  202. foreach ( $headers as $k=>$v ) {
  203. switch ( strtolower(substr($v,0,strpos($v,':'))) ) {
  204. case 'proxy-authorization':
  205. // break intentionally omitted
  206. case 'user-agent':
  207. $request .= $v . "\r\n";
  208. break;
  209. default:
  210. break;
  211. }
  212. }
  213. $request .= "\r\n";
  214. // @see ZF-3189
  215. $this->connectHandshakeRequest = $request;
  216. // Send the request
  217. if (! @fwrite($this->socket, $request)) {
  218. require_once 'Zend/Http/Client/Adapter/Exception.php';
  219. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  220. }
  221. // Read response headers only
  222. $response = '';
  223. $gotStatus = false;
  224. while ($line = @fgets($this->socket)) {
  225. $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
  226. if ($gotStatus) {
  227. $response .= $line;
  228. if (!chop($line)) break;
  229. }
  230. }
  231. // Check that the response from the proxy is 200
  232. if (Zend_Http_Response::extractCode($response) != 200) {
  233. require_once 'Zend/Http/Client/Adapter/Exception.php';
  234. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to HTTPS proxy. Server response: " . $response);
  235. }
  236. // If all is good, switch socket to secure mode. We have to fall back
  237. // through the different modes
  238. $modes = array(
  239. STREAM_CRYPTO_METHOD_TLS_CLIENT,
  240. STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
  241. STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
  242. STREAM_CRYPTO_METHOD_SSLv2_CLIENT
  243. );
  244. $success = false;
  245. foreach($modes as $mode) {
  246. $success = stream_socket_enable_crypto($this->socket, true, $mode);
  247. if ($success) break;
  248. }
  249. if (! $success) {
  250. require_once 'Zend/Http/Client/Adapter/Exception.php';
  251. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to" .
  252. " HTTPS server through proxy: could not negotiate secure connection.");
  253. }
  254. }
  255. /**
  256. * Close the connection to the server
  257. *
  258. */
  259. public function close()
  260. {
  261. parent::close();
  262. $this->negotiated = false;
  263. }
  264. /**
  265. * Destructor: make sure the socket is disconnected
  266. *
  267. */
  268. public function __destruct()
  269. {
  270. if ($this->socket) $this->close();
  271. }
  272. }