Source for file socket.php
Documentation is available at socket.php
* @package Joomla.Platform
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
* HTTP transport class for using sockets directly.
* @package Joomla.Platform
* @var array Reusable socket connections.
* @var JRegistry The client options.
* @param JRegistry $options Client options object.
* @throws RuntimeException
if (!self::isSupported())
throw
new RuntimeException('Cannot use a socket transport when fsockopen() is not available.');
* Send a request to the server and return a JHttpResponse object with the response.
* @param string $method The HTTP method for sending the request.
* @param JUri $uri The URI to the resource to request.
* @param mixed $data Either an associative array or a string to be sent with the request.
* @param array $headers An array of request headers to send with the request.
* @param integer $timeout Read timeout in seconds.
* @param string $userAgent The optional user agent string to send with the request.
* @throws RuntimeException
public function request($method, JUri $uri, $data =
null, array $headers =
null, $timeout =
null, $userAgent =
null)
$connection =
$this->connect($uri, $timeout);
// Make sure the connection is alive and valid.
if (is_resource($connection))
// Make sure the connection has not timed out.
$meta =
stream_get_meta_data($connection);
throw
new RuntimeException('Server connection timed out.');
throw
new RuntimeException('Not connected to server.');
// Get the request path from the URI object.
$path =
$uri->toString(array('path', 'query'));
// If we have data to send make sure our request is setup for it.
// If the data is not a scalar value encode it to be sent with the request.
if (!isset
($headers['Content-Type']))
$headers['Content-Type'] =
'application/x-www-form-urlencoded; charset=utf-8';
// Add the relevant headers.
$headers['Content-Length'] =
strlen($data);
// Build the request payload.
$request[] =
strtoupper($method) .
' ' .
((empty($path)) ?
'/' :
$path) .
' HTTP/1.0';
$request[] =
'Host: ' .
$uri->getHost();
// If an explicit user agent is given use it.
$headers['User-Agent'] =
$userAgent;
// If there are custom headers to send add them to the request payload.
foreach ($headers as $k =>
$v)
$request[] =
$k .
': ' .
$v;
// If we have data to send add it to the request payload.
// Send the request to the server.
// Get the response data from the server.
while (!feof($connection))
$content .=
fgets($connection, 4096);
* Method to get a response object from a server response.
* @param string $content The complete server response, including headers.
* @throws UnexpectedValueException
// Create the response object.
throw
new UnexpectedValueException('No content in response.');
// Split the response into headers and body.
$response =
explode("\r\n\r\n", $content, 2);
// Get the response headers as an array.
$headers =
explode("\r\n", $response[0]);
// Set the body for the response.
$return->body =
empty($response[1]) ?
'' :
$response[1];
// Get the response code from the first offset of the response headers.
$return->code = (int)
$code;
// No valid response code was detected.
throw
new UnexpectedValueException('No HTTP response code found.');
// Add the response headers to the response object.
foreach ($headers as $header)
* Method to connect to a server and get the resource.
* @param JUri $uri The URI to connect with.
* @param integer $timeout Read timeout in seconds.
* @return resource Socket connection resource.
* @throws RuntimeException
protected function connect(JUri $uri, $timeout =
null)
// Get the host from the uri.
$host =
($uri->isSSL()) ?
'ssl://' .
$uri->getHost() :
$uri->getHost();
// If the port is not explicitly set in the URI detect it.
$port =
($uri->getScheme() ==
'https') ?
443 :
80;
// Build the connection key for resource memory caching.
$key =
md5($host .
$port);
// If the connection already exists, use it.
// Connection reached EOF, cannot be used anymore
throw
new RuntimeException('Cannot close connection');
// Make sure the connection has not timed out.
elseif (!$meta['timed_out'])
$timeout =
ini_get('default_socket_timeout');
$track_errors =
ini_get('track_errors');
// PHP sends a warning if the uri does not exists; we silence it and throw an exception instead.
// Attempt to connect to the server
$connection =
@fsockopen($host, $port, $errno, $err, $timeout);
// Error but nothing from php? Create our own
$php_errormsg =
sprintf('Could not connect to resource: %s', $uri, $err, $errno);
// Restore error tracking to give control to the exception handler
ini_set('track_errors', $track_errors);
throw
new RuntimeException($php_errormsg);
// Restore error tracking to what it was before.
ini_set('track_errors', $track_errors);
// Since the connection was successful let's store it in case we need to use it later.
// If an explicit timeout is set, set it.
* Method to check if http transport socket available for use
* @return boolean True if available else false
Documentation generated on Tue, 19 Nov 2013 15:14:01 +0100 by phpDocumentor 1.4.3