Permalink
Browse files

Improve error handling when reading HTTP responses. Explicitly check …

…that the PHP version is supported.
  • Loading branch information...
1 parent 45e19e9 commit 69f25759b27604459ca31bd9d8d164c66f2f6b0b @davidfstr davidfstr committed Sep 4, 2012
Showing with 46 additions and 15 deletions.
  1. +3 −0 Splunk.php
  2. +3 −11 Splunk/Http.php
  3. +1 −1 Splunk/HttpResponse.php
  4. +38 −2 Splunk/Util.php
  5. +1 −1 vm/provision/splunk-4.3.2.sh
View
@@ -28,3 +28,6 @@ function Splunk_autoload($className)
}
spl_autoload_register('Splunk_autoload');
+
+if (version_compare(PHP_VERSION, '5.2.11') < 0)
+ die('The Splunk SDK for PHP requires PHP 5.2.11 or later.');
View
@@ -102,17 +102,9 @@ public function request(
$bodyStream = @fopen($url, 'rb', /*use_include_path=*/FALSE, $fopenContext);
if ($bodyStream === FALSE)
{
- if (version_compare(PHP_VERSION, '5.2.0') >= 0)
- {
- $errorInfo = error_get_last(); // requires PHP >= 5.2.0
- $errmsg = $errorInfo['message'];
- $errno = $errorInfo['type'];
- }
- else
- {
- $errmsg = 'fopen failed.';
- $errno = 0;
- }
+ $errorInfo = error_get_last();
+ $errmsg = $errorInfo['message'];
+ $errno = $errorInfo['type'];
throw new Splunk_ConnectException($errmsg, $errno);
}
View
@@ -55,7 +55,7 @@ public function __get($key)
private function getBody()
{
if ($this->body === NULL)
- $this->body = stream_get_contents($this->bodyStream);
+ $this->body = Splunk_Util::stream_get_contents($this->bodyStream);
return $this->body;
}
}
View
@@ -15,6 +15,11 @@
* under the License.
*/
+/**
+ * Internal utility functions for the PHP SDK.
+ *
+ * @package Splunk
+ */
class Splunk_Util
{
/**
@@ -52,18 +57,49 @@ public static function getArgument($dict, $key, $defaultValue)
/**
* Writes $data to $stream.
*
- * @throws IOException If an I/O error occurs.
+ * @throws Splunk_IOException If an I/O error occurs.
*/
public static function fwriteall($stream, $data)
{
while (TRUE)
{
$numBytesWritten = fwrite($stream, $data);
if ($numBytesWritten === FALSE)
- throw new IOException();
+ throw new Splunk_IOException();
if ($numBytesWritten == strlen($data))
return;
$data = substr($data, $numBytesWritten);
}
}
+
+ /**
+ * Reads the entire contents of the specified stream.
+ * Throws a Splunk_IOException upon error.
+ *
+ * @param resource $stream A stream.
+ * @return string The contents of the specified stream.
+ * @throws Splunk_IOException If an I/O error occurs.
+ */
+ public static function stream_get_contents($stream)
+ {
+ // HACK: Clear the last error
+ @trigger_error('');
+
+ $oldError = error_get_last();
+ $body = @stream_get_contents($stream);
+ $newError = error_get_last();
+
+ // HACK: Detecting whether stream_get_contents() has failed is not
+ // strightforward because it can either return FALSE or ''.
+ // However '' is also a legal return value in non-error scenarios.
+ if ($newError != $oldError)
+ {
+ $errorInfo = error_get_last();
+ $errmsg = $errorInfo['message'];
+ $errno = $errorInfo['type'];
+ throw new Splunk_IOException($errmsg, $errno);
+ }
+
+ return $body;
+ }
}
@@ -8,5 +8,5 @@ sudo dpkg -i splunk-4.3.2-123586-linux-2.6-intel.deb
sudo /opt/splunk/bin/splunk --accept-license start
# Change Splunk admin password to "weak"
-# (NOTE: Splunk must already be running for this comment to work.)
+# (NOTE: Splunk must already be running for this command to work.)
sudo /opt/splunk/bin/splunk edit user admin -password weak -role admin -auth admin:changeme

0 comments on commit 69f2575

Please sign in to comment.