Sets the transfer info of the curl request. This function is called
from the \Elastica\Client::_callService .
public setTransferInfo ( array $transferInfo ) | ||
$transferInfo | array | The curl transfer information. |
/** * @group unit */ public function testExists() { $name = 'index_template1'; $response = new Response(''); $response->setTransferInfo(array('http_code' => 200)); /** @var \PHPUnit_Framework_MockObject_MockObject|Client $clientMock */ $clientMock = $this->getMock('\\Elastica\\Client', array('request')); $clientMock->expects($this->once())->method('request')->with('/_template/' . $name, Request::HEAD, array(), array())->willReturn($response); $indexTemplate = new IndexTemplate($clientMock, $name); $this->assertTrue($indexTemplate->exists()); }
/** * Makes calls to the elasticsearch server. * * All calls that are made to the server are done through this function * * @param \Elastica\Request $request * @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(Request $request, array $params) { $connection = $this->getConnection(); $client = $this->_getGuzzleClient($this->_getBaseUrl($connection), $connection->isPersistent()); $options = array('exceptions' => false); if ($connection->getTimeout()) { $options['timeout'] = $connection->getTimeout(); } $proxy = $connection->getProxy(); // See: https://github.com/facebook/hhvm/issues/4875 if (is_null($proxy) && defined('HHVM_VERSION')) { $proxy = getenv('http_proxy') ?: null; } if (!is_null($proxy)) { $options['proxy'] = $proxy; } $req = $this->_createPsr7Request($request, $connection); try { $start = microtime(true); $res = $client->send($req, $options); $end = microtime(true); } catch (TransferException $ex) { throw new GuzzleException($ex, $request, new Response($ex->getMessage())); } $response = new Response((string) $res->getBody(), $res->getStatusCode()); $response->setQueryTime($end - $start); if ($connection->hasConfig('bigintConversion')) { $response->setJsonBigintConversion($connection->getConfig('bigintConversion')); } $response->setTransferInfo(array('request_header' => $request->getMethod(), 'http_code' => $res->getStatusCode())); if ($response->hasError()) { throw new ResponseException($request, $response); } if ($response->hasFailedShards()) { throw new PartialShardFailureException($request, $response); } return $response; }
/** * Makes calls to the elasticsearch server. * * All calls that are made to the server are done through this function * * @param \Elastica\Request $request * @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(Request $request, array $params) { $connection = $this->getConnection(); $conn = $this->_getConnection($connection->isPersistent()); // If url is set, url is taken. Otherwise port, host and path $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); } curl_setopt($conn, CURLOPT_URL, $baseUri); curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout()); curl_setopt($conn, CURLOPT_FORBID_REUSE, 0); /* @see Connection::setConnectTimeout() */ $connectTimeout = $connection->getConnectTimeout(); if ($connectTimeout > 0) { curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, $connectTimeout); } $proxy = $connection->getProxy(); // See: https://github.com/facebook/hhvm/issues/4875 if (is_null($proxy) && defined('HHVM_VERSION')) { $proxy = getenv('http_proxy') ?: null; } if (!is_null($proxy)) { curl_setopt($conn, CURLOPT_PROXY, $proxy); } $this->_setupCurl($conn); $headersConfig = $connection->hasConfig('headers') ? $connection->getConfig('headers') : array(); if (!empty($headersConfig)) { $headers = array(); while (list($header, $headerValue) = each($headersConfig)) { array_push($headers, $header . ': ' . $headerValue); } curl_setopt($conn, CURLOPT_HTTPHEADER, $headers); } // TODO: REFACTOR $data = $request->getData(); $httpMethod = $request->getMethod(); if (!empty($data) || '0' === $data) { if ($this->hasParam('postWithRequestBody') && $this->getParam('postWithRequestBody') == true) { $httpMethod = Request::POST; } if (is_array($data)) { $content = JSON::stringify($data, 'JSON_ELASTICSEARCH'); } else { $content = $data; } // Escaping of / not necessary. Causes problems in base64 encoding of files $content = str_replace('\\/', '/', $content); if ($connection->hasCompression()) { // An "Accept-Encoding" header containing all supported encoding types is sent // Curl will decode the response automatically curl_setopt($conn, CURLOPT_ENCODING, ''); // Let's precise that the request is also compressed curl_setopt($conn, CURLOPT_HTTPHEADER, array('Content-Encoding: gzip')); // Let's compress the request body, curl_setopt($conn, CURLOPT_POSTFIELDS, gzencode($content)); } else { curl_setopt($conn, CURLOPT_POSTFIELDS, $content); } } else { curl_setopt($conn, CURLOPT_POSTFIELDS, ''); } curl_setopt($conn, CURLOPT_NOBODY, $httpMethod == 'HEAD'); curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $httpMethod); $start = microtime(true); // cURL opt returntransfer leaks memory, therefore OB instead. ob_start(); curl_exec($conn); $responseString = ob_get_clean(); $end = microtime(true); // Checks if error exists $errorNumber = curl_errno($conn); $response = new Response($responseString, curl_getinfo($conn, CURLINFO_HTTP_CODE)); $response->setQueryTime($end - $start); $response->setTransferInfo(curl_getinfo($conn)); if ($connection->hasConfig('bigintConversion')) { $response->setJsonBigintConversion($connection->getConfig('bigintConversion')); } if ($response->hasError()) { throw new ResponseException($request, $response); } if ($response->hasFailedShards()) { throw new PartialShardFailureException($request, $response); } if ($errorNumber > 0) { throw new HttpException($errorNumber, $request, $response); } return $response; }
/** * Makes calls to the elasticsearch server * * All calls that are made to the server are done through this function * * @param \Elastica\Request $request * @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(Request $request, array $params) { $connection = $this->getConnection(); $conn = $this->_getConnection($connection->isPersistent()); // If url is set, url is taken. Otherwise port, host and path $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); } curl_setopt($conn, CURLOPT_URL, $baseUri); curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout()); curl_setopt($conn, CURLOPT_FORBID_REUSE, 0); $proxy = $connection->getProxy(); if (!is_null($proxy)) { curl_setopt($conn, CURLOPT_PROXY, $proxy); } $this->_setupCurl($conn); $headersConfig = $connection->hasConfig('headers') ? $connection->getConfig('headers') : array(); if (!empty($headersConfig)) { $headers = array(); while (list($header, $headerValue) = each($headersConfig)) { array_push($headers, $header . ': ' . $headerValue); } curl_setopt($conn, CURLOPT_HTTPHEADER, $headers); } // TODO: REFACTOR $data = $request->getData(); $httpMethod = $request->getMethod(); if (!empty($data) || '0' === $data) { if ($this->hasParam('postWithRequestBody') && $this->getParam('postWithRequestBody') == true) { $httpMethod = Request::POST; } if (is_array($data)) { $content = JSON::stringify($data, 'JSON_ELASTICSEARCH'); } else { $content = $data; } // Escaping of / not necessary. Causes problems in base64 encoding of files $content = str_replace('\\/', '/', $content); curl_setopt($conn, CURLOPT_POSTFIELDS, $content); } else { curl_setopt($conn, CURLOPT_POSTFIELDS, ''); } curl_setopt($conn, CURLOPT_NOBODY, $httpMethod == 'HEAD'); curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $httpMethod); if (defined('DEBUG') && DEBUG) { // Track request headers when in debug mode curl_setopt($conn, CURLINFO_HEADER_OUT, true); } $start = microtime(true); // cURL opt returntransfer leaks memory, therefore OB instead. ob_start(); curl_exec($conn); $responseString = ob_get_clean(); $end = microtime(true); // Checks if error exists $errorNumber = curl_errno($conn); $response = new Response($responseString, curl_getinfo($this->_getConnection(), CURLINFO_HTTP_CODE)); if (defined('DEBUG') && DEBUG) { $response->setQueryTime($end - $start); } $response->setTransferInfo(curl_getinfo($conn)); if ($response->hasError()) { throw new ResponseException($request, $response); } if ($response->hasFailedShards()) { throw new PartialShardFailureException($request, $response); } if ($errorNumber > 0) { throw new HttpException($errorNumber, $request, $response); } return $response; }
/** * Makes calls to the elasticsearch server. * * All calls that are made to the server are done through this function * * @param \Elastica\Request $request * @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(Request $request, array $params) { $connection = $this->getConnection(); $client = $this->_getGuzzleClient($this->_getBaseUrl($connection), $connection->isPersistent()); $options = array('exceptions' => false); if ($connection->getTimeout()) { $options['timeout'] = $connection->getTimeout(); } $proxy = $connection->getProxy(); // See: https://github.com/facebook/hhvm/issues/4875 if (is_null($proxy) && defined('HHVM_VERSION')) { $proxy = getenv('http_proxy') ?: null; } if (!is_null($proxy)) { $options['proxy'] = $proxy; } $req = $client->createRequest($request->getMethod(), $this->_getActionPath($request), $options); $req->setHeaders($connection->hasConfig('headers') ? $connection->getConfig('headers') : array()); $data = $request->getData(); if (!empty($data) || '0' === $data) { if ($req->getMethod() == Request::GET) { $req->setMethod(Request::POST); } if ($this->hasParam('postWithRequestBody') && $this->getParam('postWithRequestBody') == true) { $request->setMethod(Request::POST); $req->setMethod(Request::POST); } if (is_array($data)) { $content = JSON::stringify($data, 'JSON_ELASTICSEARCH'); } else { $content = $data; } $req->setBody(Stream::factory($content)); } try { $start = microtime(true); $res = $client->send($req); $end = microtime(true); } catch (TransferException $ex) { throw new GuzzleException($ex, $request, new Response($ex->getMessage())); } $response = new Response((string) $res->getBody(), $res->getStatusCode()); $response->setQueryTime($end - $start); $response->setTransferInfo(array('request_header' => $request->getMethod(), 'http_code' => $res->getStatusCode())); if ($response->hasError()) { throw new ResponseException($request, $response); } if ($response->hasFailedShards()) { throw new PartialShardFailureException($request, $response); } return $response; }
/** * Makes calls to the elasticsearch server * * All calls that are made to the server are done through this function * * @param \Elastica\Request $request * @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(Request $request, array $params) { $connection = $this->getConnection(); $conn = $this->_getConnection($connection->isPersistent()); // If url is set, url is taken. Otherwise port, host and path $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); } curl_setopt($conn, CURLOPT_URL, $baseUri); curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout()); curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $request->getMethod()); curl_setopt($conn, CURLOPT_FORBID_REUSE, 0); $this->_setupCurl($conn); $headersConfig = $connection->hasConfig('headers') ? $connection->getConfig('headers') : array(); if (!empty($headersConfig)) { $headers = array(); while (list($header, $headerValue) = each($headersConfig)) { array_push($headers, $header . ': ' . $headerValue); } curl_setopt($conn, CURLOPT_HTTPHEADER, $headers); } // TODO: REFACTOR $data = $request->getData(); if (isset($data) && !empty($data)) { if (is_array($data)) { $content = json_encode($data); } else { $content = $data; } // Escaping of / not necessary. Causes problems in base64 encoding of files $content = str_replace('\\/', '/', $content); curl_setopt($conn, CURLOPT_POSTFIELDS, $content); } $start = microtime(true); // cURL opt returntransfer leaks memory, therefore OB instead. ob_start(); curl_exec($conn); $responseString = ob_get_clean(); $end = microtime(true); // Checks if error exists $errorNumber = curl_errno($conn); $response = new Response($responseString); if (defined('DEBUG') && DEBUG) { $response->setQueryTime($end - $start); $response->setTransferInfo(curl_getinfo($conn)); } if ($response->hasError()) { throw new ResponseException($request, $response); } if ($errorNumber > 0) { throw new HttpException($errorNumber, $request, $response); } return $response; }
/** * Makes calls to the elasticsearch server * * All calls that are made to the server are done through this function * * @param \Elastica\Request $request * @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(Request $request, array $params) { $connection = $this->getConnection(); try { $client = $this->_getGuzzleClient($this->_getBaseUrl($connection), $connection->isPersistent()); $options = array(); if ($connection->getTimeout()) { $options['timeout'] = $connection->getTimeout(); } if ($connection->getProxy()) { $options['proxy'] = $connection->getProxy(); } $req = $client->createRequest($request->getMethod(), $this->_getActionPath($request), $options); $req->setHeaders($connection->hasConfig('headers') ?: array()); $data = $request->getData(); if (isset($data) && !empty($data)) { if ($req->getMethod() == Request::GET) { $req->setMethod(Request::POST); } if ($this->hasParam('postWithRequestBody') && $this->getParam('postWithRequestBody') == true) { $request->setMethod(Request::POST); $req->setMethod(Request::POST); } if (is_array($data)) { $content = JSON::stringify($data, 'JSON_ELASTICSEARCH'); } else { $content = $data; } $req->setBody(Stream::factory($content)); } $start = microtime(true); $res = $client->send($req); $end = microtime(true); $response = new Response((string) $res->getBody(), $res->getStatusCode()); if (defined('DEBUG') && DEBUG) { $response->setQueryTime($end - $start); } $response->setTransferInfo(array('request_header' => $request->getMethod(), 'http_code' => $res->getStatusCode())); if ($response->hasError()) { throw new ResponseException($request, $response); } if ($response->hasFailedShards()) { throw new PartialShardFailureException($request, $response); } return $response; } catch (ClientException $e) { // ignore 4xx errors } catch (TransferException $e) { throw new GuzzleException($e, $request, new Response($e->getMessage())); } }