Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Throw an exception before invalid response is parsed by getResponse() #1022

Closed
wants to merge 6 commits into
from
@@ -55,6 +55,7 @@ public function __construct(JRegistry $options)
* @return JHttpResponse
*
* @since 11.3
+ * @throws RuntimeException
*/
public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
{
@@ -134,6 +135,12 @@ public function request($method, JUri $uri, $data = null, array $headers = null,
$content = curl_exec($ch);
curl_close($ch);
+ // Received empty response (invalid uri, connection reached timeout);
+ if ($content === false)
+ {
+ throw new RuntimeException('HTTP request failed');
+ }
+
return $this->getResponse($content);
}
@@ -137,6 +137,12 @@ public function request($method, JUri $uri, $data = null, array $headers = null,
$content .= fgets($connection, 4096);
}
+ // Received empty response (invalid uri, connection reached timeout);
+ if ($content === "")
+ {
+ throw new RuntimeException('HTTP request failed');
+ }
+
return $this->getResponse($content);
}
@elinw

elinw Aug 15, 2012

Contributor

What you are doing here is different than what I did in my pull request which relate to the warning from fsockopen() at line 256 similar to what you did with fopen() in stream.

@piotr-cz

piotr-cz Aug 15, 2012

Contributor

I've tested all transports against invalid url and var_dump'ed the result:

  • for socket it was: ""
  • for curl and stream: false

So I've checked for empty response in the request method to be able to throw an exception in about the same place as in other transports.

Probably emphasizing an convention may not be optimal here as every transport is different.

@@ -62,6 +62,7 @@ public function __construct(JRegistry $options)
* @return JHttpResponse
*
* @since 11.3
+ * @throws RuntimeException
*/
public function request($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
{
@@ -122,7 +123,13 @@ public function request($method, JUri $uri, $data = null, array $headers = null,
$context = stream_context_create(array('http' => $options));
// Open the stream for reading.
- $stream = fopen((string) $uri, 'r', false, $context);
+ $stream = @fopen((string) $uri, 'r', false, $context);
+
+ // When fopen($uri) returns false (invalid uri, connection reached timeout)
+ if ($stream === false)
+ {
+ throw new RuntimeException('HTTP request failed');
+ }
// Get the metadata for the stream, including response headers.
$metadata = stream_get_meta_data($stream);