/** * Constructor. * * @param HttpAdapterInterface $adapter * @param string $apiToken */ public function __construct(HttpAdapterInterface $adapter, $apiToken) { $this->adapter = $adapter; $this->apiToken = $apiToken; $configuration = $this->adapter->getConfiguration(); $configuration->setTimeout(self::TIMEOUT); $configuration->setUserAgent(self::USER_AGENT); $configuration->setEncodingType(ConfigurationInterface::ENCODING_TYPE_URLENCODED); $configuration->getEventDispatcher()->addSubscriber(new StatusCodeSubscriber()); $this->adapter->setConfiguration($configuration); }
/** * 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 string $method * @param string $uri With or without host * @param string|resource|array $body */ private function send($method, $uri, $body = null) { if (!$this->hasHost($uri)) { $uri = rtrim($this->httpClient->getConfiguration()->getBaseUri(), '/') . '/' . ltrim($uri, '/'); } $stream = new Stream('php://memory', 'rw'); if ($body) { $stream->write($body); } $this->request = new Request($uri, $method, $stream, $this->requestHeaders); $this->response = $this->httpClient->send($uri, $method, $this->requestHeaders, $body); // Reset headers used for the HTTP request $this->requestHeaders = array(); }
/** * {@inheritdoc} */ public function getConfiguration() { return $this->httpAdapter->getConfiguration(); }
/** * {@inheritdoc} */ public function createRedirectRequest(ResponseInterface $response, InternalRequestInterface $internalRequest, HttpAdapterInterface $httpAdapter) { if ($response->getStatusCode() < 300 || $response->getStatusCode() >= 400 || !$response->hasHeader('Location')) { return false; } if ($internalRequest->getParameter(self::REDIRECT_COUNT) >= $this->max) { if ($this->throwException) { $rootRequest = $this->getRootRequest($internalRequest); $exception = HttpAdapterException::maxRedirectsExceeded((string) $rootRequest->getUri(), $this->max, $httpAdapter->getName()); $exception->setRequest($rootRequest); throw $exception; } return false; } $strict = $response->getStatusCode() === 303 || !$this->strict && $response->getStatusCode() <= 302; $headers = $internalRequest->getHeaders(); foreach ($headers as $key => $value) { if (strtolower($key) === 'host') { unset($headers[$key]); } } $redirect = $httpAdapter->getConfiguration()->getMessageFactory()->createInternalRequest($response->getHeaderLine('Location'), $strict ? InternalRequestInterface::METHOD_GET : $internalRequest->getMethod(), $internalRequest->getProtocolVersion(), $headers, $strict ? array() : $internalRequest->getDatas(), $strict ? array() : $internalRequest->getFiles(), $internalRequest->getParameters()); if ($strict) { $redirect = $redirect->withoutHeader('Content-Type')->withoutHeader('Content-Length'); } else { $redirect = $redirect->withBody($internalRequest->getBody()); } return $redirect->withParameter(self::PARENT_REQUEST, $internalRequest)->withParameter(self::REDIRECT_COUNT, $internalRequest->getParameter(self::REDIRECT_COUNT) + 1); }
/** * {@inheritdoc} */ public function createRedirectRequest(ResponseInterface $response, InternalRequestInterface $internalRequest, HttpAdapterInterface $httpAdapter) { if ($response->getStatusCode() < 300 || $response->getStatusCode() >= 400 || !$response->hasHeader('Location')) { return false; } if ($internalRequest->getParameter(self::REDIRECT_COUNT) >= $this->max) { if ($this->throwException) { throw HttpAdapterException::maxRedirectsExceeded((string) $this->getRootRequest($internalRequest)->getUrl(), $this->max, $httpAdapter->getName()); } return false; } $redirect = $httpAdapter->getConfiguration()->getMessageFactory()->cloneInternalRequest($internalRequest); if ($response->getStatusCode() === 303 || !$this->strict && $response->getStatusCode() <= 302) { $redirect->setMethod(InternalRequestInterface::METHOD_GET); $redirect->removeHeaders(array('Content-Type', 'Content-Length')); $redirect->clearRawDatas(); $redirect->clearDatas(); $redirect->clearFiles(); } $redirect->setUrl($response->getHeader('Location')); $redirect->setParameter(self::PARENT_REQUEST, $internalRequest); $redirect->setParameter(self::REDIRECT_COUNT, $internalRequest->getParameter(self::REDIRECT_COUNT) + 1); return $redirect; }