|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Common\HttpClient; |
|
11
|
|
|
|
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
|
|
14
|
|
|
class CurlHttpClient implements HttpClientInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var resource */ |
|
17
|
|
|
private $curlChannel; |
|
18
|
|
|
|
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
private $authInfo; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(array $authInfo) |
|
23
|
|
|
{ |
|
24
|
|
|
if (false === $this->curlChannel = curl_init()) { |
|
25
|
|
|
throw new RuntimeException('unable to create cURL channel'); |
|
26
|
|
|
} |
|
27
|
|
|
$this->authInfo = $authInfo; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function __destruct() |
|
31
|
|
|
{ |
|
32
|
|
|
curl_close($this->curlChannel); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function get($requestUri) |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->exec( |
|
38
|
|
|
[ |
|
39
|
|
|
CURLOPT_URL => $requestUri, |
|
40
|
|
|
] |
|
41
|
|
|
); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function post($requestUri, array $postData = []) |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->exec( |
|
47
|
|
|
[ |
|
48
|
|
|
CURLOPT_URL => $requestUri, |
|
49
|
|
|
CURLOPT_POSTFIELDS => http_build_query($postData), |
|
50
|
|
|
] |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function exec(array $curlOptions) |
|
55
|
|
|
{ |
|
56
|
|
|
// reset all cURL options |
|
57
|
|
|
$this->curlReset(); |
|
58
|
|
|
|
|
59
|
|
|
$defaultCurlOptions = [ |
|
60
|
|
|
CURLOPT_USERPWD => sprintf('%s:%s', $this->authInfo[0], $this->authInfo[1]), |
|
61
|
|
|
CURLOPT_HEADER => false, |
|
62
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
63
|
|
|
CURLOPT_FOLLOWLOCATION => false, |
|
64
|
|
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTP | CURLPROTO_HTTPS, |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
if (false === curl_setopt_array($this->curlChannel, $curlOptions + $defaultCurlOptions)) { |
|
68
|
|
|
throw new RuntimeException('unable to set cURL options'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (false === $responseData = curl_exec($this->curlChannel)) { |
|
72
|
|
|
$curlError = curl_error($this->curlChannel); |
|
73
|
|
|
throw new RuntimeException(sprintf('failure performing the HTTP request: "%s"', $curlError)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return [ |
|
77
|
|
|
curl_getinfo($this->curlChannel, CURLINFO_HTTP_CODE), |
|
78
|
|
|
json_decode($responseData, true), |
|
79
|
|
|
]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
private function curlReset() |
|
83
|
|
|
{ |
|
84
|
|
|
// requires PHP >= 5.5 for curl_reset |
|
85
|
|
|
if (function_exists('curl_reset')) { |
|
86
|
|
|
curl_reset($this->curlChannel); |
|
87
|
|
|
|
|
88
|
|
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// reset the request method to GET, that is enough to allow for |
|
92
|
|
|
// multiple requests using the same cURL channel |
|
93
|
|
|
if (false === curl_setopt($this->curlChannel, CURLOPT_HTTPGET, true)) { |
|
94
|
|
|
throw new RuntimeException('unable to set cURL options'); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|