PageRenderTime 52ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

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

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