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

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

https://bitbucket.org/lanoversolutions/afterschool
PHP | 272 lines | 131 code | 31 blank | 110 comment | 28 complexity | d2237be8a6748d164f2b614aedf5f0d0 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 20947 2010-02-06 17:09:07Z padraic $
  19. * @copyright Copyright (c) 2005-2010 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-2010 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. '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. // Add the request body
  154. $request .= "\r\n" . $body;
  155. // Send the request
  156. if (! @fwrite($this->socket, $request)) {
  157. require_once 'Zend/Http/Client/Adapter/Exception.php';
  158. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  159. }
  160. return $request;
  161. }
  162. /**
  163. * Preform handshaking with HTTPS proxy using CONNECT method
  164. *
  165. * @param string $host
  166. * @param integer $port
  167. * @param string $http_ver
  168. * @param array $headers
  169. */
  170. protected function connectHandshake($host, $port = 443, $http_ver = '1.1', array &$headers = array())
  171. {
  172. $request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
  173. "Host: " . $this->config['proxy_host'] . "\r\n";
  174. // Add the user-agent header
  175. if (isset($this->config['useragent'])) {
  176. $request .= "User-agent: " . $this->config['useragent'] . "\r\n";
  177. }
  178. // If the proxy-authorization header is set, send it to proxy but remove
  179. // it from headers sent to target host
  180. if (isset($headers['proxy-authorization'])) {
  181. $request .= "Proxy-authorization: " . $headers['proxy-authorization'] . "\r\n";
  182. unset($headers['proxy-authorization']);
  183. }
  184. $request .= "\r\n";
  185. // Send the request
  186. if (! @fwrite($this->socket, $request)) {
  187. require_once 'Zend/Http/Client/Adapter/Exception.php';
  188. throw new Zend_Http_Client_Adapter_Exception("Error writing request to proxy server");
  189. }
  190. // Read response headers only
  191. $response = '';
  192. $gotStatus = false;
  193. while ($line = @fgets($this->socket)) {
  194. $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
  195. if ($gotStatus) {
  196. $response .= $line;
  197. if (!chop($line)) break;
  198. }
  199. }
  200. // Check that the response from the proxy is 200
  201. if (Zend_Http_Response::extractCode($response) != 200) {
  202. require_once 'Zend/Http/Client/Adapter/Exception.php';
  203. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to HTTPS proxy. Server response: " . $response);
  204. }
  205. // If all is good, switch socket to secure mode. We have to fall back
  206. // through the different modes
  207. $modes = array(
  208. STREAM_CRYPTO_METHOD_TLS_CLIENT,
  209. STREAM_CRYPTO_METHOD_SSLv3_CLIENT,
  210. STREAM_CRYPTO_METHOD_SSLv23_CLIENT,
  211. STREAM_CRYPTO_METHOD_SSLv2_CLIENT
  212. );
  213. $success = false;
  214. foreach($modes as $mode) {
  215. $success = stream_socket_enable_crypto($this->socket, true, $mode);
  216. if ($success) break;
  217. }
  218. if (! $success) {
  219. require_once 'Zend/Http/Client/Adapter/Exception.php';
  220. throw new Zend_Http_Client_Adapter_Exception("Unable to connect to" .
  221. " HTTPS server through proxy: could not negotiate secure connection.");
  222. }
  223. }
  224. /**
  225. * Close the connection to the server
  226. *
  227. */
  228. public function close()
  229. {
  230. parent::close();
  231. $this->negotiated = false;
  232. }
  233. /**
  234. * Destructor: make sure the socket is disconnected
  235. *
  236. */
  237. public function __destruct()
  238. {
  239. if ($this->socket) $this->close();
  240. }
  241. }