PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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