1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Manticoresearch\Transport; |
4
|
|
|
|
5
|
|
|
use Manticoresearch\Exceptions\ConnectionException; |
6
|
|
|
use Manticoresearch\Exceptions\ResponseException; |
7
|
|
|
use Manticoresearch\Request; |
8
|
|
|
use Manticoresearch\Response; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Http |
12
|
|
|
* @package Manticoresearch\Transport |
13
|
|
|
*/ |
14
|
|
|
class Http extends \Manticoresearch\Transport implements TransportInterface |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $scheme = 'http'; |
21
|
|
|
|
22
|
|
|
protected static $curl = null; |
23
|
|
|
|
24
|
|
|
public function execute(Request $request, $params = []) |
25
|
|
|
{ |
26
|
|
|
$connection = $this->getConnection(); |
27
|
|
|
$conn = $this->getCurlConnection($connection->getConfig('persistent')); |
|
|
|
|
28
|
|
|
$url = $this->scheme . '://' . $connection->getHost() . ':' . $connection->getPort() . $connection->getPath(); |
29
|
|
|
$endpoint = $request->getPath(); |
30
|
|
|
$url .= $endpoint; |
31
|
|
|
$url = $this->setupURI($url, $request->getQuery()); |
|
|
|
|
32
|
|
|
|
33
|
|
|
curl_setopt($conn, CURLOPT_URL, $url); |
34
|
|
|
curl_setopt($conn, CURLOPT_TIMEOUT, $connection->getTimeout()); |
35
|
|
|
curl_setopt($conn, CURLOPT_ENCODING, ''); |
36
|
|
|
curl_setopt($conn, CURLOPT_FORBID_REUSE, 0); |
37
|
|
|
$data = $request->getBody(); |
38
|
|
|
$method = $request->getMethod(); |
39
|
|
|
$headers = $connection->getHeaders(); |
40
|
|
|
$headers[] = sprintf('Content-Type: %s', $request->getContentType()); |
41
|
|
|
if (!empty($data)) { |
42
|
|
|
if (is_array($data)) { |
43
|
|
|
$content = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
44
|
|
|
} else { |
45
|
|
|
$content = $data; |
46
|
|
|
} |
47
|
|
|
curl_setopt($conn, CURLOPT_POSTFIELDS, $content); |
48
|
|
|
} else { |
49
|
|
|
curl_setopt($conn, CURLOPT_POSTFIELDS, ''); |
50
|
|
|
} |
51
|
|
|
curl_setopt($conn, CURLOPT_CUSTOMREQUEST, $method); |
52
|
|
|
curl_setopt($conn, CURLOPT_HTTPHEADER, $headers); |
53
|
|
|
|
54
|
|
|
if ($connection->getConnectTimeout()>0) { |
55
|
|
|
curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, $connection->getConnectTimeout()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($connection->getConfig('username') !== null && $connection->getConfig('password') !== null) { |
59
|
|
|
curl_setopt($conn, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
60
|
|
|
curl_setopt( |
61
|
|
|
$conn, |
62
|
|
|
CURLOPT_USERPWD, |
63
|
|
|
$connection->getConfig('username').":".$connection->getConfig('password') |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
if ($connection->getConfig('proxy') !== null) { |
67
|
|
|
curl_setopt($conn, CURLOPT_PROXY, $connection->getConfig('proxy')); |
68
|
|
|
} |
69
|
|
|
if (!empty($connection->getConfig('curl'))) { |
70
|
|
|
foreach ($connection->getConfig('curl') as $k => $v) { |
71
|
|
|
curl_setopt($conn, $k, $v); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
$start = microtime(true); |
75
|
|
|
ob_start(); |
76
|
|
|
curl_exec($conn); |
77
|
|
|
$responseString = \ob_get_clean(); |
78
|
|
|
$end = microtime(true); |
79
|
|
|
$errorno = curl_errno($conn); |
80
|
|
|
$status = curl_getinfo($conn, CURLINFO_HTTP_CODE); |
81
|
|
|
if (isset($params['responseClass'])) { |
82
|
|
|
$responseClass = $params['responseClass']; |
83
|
|
|
$responseClassParams = isset($params['responseClassParams'])?$params['responseClassParams']:[]; |
84
|
|
|
$response = new $responseClass($responseString, $status, $responseClassParams); |
85
|
|
|
} else { |
86
|
|
|
$response = new Response($responseString, $status); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$time = $end-$start; |
90
|
|
|
$response->setTime($time); |
91
|
|
|
$response->setTransportInfo([ |
92
|
|
|
'url' => $url, |
93
|
|
|
'headers' => $headers, |
94
|
|
|
'body' => $request->getBody() |
95
|
|
|
]); |
96
|
|
|
//hard error |
97
|
|
|
if ($errorno>0) { |
98
|
|
|
$error = curl_error($conn); |
99
|
|
|
|
100
|
|
|
static::$curl = null; |
101
|
|
|
throw new ConnectionException($error, $request); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
$this->logger->debug('Request body:', [ |
106
|
|
|
'connection' => $connection->getConfig(), |
107
|
|
|
'payload' => $request->getBody() |
108
|
|
|
]); |
109
|
|
|
$this->logger->info('Request:', [ |
110
|
|
|
'url' => $url, |
111
|
|
|
'status' => $status, |
112
|
|
|
'time' => $time, |
113
|
|
|
]); |
114
|
|
|
$this->logger->debug('Response body:', [json_decode($responseString, true)]); |
115
|
|
|
//soft error |
116
|
|
|
if ($response->hasError()) { |
117
|
|
|
$this->logger->error('Response:', [ |
118
|
|
|
'url' => $url, |
119
|
|
|
'error' => $response->getError(), |
120
|
|
|
'payload' => $request->getBody(), |
121
|
|
|
]); |
122
|
|
|
throw new ResponseException($request, $response); |
123
|
|
|
} |
124
|
|
|
return $response; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
protected function getCurlConnection(bool $persistent = true) |
128
|
|
|
{ |
129
|
|
|
if (!$persistent || !static::$curl) { |
130
|
|
|
static::$curl = curl_init(); |
131
|
|
|
} |
132
|
|
|
return static::$curl; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|