PageRenderTime 61ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/digitalstrategyworks/Reese-WordPress
PHP | 267 lines | 128 code | 30 blank | 109 comment | 27 complexity | c86411be215ac50f283a2c87d9d80ec3 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. /**
  23. * @see Microsoft_Uri_Http
  24. */
  25. require_once 'Microsoft/Uri/Http.php';
  26. /**
  27. * @see Microsoft_Http_Client
  28. */
  29. require_once 'Microsoft/Http/Client.php';
  30. /**
  31. * @see Microsoft_Http_Client_Adapter_Socket
  32. */
  33. require_once 'Microsoft/Http/Client/Adapter/Socket.php';
  34. /**
  35. * HTTP Proxy-supporting Microsoft_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 Microsoft_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 Microsoft
  44. * @package Microsoft_Http
  45. * @subpackage Client_Adapter
  46. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  47. * @license http://framework.zend.com/license/new-bsd New BSD License
  48. */
  49. class Microsoft_Http_Client_Adapter_Proxy extends Microsoft_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. 'proxy_host' => '',
  61. 'proxy_port' => 8080,
  62. 'proxy_user' => '',
  63. 'proxy_pass' => '',
  64. 'proxy_auth' => Microsoft_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. // Connect (a non-secure connection) to the proxy server
  90. return parent::connect(
  91. $this->config['proxy_host'],
  92. $this->config['proxy_port'],
  93. false
  94. );
  95. }
  96. /**
  97. * Send request to the proxy server
  98. *
  99. * @param string $method
  100. * @param Microsoft_Uri_Http $uri
  101. * @param string $http_ver
  102. * @param array $headers
  103. * @param string $body
  104. * @return string Request as string
  105. */
  106. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  107. {
  108. // If no proxy is set, fall back to default Socket adapter
  109. if (! $this->config['proxy_host']) return parent::write($method, $uri, $http_ver, $headers, $body);
  110. // Make sure we're properly connected
  111. if (! $this->socket) {
  112. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  113. throw new Microsoft_Http_Client_Adapter_Exception("Trying to write but we are not connected");
  114. }
  115. $host = $this->config['proxy_host'];
  116. $port = $this->config['proxy_port'];
  117. if ($this->connected_to[0] != "tcp://$host" || $this->connected_to[1] != $port) {
  118. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  119. throw new Microsoft_Http_Client_Adapter_Exception("Trying to write but we are connected to the wrong proxy server");
  120. }
  121. // Add Proxy-Authorization header
  122. if ($this->config['proxy_user'] && ! isset($headers['proxy-authorization'])) {
  123. $headers['proxy-authorization'] = Microsoft_Http_Client::encodeAuthHeader(
  124. $this->config['proxy_user'], $this->config['proxy_pass'], $this->config['proxy_auth']
  125. );
  126. }
  127. // if we are proxying HTTPS, preform CONNECT handshake with the proxy
  128. if ($uri->getScheme() == 'https' && (! $this->negotiated)) {
  129. $this->connectHandshake($uri->getHost(), $uri->getPort(), $http_ver, $headers);
  130. $this->negotiated = true;
  131. }
  132. // Save request method for later
  133. $this->method = $method;
  134. // Build request headers
  135. if ($this->negotiated) {
  136. $path = $uri->getPath();
  137. if ($uri->getQuery()) {
  138. $path .= '?' . $uri->getQuery();
  139. }
  140. $request = "$method $path HTTP/$http_ver\r\n";
  141. } else {
  142. $request = "$method $uri HTTP/$http_ver\r\n";
  143. }
  144. // Add all headers to the request string
  145. foreach ($headers as $k => $v) {
  146. if (is_string($k)) $v = "$k: $v";
  147. $request .= "$v\r\n";
  148. }
  149. // Add the request body
  150. $request .= "\r\n" . $body;
  151. // Send the request
  152. if (! @fwrite($this->socket, $request)) {
  153. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  154. throw new Microsoft_Http_Client_Adapter_Exception("Error writing request to proxy server");
  155. }
  156. return $request;
  157. }
  158. /**
  159. * Preform handshaking with HTTPS proxy using CONNECT method
  160. *
  161. * @param string $host
  162. * @param integer $port
  163. * @param string $http_ver
  164. * @param array $headers
  165. */
  166. protected function connectHandshake($host, $port = 443, $http_ver = '1.1', array &$headers = array())
  167. {
  168. $request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
  169. "Host: " . $this->config['proxy_host'] . "\r\n";
  170. // Add the user-agent header
  171. if (isset($this->config['useragent'])) {
  172. $request .= "User-agent: " . $this->config['useragent'] . "\r\n";
  173. }
  174. // If the proxy-authorization header is set, send it to proxy but remove
  175. // it from headers sent to target host
  176. if (isset($headers['proxy-authorization'])) {
  177. $request .= "Proxy-authorization: " . $headers['proxy-authorization'] . "\r\n";
  178. unset($headers['proxy-authorization']);
  179. }
  180. $request .= "\r\n";
  181. // Send the request
  182. if (! @fwrite($this->socket, $request)) {
  183. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  184. throw new Microsoft_Http_Client_Adapter_Exception("Error writing request to proxy server");
  185. }
  186. // Read response headers only
  187. $response = '';
  188. $gotStatus = false;
  189. while ($line = @fgets($this->socket)) {
  190. $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
  191. if ($gotStatus) {
  192. $response .= $line;
  193. if (!chop($line)) break;
  194. }
  195. }
  196. // Check that the response from the proxy is 200
  197. if (Microsoft_Http_Response::extractCode($response) != 200) {
  198. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  199. throw new Microsoft_Http_Client_Adapter_Exception("Unable to connect to HTTPS proxy. Server response: " . $response);
  200. }
  201. // If all is good, switch socket to secure mode. We have to fall back
  202. // through the different modes
  203. $modes = array(
  204. STREAM_CRYPTO_METHOD_TLS_CLIENT,
  205. STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
  206. STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
  207. STREAM_CRYPTO_METHOD_SSLv2_CLIENT
  208. );
  209. $success = false;
  210. foreach($modes as $mode) {
  211. $success = stream_socket_enable_crypto($this->socket, true, $mode);
  212. if ($success) break;
  213. }
  214. if (! $success) {
  215. require_once 'Microsoft/Http/Client/Adapter/Exception.php';
  216. throw new Microsoft_Http_Client_Adapter_Exception("Unable to connect to" .
  217. " HTTPS server through proxy: could not negotiate secure connection.");
  218. }
  219. }
  220. /**
  221. * Close the connection to the server
  222. *
  223. */
  224. public function close()
  225. {
  226. parent::close();
  227. $this->negotiated = false;
  228. }
  229. /**
  230. * Destructor: make sure the socket is disconnected
  231. *
  232. */
  233. public function __destruct()
  234. {
  235. if ($this->socket) $this->close();
  236. }
  237. }