PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 339 lines | 186 code | 34 blank | 119 comment | 35 complexity | 6a2e6888e730aa27bd85b2726faff50a 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 25261 2013-02-14 12:31:01Z frosch $
  19. * @copyright Copyright (c) 2005-2012 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-2012 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. * Stores the last CONNECT handshake request
  76. *
  77. * @var string
  78. */
  79. protected $connectHandshakeRequest;
  80. /**
  81. * Connect to the remote server
  82. *
  83. * Will try to connect to the proxy server. If no proxy was set, will
  84. * fall back to the target server (behave like regular Socket adapter)
  85. *
  86. * @param string $host
  87. * @param int $port
  88. * @param boolean $secure
  89. */
  90. public function connect($host, $port = 80, $secure = false)
  91. {
  92. // If no proxy is set, fall back to Socket adapter
  93. if (! $this->config['proxy_host']) {
  94. return parent::connect($host, $port, $secure);
  95. }
  96. /* Url might require stream context even if proxy connection doesn't */
  97. if ($secure) {
  98. $this->config['sslusecontext'] = true;
  99. }
  100. // Connect (a non-secure connection) to the proxy server
  101. return parent::connect(
  102. $this->config['proxy_host'],
  103. $this->config['proxy_port'],
  104. false
  105. );
  106. }
  107. /**
  108. * Send request to the proxy server
  109. *
  110. * @param string $method
  111. * @param Zend_Uri_Http $uri
  112. * @param string $http_ver
  113. * @param array $headers
  114. * @param string $body
  115. * @return string Request as string
  116. * @throws Zend_Http_Client_Adapter_Exception
  117. */
  118. public function write($method, $uri, $http_ver = '1.1', $headers = array(), $body = '')
  119. {
  120. // If no proxy is set, fall back to default Socket adapter
  121. if (!$this->config['proxy_host']) {
  122. return parent::write($method, $uri, $http_ver, $headers, $body);
  123. }
  124. // Make sure we're properly connected
  125. if (!$this->socket) {
  126. require_once 'Zend/Http/Client/Adapter/Exception.php';
  127. throw new Zend_Http_Client_Adapter_Exception(
  128. 'Trying to write but we are not connected'
  129. );
  130. }
  131. $host = $this->config['proxy_host'];
  132. $port = $this->config['proxy_port'];
  133. if ($this->connected_to[0] != "tcp://$host" || $this->connected_to[1] != $port) {
  134. require_once 'Zend/Http/Client/Adapter/Exception.php';
  135. throw new Zend_Http_Client_Adapter_Exception(
  136. 'Trying to write but we are connected to the wrong proxy server'
  137. );
  138. }
  139. // Add Proxy-Authorization header
  140. if ($this->config['proxy_user']) {
  141. // Check to see if one already exists
  142. $hasProxyAuthHeader = false;
  143. foreach ($headers as $k => $v) {
  144. if ((string) $k == 'proxy-authorization'
  145. || preg_match("/^proxy-authorization:/i", $v)
  146. ) {
  147. $hasProxyAuthHeader = true;
  148. break;
  149. }
  150. }
  151. if (!$hasProxyAuthHeader) {
  152. $headers[] = 'Proxy-authorization: '
  153. . Zend_Http_Client::encodeAuthHeader(
  154. $this->config['proxy_user'],
  155. $this->config['proxy_pass'], $this->config['proxy_auth']
  156. );
  157. }
  158. }
  159. // if we are proxying HTTPS, preform CONNECT handshake with the proxy
  160. if ($uri->getScheme() == 'https' && (!$this->negotiated)) {
  161. $this->connectHandshake(
  162. $uri->getHost(), $uri->getPort(), $http_ver, $headers
  163. );
  164. $this->negotiated = true;
  165. }
  166. // Save request method for later
  167. $this->method = $method;
  168. // Build request headers
  169. if ($this->negotiated) {
  170. $path = $uri->getPath();
  171. if ($uri->getQuery()) {
  172. $path .= '?' . $uri->getQuery();
  173. }
  174. $request = "$method $path HTTP/$http_ver\r\n";
  175. } else {
  176. $request = "$method $uri HTTP/$http_ver\r\n";
  177. }
  178. // Add all headers to the request string
  179. foreach ($headers as $k => $v) {
  180. if (is_string($k)) $v = "$k: $v";
  181. $request .= "$v\r\n";
  182. }
  183. if(is_resource($body)) {
  184. $request .= "\r\n";
  185. } else {
  186. // Add the request body
  187. $request .= "\r\n" . $body;
  188. }
  189. // Send the request
  190. if (!@fwrite($this->socket, $request)) {
  191. require_once 'Zend/Http/Client/Adapter/Exception.php';
  192. throw new Zend_Http_Client_Adapter_Exception(
  193. 'Error writing request to proxy server'
  194. );
  195. }
  196. if(is_resource($body)) {
  197. if(stream_copy_to_stream($body, $this->socket) == 0) {
  198. require_once 'Zend/Http/Client/Adapter/Exception.php';
  199. throw new Zend_Http_Client_Adapter_Exception(
  200. 'Error writing request to server'
  201. );
  202. }
  203. }
  204. return $request;
  205. }
  206. /**
  207. * Preform handshaking with HTTPS proxy using CONNECT method
  208. *
  209. * @param string $host
  210. * @param integer $port
  211. * @param string $http_ver
  212. * @param array $headers
  213. * @return void
  214. * @throws Zend_Http_Client_Adapter_Exception
  215. */
  216. protected function connectHandshake(
  217. $host, $port = 443, $http_ver = '1.1', array &$headers = array()
  218. )
  219. {
  220. $request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
  221. "Host: " . $this->config['proxy_host'] . "\r\n";
  222. // Process provided headers, including important ones to CONNECT request
  223. foreach ($headers as $k => $v) {
  224. switch (strtolower(substr($v,0,strpos($v,':')))) {
  225. case 'proxy-authorization':
  226. // break intentionally omitted
  227. case 'user-agent':
  228. $request .= $v . "\r\n";
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. $request .= "\r\n";
  235. // @see ZF-3189
  236. $this->connectHandshakeRequest = $request;
  237. // Send the request
  238. if (!@fwrite($this->socket, $request)) {
  239. require_once 'Zend/Http/Client/Adapter/Exception.php';
  240. throw new Zend_Http_Client_Adapter_Exception(
  241. 'Error writing request to proxy server'
  242. );
  243. }
  244. // Read response headers only
  245. $response = '';
  246. $gotStatus = false;
  247. while ($line = @fgets($this->socket)) {
  248. $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
  249. if ($gotStatus) {
  250. $response .= $line;
  251. if (!chop($line)) {
  252. break;
  253. }
  254. }
  255. }
  256. // Check that the response from the proxy is 200
  257. if (Zend_Http_Response::extractCode($response) != 200) {
  258. require_once 'Zend/Http/Client/Adapter/Exception.php';
  259. throw new Zend_Http_Client_Adapter_Exception(
  260. 'Unable to connect to HTTPS proxy. Server response: ' . $response
  261. );
  262. }
  263. // If all is good, switch socket to secure mode. We have to fall back
  264. // through the different modes
  265. $modes = array(
  266. STREAM_CRYPTO_METHOD_TLS_CLIENT,
  267. STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
  268. STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
  269. STREAM_CRYPTO_METHOD_SSLv2_CLIENT
  270. );
  271. $success = false;
  272. foreach($modes as $mode) {
  273. $success = stream_socket_enable_crypto($this->socket, true, $mode);
  274. if ($success) {
  275. break;
  276. }
  277. }
  278. if (false !== $success) {
  279. require_once 'Zend/Http/Client/Adapter/Exception.php';
  280. throw new Zend_Http_Client_Adapter_Exception(
  281. 'Unable to connect to HTTPS server through proxy: could not '
  282. . 'negotiate secure connection.'
  283. );
  284. }
  285. }
  286. /**
  287. * Close the connection to the server
  288. *
  289. */
  290. public function close()
  291. {
  292. parent::close();
  293. $this->negotiated = false;
  294. }
  295. /**
  296. * Destructor: make sure the socket is disconnected
  297. *
  298. */
  299. public function __destruct()
  300. {
  301. if ($this->socket) {
  302. $this->close();
  303. }
  304. }
  305. }