PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/kervin/kyzstudio
PHP | 284 lines | 132 code | 32 blank | 120 comment | 33 complexity | e87396e6ebcf087e63ab33f4ee3e5132 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 21792 2010-04-08 00:27:06Z stas $
  19. * @copyright Copyright (c) 2005-2010 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-2010 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. * Connect to the remote server
  76. *
  77. * Will try to connect to the proxy server. If no proxy was set, will
  78. * fall back to the target server (behave like regular Socket adapter)
  79. *
  80. * @param string $host
  81. * @param int $port
  82. * @param boolean $secure
  83. */
  84. public function connect($host, $port = 80, $secure = false)
  85. {
  86. // If no proxy is set, fall back to Socket adapter
  87. if (! $this->config['proxy_host']) {
  88. return parent::connect($host, $port, $secure);
  89. }
  90. /* Url might require stream context even if proxy connection doesn't */
  91. if ($secure) {
  92. $this->config['sslusecontext'] = true;
  93. }
  94. // Connect (a non-secure connection) to the proxy server
  95. return parent::connect(
  96. $this->config['proxy_host'],
  97. $this->config['proxy_port'],
  98. false
  99. );
  100. }
  101. /**
  102. * Send request to the proxy server
  103. *
  104. * @param string $method
  105. * @param Zend_Uri_Http $uri
  106. * @param string $http_ver
  107. * @param array $headers
  108. * @param string $body
  109. * @return string Request as string
  110. */
  111. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  112. {
  113. // If no proxy is set, fall back to default Socket adapter
  114. if (! $this->config['proxy_host']) return parent::write($method, $uri, $http_ver, $headers, $body);
  115. // Make sure we're properly connected
  116. if (! $this->socket) {
  117. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  118. throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are not connected");
  119. }
  120. $host = $this->config['proxy_host'];
  121. $port = $this->config['proxy_port'];
  122. if ($this->connected_to[0] != "tcp://$host" || $this->connected_to[1] != $port) {
  123. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  124. throw new Zend_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong proxy server");
  125. }
  126. // Add Proxy-Authorization header
  127. if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) {
  128. $headers['proxy-authorization'] = Zend_Http_Client::encodeAuthHeader(
  129. $this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
  130. );
  131. }
  132. // if we are proxying HTTPS, preform CONNECT handshake with the proxy
  133. if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
  134. $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
  135. $this->negotiated = true;
  136. }
  137. // Save request method for later
  138. $this->method = $method;
  139. // Build request headers
  140. if ($this->negotiated) {
  141. $path = $uri->getPath();
  142. if ($uri->getQuery()) {
  143. $path .= '?' . $uri->getQuery();
  144. }
  145. $request = "$method $path HTTP/$http_ver\r\n";
  146. } else {
  147. $request = "$method $uri HTTP/$http_ver\r\n";
  148. }
  149. // Add all headers to the request string
  150. foreach ($headers as $k => $v) {
  151. if (is_string($k)) $v = "$k: $v";
  152. $request .= "$v\r\n";
  153. }
  154. if(is_resource($body)) {
  155. $request .= "\r\n";
  156. } else {
  157. // Add the request body
  158. $request .= "\r\n" . $body;
  159. }
  160. // Send the request
  161. if (! @fwrite($this->socket, $request)) {
  162. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  163. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  164. }
  165. if(is_resource($body)) {
  166. if(stream_copy_to_stream($body, $this->socket) == 0) {
  167. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  168. throw new Zend_Http_Client_Adapter_Exception('Error writing request to server');
  169. }
  170. }
  171. return $request;
  172. }
  173. /**
  174. * Preform handshaking with HTTPS proxy using CONNECT method
  175. *
  176. * @param string $host
  177. * @param integer $port
  178. * @param string $http_ver
  179. * @param array $headers
  180. */
  181. protected function connectHandshake($host, $port = 443, $http_ver = '1.1', array &$headers = array())
  182. {
  183. $request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
  184. "Host: " . $this->config['proxy_host'] . "\r\n";
  185. // Add the user-agent header
  186. if (isset($this->config['useragent'])) {
  187. $request .= "User-agent: " . $this->config['useragent'] . "\r\n";
  188. }
  189. // If the proxy-authorization header is set, send it to proxy but remove
  190. // it from headers sent to target host
  191. if (isset($headers['proxy-authorization'])) {
  192. $request .= "Proxy-authorization: " . $headers['proxy-authorization'] . "\r\n";
  193. unset($headers['proxy-authorization']);
  194. }
  195. $request .= "\r\n";
  196. // Send the request
  197. if (! @fwrite($this->socket, $request)) {
  198. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  199. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  200. }
  201. // Read response headers only
  202. $response = '';
  203. $gotStatus = false;
  204. while ($line = @fgets($this->socket)) {
  205. $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
  206. if ($gotStatus) {
  207. $response .= $line;
  208. if (!chop($line)) break;
  209. }
  210. }
  211. // Check that the response from the proxy is 200
  212. if (Zend_Http_Response::extractCode($response) != 200) {
  213. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  214. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to HTTPS proxy. Server response: " . $response);
  215. }
  216. // If all is good, switch socket to secure mode. We have to fall back
  217. // through the different modes
  218. $modes = array(
  219. STREAM_CRYPTO_METHOD_TLS_CLIENT,
  220. STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
  221. STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
  222. STREAM_CRYPTO_METHOD_SSLv2_CLIENT
  223. );
  224. $success = false;
  225. foreach($modes as $mode) {
  226. $success = stream_socket_enable_crypto($this->socket, true, $mode);
  227. if ($success) break;
  228. }
  229. if (! $success) {
  230. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  231. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to" .
  232. " HTTPS server through proxy: could not negotiate secure connection.");
  233. }
  234. }
  235. /**
  236. * Close the connection to the server
  237. *
  238. */
  239. public function close()
  240. {
  241. parent::close();
  242. $this->negotiated = false;
  243. }
  244. /**
  245. * Destructor: make sure the socket is disconnected
  246. *
  247. */
  248. public function __destruct()
  249. {
  250. if ($this->socket) $this->close();
  251. }
  252. }