Skip to content
This repository has been archived by the owner on Nov 26, 2017. It is now read-only.

Handling in HttpTransport when the requested uri does not exist #1431

Merged
merged 2 commits into from Oct 25, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions libraries/joomla/http/factory.php
Expand Up @@ -19,7 +19,7 @@
class JHttpFactory class JHttpFactory
{ {
/** /**
* method to recieve Http instance. * Method to recieve Http instance.
* *
* @param JRegistry $options Client options object. * @param JRegistry $options Client options object.
* @param mixed $adapters Adapter (string) or queue of adapters (array) to use for communication. * @param mixed $adapters Adapter (string) or queue of adapters (array) to use for communication.
Expand Down Expand Up @@ -58,7 +58,7 @@ public static function getAvailableDriver(JRegistry $options, $default = null)
settype($default, 'array'); settype($default, 'array');
$availableAdapters = $default; $availableAdapters = $default;
} }
// Check if there is available http transport adapters // Check if there is at least one available http transport adapter
if (!count($availableAdapters)) if (!count($availableAdapters))
{ {
return false; return false;
Expand Down
25 changes: 18 additions & 7 deletions libraries/joomla/http/transport/curl.php
Expand Up @@ -79,6 +79,7 @@ public function request($method, JUri $uri, $data = null, array $headers = null,
{ {
$options[CURLOPT_POSTFIELDS] = $data; $options[CURLOPT_POSTFIELDS] = $data;
} }

// Otherwise we need to encode the value first. // Otherwise we need to encode the value first.
else else
{ {
Expand Down Expand Up @@ -146,6 +147,20 @@ public function request($method, JUri $uri, $data = null, array $headers = null,
// Execute the request and close the connection. // Execute the request and close the connection.
$content = curl_exec($ch); $content = curl_exec($ch);


// Check if the content is a string. If it is not, it must be an error.
if (!is_string($content))
{
$message = curl_error($ch);

if (empty($message))
{
// Error but nothing from cURL? Create our own
$message = 'No HTTP response received';
}

throw new RuntimeException($message);
}

// Get the request information. // Get the request information.
$info = curl_getinfo($ch); $info = curl_getinfo($ch);


Expand All @@ -158,7 +173,8 @@ public function request($method, JUri $uri, $data = null, array $headers = null,
/** /**
* Method to get a response object from a server response. * Method to get a response object from a server response.
* *
* @param string $content The complete server response, including headers. * @param string $content The complete server response, including headers
* as a string if the response has no errors.
* @param array $info The cURL request information. * @param array $info The cURL request information.
* *
* @return JHttpResponse * @return JHttpResponse
Expand All @@ -171,12 +187,6 @@ protected function getResponse($content, $info)
// Create the response object. // Create the response object.
$return = new JHttpResponse; $return = new JHttpResponse;


// Check if the content is actually a string.
if (!is_string($content))
{
throw new UnexpectedValueException('No HTTP response received.');
}

// Get the number of redirects that occurred. // Get the number of redirects that occurred.
$redirects = isset($info['redirect_count']) ? $info['redirect_count'] : 0; $redirects = isset($info['redirect_count']) ? $info['redirect_count'] : 0;


Expand All @@ -202,6 +212,7 @@ protected function getResponse($content, $info)
{ {
$return->code = (int) $code; $return->code = (int) $code;
} }

// No valid response code was detected. // No valid response code was detected.
else else
{ {
Expand Down
40 changes: 33 additions & 7 deletions libraries/joomla/http/transport/socket.php
Expand Up @@ -160,14 +160,19 @@ protected function getResponse($content)
// Create the response object. // Create the response object.
$return = new JHttpResponse; $return = new JHttpResponse;


if (empty($content))
{
throw new UnexpectedValueException('No content in response.');
}

// Split the response into headers and body. // Split the response into headers and body.
$response = explode("\r\n\r\n", $content, 2); $response = explode("\r\n\r\n", $content, 2);


// Get the response headers as an array. // Get the response headers as an array.
$headers = explode("\r\n", $response[0]); $headers = explode("\r\n", $response[0]);


// Set the body for the response. // Set the body for the response.
$return->body = $response[1]; $return->body = empty($response[1]) ? '' : $response[1];


// Get the response code from the first offset of the response headers. // Get the response code from the first offset of the response headers.
preg_match('/[0-9]{3}/', array_shift($headers), $matches); preg_match('/[0-9]{3}/', array_shift($headers), $matches);
Expand All @@ -177,6 +182,7 @@ protected function getResponse($content)
{ {
$return->code = (int) $code; $return->code = (int) $code;
} }

// No valid response code was detected. // No valid response code was detected.
else else
{ {
Expand Down Expand Up @@ -217,6 +223,7 @@ protected function connect(JUri $uri, $timeout = null)
{ {
$port = ($uri->getScheme() == 'https') ? 443 : 80; $port = ($uri->getScheme() == 'https') ? 443 : 80;
} }

// Use the set port. // Use the set port.
else else
{ {
Expand All @@ -239,6 +246,7 @@ protected function connect(JUri $uri, $timeout = null)
throw new RuntimeException('Cannot close connection'); throw new RuntimeException('Cannot close connection');
} }
} }

// Make sure the connection has not timed out. // Make sure the connection has not timed out.
elseif (!$meta['timed_out']) elseif (!$meta['timed_out'])
{ {
Expand All @@ -248,15 +256,33 @@ protected function connect(JUri $uri, $timeout = null)


if (!is_numeric($timeout)) if (!is_numeric($timeout))
{ {
$timeout = ini_get("default_socket_timeout"); $timeout = ini_get('default_socket_timeout');
} }
// Attempt to connect to the server.
$connection = fsockopen($host, $port, $errno, $err, $timeout); // Capture PHP errors
$php_errormsg = '';
$track_errors = ini_get('track_errors');
ini_set('track_errors', true);

// 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);


if (!$connection) if (!$connection)
{ {
throw new RuntimeException($err, $errno); if (!$php_errormsg)
{
// 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. // Since the connection was successful let's store it in case we need to use it later.
$this->connections[$key] = $connection; $this->connections[$key] = $connection;
Expand All @@ -271,9 +297,9 @@ protected function connect(JUri $uri, $timeout = null)
} }


/** /**
* method to check if http transport socket available for using * Method to check if http transport socket available for use
* *
* @return bool true if available else false * @return boolean True if available else false
* *
* @since 12.1 * @since 12.1
*/ */
Expand Down
22 changes: 19 additions & 3 deletions libraries/joomla/http/transport/stream.php
Expand Up @@ -126,15 +126,30 @@ public function request($method, JUri $uri, $data = null, array $headers = null,
// Create the stream context for the request. // Create the stream context for the request.
$context = stream_context_create(array('http' => $options)); $context = stream_context_create(array('http' => $options));


// Capture PHP errors
$php_errormsg = '';
$track_errors = ini_get('track_errors');
ini_set('track_errors', true);

// Open the stream for reading. // Open the stream for reading.
$stream = @fopen((string) $uri, 'r', false, $context); $stream = @fopen((string) $uri, 'r', false, $context);


// Check if the stream is open.
if (!$stream) if (!$stream)
{ {
throw new RuntimeException(sprintf('Could not connect to resource: %s', $uri)); if (!$php_errormsg)
{
// 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);

// Get the metadata for the stream, including response headers. // Get the metadata for the stream, including response headers.
$metadata = stream_get_meta_data($stream); $metadata = stream_get_meta_data($stream);


Expand Down Expand Up @@ -174,6 +189,7 @@ protected function getResponse(array $headers, $body)
{ {
$return->code = (int) $code; $return->code = (int) $code;
} }

// No valid response code was detected. // No valid response code was detected.
else else
{ {
Expand All @@ -191,7 +207,7 @@ protected function getResponse(array $headers, $body)
} }


/** /**
* method to check if http transport stream available for using * Method to check if http transport stream available for use
* *
* @return bool true if available else false * @return bool true if available else false
* *
Expand Down
23 changes: 23 additions & 0 deletions tests/suites/unit/joomla/http/JHttpTransportTest.php
Expand Up @@ -99,6 +99,29 @@ public function testRequestGet($transportClass)
); );
} }


/**
* Tests the request method with a get request with a bad domain
*
* @dataProvider transportProvider
* @expectedException RuntimeException
*/
public function testBadDomainRequestGet($transportClass)
{
$transport = new $transportClass($this->options);
$response = $transport->request('get', new JUri('http://xommunity.joomla.org'));
}

/**
* Tests the request method with a get request for non existant url
*
* @dataProvider transportProvider
*/
public function testRequestGet404($transportClass)
{
$transport = new $transportClass($this->options);
$response = $transport->request('get', new JUri($this->stubUrl . ':80'));
}

/** /**
* Tests the request method with a put request * Tests the request method with a put request
* *
Expand Down