-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathCurlTransport.php
294 lines (266 loc) · 7.32 KB
/
CurlTransport.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\FunctionalTestingFramework\Util\Protocol;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
/**
* HTTP Curl Adapter.
*/
class CurlTransport implements CurlInterface
{
/**
* Parameters array.
*
* @var array
*/
protected $config = [];
/**
* Curl handle.
*
* @var resource
*/
protected $resource;
/**
* Allow parameters.
*
* @var array
*/
protected $allowedParams = [
'timeout' => CURLOPT_TIMEOUT,
'maxredirects' => CURLOPT_MAXREDIRS,
'proxy' => CURLOPT_PROXY,
'ssl_cert' => CURLOPT_SSLCERT,
'userpwd' => CURLOPT_USERPWD,
];
/**
* Array of CURL options.
*
* @var array
*/
protected $options = [];
/**
* A list of successful HTTP responses that will not trigger an exception.
*
* @var int[] SUCCESSFUL_HTTP_CODES
*/
const SUCCESSFUL_HTTP_CODES = [200, 201, 202, 203, 204, 205];
/**
* Apply current configuration array to curl resource.
*
* @return $this
*/
protected function applyConfig()
{
// apply additional options to cURL
foreach ($this->options as $option => $value) {
curl_setopt($this->getResource(), $option, $value);
}
if (empty($this->config)) {
return $this;
}
foreach (array_keys($this->config) as $param) {
if (array_key_exists($param, $this->allowedParams)) {
curl_setopt($this->getResource(), $this->allowedParams[$param], $this->config[$param]);
}
}
return $this;
}
/**
* Set array of additional cURL options.
*
* @param array $options
* @return $this
*/
public function setOptions(array $options = [])
{
$this->options = $options;
return $this;
}
/**
* Add additional option to cURL.
*
* @param integer $option
* @param integer|string|boolean|array $value
* @return $this
*/
public function addOption($option, $value)
{
$this->options[$option] = $value;
return $this;
}
/**
* Set the configuration array for the adapter.
*
* @param array $config
* @return $this
*/
public function setConfig(array $config = [])
{
$this->config = $config;
return $this;
}
/**
* Send request to the remote server.
*
* @param string $url
* @param array|string $body
* @param string $method
* @param array $headers
* @return void
* @throws TestFrameworkException
*/
public function write($url, $body = [], $method = CurlInterface::POST, $headers = [])
{
$this->applyConfig();
$options = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_COOKIEFILE => '',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
];
switch ($method) {
case CurlInterface::POST:
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = $body;
break;
case CurlInterface::PUT:
$options[CURLOPT_CUSTOMREQUEST] = self::PUT;
$options[CURLOPT_POSTFIELDS] = $body;
break;
case CurlInterface::DELETE:
$options[CURLOPT_CUSTOMREQUEST] = self::DELETE;
break;
case CurlInterface::GET:
$options[CURLOPT_HTTPGET] = true;
break;
default:
throw new TestFrameworkException("Undefined curl method: $method");
}
curl_setopt_array($this->getResource(), $options);
}
/**
* Read response from server.
*
* @param string $successRegex
* @param string $returnRegex
* @param string|null $returnIndex
* @return string
* @throws TestFrameworkException
*/
public function read($successRegex = null, $returnRegex = null, $returnIndex = null)
{
$response = curl_exec($this->getResource());
if ($response === false) {
throw new TestFrameworkException(curl_error($this->getResource()));
}
$http_code = $this->getInfo(CURLINFO_HTTP_CODE);
if (!in_array($http_code, self::SUCCESSFUL_HTTP_CODES)) {
throw new TestFrameworkException('Error HTTP response code: ' . $http_code . ' Response:' . $response);
}
return $response;
}
/**
* Close the connection to the server.
*
* @return void
*/
public function close()
{
curl_close($this->getResource());
$this->resource = null;
}
/**
* Returns a cURL handle on success.
*
* @return resource
*/
protected function getResource()
{
if ($this->resource === null) {
$this->resource = curl_init();
}
return $this->resource;
}
/**
* Get last error number.
*
* @return integer
*/
public function getErrno()
{
return curl_errno($this->getResource());
}
/**
* Get string with last error for the current session.
*
* @return string
*/
public function getError()
{
return curl_error($this->getResource());
}
/**
* Get information regarding a specific transfer.
*
* @param integer $opt CURLINFO option.
* @return string|array
*/
public function getInfo($opt = 0)
{
return curl_getinfo($this->getResource(), $opt);
}
/**
* Provide curl_multi_* requests support.
*
* @param array $urls
* @param array $options
* @return array
*/
public function multiRequest(array $urls, array $options = [])
{
$handles = [];
$result = [];
$multiHandle = curl_multi_init();
foreach ($urls as $key => $url) {
$handles[$key] = curl_init();
curl_setopt($handles[$key], CURLOPT_URL, $url);
curl_setopt($handles[$key], CURLOPT_HEADER, 0);
curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, 1);
if (!empty($options)) {
curl_setopt_array($handles[$key], $options);
}
curl_multi_add_handle($multiHandle, $handles[$key]);
}
$process = null;
do {
curl_multi_exec($multiHandle, $process);
usleep(100);
} while ($process > 0);
foreach ($handles as $key => $handle) {
$result[$key] = curl_multi_getcontent($handle);
curl_multi_remove_handle($multiHandle, $handle);
}
curl_multi_close($multiHandle);
return $result;
}
/**
* Extract the response code from a response string.
*
* @param string $responseStr
* @return integer
*/
public static function extractCode($responseStr)
{
preg_match("|^HTTP/[\d\.x]+ (\d+)|", $responseStr, $m);
if (isset($m[1])) {
return (int)$m[1];
} else {
return false;
}
}
}