Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Zend_Http_Client_Adapter_Proxy Class Reference
Inheritance diagram for Zend_Http_Client_Adapter_Proxy:
Zend_Http_Client_Adapter_Socket Zend_Http_Client_Adapter_Interface Zend_Http_Client_Adapter_Stream

Public Member Functions

 connect ($host, $port=80, $secure=false)
 
 write ( $method, $uri, $http_ver='1.1', $headers=array(), $body='')
 
 close ()
 
 __destruct ()
 
- Public Member Functions inherited from Zend_Http_Client_Adapter_Socket
 __construct ()
 
 setConfig ($config=array())
 
 getConfig ()
 
 setStreamContext ($context)
 
 getStreamContext ()
 
 connect ($host, $port=80, $secure=false)
 
 write ($method, $uri, $http_ver='1.1', $headers=array(), $body='')
 
 read ()
 
 close ()
 
 setOutputStream ($stream)
 
 __destruct ()
 

Protected Member Functions

 connectHandshake ( $host, $port=443, $http_ver='1.1', array &$headers=array())
 
- Protected Member Functions inherited from Zend_Http_Client_Adapter_Socket
 _checkSocketReadTimeout ()
 

Protected Attributes

 $config
 
 $negotiated = false
 
 $connectHandshakeRequest
 
- Protected Attributes inherited from Zend_Http_Client_Adapter_Socket
 $socket = null
 
 $connected_to = array(null, null)
 
 $out_stream = null
 
 $config
 
 $method = null
 
 $_context = null
 

Detailed Description

Definition at line 52 of file Proxy.php.

Constructor & Destructor Documentation

◆ __destruct()

__destruct ( )

Destructor: make sure the socket is disconnected

Definition at line 335 of file Proxy.php.

336  {
337  if ($this->socket) {
338  $this->close();
339  }
340  }

Member Function Documentation

◆ close()

close ( )

Close the connection to the server

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 325 of file Proxy.php.

326  {
327  parent::close();
328  $this->negotiated = false;
329  }

◆ connect()

connect (   $host,
  $port = 80,
  $secure = false 
)

Connect to the remote server

Will try to connect to the proxy server. If no proxy was set, will fall back to the target server (behave like regular Socket adapter)

Parameters
string$host
int$port
boolean$secure

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 96 of file Proxy.php.

97  {
98  // If no proxy is set, fall back to Socket adapter
99  if (!$this->config['proxy_host']) {
100  return parent::connect($host, $port, $secure);
101  }
102 
103  /* Url might require stream context even if proxy connection doesn't */
104  if ($secure) {
105  $this->config['sslusecontext'] = true;
106  }
107 
108  // Connect (a non-secure connection) to the proxy server
109  return parent::connect(
110  $this->config['proxy_host'],
111  $this->config['proxy_port'],
112  false
113  );
114  }

◆ connectHandshake()

connectHandshake (   $host,
  $port = 443,
  $http_ver = '1.1',
array &  $headers = array() 
)
protected

Preform handshaking with HTTPS proxy using CONNECT method

Parameters
string$host
integer$port
string$http_ver
array$headers
Returns
void
Exceptions
Zend_Http_Client_Adapter_Exception

Definition at line 242 of file Proxy.php.

245  {
246  $request = "CONNECT $host:$port HTTP/$http_ver\r\n" .
247  "Host: " . $host . "\r\n";
248 
249  // Process provided headers, including important ones to CONNECT request
250  foreach ($headers as $k => $v) {
251  switch (strtolower(substr($v,0,strpos($v,':')))) {
252  case 'proxy-authorization':
253  // break intentionally omitted
254 
255  case 'user-agent':
256  $request .= $v . "\r\n";
257  break;
258 
259  default:
260  break;
261  }
262  }
263  $request .= "\r\n";
264 
265  // @see ZF-3189
266  $this->connectHandshakeRequest = $request;
267 
268  // Send the request
269  if (!@fwrite($this->socket, $request)) {
270  #require_once 'Zend/Http/Client/Adapter/Exception.php';
272  'Error writing request to proxy server'
273  );
274  }
275 
276  // Read response headers only
277  $response = '';
278  $gotStatus = false;
279  while ($line = @fgets($this->socket)) {
280  $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
281  if ($gotStatus) {
282  $response .= $line;
283  if (!chop($line)) {
284  break;
285  }
286  }
287  }
288 
289  // Check that the response from the proxy is 200
291  #require_once 'Zend/Http/Client/Adapter/Exception.php';
293  'Unable to connect to HTTPS proxy. Server response: ' . $response
294  );
295  }
296 
297  // If all is good, switch socket to secure mode. We have to fall back
298  // through the different modes
299  $modes = array(
300  // TODO: Add STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT in the future when it is supported by PHP
301  STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT
302  );
303 
304  $success = false;
305  foreach($modes as $mode) {
306  $success = stream_socket_enable_crypto($this->socket, true, $mode);
307  if ($success) {
308  break;
309  }
310  }
311 
312  if (!$success) {
313  #require_once 'Zend/Http/Client/Adapter/Exception.php';
315  'Unable to connect to HTTPS server through proxy: could not '
316  . 'negotiate secure connection.'
317  );
318  }
319  }
$response
Definition: 404.php:11
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
static extractCode($response_str)
Definition: Response.php:449

