/
HttpService.php
378 lines (357 loc) · 10.7 KB
/
HttpService.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<?php
/**
* VuFind HTTP service class file.
*
* PHP version 7
*
* Copyright (C) Villanova University 2010.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package Http
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development
*/
namespace VuFindHttp;
/**
* VuFind HTTP service.
*
* @category VuFind
* @package Http
* @author David Maus <maus@hab.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development
*/
class HttpService implements HttpServiceInterface
{
/**
* Default regular expression matching a request to localhost.
*
* @var string
*/
public const LOCAL_ADDRESS_RE = '@^(localhost|127(\.\d+){3}|\[::1\])@';
/**
* Proxy configuration.
*
* @see \Laminas\Http\Client\Adapter\Proxy::$config
*
* @var array
*/
protected $proxyConfig;
/**
* Regular expression matching a request to localhost or hosts
* that are not proxied.
*
* @see \Laminas\Http\Client\Adapter\Proxy::$config
*
* @var string
*/
protected $localAddressesRegEx = self::LOCAL_ADDRESS_RE;
/**
* Default client options.
*
* @var array
*/
protected $defaults;
/**
* Default adapter
*
* @var \Laminas\Http\Client\Adapter\AdapterInterface
*/
protected $defaultAdapter = null;
/**
* Constructor.
*
* @param array $proxyConfig Proxy configuration
* @param array $defaults Default HTTP options
* @param array $config Other configuration
*
* @return void
*/
public function __construct(
array $proxyConfig = [],
array $defaults = [],
array $config = []
) {
$this->proxyConfig = $proxyConfig;
$this->defaults = $defaults;
if (isset($config['localAddressesRegEx'])) {
$this->localAddressesRegEx = $config['localAddressesRegEx'];
}
}
/**
* Set proxy options in a Curl adapter.
*
* @param \Laminas\Http\Client\Adapter\Curl $adapter Adapter to configure
*
* @return void
*/
protected function setCurlProxyOptions($adapter)
{
$adapter->setCurlOption(CURLOPT_PROXY, $this->proxyConfig['proxy_host']);
if (!empty($this->proxyConfig['proxy_port'])) {
$adapter
->setCurlOption(CURLOPT_PROXYPORT, $this->proxyConfig['proxy_port']);
}
// HTTP is default, so handle only the SOCKS 5 proxy types
switch ($this->proxyConfig['proxy_type'] ?? '') {
case 'socks5':
$adapter->setCurlOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
break;
case 'socks5_hostname':
$adapter->setCurlOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
break;
}
}
/**
* Are we configured to use the CURL adapter?
*
* @return bool
*/
protected function hasCurlAdapterAsDefault()
{
$default = $this->defaults['adapter']
?? ($this->defaultAdapter ? get_class($this->defaultAdapter) : '');
return $default === 'Laminas\Http\Client\Adapter\Curl';
}
/**
* Proxify an existing client.
*
* Returns the client given as argument with appropriate proxy setup.
*
* @param \Laminas\Http\Client $client HTTP client
* @param array $options Laminas ProxyAdapter options
*
* @return \Laminas\Http\Client
*/
public function proxify(\Laminas\Http\Client $client, array $options = [])
{
if ($this->proxyConfig) {
$host = $client->getUri()->getHost();
if (null === $host || !$this->isLocal($host)) {
$proxyType = $this->proxyConfig['proxy_type'] ?? 'default';
if (in_array($proxyType, ['socks5', 'socks5_hostname'])) {
$adapter = new \Laminas\Http\Client\Adapter\Curl();
// Apply proxy options for Curl adapter:
$this->setCurlProxyOptions($adapter);
$client->setAdapter($adapter);
} elseif ($proxyType == 'default') {
// If the user has manually configured a Curl adapter,
// configure it for proxy compatibility; otherwise, create
// a fresh Proxy adapter.
if ($this->hasCurlAdapterAsDefault()) {
$adapter = new \Laminas\Http\Client\Adapter\Curl();
$this->setCurlProxyOptions($adapter);
} else {
$adapter = new \Laminas\Http\Client\Adapter\Proxy();
$options = array_replace($this->proxyConfig, $options);
$adapter->setOptions($options);
}
$client->setAdapter($adapter);
}
}
}
return $client;
}
/**
* Perform a GET request.
*
* @param string $url Request URL
* @param array $params Request parameters
* @param float $timeout Request timeout in seconds
* @param array $headers Request headers
*
* @return \Laminas\Http\Response
*/
public function get(
$url,
array $params = [],
$timeout = null,
array $headers = []
) {
if ($params) {
$query = $this->createQueryString($params);
if (strpos($url, '?') !== false) {
$url .= '&' . $query;
} else {
$url .= '?' . $query;
}
}
$client
= $this->createClient($url, \Laminas\Http\Request::METHOD_GET, $timeout);
if ($headers) {
$client->setHeaders($headers);
}
return $this->send($client);
}
/**
* Perform a POST request.
*
* @param string $url Request URL
* @param mixed $body Request body document
* @param string $type Request body content type
* @param float $timeout Request timeout in seconds
* @param array $headers Request http-headers
*
* @return \Laminas\Http\Response
*/
public function post(
$url,
$body = null,
$type = 'application/octet-stream',
$timeout = null,
array $headers = []
) {
$client = $this
->createClient($url, \Laminas\Http\Request::METHOD_POST, $timeout);
$client->setRawBody($body);
$client->setHeaders(
array_merge(
['Content-Type' => $type, 'Content-Length' => strlen($body)],
$headers
)
);
return $this->send($client);
}
/**
* Post form data.
*
* @param string $url Request URL
* @param array $params Form data
* @param float $timeout Request timeout in seconds
*
* @return \Laminas\Http\Response
*/
public function postForm($url, array $params = [], $timeout = null)
{
$body = $this->createQueryString($params);
return $this->post(
$url,
$body,
\Laminas\Http\Client::ENC_URLENCODED,
$timeout
);
}
/**
* Set a default HTTP adapter (primarily for testing purposes).
*
* @param \Laminas\Http\Client\Adapter\AdapterInterface $adapter Adapter
*
* @return void
*/
public function setDefaultAdapter(
\Laminas\Http\Client\Adapter\AdapterInterface $adapter
) {
$this->defaultAdapter = $adapter;
}
/**
* Return a new HTTP client.
*
* @param string $url Target URL
* @param string $method Request method
* @param float $timeout Request timeout in seconds
*
* @return \Laminas\Http\Client
*/
public function createClient(
$url = null,
$method = \Laminas\Http\Request::METHOD_GET,
$timeout = null
) {
$client = new \Laminas\Http\Client();
$client->setMethod($method);
if (!empty($this->defaults)) {
$client->setOptions($this->defaults);
}
if (null !== $this->defaultAdapter) {
$client->setAdapter($this->defaultAdapter);
}
if (null !== $url) {
$client->setUri($url);
}
if ($timeout) {
$client->setOptions(['timeout' => $timeout]);
}
$this->proxify($client);
return $client;
}
/// Internal API
/**
* Return query string based on params.
*
* @param array $params Parameters
*
* @return string
*/
protected function createQueryString(array $params = [])
{
if ($this->isAssocParams($params)) {
return http_build_query($params);
} else {
return implode('&', $params);
}
}
/**
* Send HTTP request and return response.
*
* @param \Laminas\Http\Client $client HTTP client to use
*
* @throws Exception\RuntimeException
* @return \Laminas\Http\Response
*
* @todo Catch more exceptions, maybe?
*/
protected function send(\Laminas\Http\Client $client)
{
try {
$response = $client->send();
} catch (\Laminas\Http\Client\Exception\RuntimeException $e) {
throw new Exception\RuntimeException(
sprintf('Laminas HTTP Client exception: %s', $e),
-1,
$e
);
}
return $response;
}
/**
* Return TRUE if argument is an associative array.
*
* @param array $array Array to test
*
* @return boolean
*/
public static function isAssocParams(array $array)
{
foreach (array_keys($array) as $key) {
if (!is_numeric($key)) {
return true;
}
}
return false;
}
/**
* Return TRUE if argument refers to localhost.
*
* @param string $host Host to check
*
* @return boolean
*/
protected function isLocal($host)
{
return preg_match($this->localAddressesRegEx, $host);
}
}