|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
namespace SURFnet\VPN\Common\HttpClient; |
|
19
|
|
|
|
|
20
|
|
|
use SURFnet\VPN\Common\HttpClient\Exception\HttpClientException; |
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
|
|
23
|
|
|
class CurlHttpClient implements HttpClientInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var resource */ |
|
26
|
|
|
private $curlChannel; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string */ |
|
29
|
|
|
private $authUser; |
|
30
|
|
|
|
|
31
|
|
|
/** @var string */ |
|
32
|
|
|
private $authPass; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct($authUser, $authPass) |
|
35
|
|
|
{ |
|
36
|
|
|
if (false === $this->curlChannel = curl_init()) { |
|
37
|
|
|
throw new RuntimeException('unable to initialize a cURL session'); |
|
38
|
|
|
} |
|
39
|
|
|
$this->authUser = $authUser; |
|
40
|
|
|
$this->authPass = $authPass; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function setOptions(array $additionalOptions) |
|
44
|
|
|
{ |
|
45
|
|
|
curl_reset($this->curlChannel); |
|
46
|
|
|
$curlOptions = array_merge( |
|
47
|
|
|
[ |
|
48
|
|
|
CURLOPT_FOLLOWLOCATION => false, |
|
49
|
|
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS | CURLPROTO_HTTP, |
|
50
|
|
|
CURLOPT_SSL_VERIFYPEER => true, |
|
51
|
|
|
CURLOPT_SSL_VERIFYHOST => 2, |
|
52
|
|
|
CURLOPT_HTTPAUTH => CURLAUTH_BASIC, |
|
53
|
|
|
CURLOPT_USERPWD => sprintf('%s:%s', $this->authUser, $this->authPass), |
|
54
|
|
|
CURLOPT_RETURNTRANSFER => true, |
|
55
|
|
|
CURLOPT_HTTPHEADER, [ |
|
56
|
|
|
'Accept: application/json', |
|
57
|
|
|
], |
|
58
|
|
|
], |
|
59
|
|
|
$additionalOptions |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
if (false === curl_setopt_array($this->curlChannel, $curlOptions)) { |
|
63
|
|
|
throw new RuntimeException('unable to set cURL options'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function __destruct() |
|
68
|
|
|
{ |
|
69
|
|
|
curl_close($this->curlChannel); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Send a HTTP GET request. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $requestUri the request URI |
|
76
|
|
|
* |
|
77
|
|
|
* @throws HttpClientException if the response is a 4xx error with the |
|
78
|
|
|
* JSON "error" field of the response body as the exception message |
|
79
|
|
|
*/ |
|
80
|
|
|
public function get($requestUri) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->setOptions( |
|
83
|
|
|
[ |
|
84
|
|
|
CURLOPT_URL => $requestUri, |
|
85
|
|
|
] |
|
86
|
|
|
); |
|
87
|
|
|
|
|
88
|
|
|
return $this->handleRequest(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Send a HTTP POST request. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $requestUri the request URI |
|
95
|
|
|
* @param array $postData the HTTP POST fields to send |
|
96
|
|
|
* |
|
97
|
|
|
* @throws HttpClientException if the response is a 4xx error with the |
|
98
|
|
|
* JSON "error" field of the response body as the exception message |
|
99
|
|
|
*/ |
|
100
|
|
|
public function post($requestUri, array $postData) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->setOptions( |
|
103
|
|
|
[ |
|
104
|
|
|
CURLOPT_URL => $requestUri, |
|
105
|
|
|
CURLOPT_POSTFIELDS => http_build_query($postData), |
|
106
|
|
|
] |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
return $this->handleRequest(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
private function handleRequest() |
|
113
|
|
|
{ |
|
114
|
|
|
$curlResponse = curl_exec($this->curlChannel); |
|
115
|
|
|
|
|
116
|
|
|
// error in the transfer? |
|
117
|
|
|
if (false === $curlResponse) { |
|
118
|
|
|
throw new RuntimeException( |
|
119
|
|
|
sprintf( |
|
120
|
|
|
'cURL error: %s (%s)', |
|
121
|
|
|
curl_error($this->curlChannel) |
|
122
|
|
|
) |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$decodedResponseData = json_decode($curlResponse, true); |
|
127
|
|
|
|
|
128
|
|
|
$curlResponseCode = curl_getinfo($this->curlChannel, CURLINFO_HTTP_CODE); |
|
129
|
|
|
// OK? |
|
130
|
|
|
if ($curlResponseCode >= 200 && $curlResponseCode < 300) { |
|
131
|
|
|
return $decodedResponseData; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
if (is_array($decodedResponseData) && array_key_exists('error', $decodedResponseData)) { |
|
135
|
|
|
throw new HttpClientException( |
|
136
|
|
|
sprintf('[%d]: %s', $curlResponseCode, $decodedResponseData['error']) |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
throw new HttpClientException( |
|
141
|
|
|
sprintf('[%d]: unexpected error', $curlResponseCode) |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|