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_Socket Class Reference
Inheritance diagram for Zend_Http_Client_Adapter_Socket:
Zend_Http_Client_Adapter_Interface Zend_Http_Client_Adapter_Stream Zend_Http_Client_Adapter_Proxy

Public Member Functions

 __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

 _checkSocketReadTimeout ()
 

Protected Attributes

 $socket = null
 
 $connected_to = array(null, null)
 
 $out_stream = null
 
 $config
 
 $method = null
 
 $_context = null
 

Detailed Description

Definition at line 47 of file Socket.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( )

Adapter constructor, currently empty. Config is set using setConfig()

Definition at line 101 of file Socket.php.

102  {
103  }

◆ __destruct()

__destruct ( )

Destructor: make sure the socket is disconnected

If we are in persistent TCP mode, will not close the connection

Definition at line 537 of file Socket.php.

538  {
539  if (! $this->config['persistent']) {
540  if ($this->socket) $this->close();
541  }
542  }

Member Function Documentation

◆ _checkSocketReadTimeout()

_checkSocketReadTimeout ( )
protected

Check if the socket has timed out - if so close connection and throw an exception

Exceptions
Zend_Http_Client_Adapter_Exceptionwith READ_TIMEOUT code

Definition at line 503 of file Socket.php.

504  {
505  if ($this->socket) {
506  $info = stream_get_meta_data($this->socket);
507  $timedout = $info['timed_out'];
508  if ($timedout) {
509  $this->close();
510  #require_once 'Zend/Http/Client/Adapter/Exception.php';
512  "Read timed out after {$this->config['timeout']} seconds",
514  );
515  }
516  }
517  }
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52

◆ close()

close ( )

Close the connection to the server

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 490 of file Socket.php.

491  {
492  if (is_resource($this->socket)) @fclose($this->socket);
493  $this->socket = null;
494  $this->connected_to = array(null, null);
495  }

◆ connect()

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

Connect to the remote server

Parameters
string$host
int$port
boolean$secure

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 192 of file Socket.php.

193  {
194  // If the URI should be accessed via SSL, prepend the Hostname with ssl://
195  $host = ($secure ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
196 
197  // If we are connected to the wrong host, disconnect first
198  if (($this->connected_to[0] != $host || $this->connected_to[1] != $port)) {
199  if (is_resource($this->socket)) $this->close();
200  }
201 
202  // Now, if we are not connected, connect
203  if (! is_resource($this->socket) || ! $this->config['keepalive']) {
204  $context = $this->getStreamContext();
205  if ($secure || $this->config['sslusecontext']) {
206  if ($this->config['sslcert'] !== null) {
207  if (! stream_context_set_option($context, 'ssl', 'local_cert',
208  $this->config['sslcert'])) {
209  #require_once 'Zend/Http/Client/Adapter/Exception.php';
210  throw new Zend_Http_Client_Adapter_Exception('Unable to set sslcert option');
211  }
212  }
213  if ($this->config['sslpassphrase'] !== null) {
214  if (! stream_context_set_option($context, 'ssl', 'passphrase',
215  $this->config['sslpassphrase'])) {
216  #require_once 'Zend/Http/Client/Adapter/Exception.php';
217  throw new Zend_Http_Client_Adapter_Exception('Unable to set sslpassphrase option');
218  }
219  }
220  }
221 
222  $flags = STREAM_CLIENT_CONNECT;
223  if ($this->config['persistent']) $flags |= STREAM_CLIENT_PERSISTENT;
224 
225  $this->socket = @stream_socket_client($host . ':' . $port,
226  $errno,
227  $errstr,
228  (int) $this->config['timeout'],
229  $flags,
230  $context);
231 
232  if (! $this->socket) {
233  $this->close();
234  #require_once 'Zend/Http/Client/Adapter/Exception.php';
236  'Unable to Connect to ' . $host . ':' . $port . '. Error #' . $errno . ': ' . $errstr);
237  }
238 
239  // Set the stream timeout
240  if (! stream_set_timeout($this->socket, (int) $this->config['timeout'])) {
241  #require_once 'Zend/Http/Client/Adapter/Exception.php';
242  throw new Zend_Http_Client_Adapter_Exception('Unable to set the connection timeout');
243  }
244 
245  // Update connected_to
246  $this->connected_to = array($host, $port);
247  }
248  }

◆ getConfig()

getConfig ( )

Retrieve the array of all configuration options

Returns
array

Definition at line 132 of file Socket.php.

133  {
134  return $this->config;
135  }

◆ getStreamContext()

getStreamContext ( )

Get the stream context for the TCP connection to the server.

If no stream context is set, will create a default one.

Returns
resource

Definition at line 176 of file Socket.php.

177  {
178  if (! $this->_context) {
179  $this->_context = stream_context_create();
180  }
181 
182  return $this->_context;
183  }

◆ read()

read ( )

Read response from server

Returns
string

Responses to HEAD requests and 204 or 304 responses are not expected to have a body - stop reading here

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 315 of file Socket.php.

