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);
}
}
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);
}
}
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.
*
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) {
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;
}
$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;
}
/**
*
* @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
*/
public function getStream()
{
- return $this->_stream;
+ $body = new Horde_Support_StringStream($this->getBody());
+ return $body->fopen();
}
/**
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);
}
/**
{
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;
+ }
}
+<?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;
+ }
+}
--- /dev/null
+<?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
+{
+}
+<?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();
+ }
+}