PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 343 lines | 180 code | 34 blank | 129 comment | 37 complexity | 6892b7d0c9de3a441adb25d4fbbd22ec 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$
  19. * @copyright Copyright (c) 2005-2015 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-2015 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(
  119. $method, $uri, $http_ver = '1.1', $headers = array(), $body = ''
  120. )
  121. {
  122. // If no proxy is set, fall back to default Socket adapter
  123. if (!$this->config['proxy_host']) {
  124. return parent::write($method, $uri, $http_ver, $headers, $body);
  125. }
  126. // Make sure we're properly connected
  127. if (!$this->socket) {
  128. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  129. throw new Zend_Http_Client_Adapter_Exception(
  130. 'Trying to write but we are not connected'
  131. );
  132. }
  133. $host = $this->config['proxy_host'];
  134. $port = $this->config['proxy_port'];
  135. if ($this->connected_to[0] != "tcp://$host"
  136. || $this->connected_to[1] != $port
  137. ) {
  138. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  139. throw new Zend_Http_Client_Adapter_Exception(
  140. 'Trying to write but we are connected to the wrong proxy server'
  141. );
  142. }
  143. // Add Proxy-Authorization header
  144. if ($this->config['proxy_user']) {
  145. // Check to see if one already exists
  146. $hasProxyAuthHeader = false;
  147. foreach ($headers as $k => $v) {
  148. if ((string) $k == 'proxy-authorization'
  149. || preg_match("/^proxy-authorization:/i", $v)
  150. ) {
  151. $hasProxyAuthHeader = true;
  152. break;
  153. }
  154. }
  155. if (!$hasProxyAuthHeader) {
  156. $headers[] = 'Proxy-authorization: '
  157. . Zend_Http_Client::encodeAuthHeader(
  158. $this->config['proxy_user'],
  159. $this->config['proxy_pass'], $this->config['proxy_auth']
  160. );
  161. }
  162. }
  163. // if we are proxying HTTPS, preform CONNECT handshake with the proxy
  164. if ($uri->getScheme() == 'https' && (!$this->negotiated)) {
  165. $this->connectHandshake(
  166. $uri->getHost(), $uri->getPort(), $http_ver, $headers
  167. );
  168. $this->negotiated = true;
  169. }
  170. // Save request method for later
  171. $this->method = $method;
  172. // Build request headers
  173. if ($this->negotiated) {
  174. $path = $uri->getPath();
  175. if ($uri->getQuery()) {
  176. $path .= '?' . $uri->getQuery();
  177. }
  178. $request = "$method $path HTTP/$http_ver\r\n";
  179. } else {
  180. $request = "$method $uri HTTP/$http_ver\r\n";
  181. }
  182. // Add all headers to the request string
  183. foreach ($headers as $k => $v) {
  184. if (is_string($k)) $v = "$k: $v";
  185. $request .= "$v\r\n";
  186. }
  187. if(is_resource($body)) {
  188. $request .= "\r\n";
  189. } else {
  190. // Add the request body
  191. $request .= "\r\n" . $body;
  192. }
  193. // Send the request
  194. if (!@fwrite($this->socket, $request)) {
  195. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  196. throw new Zend_Http_Client_Adapter_Exception(
  197. 'Error writing request to proxy server'
  198. );
  199. }
  200. if(is_resource($body)) {
  201. if(stream_copy_to_stream($body, $this->socket) == 0) {
  202. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  203. throw new Zend_Http_Client_Adapter_Exception(
  204. 'Error writing request to server'
  205. );
  206. }
  207. }
  208. return $request;
  209. }
  210. /**
  211. * Preform handshaking with HTTPS proxy using CONNECT method
  212. *
  213. * @param string $host
  214. * @param integer $port
  215. * @param string $http_ver
  216. * @param array $headers
  217. * @return void
  218. * @throws Zend_Http_Client_Adapter_Exception
  219. */
  220. protected function connectHandshake(
  221. $host, $port = 443, $http_ver = '1.1', array &$headers = array()
  222. )
  223. {
  224. $request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
  225. "Host: " . $host . "\r\n";
  226. // Process provided headers, including important ones to CONNECT request
  227. foreach ($headers as $k => $v) {
  228. switch (strtolower(substr($v,0,strpos($v,':')))) {
  229. case 'proxy-authorization':
  230. // break intentionally omitted
  231. case 'user-agent':
  232. $request .= $v . "\r\n";
  233. break;
  234. default:
  235. break;
  236. }
  237. }
  238. $request .= "\r\n";
  239. // @see ZF-3189
  240. $this->connectHandshakeRequest = $request;
  241. // Send the request
  242. if (!@fwrite($this->socket, $request)) {
  243. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  244. throw new Zend_Http_Client_Adapter_Exception(
  245. 'Error writing request to proxy server'
  246. );
  247. }
  248. // Read response headers only
  249. $response = '';
  250. $gotStatus = false;
  251. while ($line = @fgets($this->socket)) {
  252. $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
  253. if ($gotStatus) {
  254. $response .= $line;
  255. if (!chop($line)) {
  256. break;
  257. }
  258. }
  259. }
  260. // Check that the response from the proxy is 200
  261. if (Zend_Http_Response::extractCode($response) != 200) {
  262. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  263. throw new Zend_Http_Client_Adapter_Exception(
  264. 'Unable to connect to HTTPS proxy. Server response: ' . $response
  265. );
  266. }
  267. // If all is good, switch socket to secure mode. We have to fall back
  268. // through the different modes
  269. $modes = array(
  270. STREAM_CRYPTO_METHOD_TLS_CLIENT,
  271. STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
  272. STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
  273. STREAM_CRYPTO_METHOD_SSLv2_CLIENT
  274. );
  275. $success = false;
  276. foreach($modes as $mode) {
  277. $success = stream_socket_enable_crypto($this->socket, true, $mode);
  278. if ($success) {
  279. break;
  280. }
  281. }
  282. if (!$success) {
  283. #require_once 'Zend/Http/Client/Adapter/Exception.php';
  284. throw new Zend_Http_Client_Adapter_Exception(
  285. 'Unable to connect to HTTPS server through proxy: could not '
  286. . 'negotiate secure connection.'
  287. );
  288. }
  289. }
  290. /**
  291. * Close the connection to the server
  292. *
  293. */
  294. public function close()
  295. {
  296. parent::close();
  297. $this->negotiated = false;
  298. }
  299. /**
  300. * Destructor: make sure the socket is disconnected
  301. *
  302. */
  303. public function __destruct()
  304. {
  305. if ($this->socket) {
  306. $this->close();
  307. }
  308. }
  309. }