316  {
317  // First, read headers only
318  $response = '';
319  $gotStatus = false;
320 
321  while (($line = @fgets($this->socket)) !== false) {
322  $gotStatus = $gotStatus || (strpos($line, 'HTTP') !== false);
323  if ($gotStatus) {
324  $response .= $line;
325  if (rtrim($line) === '') break;
326  }
327  }
328 
329  $this->_checkSocketReadTimeout();
330 
332 
333  // Handle 100 and 101 responses internally by restarting the read again
334  if ($statusCode == 100 || $statusCode == 101) return $this->read();
335 
336  // Check headers to see what kind of connection / transfer encoding we have
338 
343  if ($statusCode == 304 || $statusCode == 204 ||
344  $this->method == Zend_Http_Client::HEAD) {
345 
346  // Close the connection if requested to do so by the server
347  if (isset($headers['connection']) && $headers['connection'] == 'close') {
348  $this->close();
349  }
350  return $response;
351  }
352 
353  // If we got a 'transfer-encoding: chunked' header
354  if (isset($headers['transfer-encoding'])) {
355 
356  if (strtolower($headers['transfer-encoding']) == 'chunked') {
357 
358  do {
359  $line = @fgets($this->socket);
360  $this->_checkSocketReadTimeout();
361 
362  $chunk = $line;
363 
364  // Figure out the next chunk size
365  $chunksize = trim($line);
366  if (! ctype_xdigit($chunksize)) {
367  $this->close();
368  #require_once 'Zend/Http/Client/Adapter/Exception.php';
369  throw new Zend_Http_Client_Adapter_Exception('Invalid chunk size "' .
370  $chunksize . '" unable to read chunked body');
371  }
372 
373  // Convert the hexadecimal value to plain integer
374  $chunksize = hexdec($chunksize);
375 
376  // Read next chunk
377  $read_to = ftell($this->socket) + $chunksize;
378 
379  do {
380  $current_pos = ftell($this->socket);
381  if ($current_pos >= $read_to) break;
382 
383  if($this->out_stream) {
384  if(stream_copy_to_stream($this->socket, $this->out_stream, $read_to - $current_pos) == 0) {
385  $this->_checkSocketReadTimeout();
386  break;
387  }
388  } else {
389  $line = @fread($this->socket, $read_to - $current_pos);
390  if ($line === false || strlen($line) === 0) {
391  $this->_checkSocketReadTimeout();
392  break;
393  }
394  $chunk .= $line;
395  }
396  } while (! feof($this->socket));
397 
398  $chunk .= @fgets($this->socket);
399  $this->_checkSocketReadTimeout();
400 
401  if(!$this->out_stream) {
402  $response .= $chunk;
403  }
404  } while ($chunksize > 0);
405  } else {
406  $this->close();
407  #require_once 'Zend/Http/Client/Adapter/Exception.php';
408  throw new Zend_Http_Client_Adapter_Exception('Cannot handle "' .
409  $headers['transfer-encoding'] . '" transfer encoding');
410  }
411 
412  // We automatically decode chunked-messages when writing to a stream
413  // this means we have to disallow the Zend_Http_Response to do it again
414  if ($this->out_stream) {
415  $response = str_ireplace("Transfer-Encoding: chunked\r\n", '', $response);
416  }
417  // Else, if we got the content-length header, read this number of bytes
418  } elseif (isset($headers['content-length'])) {
419 
420  // If we got more than one Content-Length header (see ZF-9404) use
421  // the last value sent
422  if (is_array($headers['content-length'])) {
423  $contentLength = $headers['content-length'][count($headers['content-length']) - 1];
424  } else {
425  $contentLength = $headers['content-length'];
426  }
427 
428  $current_pos = ftell($this->socket);
429  $chunk = '';
430 
431  for ($read_to = $current_pos + $contentLength;
432  $read_to > $current_pos;
433  $current_pos = ftell($this->socket)) {
434 
435  if($this->out_stream) {
436  if(@stream_copy_to_stream($this->socket, $this->out_stream, $read_to - $current_pos) == 0) {
437  $this->_checkSocketReadTimeout();
438  break;
439  }
440  } else {
441  $chunk = @fread($this->socket, $read_to - $current_pos);
442  if ($chunk === false || strlen($chunk) === 0) {
443  $this->_checkSocketReadTimeout();
444  break;
445  }
446 
447  $response .= $chunk;
448  }
449 
450  // Break if the connection ended prematurely
451  if (feof($this->socket)) break;
452  }
453 
454  // Fallback: just read the response until EOF
455  } else {
456 
457  do {
458  if($this->out_stream) {
459  if(@stream_copy_to_stream($this->socket, $this->out_stream) == 0) {
460  $this->_checkSocketReadTimeout();
461  break;
462  }
463  } else {
464  $buff = @fread($this->socket, 8192);
465  if ($buff === false || strlen($buff) === 0) {
466  $this->_checkSocketReadTimeout();
467  break;
468  } else {
469  $response .= $buff;
470  }
471  }
472 
473  } while (feof($this->socket) === false);
474 
475  $this->close();
476  }
477 
478  // Close the connection if requested to do so by the server
479  if (isset($headers['connection']) && $headers['connection'] == 'close') {
480  $this->close();
481  }
482 
483  return $response;
484  }
static extractHeaders($response_str)
Definition: Response.php:500
$response
Definition: 404.php:11
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static extractCode($response_str)
Definition: Response.php:449