◆ write()

write (   $method,
  $uri,
  $http_ver = '1.1',
  $headers = array(),
  $body = '' 
)

Send request to the proxy server

Parameters
string$method
Zend_Uri_Http$uri
string$http_ver
array$headers
string$body
Returns
string Request as string
Exceptions
Zend_Http_Client_Adapter_Exception

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 127 of file Proxy.php.

130  {
131  // If no proxy is set, fall back to default Socket adapter
132  if (!$this->config['proxy_host']) {
133  return parent::write($method, $uri, $http_ver, $headers, $body);
134  }
135 
136  // Make sure we're properly connected
137  if (!$this->socket) {
138  #require_once 'Zend/Http/Client/Adapter/Exception.php';
140  'Trying to write but we are not connected'
141  );
142  }
143 
144  $host = $this->config['proxy_host'];
145  $port = $this->config['proxy_port'];
146 
147  if ($this->connected_to[0] != "tcp://$host"
148  || $this->connected_to[1] != $port
149  ) {
150  #require_once 'Zend/Http/Client/Adapter/Exception.php';
152  'Trying to write but we are connected to the wrong proxy server'
153  );
154  }
155 
156  // Add Proxy-Authorization header
157  if ($this->config['proxy_user']) {
158  // Check to see if one already exists
159  $hasProxyAuthHeader = false;
160  foreach ($headers as $k => $v) {
161  if ((string) $k == 'proxy-authorization'
162  || preg_match("/^proxy-authorization:/i", $v)
163  ) {
164  $hasProxyAuthHeader = true;
165  break;
166  }
167  }
168  if (!$hasProxyAuthHeader) {
169  $headers[] = 'Proxy-authorization: '
171  $this->config['proxy_user'],
172  $this->config['proxy_pass'], $this->config['proxy_auth']
173  );
174  }
175  }
176 
177  // if we are proxying HTTPS, preform CONNECT handshake with the proxy
178  if ($uri->getScheme() == 'https' && (!$this->negotiated)) {
179  $this->connectHandshake(
180  $uri->getHost(), $uri->getPort(), $http_ver, $headers
181  );
182  $this->negotiated = true;
183  }
184 
185  // Save request method for later
186  $this->method = $method;
187 
188  // Build request headers
189  if ($this->negotiated) {
190  $path = $uri->getPath();
191  if ($uri->getQuery()) {
192  $path .= '?' . $uri->getQuery();
193  }
194  $request = "$method $path HTTP/$http_ver\r\n";
195  } else {
196  $request = "$method $uri HTTP/$http_ver\r\n";
197  }
198 
199  // Add all headers to the request string
200  foreach ($headers as $k => $v) {
201  if (is_string($k)) $v = "$k: $v";
202  $request .= "$v\r\n";
203  }
204 
205  if(is_resource($body)) {
206  $request .= "\r\n";
207  } else {
208  // Add the request body
209  $request .= "\r\n" . $body;
210  }
211 
212  // Send the request
213  if (!@fwrite($this->socket, $request)) {
214  #require_once 'Zend/Http/Client/Adapter/Exception.php';
216  'Error writing request to proxy server'
217  );
218  }
219 
220  if(is_resource($body)) {
221  if(stream_copy_to_stream($body, $this->socket) == 0) {
222  #require_once 'Zend/Http/Client/Adapter/Exception.php';
224  'Error writing request to server'
225  );
226  }
227  }
228 
229  return $request;
230  }
connectHandshake( $host, $port=443, $http_ver='1.1', array &$headers=array())
Definition: Proxy.php:242
static encodeAuthHeader($user, $password, $type=self::AUTH_BASIC)
Definition: Client.php:1502

Field Documentation

◆ $config

$config
protected
Initial value:
= array(
'ssltransport' => 'ssl',
'sslcert' => null,
'sslpassphrase' => null,
'sslusecontext' => false,
'proxy_host' => '',
'proxy_port' => 8080,
'proxy_user' => '',
'proxy_pass' => '',
'persistent' => false,
)

Definition at line 59 of file Proxy.php.

◆ $connectHandshakeRequest

$connectHandshakeRequest
protected

Definition at line 84 of file Proxy.php.

◆ $negotiated

$negotiated = false
protected

Definition at line 77 of file Proxy.php.


The documentation for this class was generated from the following file: