Commit 0b2b8351 authored by Gasol Wu's avatar Gasol Wu

use pecl HTTP classes instead of functions

parent bd44a93a
...@@ -71,53 +71,88 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter ...@@ -71,53 +71,88 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
*/ */
public function execute($request) public function execute($request)
{ {
list($data, $headers) = $this->_getData($request); $httpRequest = $this->getHttpRequest($request);
$this->check($data, $headers);
return new Solarium_Client_Response($data, $headers); try {
$httpMessage = $httpRequest->send();
} catch (Exception $e) {
throw new Solarium_Client_HttpException($e->getMessage());
}
if ($error = $httpRequest->getResponseInfo('error')) {
throw new Solarium_Client_HttpException($error);
}
return new Solarium_Client_Response($httpMessage->getBody(),
$this->_getRawHeaders($httpMessage));
} }
/** /**
* Execute request * Convert key/value pair header to raw header.
* *
* @param Solarium_Client_Request $request * <code>
* //before
* $headers['Content-Type'] = 'text/plain';
*
* ...
*
* //after
* $headers[0] = 'Content-Type: text/plain';
* </code>
*
* @param $message HttpMessage
* @return array * @return array
*/ */
protected function _getData($request) protected function _getRawHeaders($message)
{ {
// @codeCoverageIgnoreStart $headers[] = 'HTTP/' . $message->getHttpVersion()
$uri = $this->getBaseUri() . $request->getUri(); . ' ' . $message->getResponseCode()
$method = $request->getMethod(); . ' ' . $message->getResponseStatus();
$options = $this->_createOptions($request);
if ($method == Solarium_Client_Request::METHOD_POST) { foreach ($message->getHeaders() as $header => $value) {
if (!isset($options['headers']['Content-Type'])) { $headers[] = "$header: $value";
$options['headers']['Content-Type'] = 'text/xml; charset=utf-8';
} }
$httpResponse = http_post_data(
$uri, $request->getRawData(), $options return $headers;
);
} else if ($method == Solarium_Client_Request::METHOD_GET) {
$httpResponse = http_get($uri, $options);
} else if ($method == Solarium_Client_Request::METHOD_HEAD) {
$httpResponse = http_head($uri, $options);
} else {
throw new Solarium_Exception("unsupported method: $method");
} }
$headers = array(); /**
$data = ''; *
if ($message = http_parse_message($httpResponse)) { * adapt Solarium_Client_Request to HttpRequest
$data = $message->body; *
if ($firstPositionOfCRLF = strpos($httpResponse, "\r\n\r\n")) { * {@link http://us.php.net/manual/en/http.constants.php
$headersAsString = substr( * HTTP Predefined Constant}
$httpResponse, 0, $firstPositionOfCRLF *
); * @param Solarium_Client_Request $request
$headers = explode("\n", $headersAsString); * @param HttpRequest
*/
protected function getHttpRequest($request)
{
$url = $this->getBaseUri() . $request->getUri();
switch($request->getMethod()) {
case Solarium_Client_Request::METHOD_GET:
$method = HTTP_METH_GET;
break;
case Solarium_Client_Request::METHOD_POST:
$method = HTTP_METH_POST;
break;
case Solarium_Client_Request::METHOD_HEAD:
$method = HTTP_METH_HEAD;
break;
default:
throw new Solarium_Exception('Unsupported method: ' .
$request->getMethod());
} }
$options = $this->_createOptions($request);
$httpRequest = new HttpRequest($url, $method, $options);
if (HTTP_METH_POST == $httpRequest->getMethod()) {
$httpRequest->setBody($request->getRawData());
} }
return array($data, $headers); return $httpRequest;
// @codeCoverageIgnoreEnd
} }
/** /**
...@@ -140,24 +175,11 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter ...@@ -140,24 +175,11 @@ class Solarium_Client_Adapter_PeclHttp extends Solarium_Client_Adapter
$options['headers'][$header] = trim($value); $options['headers'][$header] = trim($value);
} }
} }
return $options; if ($request->getMethod() == Solarium_Client_Request::METHOD_POST) {
// @codeCoverageIgnoreEnd if (!isset($options['headers']['Content-Type'])) {
$options['headers']['Content-Type'] = 'text/xml; charset=utf-8';
} }
/**
* Check result of a request
*
* @throws Solarium_Client_HttpException
* @param string $data
* @param array $headers
* @return void
*/
public function check($data, $headers)
{
// if there is no data and there are no headers it's a total failure,
// a connection to the host was impossible.
if (empty($data) && count($headers) == 0) {
throw new Solarium_Client_HttpException("HTTP request failed");
} }
return $options;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment