From: Mikael Nordfeldth Date: Mon, 11 Jan 2016 01:36:59 +0000 (+0100) Subject: HTTPClient would return null instead of exception X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=b13f8df79b5057aa77947fc07f2fb3867dad8809;p=quix0rs-gnu-social.git HTTPClient would return null instead of exception This caused $response->isOK() tests to call a function on a non-existing object, causing all hell to break loose. --- diff --git a/lib/httpclient.php b/lib/httpclient.php index 3de88e2259..dce8e821ff 100644 --- a/lib/httpclient.php +++ b/lib/httpclient.php @@ -194,7 +194,7 @@ class HTTPClient extends HTTP_Request2 } $response = $client->get($url); if (!$response->isOk()) { - // TRANS: Exception. %s is a profile URL. + // TRANS: Exception. %s is the URL we tried to GET. throw new Exception(sprintf(_m('Could not GET URL %s.'), $url), $response->getStatus()); } return $response->getBody(); @@ -288,6 +288,10 @@ class HTTPClient extends HTTP_Request2 } } $response = $this->send(); + if (is_null($response)) { + // TRANS: Failed to retrieve a remote web resource, %s is the target URL. + throw new HTTP_Request2_Exception(sprintf(_m('HTTP request failed without response to URL: %s'), var_export($target, true))); + } return $response; } diff --git a/lib/queuemonitor.php b/lib/queuemonitor.php index 3dc0ea65aa..ecc76197cf 100644 --- a/lib/queuemonitor.php +++ b/lib/queuemonitor.php @@ -96,11 +96,14 @@ class QueueMonitor protected function pingHttp($target, $data) { $client = new HTTPClient(); - $result = $client->post($target, array(), $data); + try { + $result = $client->post($target, array(), $data); - if (!$result->isOk()) { - common_log(LOG_ERR, __METHOD__ . ' HTTP ' . $result->getStatus() . - ': ' . $result->getBody()); + if (!$result->isOk()) { + common_log(LOG_ERR, __METHOD__ . ' HTTP ' . $result->getStatus() . ': ' . $result->getBody()); + } + } catch (HTTP_Request2_Exception $e) { + common_log(LOG_ERR, __METHOD__ . ' HTTP request generated PHP level error (check logs, could be DNS failure etc.). URL: '.var_export($target,true)); } } diff --git a/plugins/OStatus/lib/discoveryhints.php b/plugins/OStatus/lib/discoveryhints.php index 4df7690038..2a1309d33f 100644 --- a/plugins/OStatus/lib/discoveryhints.php +++ b/plugins/OStatus/lib/discoveryhints.php @@ -58,9 +58,14 @@ class DiscoveryHints { { $client = new HTTPClient(); $client->setHeader('Accept', 'text/html,application/xhtml+xml'); - $response = $client->get($url); + try { + $response = $client->get($url); - if (!$response->isOk()) { + if (!$response->isOk()) { + return null; + } + } catch (HTTP_Request2_Exception $e) { + // Any HTTPClient error that might've been thrown return null; } diff --git a/plugins/OStatus/tests/slap.php b/plugins/OStatus/tests/slap.php index e3f5439aed..ba4af4e6a1 100755 --- a/plugins/OStatus/tests/slap.php +++ b/plugins/OStatus/tests/slap.php @@ -75,10 +75,14 @@ if (have_option('--verify')) { print "Sending for verification to $url ...\n"; $client = new HTTPClient(); - $response = $client->post($url, array(), array('magic_env' => $envxml)); + try { + $response = $client->post($url, array(), array('magic_env' => $envxml)); - print $response->getStatus() . "\n\n"; - print $response->getBody() . "\n\n"; + print $response->getStatus() . "\n\n"; + print $response->getBody() . "\n\n"; + } catch (HTTP_Request2_Exception $e) { + print 'Failed POST to URL '.var_export($url, true).': '.$e->getMessage(); + } } if (have_option('--slap')) {