Author: Nicolas Ruflin (spam@ruflin.com)
Inheritance: extends Param
Beispiel #1
0
 /**
  * @group unit
  * @expectedException \Elastica\Exception\ConnectionException
  */
 public function testInvalidConnection()
 {
     $connection = new Connection(array('port' => 9999));
     $request = new Request('_stats', Request::GET);
     $request->setConnection($connection);
     // Throws exception because no valid connection
     $request->send();
 }
 /**
  * @group unit
  */
 public function testToString()
 {
     $path = 'test';
     $method = Request::POST;
     $query = array('no' => 'params');
     $data = array('key' => 'value');
     $connection = new Connection();
     $connection->setHost($this->_getHost());
     $connection->setPort('9200');
     $request = new Request($path, $method, $data, $query, $connection);
     $data = $request->toArray();
     $this->assertInternalType('array', $data);
     $this->assertArrayHasKey('method', $data);
     $this->assertArrayHasKey('path', $data);
     $this->assertArrayHasKey('query', $data);
     $this->assertArrayHasKey('data', $data);
     $this->assertArrayHasKey('connection', $data);
     $this->assertEquals($request->getMethod(), $data['method']);
     $this->assertEquals($request->getPath(), $data['path']);
     $this->assertEquals($request->getQuery(), $data['query']);
     $this->assertEquals($request->getData(), $data['data']);
     $this->assertInternalType('array', $data['connection']);
     $this->assertArrayHasKey('host', $data['connection']);
     $this->assertArrayHasKey('port', $data['connection']);
     $this->assertEquals($request->getConnection()->getHost(), $data['connection']['host']);
     $this->assertEquals($request->getConnection()->getPort(), $data['connection']['port']);
     $string = $request->toString();
     $this->assertInternalType('string', $string);
     $string = (string) $request;
     $this->assertInternalType('string', $string);
 }
Beispiel #3
0
 /**
  * Makes calls to the elasticsearch server
  *
  * @param \Elastica\Request $request
  * @param  array                               $params Host, Port, ...
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\InvalidException
  * @return \Elastica\Response                   Response object
  */
 public function exec(Request $request, array $params)
 {
     $memcache = new \Memcache();
     $memcache->connect($this->getConnection()->getHost(), $this->getConnection()->getPort());
     // Finds right function name
     $function = strtolower($request->getMethod());
     $data = $request->getData();
     $content = '';
     if (!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);
     }
     $responseString = '';
     switch ($function) {
         case 'post':
         case 'put':
             $memcache->set($request->getPath(), $content);
             break;
         case 'get':
             $responseString = $memcache->get($request->getPath() . '?source=' . $content);
             echo $responseString . PHP_EOL;
             break;
         case 'delete':
             break;
         default:
             throw new InvalidException('Method ' . $function . ' is not supported in memcache transport');
     }
     $response = new Response($responseString);
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($response->hasFailedShards()) {
         throw new PartialShardFailureException($request, $response);
     }
     return $response;
 }
Beispiel #4
0
 /**
  * Makes calls to the elasticsearch server.
  *
  * @param \Elastica\Request $request
  * @param array             $params  Host, Port, ...
  *
  * @throws \Elastica\Exception\ResponseException
  * @throws \Elastica\Exception\InvalidException
  *
  * @return \Elastica\Response Response object
  */
 public function exec(Request $request, array $params)
 {
     $memcache = new \Memcache();
     $memcache->connect($this->getConnection()->getHost(), $this->getConnection()->getPort());
     $data = $request->getData();
     $content = '';
     if (!empty($data) || '0' === $data) {
         if (is_array($data)) {
             $content = JSON::stringify($data);
         } else {
             $content = $data;
         }
         // Escaping of / not necessary. Causes problems in base64 encoding of files
         $content = str_replace('\\/', '/', $content);
     }
     $responseString = '';
     $start = microtime(true);
     switch ($request->getMethod()) {
         case Request::POST:
         case Request::PUT:
             $key = $request->getPath();
             $this->_checkKeyLength($key);
             $memcache->set($key, $content);
             break;
         case Request::GET:
             $key = $request->getPath() . '?source=' . $content;
             $this->_checkKeyLength($key);
             $responseString = $memcache->get($key);
             break;
         case Request::DELETE:
             $key = $request->getPath() . '?source=' . $content;
             $this->_checkKeyLength($key);
             $responseString = $memcache->delete($key);
             break;
         default:
         case Request::HEAD:
             throw new InvalidException('Method ' . $request->getMethod() . ' is not supported in memcache transport');
     }
     $end = microtime(true);
     $response = new Response($responseString);
     if (\Elastica\Util::debugEnabled()) {
         $response->setQueryTime($end - $start);
     }
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($response->hasFailedShards()) {
         throw new PartialShardFailureException($request, $response);
     }
     return $response;
 }
