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