From: Chuck Hagenbuch Date: Tue, 15 Sep 2009 03:38:30 +0000 (-0400) Subject: pecl_http, cURL, and fopen requests all work for the basics now X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=4dfca4614d1acdedb7741e9e37f2f9bc6dd4c140;p=horde.git pecl_http, cURL, and fopen requests all work for the basics now --- diff --git a/framework/Http/lib/Horde/Http/Request/Curl.php b/framework/Http/lib/Horde/Http/Request/Curl.php index 9658c3e09..0b1ac8805 100644 --- a/framework/Http/lib/Horde/Http/Request/Curl.php +++ b/framework/Http/lib/Horde/Http/Request/Curl.php @@ -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); } } diff --git a/framework/Http/lib/Horde/Http/Request/Peclhttp.php b/framework/Http/lib/Horde/Http/Request/Peclhttp.php index 5547e5aa7..18de6c5bc 100644 --- a/framework/Http/lib/Horde/Http/Request/Peclhttp.php +++ b/framework/Http/lib/Horde/Http/Request/Peclhttp.php @@ -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); } } diff --git a/framework/Http/lib/Horde/Http/Response/Base.php b/framework/Http/lib/Horde/Http/Response/Base.php index 317f0af40..c7e514a42 100644 --- a/framework/Http/lib/Horde/Http/Response/Base.php +++ b/framework/Http/lib/Horde/Http/Response/Base.php @@ -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(); } /** diff --git a/framework/Http/lib/Horde/Http/Response/Curl.php b/framework/Http/lib/Horde/Http/Response/Curl.php index 7b2eb5fea..f3a1b97d2 100644 --- a/framework/Http/lib/Horde/Http/Response/Curl.php +++ b/framework/Http/lib/Horde/Http/Response/Curl.php @@ -17,25 +17,29 @@ 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; + } } diff --git a/framework/Http/lib/Horde/Http/Response/Fopen.php b/framework/Http/lib/Horde/Http/Response/Fopen.php index e69de29bb..47ea53586 100644 --- a/framework/Http/lib/Horde/Http/Response/Fopen.php +++ b/framework/Http/lib/Horde/Http/Response/Fopen.php @@ -0,0 +1,57 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Http + */ + +/** + * @author Chuck Hagenbuch + * @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 index 000000000..0907c28e7 --- /dev/null +++ b/framework/Http/lib/Horde/Http/Response/Mock.php @@ -0,0 +1,19 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Http + */ + +/** + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Http + */ +class Horde_Http_Response_Mock extends Horde_Http_Response_Base +{ +} diff --git a/framework/Http/lib/Horde/Http/Response/Peclhttp.php b/framework/Http/lib/Horde/Http/Response/Peclhttp.php index e69de29bb..6a0802449 100644 --- a/framework/Http/lib/Horde/Http/Response/Peclhttp.php +++ b/framework/Http/lib/Horde/Http/Response/Peclhttp.php @@ -0,0 +1,48 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Http + */ + +/** + * @author Chuck Hagenbuch + * @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(); + } +}