Beispiel #5
0
 /**
  * Makes calls to the elasticsearch server
  *
  * @param \Elastica\Request $request
  * @param  array             $params Host, Port, ...
  * @throws \Elastica\Exception\Connection\ThriftException
  * @throws \Elastica\Exception\ResponseException
  * @return \Elastica\Response Response object
  */
 public function exec(Request $request, array $params)
 {
     $connection = $this->getConnection();
     $sendTimeout = $connection->hasConfig('sendTimeout') ? $connection->getConfig('sendTimeout') : null;
     $recvTimeout = $connection->hasConfig('recvTimeout') ? $connection->getConfig('recvTimeout') : null;
     $framedTransport = $connection->hasConfig('framedTransport') ? (bool) $connection->getConfig('framedTransport') : false;
     try {
         $client = $this->_getClient($connection->getHost(), $connection->getPort(), $sendTimeout, $recvTimeout, $framedTransport);
         $restRequest = new RestRequest();
         $restRequest->method = array_search($request->getMethod(), Method::$__names);
         $restRequest->uri = $request->getPath();
         $query = $request->getQuery();
         if (!empty($query)) {
             $restRequest->parameters = $query;
         }
         $data = $request->getData();
         if (!empty($data)) {
             if (is_array($data)) {
                 $content = JSON::stringify($data);
             } else {
                 $content = $data;
             }
             $restRequest->body = $content;
         }
         /* @var $result RestResponse */
         $start = microtime(true);
         $result = $client->execute($restRequest);
         $response = new Response($result->body);
         $end = microtime(true);
     } catch (TException $e) {
         $response = new Response('');
         throw new ThriftException($e, $request, $response);
     }
     if (defined('DEBUG') && DEBUG) {
         $response->setQueryTime($end - $start);
     }
     if ($response->hasError()) {
         throw new ResponseException($request, $response);
     }
     if ($response->hasFailedShards()) {
         throw new PartialShardFailureException($request, $response);
     }
     return $response;
 }
Beispiel #6
0
 /**
  * logging
  *
  * @throws Exception\RuntimeException
  *
  * @param string|\Elastica\Request $context
  */
 protected function _log($context)
 {
     $log = $this->getConfig('log');
     if ($log && !class_exists('Psr\\Log\\AbstractLogger')) {
         throw new RuntimeException('Class Psr\\Log\\AbstractLogger not found');
     } elseif (!$this->_logger && $log) {
         $this->setLogger(new Log($this->getConfig('log')));
     }
     if ($this->_logger) {
         if ($context instanceof Request) {
             $data = $context->toArray();
         } else {
             $data = array('message' => $context);
         }
         $this->_logger->debug('logging Request', $data);
     }
 }
Beispiel #7
0
 /**
  * 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;
 }
Beispiel #8
0
 /**
  * Converts Request to Curl console command
  *
  * @param Request $request
  * @return string
  */
 public static function convertRequestToCurlCommand(Request $request)
 {
     $message = 'curl -X' . strtoupper($request->getMethod()) . ' ';
     $message .= '\'http://' . $request->getConnection()->getHost() . ':' . $request->getConnection()->getPort() . '/';
     $message .= $request->getPath();
     $query = $request->getQuery();
     if (!empty($query)) {
         $message .= '?' . http_build_query($query);
     }
     $message .= '\'';
     $data = $request->getData();
     if (!empty($data)) {
         $message .= ' -d \'' . JSON::stringify($data) . '\'';
     }
     return $message;
 }
 /**
  * @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;
 }
 /**
  * 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;
 }
Beispiel #11
0
 /**
  * Builds the action path url for each request.
  *
  * @param \Elastica\Request $request
  *
  * @return string
  */
 protected function _getActionPath(Request $request)
 {
     $action = $request->getPath();
     if ($action) {
         $action = '/' . ltrim($action, '/');
     }
     $query = $request->getQuery();
     if (!empty($query)) {
         $action .= '?' . http_build_query($query);
     }
     return $action;
 }
Beispiel #12
0
 /**
  * Log requests to Elastic Search.
  *
  * @param Request|array $context The context of the request made.
  * @return void
  */
 protected function _log($context)
 {
     if (!$this->logQueries) {
         return;
     }
     if (!isset($this->_logger)) {
         $this->_logger = Log::engine('elasticsearch') ?: new ElasticaLog();
     }
     if ($context instanceof Request) {
         $data = $context->toArray();
     } else {
         $data = ['message' => $context];
     }
     $logData = ['method' => $data['method'], 'path' => $data['path'], 'data' => $data['data']];
     $data = json_encode($logData, JSON_PRETTY_PRINT);
     $loggedQuery = new LoggedQuery();
     $loggedQuery->query = $data;
     if ($this->_logger instanceof \Psr\Log\LoggerInterface) {
         $this->_logger->log('debug', $loggedQuery);
     } else {
         $this->_logger->log($loggedQuery);
     }
 }
Beispiel #13
0
 /**
  * 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;
 }
 /**
  * Log requests to Elastic Search.
  *
  * @param Request|array $context The context of the request made.
  * @return void
  */
 protected function _log($context)
 {
     if (!$this->logQueries) {
         return;
     }
     if ($context instanceof Request) {
         $data = $context->toArray();
     } else {
         $data = ['message' => $context];
     }
     $logData = ['method' => $data['method'], 'path' => $data['path'], 'data' => $data['data']];
     $data = json_encode($logData, JSON_PRETTY_PRINT);
     $loggedQuery = new LoggedQuery();
     $loggedQuery->query = $data;
     $this->_logger->log($loggedQuery);
 }