Make getBody() repeatable
authorChuck Hagenbuch <chuck@horde.org>
Sat, 22 Jan 2011 03:36:57 +0000 (22:36 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Sat, 22 Jan 2011 03:37:23 +0000 (22:37 -0500)
framework/Http/lib/Horde/Http/Response/Fopen.php

index e681069..28ce5af 100644 (file)
@@ -24,6 +24,11 @@ class Horde_Http_Response_Fopen extends Horde_Http_Response_Base
     protected $_stream;
 
     /**
+     * Response content
+     */
+    protected $_content;
+
+    /**
      * Constructor.
      */
     public function __construct($uri, $stream, $headers = array())
@@ -41,17 +46,21 @@ class Horde_Http_Response_Fopen extends Horde_Http_Response_Base
      */
     public function getBody()
     {
-        $oldTrackErrors = ini_set('track_errors', 1);
-        $content = @stream_get_contents($this->_stream);
-        ini_set('track_errors', $oldTrackErrors);
-        if ($content === false) {
-            $msg = 'Problem reading data from ' . $this->uri;
-            if (isset($php_errormsg)) {
-                $msg .= ': ' . $php_errormsg;
+        if (is_null($this->_content)) {
+            $oldTrackErrors = ini_set('track_errors', 1);
+            $content = @stream_get_contents($this->_stream);
+            ini_set('track_errors', $oldTrackErrors);
+            if ($content === false) {
+                $msg = 'Problem reading data from ' . $this->uri;
+                if (isset($php_errormsg)) {
+                    $msg .= ': ' . $php_errormsg;
+                }
+                throw new Horde_Http_Exception($msg);
             }
-            throw new Horde_Http_Exception($msg);
+            $this->_content = $content;
         }
-        return $content;
+
+        return $this->_content;
     }
 
     /**