pecl_http, cURL, and fopen requests all work for the basics now
authorChuck Hagenbuch <chuck@horde.org>
Tue, 15 Sep 2009 03:38:30 +0000 (23:38 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Tue, 15 Sep 2009 03:38:30 +0000 (23:38 -0400)
framework/Http/lib/Horde/Http/Request/Curl.php
framework/Http/lib/Horde/Http/Request/Peclhttp.php
framework/Http/lib/Horde/Http/Response/Base.php
framework/Http/lib/Horde/Http/Response/Curl.php
framework/Http/lib/Horde/Http/Response/Fopen.php
framework/Http/lib/Horde/Http/Response/Mock.php [new file with mode: 0644]
framework/Http/lib/Horde/Http/Response/Peclhttp.php

index 9658c3e..0b1ac88 100644 (file)
@@ -51,6 +51,6 @@ class Horde_Http_Request_Curl extends Horde_Http_Request_Base
             throw new Horde_Http_Exception(curl_error($curl), curl_errno($curl));
         }
         $info = curl_getinfo($curl);
-        return new Horde_Http_Response_Curl($result, $info);
+        return new Horde_Http_Response_Curl($this->uri, $result, $info);
     }
 }
index 5547e5a..18de6c5 100644 (file)
@@ -54,6 +54,6 @@ class Horde_Http_Request_Peclhttp extends Horde_Http_Request_Base
             throw new Horde_Http_Exception($e->getMessage(), $e->getCode(), $e);
         }
 
-        return new Horde_Http_Response_Peclhttp($httpResponse);
+        return new Horde_Http_Response_Peclhttp($this->uri, $httpResponse);
     }
 }
index 317f0af..c7e514a 100644 (file)
@@ -41,22 +41,6 @@ abstract class Horde_Http_Response_Base
     public $headers;
 
     /**
-     * Response body
-     * @var stream
-     */
-    protected $_stream;
-
-    /**
-     * Constructor
-     */
-    public function __construct($uri, $stream, $headers = array())
-    {
-        $this->uri = $uri;
-        $this->_stream = $stream;
-        $this->headers = $this->_parseHeaders($headers);
-    }
-
-    /**
      * Parse an array of response headers, mindful of line
      * continuations, etc.
      *
@@ -66,10 +50,10 @@ abstract class Horde_Http_Response_Base
     protected function _parseHeaders($headers)
     {
         if (!is_array($headers)) {
-            $headers = explode("\n", $headers);
+            $headers = preg_split("/\r?\n/", $headers);
         }
 
-        $parsedHeaders = array();
+        $this->headers = array();
 
         $lastHeader = null;
         foreach ($headers as $headerLine) {
@@ -80,7 +64,7 @@ abstract class Horde_Http_Response_Base
             if (preg_match('/^HTTP\/(\d.\d) (\d{3})/', $headerLine, $httpMatches)) {
                 $this->httpVersion = $httpMatches[1];
                 $this->code = (int)$httpMatches[2];
-                $parsedHeaders = array();
+                $this->headers = array();
                 $lastHeader = null;
             }
 
@@ -93,27 +77,25 @@ abstract class Horde_Http_Response_Base
                 $headerName = strtolower($m[1]);
                 $headerValue = $m[2];
 
-                if (isset($parsedHeaders[$headerName])) {
-                    if (!is_array($parsedHeaders[$headerName])) {
-                        $parsedHeaders[$headerName] = array($parsedHeaders[$headerName]);
+                if (isset($this->headers[$headerName])) {
+                    if (!is_array($this->headers[$headerName])) {
+                        $this->headers[$headerName] = array($this->headers[$headerName]);
                     }
 
-                    $parsedHeaders[$headerName][] = $headerValue;
+                    $this->headers[$headerName][] = $headerValue;
                 } else {
-                    $parsedHeaders[$headerName] = $headerValue;
+                    $this->headers[$headerName] = $headerValue;
                 }
                 $lastHeader = $headerName;
             } elseif (preg_match("|^\s+(.+)$|", $headerLine, $m) && !is_null($lastHeader)) {
-                if (is_array($parsedHeaders[$lastHeader])) {
-                    end($parsedHeaders[$lastHeader]);
-                    $parsedHeaders[$lastHeader][key($parsedHeaders[$lastHeader])] .= $m[1];
+                if (is_array($this->headers[$lastHeader])) {
+                    end($this->headers[$lastHeader]);
+                    $this->headers[$lastHeader][key($this->headers[$lastHeader])] .= $m[1];
                 } else {
-                    $parsedHeaders[$lastHeader] .= $m[1];
+                    $this->headers[$lastHeader] .= $m[1];
                 }
             }
         }
-
-        return $parsedHeaders;
     }
 
     /**
@@ -121,14 +103,7 @@ abstract class Horde_Http_Response_Base
      *
      * @return string HTTP response body.
      */