◆ setConfig()

setConfig (   $config = array())

Set the configuration array for the adapter

Parameters
Zend_Config  |  array$config

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 110 of file Socket.php.

111  {
112  if ($config instanceof Zend_Config) {
113  $config = $config->toArray();
114 
115  } elseif (! is_array($config)) {
116  #require_once 'Zend/Http/Client/Adapter/Exception.php';
118  'Array or Zend_Config object expected, got ' . gettype($config)
119  );
120  }
121 
122  foreach ($config as $k => $v) {
123  $this->config[strtolower($k)] = $v;
124  }
125  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ setOutputStream()

setOutputStream (   $stream)

Set output stream for the response

Parameters
resource$stream
Returns
Zend_Http_Client_Adapter_Socket

Implements Zend_Http_Client_Adapter_Stream.

Definition at line 525 of file Socket.php.

526  {
527  $this->out_stream = $stream;
528  return $this;
529  }

◆ setStreamContext()

setStreamContext (   $context)

Set the stream context for the TCP connection to the server

Can accept either a pre-existing stream context resource, or an array of stream options, similar to the options array passed to the stream_context_create() PHP function. In such case a new stream context will be created using the passed options.

Since
Zend Framework 1.9
Parameters
mixed$contextStream context or array of context options
Returns
Zend_Http_Client_Adapter_Socket

Definition at line 150 of file Socket.php.

151  {
152  if (is_resource($context) && get_resource_type($context) == 'stream-context') {
153  $this->_context = $context;
154 
155  } elseif (is_array($context)) {
156  $this->_context = stream_context_create($context);
157 
158  } else {
159  // Invalid parameter
160  #require_once 'Zend/Http/Client/Adapter/Exception.php';
162  "Expecting either a stream context resource or array, got " . gettype($context)
163  );
164  }
165 
166  return $this;
167  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ write()

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

Send request to the remote server

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

Implements Zend_Http_Client_Adapter_Interface.

Definition at line 260 of file Socket.php.

261  {
262  // Make sure we're properly connected
263  if (! $this->socket) {
264  #require_once 'Zend/Http/Client/Adapter/Exception.php';
265  throw new Zend_Http_Client_Adapter_Exception('Trying to write but we are not connected');
266  }
267 
268  $host = $uri->getHost();
269  $host = (strtolower($uri->getScheme()) == 'https' ? $this->config['ssltransport'] : 'tcp') . '://' . $host;
270  if ($this->connected_to[0] != $host || $this->connected_to[1] != $uri->getPort()) {
271  #require_once 'Zend/Http/Client/Adapter/Exception.php';
272  throw new Zend_Http_Client_Adapter_Exception('Trying to write but we are connected to the wrong host');
273  }
274 
275  // Save request method for later
276  $this->method = $method;
277 
278  // Build request headers
279  $path = $uri->getPath();
280  if ($uri->getQuery()) $path .= '?' . $uri->getQuery();
281  $request = "{$method} {$path} HTTP/{$http_ver}\r\n";
282  foreach ($headers as $k => $v) {
283  if (is_string($k)) $v = ucfirst($k) . ": $v";
284  $request .= "$v\r\n";
285  }
286 
287  if(is_resource($body)) {
288  $request .= "\r\n";
289  } else {
290  // Add the request body
291  $request .= "\r\n" . $body;
292  }
293 
294  // Send the request
295  if (! @fwrite($this->socket, $request)) {
296  #require_once 'Zend/Http/Client/Adapter/Exception.php';
297  throw new Zend_Http_Client_Adapter_Exception('Error writing request to server');
298  }
299 
300  if(is_resource($body)) {
301  if(stream_copy_to_stream($body, $this->socket) == 0) {
302  #require_once 'Zend/Http/Client/Adapter/Exception.php';
303  throw new Zend_Http_Client_Adapter_Exception('Error writing request to server');
304  }
305  }
306 
307  return $request;
308  }

Field Documentation

◆ $_context

$_context = null
protected

Definition at line 95 of file Socket.php.

◆ $config

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

Definition at line 75 of file Socket.php.

◆ $connected_to

$connected_to = array(null, null)
protected

Definition at line 61 of file Socket.php.

◆ $method

$method = null
protected

Definition at line 88 of file Socket.php.

◆ $out_stream

$out_stream = null
protected

Definition at line 68 of file Socket.php.

◆ $socket

$socket = null
protected

Definition at line 54 of file Socket.php.


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