PageRenderTime 46ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/lib/Microsoft/Http/Client/Adapter/Proxy.php

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