vendor/ruflin/elastica/lib/Elastica/Transport/Http.php line 187

Open in your IDE?
  1. <?php
  2. namespace Elastica\Transport;
  3. use Elastica\Exception\Connection\HttpException;
  4. use Elastica\Exception\PartialShardFailureException;
  5. use Elastica\Exception\ResponseException;
  6. use Elastica\JSON;
  7. use Elastica\Request;
  8. use Elastica\Response;
  9. use Elastica\Util;
  10. /**
  11.  * Elastica Http Transport object.
  12.  *
  13.  * @author Nicolas Ruflin <spam@ruflin.com>
  14.  */
  15. class Http extends AbstractTransport
  16. {
  17.     /**
  18.      * Http scheme.
  19.      *
  20.      * @var string Http scheme
  21.      */
  22.     protected $_scheme 'http';
  23.     /**
  24.      * Curl resource to reuse.
  25.      *
  26.      * @var resource Curl resource to reuse
  27.      */
  28.     protected static $_curlConnection;
  29.     /**
  30.      * Makes calls to the elasticsearch server.
  31.      *
  32.      * All calls that are made to the server are done through this function
  33.      *
  34.      * @param \Elastica\Request $request
  35.      * @param array             $params  Host, Port, ...
  36.      *
  37.      * @throws \Elastica\Exception\ConnectionException
  38.      * @throws \Elastica\Exception\ResponseException
  39.      * @throws \Elastica\Exception\Connection\HttpException
  40.      *
  41.      * @return \Elastica\Response Response object
  42.      */
  43.     public function exec(Request $request, array $params)
  44.     {
  45.         $connection $this->getConnection();
  46.         $conn $this->_getConnection($connection->isPersistent());
  47.         // If url is set, url is taken. Otherwise port, host and path
  48.         $url $connection->hasConfig('url') ? $connection->getConfig('url') : '';
  49.         if (!empty($url)) {
  50.             $baseUri $url;
  51.         } else {
  52.             $baseUri $this->_scheme.'://'.$connection->getHost().':'.$connection->getPort().'/'.$connection->getPath();
  53.         }
  54.         $requestPath $request->getPath();
  55.         if (!Util::isDateMathEscaped($requestPath)) {
  56.             $requestPath Util::escapeDateMath($requestPath);
  57.         }
  58.         $baseUri .= $requestPath;
  59.         $query $request->getQuery();
  60.         if (!empty($query)) {
  61.             $baseUri .= '?'.http_build_query($query);
  62.         }
  63.         curl_setopt($connCURLOPT_URL$baseUri);
  64.         curl_setopt($connCURLOPT_TIMEOUT$connection->getTimeout());
  65.         curl_setopt($connCURLOPT_FORBID_REUSE0);
  66.         // Tell ES that we support the compressed responses
  67.         // An "Accept-Encoding" header containing all supported encoding types is sent
  68.         // curl will decode the response automatically if the response is encoded
  69.         curl_setopt($connCURLOPT_ENCODING'');
  70.         /* @see Connection::setConnectTimeout() */
  71.         $connectTimeout $connection->getConnectTimeout();
  72.         if ($connectTimeout 0) {
  73.             curl_setopt($connCURLOPT_CONNECTTIMEOUT$connectTimeout);
  74.         }
  75.         $proxy $connection->getProxy();
  76.         // See: https://github.com/facebook/hhvm/issues/4875
  77.         if (is_null($proxy) && defined('HHVM_VERSION')) {
  78.             $proxy getenv('http_proxy') ?: null;
  79.         }
  80.         if (!is_null($proxy)) {
  81.             curl_setopt($connCURLOPT_PROXY$proxy);
  82.         }
  83.         $username $connection->getUsername();
  84.         $password $connection->getPassword();
  85.         if (!is_null($username) && !is_null($password)) {
  86.             curl_setopt($connCURLOPT_HTTPAUTHCURLAUTH_ANY);
  87.             curl_setopt($connCURLOPT_USERPWD"$username:$password");
  88.         }
  89.         $this->_setupCurl($conn);
  90.         $headersConfig $connection->hasConfig('headers') ? $connection->getConfig('headers') : [];
  91.         $headers = [];
  92.         if (!empty($headersConfig)) {
  93.             $headers = [];
  94.             foreach ($headersConfig as $header => $headerValue) {
  95.                 array_push($headers$header.': '.$headerValue);
  96.             }
  97.         }
  98.         // TODO: REFACTOR
  99.         $data $request->getData();
  100.         $httpMethod $request->getMethod();
  101.         if (!empty($data) || '0' === $data) {
  102.             if ($this->hasParam('postWithRequestBody') && $this->getParam('postWithRequestBody') == true) {
  103.                 $httpMethod Request::POST;
  104.             }
  105.             if (is_array($data)) {
  106.                 $content JSON::stringify($dataJSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  107.             } else {
  108.                 $content $data;
  109.                 // Escaping of / not necessary. Causes problems in base64 encoding of files
  110.                 $content str_replace('\/''/'$content);
  111.             }
  112.             array_push($headerssprintf('Content-Type: %s'$request->getContentType()));
  113.             if ($connection->hasCompression()) {
  114.                 // Compress the body of the request ...
  115.                 curl_setopt($connCURLOPT_POSTFIELDSgzencode($content));
  116.                 // ... and tell ES that it is compressed
  117.                 array_push($headers'Content-Encoding: gzip');
  118.             } else {
  119.                 curl_setopt($connCURLOPT_POSTFIELDS$content);
  120.             }
  121.         } else {
  122.             curl_setopt($connCURLOPT_POSTFIELDS'');
  123.         }
  124.         curl_setopt($connCURLOPT_HTTPHEADER$headers);
  125.         curl_setopt($connCURLOPT_NOBODY$httpMethod == 'HEAD');
  126.         curl_setopt($connCURLOPT_CUSTOMREQUEST$httpMethod);
  127.         $start microtime(true);
  128.         // cURL opt returntransfer leaks memory, therefore OB instead.
  129.         ob_start();
  130.         curl_exec($conn);
  131.         $responseString ob_get_clean();
  132.         $end microtime(true);
  133.         // Checks if error exists
  134.         $errorNumber curl_errno($conn);
  135.         $response = new Response($responseStringcurl_getinfo($connCURLINFO_HTTP_CODE));
  136.         $response->setQueryTime($end $start);
  137.         $response->setTransferInfo(curl_getinfo($conn));
  138.         if ($connection->hasConfig('bigintConversion')) {
  139.             $response->setJsonBigintConversion($connection->getConfig('bigintConversion'));
  140.         }
  141.         if ($response->hasError()) {
  142.             throw new ResponseException($request$response);
  143.         }
  144.         if ($response->hasFailedShards()) {
  145.             throw new PartialShardFailureException($request$response);
  146.         }
  147.         if ($errorNumber 0) {
  148.             throw new HttpException($errorNumber$request$response);
  149.         }
  150.         return $response;
  151.     }
  152.     /**
  153.      * Called to add additional curl params.
  154.      *
  155.      * @param resource $curlConnection Curl connection
  156.      */
  157.     protected function _setupCurl($curlConnection)
  158.     {
  159.         if ($this->getConnection()->hasConfig('curl')) {
  160.             foreach ($this->getConnection()->getConfig('curl') as $key => $param) {
  161.                 curl_setopt($curlConnection$key$param);
  162.             }
  163.         }
  164.     }
  165.     /**
  166.      * Return Curl resource.
  167.      *
  168.      * @param bool $persistent False if not persistent connection
  169.      *
  170.      * @return resource Connection resource
  171.      */
  172.     protected function _getConnection($persistent true)
  173.     {
  174.         if (!$persistent || !self::$_curlConnection) {
  175.             self::$_curlConnection curl_init();
  176.         }
  177.         return self::$_curlConnection;
  178.     }
  179. }