-    public function getBody()
-    {
-        $content = @stream_get_contents($this->_stream);
-        if ($content === false) {
-            throw new Horde_Http_Exception('Problem reading data from ' . $this->uri . ': ' . $php_errormsg);
-        }
-        return $content;
-    }
+    abstract public function getBody();
 
     /**
      * Return a stream pointing to the response body that can be used
@@ -136,7 +111,8 @@ abstract class Horde_Http_Response_Base
      */
     public function getStream()
     {
-        return $this->_stream;
+        $body = new Horde_Support_StringStream($this->getBody());
+        return $body->fopen();
     }
 
     /**
index 7b2eb5f..f3a1b97 100644 (file)
 class Horde_Http_Response_Curl extends Horde_Http_Response_Base
 {
     /**
-     * Info on the request
+     * Info on the request obtained from curl_getinfo()
      * @var array
      */
-    protected $_curlinfo = array();
+    protected $_info = array();
 
     /**
+     * Response body
+     * @var string
      */
-    public function __construct($curlresult, $curlinfo)
-    {
-        echo $curlresult;
-        $this->parseResponse($curlresult, $curlinfo);
-    }
+    protected $_body;
 
     /**
+     * Constructor
      *
+     * @param string $uri
+     * @param string $curlresult
+     * @param array $curlinfo
      */
-    public function parseResponse($curlresult, $curlinfo)
+    public function __construct($uri, $curlresult, $curlinfo)
     {
-        $this->_curlinfo = $curlinfo;
+        $this->uri = $uri;
+        $this->_parseInfo($curlinfo);
+        $this->_parseResult($curlresult);
     }
 
     /**
@@ -47,4 +51,31 @@ class Horde_Http_Response_Curl extends Horde_Http_Response_Base
     {
         return $this->_body;
     }
+
+    /**
+     * Parse the combined header/body result from cURL.
+     *
+     * @param string $curlresult
+     */
+    protected function _parseResult($curlresult)
+    {
+        $endOfHeaders = strpos($curlresult, "\r\n\r\n");
+
+        $headers = substr($curlresult, 0, $endOfHeaders);
+        $this->_parseHeaders($headers);
+
+        $this->_body = substr($curlresult, $endOfHeaders + 4);
+    }
+
+    /**
+     * Process the results of curl_getinfo
+     *
+     * @param array $curlinfo
+     */
+    protected function _parseInfo($curlinfo)
+    {
+        $this->uri = $curlinfo['url'];
+
+        $this->_info = $curlinfo;
+    }
 }
index e69de29..47ea535 100644 (file)
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Http
+ */
+
+/**
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Http
+ */
+class Horde_Http_Response_Fopen extends Horde_Http_Response_Base
+{
+    /**
+     * Response body
+     * @var stream
+     */
+    protected $_stream;
+
+    /**
+     * Constructor
+     */
+    public function __construct($uri, $stream, $headers = array())
+    {
+        $this->uri = $uri;
+        $this->_stream = $stream;
+        $this->_parseHeaders($headers);
+    }
+
+    /**
+     * Return the body of the HTTP response.
+     *
+     * @return string HTTP response body.
+     */
+    public function getBody()
+    {
+        $content = @stream_get_contents($this->_stream);
+        if ($content === false) {
+            throw new Horde_Http_Exception('Problem reading data from ' . $this->uri . ': ' . $php_errormsg);
+        }
+        return $content;
+    }
+
+    /**
+     * Return a stream pointing to the response body that can be used
+     * with all standard PHP stream functions.
+     */
+    public function getStream()
+    {
+        return $this->_stream;
+    }
+}
diff --git a/framework/Http/lib/Horde/Http/Response/Mock.php b/framework/Http/lib/Horde/Http/Response/Mock.php
new file mode 100644 (file)
index 0000000..0907c28
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+/**
+ * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Http
+ */
+
+/**
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Http
+ */
+class Horde_Http_Response_Mock extends Horde_Http_Response_Base
+{
+}
index e69de29..6a08024 100644 (file)
@@ -0,0 +1,48 @@
+<?php
+/**
+ * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Http
+ */
+
+/**
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Http
+ */
+class Horde_Http_Response_Peclhttp extends Horde_Http_Response_Base
+{
+    /**
+     * HttpMessage object
+     * @var HttpMessage
+     */
+    protected $_message;
+
+    /**
+     * Constructor
+     *
+     * @param string $uri
+     * @param HttpMessage $message
+     */
+    public function __construct($uri, HttpMessage $message)
+    {
+        $this->uri = $uri;
+        $this->httpVersion = $message->getHttpVersion();
+        $this->code = $message->getResponseCode();
+
+        $this->_message = $message;
+
+        foreach ($message->getHeaders() as $k => $v) {
+            $this->_headers[strtolower($k)] = $v;
+        }
+    }
+
+    public function getBody()
+    {
+        return $this->_message->getBody();
+    }
+}