-
Notifications
You must be signed in to change notification settings - Fork 301
/
Curl.php
234 lines (209 loc) · 7.08 KB
/
Curl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
namespace Solarium\Core\Client\Adapter;
use Solarium\Core\Client\Endpoint;
use Solarium\Core\Client\Request;
use Solarium\Core\Client\Response;
use Solarium\Core\Configurable;
use Solarium\Exception\HttpException;
use Solarium\Exception\InvalidArgumentException;
use Solarium\Exception\RuntimeException;
/**
* cURL HTTP adapter.
*
* @author Intervals <info@myintervals.com>
*/
class Curl extends Configurable implements AdapterInterface
{
/**
* Execute a Solr request using the cURL Http.
*
* @param Request $request
* @param Endpoint $endpoint
*
* @return Response
*/
public function execute($request, $endpoint)
{
return $this->getData($request, $endpoint);
}
/**
* Get the response for a curl handle.
*
* @param resource $handle
* @param string $httpResponse
*
* @return Response
*/
public function getResponse($handle, $httpResponse)
{
// @codeCoverageIgnoreStart
if (false !== $httpResponse && null !== $httpResponse) {
$data = $httpResponse;
$info = curl_getinfo($handle);
$headers = [];
$headers[] = 'HTTP/1.1 '.$info['http_code'].' OK';
} else {
$headers = [];
$data = '';
}
$this->check($data, $headers, $handle);
curl_close($handle);
return new Response($data, $headers);
// @codeCoverageIgnoreEnd
}
/**
* Create curl handle for a request.
*
*
* @param Request $request
* @param Endpoint $endpoint
*
* @throws InvalidArgumentException
*
* @return resource
*/
public function createHandle($request, $endpoint)
{
// @codeCoverageIgnoreStart
$baseUri = $request->getIsServerRequest() ? $endpoint->getServerUri() : $endpoint->getCoreBaseUri();
$uri = $baseUri.$request->getUri();
$method = $request->getMethod();
$options = $this->createOptions($request, $endpoint);
$handler = curl_init();
curl_setopt($handler, CURLOPT_URL, $uri);
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
if (!(function_exists('ini_get') && ini_get('open_basedir'))) {
curl_setopt($handler, CURLOPT_FOLLOWLOCATION, true);
}
curl_setopt($handler, CURLOPT_TIMEOUT, $options['timeout']);
curl_setopt($handler, CURLOPT_CONNECTTIMEOUT, $options['timeout']);
if (null !== ($proxy = $this->getOption('proxy'))) {
curl_setopt($handler, CURLOPT_PROXY, $proxy);
}
if (!isset($options['headers']['Content-Type'])) {
if (Request::METHOD_GET == $method) {
$options['headers']['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
} else {
$options['headers']['Content-Type'] = 'application/xml; charset=utf-8';
}
}
// Try endpoint authentication first, fallback to request for backwards compatibility
$authData = $endpoint->getAuthentication();
if (empty($authData['username'])) {
$authData = $request->getAuthentication();
}
if (!empty($authData['username']) && !empty($authData['password'])) {
curl_setopt($handler, CURLOPT_USERPWD, $authData['username'].':'.$authData['password']);
curl_setopt($handler, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
}
if (count($options['headers'])) {
$headers = [];
foreach ($options['headers'] as $key => $value) {
$headers[] = $key.': '.$value;
}
curl_setopt($handler, CURLOPT_HTTPHEADER, $headers);
}
if (Request::METHOD_POST == $method) {
curl_setopt($handler, CURLOPT_POST, true);
if ($request->getFileUpload()) {
$helper = new AdapterHelper();
$data = $helper->buildUploadBodyFromRequest($request);
curl_setopt($handler, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData());
}
} elseif (Request::METHOD_GET == $method) {
curl_setopt($handler, CURLOPT_HTTPGET, true);
} elseif (Request::METHOD_HEAD == $method) {
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'HEAD');
} elseif (Request::METHOD_DELETE == $method) {
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'DELETE');
} elseif (Request::METHOD_PUT == $method) {
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'PUT');
if ($request->getFileUpload()) {
$helper = new AdapterHelper();
$data = $helper->buildUploadBodyFromRequest($request);
curl_setopt($handler, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData());
}
} else {
throw new InvalidArgumentException("unsupported method: $method");
}
return $handler;
// @codeCoverageIgnoreEnd
}
/**
* Check result of a request.
*
*
* @param string $data
* @param array $headers
* @param resource $handle
*
* @throws HttpException
*/
public function check($data, $headers, $handle)
{
// if there is no data and there are no headers it's a total failure,
// a connection to the host was impossible.
if (empty($data) && 0 == count($headers)) {
throw new HttpException('HTTP request failed, '.curl_error($handle));
}
}
/**
* Execute request.
*
* @param Request $request
* @param Endpoint $endpoint
*
* @return Response
*/
protected function getData($request, $endpoint)
{
// @codeCoverageIgnoreStart
$handle = $this->createHandle($request, $endpoint);
$httpResponse = curl_exec($handle);
return $this->getResponse($handle, $httpResponse);
// @codeCoverageIgnoreEnd
}
/**
* Initialization hook.
*
* Checks the availability of Curl_http
*
* @throws RuntimeException
*/
protected function init()
{
// @codeCoverageIgnoreStart
if (!function_exists('curl_init')) {
throw new RuntimeException('cURL is not available, install it to use the CurlHttp adapter');
}
parent::init();
// @codeCoverageIgnoreEnd
}
/**
* Create http request options from request.
*
* @param Request $request
* @param Endpoint $endpoint
*
* @return array
*/
protected function createOptions($request, $endpoint)
{
// @codeCoverageIgnoreStart
$options = [
'timeout' => $endpoint->getTimeout(),
];
foreach ($request->getHeaders() as $headerLine) {
list($header, $value) = explode(':', $headerLine);
if ($header = trim($header)) {
$options['headers'][$header] = trim($value);
}
}
return $options;
// @codeCoverageIgnoreEnd
}
}