diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2015-12-17 09:15:42 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2015-12-17 09:44:51 +0100 |
commit | a1789ddde42033f1b05cc4929491214ee6e79383 (patch) | |
tree | 63615735c4ddffaaabf2428946bb26f90899f7bf /vendor/ruflin/elastica/lib/Elastica/Transport/HttpAdapter.php | |
parent | 9e06a62f265e3a2aaabecc598d4bc617e06fa32d (diff) |
Update to MediaWiki 1.26.0
Diffstat (limited to 'vendor/ruflin/elastica/lib/Elastica/Transport/HttpAdapter.php')
-rw-r--r-- | vendor/ruflin/elastica/lib/Elastica/Transport/HttpAdapter.php | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/vendor/ruflin/elastica/lib/Elastica/Transport/HttpAdapter.php b/vendor/ruflin/elastica/lib/Elastica/Transport/HttpAdapter.php new file mode 100644 index 00000000..efc27ab5 --- /dev/null +++ b/vendor/ruflin/elastica/lib/Elastica/Transport/HttpAdapter.php @@ -0,0 +1,156 @@ +<?php +namespace Elastica\Transport; + +use Elastica\Connection; +use Elastica\Exception\PartialShardFailureException; +use Elastica\Exception\ResponseException; +use Elastica\JSON; +use Elastica\Request as ElasticaRequest; +use Elastica\Response as ElasticaResponse; +use Ivory\HttpAdapter\HttpAdapterInterface; +use Ivory\HttpAdapter\Message\Request as HttpAdapterRequest; +use Ivory\HttpAdapter\Message\Response as HttpAdapterResponse; +use Ivory\HttpAdapter\Message\Stream\StringStream; + +class HttpAdapter extends AbstractTransport +{ + /** + * @var HttpAdapterInterface + */ + private $httpAdapter; + + /** + * @var string + */ + private $_scheme = 'http'; + + /** + * Construct transport. + */ + public function __construct(Connection $connection = null, HttpAdapterInterface $httpAdapter) + { + parent::__construct($connection); + $this->httpAdapter = $httpAdapter; + } + + /** + * Makes calls to the elasticsearch server. + * + * All calls that are made to the server are done through this function + * + * @param \Elastica\Request $elasticaRequest + * @param array $params Host, Port, ... + * + * @throws \Elastica\Exception\ConnectionException + * @throws \Elastica\Exception\ResponseException + * @throws \Elastica\Exception\Connection\HttpException + * + * @return \Elastica\Response Response object + */ + public function exec(ElasticaRequest $elasticaRequest, array $params) + { + $connection = $this->getConnection(); + + if ($timeout = $connection->getTimeout()) { + $this->httpAdapter->getConfiguration()->setTimeout($timeout); + } + + $httpAdapterRequest = $this->_createHttpAdapterRequest($elasticaRequest, $connection); + + $start = microtime(true); + $httpAdapterResponse = $this->httpAdapter->sendRequest($httpAdapterRequest); + $end = microtime(true); + + $elasticaResponse = $this->_createElasticaResponse($httpAdapterResponse, $connection); + $elasticaResponse->setQueryTime($end - $start); + + $elasticaResponse->setTransferInfo( + array( + 'request_header' => $httpAdapterRequest->getMethod(), + 'http_code' => $httpAdapterResponse->getStatusCode(), + ) + ); + + if ($elasticaResponse->hasError()) { + throw new ResponseException($elasticaRequest, $elasticaResponse); + } + + if ($elasticaResponse->hasFailedShards()) { + throw new PartialShardFailureException($elasticaRequest, $elasticaResponse); + } + + return $elasticaResponse; + } + + /** + * @param HttpAdapterResponse $httpAdapterResponse + * + * @return ElasticaResponse + */ + protected function _createElasticaResponse(HttpAdapterResponse $httpAdapterResponse) + { + return new ElasticaResponse((string) $httpAdapterResponse->getBody(), $httpAdapterResponse->getStatusCode()); + } + + /** + * @param ElasticaRequest $elasticaRequest + * @param Connection $connection + * + * @return HttpAdapterRequest + */ + protected function _createHttpAdapterRequest(ElasticaRequest $elasticaRequest, Connection $connection) + { + $data = $elasticaRequest->getData(); + $body = null; + $method = $elasticaRequest->getMethod(); + $headers = $connection->hasConfig('headers') ?: array(); + if (!empty($data) || '0' === $data) { + if ($method == ElasticaRequest::GET) { + $method = ElasticaRequest::POST; + } + + if ($this->hasParam('postWithRequestBody') && $this->getParam('postWithRequestBody') == true) { + $elasticaRequest->setMethod(ElasticaRequest::POST); + $method = ElasticaRequest::POST; + } + + if (is_array($data)) { + $body = JSON::stringify($data, 'JSON_ELASTICSEARCH'); + } else { + $body = $data; + } + } + + $url = $this->_getUri($elasticaRequest, $connection); + $streamBody = new StringStream($body); + + return new HttpAdapterRequest($url, $method, HttpAdapterRequest::PROTOCOL_VERSION_1_1, $headers, $streamBody); + } + + /** + * @param ElasticaRequest $request + * @param \Elastica\Connection $connection + * + * @return string + */ + protected function _getUri(ElasticaRequest $request, Connection $connection) + { + $url = $connection->hasConfig('url') ? $connection->getConfig('url') : ''; + + if (!empty($url)) { + $baseUri = $url; + } else { + $baseUri = $this->_scheme.'://'.$connection->getHost().':'.$connection->getPort().'/'.$connection->getPath(); + } + + $baseUri .= $request->getPath(); + + $query = $request->getQuery(); + + if (!empty($query)) { + $baseUri .= '?'.http_build_query($query); + } + + return $baseUri; + } +} |