Guzzle.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace Elastica\Transport;
  3. use Elastica\Connection;
  4. use Elastica\Exception\Connection\GuzzleException;
  5. use Elastica\Exception\PartialShardFailureException;
  6. use Elastica\Exception\ResponseException;
  7. use Elastica\JSON;
  8. use Elastica\Request;
  9. use Elastica\Response;
  10. use Elastica\Util;
  11. use GuzzleHttp\Client;
  12. use GuzzleHttp\Exception\TransferException;
  13. use GuzzleHttp\Psr7;
  14. use GuzzleHttp\Psr7\Uri;
  15. /**
  16. * Elastica Guzzle Transport object.
  17. *
  18. * @author Milan Magudia <milan@magudia.com>
  19. */
  20. class Guzzle extends AbstractTransport
  21. {
  22. /**
  23. * Http scheme.
  24. *
  25. * @var string Http scheme
  26. */
  27. protected $_scheme = 'http';
  28. /**
  29. * Curl resource to reuse.
  30. *
  31. * @var Client Guzzle client to reuse
  32. */
  33. protected static $_guzzleClientConnection;
  34. /**
  35. * Makes calls to the elasticsearch server.
  36. *
  37. * All calls that are made to the server are done through this function
  38. *
  39. * @param \Elastica\Request $request
  40. * @param array $params Host, Port, ...
  41. *
  42. * @throws \Elastica\Exception\ConnectionException
  43. * @throws \Elastica\Exception\ResponseException
  44. * @throws \Elastica\Exception\Connection\HttpException
  45. *
  46. * @return \Elastica\Response Response object
  47. */
  48. public function exec(Request $request, array $params)
  49. {
  50. $connection = $this->getConnection();
  51. $client = $this->_getGuzzleClient($this->_getBaseUrl($connection), $connection->isPersistent(), $request);
  52. $options = [
  53. 'exceptions' => false, // 4xx and 5xx is expected and NOT an exceptions in this context
  54. ];
  55. if ($connection->getTimeout()) {
  56. $options['timeout'] = $connection->getTimeout();
  57. }
  58. $proxy = $connection->getProxy();
  59. // See: https://github.com/facebook/hhvm/issues/4875
  60. if (is_null($proxy) && defined('HHVM_VERSION')) {
  61. $proxy = getenv('http_proxy') ?: null;
  62. }
  63. if (!is_null($proxy)) {
  64. $options['proxy'] = $proxy;
  65. }
  66. $req = $this->_createPsr7Request($request, $connection);
  67. try {
  68. $start = microtime(true);
  69. $res = $client->send($req, $options);
  70. $end = microtime(true);
  71. } catch (TransferException $ex) {
  72. throw new GuzzleException($ex, $request, new Response($ex->getMessage()));
  73. }
  74. $responseBody = (string) $res->getBody();
  75. $response = new Response($responseBody, $res->getStatusCode());
  76. $response->setQueryTime($end - $start);
  77. if ($connection->hasConfig('bigintConversion')) {
  78. $response->setJsonBigintConversion($connection->getConfig('bigintConversion'));
  79. }
  80. $response->setTransferInfo(
  81. [
  82. 'request_header' => $request->getMethod(),
  83. 'http_code' => $res->getStatusCode(),
  84. ]
  85. );
  86. if ($response->hasError()) {
  87. throw new ResponseException($request, $response);
  88. }
  89. if ($response->hasFailedShards()) {
  90. throw new PartialShardFailureException($request, $response);
  91. }
  92. return $response;
  93. }
  94. /**
  95. * @param Request $request
  96. * @param Connection $connection
  97. *
  98. * @return Psr7\Request
  99. */
  100. protected function _createPsr7Request(Request $request, Connection $connection)
  101. {
  102. $req = new Psr7\Request(
  103. $request->getMethod(),
  104. $this->_getActionPath($request),
  105. $connection->hasConfig('headers') && is_array($connection->getConfig('headers'))
  106. ? $connection->getConfig('headers')
  107. : []
  108. );
  109. $data = $request->getData();
  110. if (!empty($data) || '0' === $data) {
  111. if (Request::GET == $req->getMethod()) {
  112. $req = $req->withMethod(Request::POST);
  113. }
  114. if ($this->hasParam('postWithRequestBody') && true == $this->getParam('postWithRequestBody')) {
  115. $request->setMethod(Request::POST);
  116. $req = $req->withMethod(Request::POST);
  117. }
  118. $req = $req->withBody(
  119. Psr7\stream_for(is_array($data)
  120. ? JSON::stringify($data, JSON_UNESCAPED_UNICODE)
  121. : $data
  122. )
  123. );
  124. }
  125. return $req;
  126. }
  127. /**
  128. * Return Guzzle resource.
  129. *
  130. * @param string $baseUrl
  131. * @param bool $persistent False if not persistent connection
  132. * @param Request $request Elastica Request Object
  133. *
  134. * @return Client
  135. */
  136. protected function _getGuzzleClient($baseUrl, $persistent = true, Request $request)
  137. {
  138. if (!$persistent || !self::$_guzzleClientConnection) {
  139. self::$_guzzleClientConnection = new Client([
  140. 'base_uri' => $baseUrl,
  141. 'headers' => [
  142. 'Content-Type' => $request->getContentType(),
  143. ],
  144. ]);
  145. }
  146. return self::$_guzzleClientConnection;
  147. }
  148. /**
  149. * Builds the base url for the guzzle connection.
  150. *
  151. * @param \Elastica\Connection $connection
  152. *
  153. * @return string
  154. */
  155. protected function _getBaseUrl(Connection $connection)
  156. {
  157. // If url is set, url is taken. Otherwise port, host and path
  158. $url = $connection->hasConfig('url') ? $connection->getConfig('url') : '';
  159. if (!empty($url)) {
  160. $baseUri = $url;
  161. } else {
  162. $baseUri = (string) Uri::fromParts([
  163. 'scheme' => $this->_scheme,
  164. 'host' => $connection->getHost(),
  165. 'port' => $connection->getPort(),
  166. 'path' => ltrim('/', $connection->getPath()),
  167. ]);
  168. }
  169. return rtrim($baseUri, '/');
  170. }
  171. /**
  172. * Builds the action path url for each request.
  173. *
  174. * @param \Elastica\Request $request
  175. *
  176. * @return string
  177. */
  178. protected function _getActionPath(Request $request)
  179. {
  180. $action = $request->getPath();
  181. if ($action) {
  182. $action = '/'.ltrim($action, '/');
  183. }
  184. if (!Util::isDateMathEscaped($action)) {
  185. $action = Util::escapeDateMath($action);
  186. }
  187. $query = $request->getQuery();
  188. if (!empty($query)) {
  189. $action .= '?'.http_build_query(
  190. $this->sanityzeQueryStringBool($query)
  191. );
  192. }
  193. return $action;
  194. }
  195. }