-
-
Notifications
You must be signed in to change notification settings - Fork 346
/
ParseCurlHttpClient.php
376 lines (323 loc) · 9.19 KB
/
ParseCurlHttpClient.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
<?php
/**
* Class ParseCurlHttpClient | Parse/HttpClients/ParseCurlHttpClient.php
*/
namespace Parse\HttpClients;
use Parse\ParseException;
/**
* Class ParseCurlHttpClient - Curl http client
*
* @author Ben Friedman <friedman.benjamin@gmail.com>
* @package Parse\HttpClients
*/
class ParseCurlHttpClient implements ParseHttpable
{
/**
* Curl handle
*
* @var ParseCurl
*/
private $parseCurl;
/**
* Request Headers
*
* @var array
*/
private $headers = array();
/**
* Response headers
*
* @var array
*/
private $responseHeaders = array();
/**
* Response code
*
* @var int
*/
private $responseCode = 0;
/**
* Content type of our response
*
* @var string|null
*/
private $responseContentType;
/**
* cURL error code
*
* @var int
*/
private $curlErrorCode;
/**
* cURL error message
*
* @var string
*/
private $curlErrorMessage;
/**
* @const Curl Version which is unaffected by the proxy header length error.
*/
const CURL_PROXY_QUIRK_VER = 0x071E00;
/**
* @const "Connection Established" header text
*/
const CONNECTION_ESTABLISHED = "HTTP/1.0 200 Connection established\r\n\r\n";
/**
* Response from our request
*
* @var string
*/
private $response;
/**
* ParseCurlHttpClient constructor.
*/
public function __construct()
{
if (!isset($this->parseCurl)) {
$this->parseCurl = new ParseCurl();
}
}
/**
* Adds a header to this request
*
* @param string $key Header name
* @param string $value Header value
*/
public function addRequestHeader($key, $value)
{
$this->headers[$key] = $value;
}
/**
* Builds and returns the coalesced request headers
*
* @return array
*/
private function buildRequestHeaders()
{
// coalesce our header key/value pairs
$headers = [];
foreach ($this->headers as $key => $value) {
$headers[] = $key.': '.$value;
}
return $headers;
}
/**
* Gets headers in the response
*
* @return array
*/
public function getResponseHeaders()
{
return $this->responseHeaders;
}
/**
* Returns the status code of the response
*
* @return int
*/
public function getResponseStatusCode()
{
return $this->responseCode;
}
/**
* Returns the content type of the response
*
* @return null|string
*/
public function getResponseContentType()
{
return $this->responseContentType;
}
/**
* Sets up our cURL request in advance
*/
public function setup() : void
{
// init parse curl
$this->parseCurl->init();
$this->parseCurl->setOptionsArray(array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
));
}
/**
* Sends an HTTP request
*
* @param string $url Url to send this request to
* @param string $method Method to send this request via
* @param array $data Data to send in this request
* @return string
* @throws ParseException
*/
public function send($url, $method = 'GET', $data = array())
{
if ($method == "GET" && !empty($data)) {
// handle get
$url .= '?'.http_build_query($data, '', '&');
} elseif ($method == "POST") {
// handle post
$this->parseCurl->setOptionsArray(array(
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $data
));
} elseif ($method == "PUT") {
// handle put
$this->parseCurl->setOptionsArray(array(
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_POSTFIELDS => $data
));
} elseif ($method == "DELETE") {
// handle delete
$this->parseCurl->setOption(CURLOPT_CUSTOMREQUEST, $method);
}
if (count($this->headers) > 0) {
// set our custom request headers
$this->parseCurl->setOption(CURLOPT_HTTPHEADER, $this->buildRequestHeaders());
}
// set url
$this->parseCurl->setOption(CURLOPT_URL, $url);
// perform our request and get our response
$this->response = $this->parseCurl->exec();
// get our response code
$this->responseCode = $this->parseCurl->getInfo(CURLINFO_HTTP_CODE);
// get our content type
$this->responseContentType = $this->parseCurl->getInfo(CURLINFO_CONTENT_TYPE);
// get any error code and message
$this->curlErrorMessage = $this->parseCurl->getError();
$this->curlErrorCode = $this->parseCurl->getErrorCode();
// calculate size of our headers
$headerSize = $this->getHeaderSize();
// get and set response headers
$headerContent = trim(substr($this->response, 0, $headerSize));
$this->responseHeaders = $this->getHeadersArray($headerContent);
// get our final response
$response = trim(substr($this->response, $headerSize));
// close our handle
$this->parseCurl->close();
// flush our existing headers
$this->headers = array();
return $response;
}
/**
* Convert and return response headers as an array
* @param string $headerContent Raw headers to parse
*
* @return array
*/
private function getHeadersArray($headerContent)
{
$headers = [];
// normalize our line breaks
$headerContent = str_replace("\r\n", "\n", $headerContent);
// Separate our header sets, particularly if we followed a 301 redirect
$headersSet = explode("\n\n", $headerContent);
// Get the last set of headers, ignoring all others
$rawHeaders = array_pop($headersSet);
// sepearate our header components
$headerComponents = explode("\n", $rawHeaders);
foreach ($headerComponents as $component) {
if (strpos($component, ': ') === false) {
// set our http_code
$headers['http_code'] = $component;
} else {
// set this header key/value pair
list($key, $value) = explode(': ', $component);
$headers[$key] = $value;
}
}
// return our completed headers
return $headers;
}
/**
* Sets the connection timeout
*
* @param int $timeout Timeout to set
*/
public function setConnectionTimeout($timeout)
{
$this->parseCurl->setOption(CURLOPT_CONNECTTIMEOUT, $timeout);
}
/**
* Sets the request timeout
*
* @param int $timeout Sets the timeout for the request
*/
public function setTimeout($timeout)
{
$this->parseCurl->setOption(CURLOPT_TIMEOUT, $timeout);
}
/**
* Sets the CA file to validate requests with
*
* @param string $caFile CA file to set
*/
public function setCAFile($caFile)
{
// name of a file holding one or more certificates to verify the peer with
$this->parseCurl->setOption(CURLOPT_CAINFO, $caFile);
}
/**
* Sets multiple curl options
* https://www.php.net/manual/en/function.curl-setopt.php
*
* @param array $options Array of options to set
* @throws ParseException
*/
public function setHttpOptions($options)
{
$this->parseCurl->setOptionsArray($options);
}
/**
* Gets the error code
*
* @return int
*/
public function getErrorCode()
{
return $this->curlErrorCode;
}
/**
* Gets the error message
*
* @return string
*/
public function getErrorMessage()
{
return $this->curlErrorMessage;
}
/**
* Return proper header size
*
* @return integer
*/
private function getHeaderSize()
{
$headerSize = $this->parseCurl->getInfo(CURLINFO_HEADER_SIZE);
// This corrects a Curl bug where header size does not account
// for additional Proxy headers.
if ($this->needsCurlProxyFix()) {
// Additional way to calculate the request body size.
if (preg_match('/Content-Length: (\d+)/', $this->response, $match)) {
$headerSize = mb_strlen($this->response) - $match[1];
} elseif (stripos($this->response, self::CONNECTION_ESTABLISHED) !== false) {
$headerSize += mb_strlen(self::CONNECTION_ESTABLISHED);
}
}
return $headerSize;
}
/**
* Detect versions of Curl which report incorrect header lengths when
* using Proxies.
*
* @return boolean
*/
private function needsCurlProxyFix()
{
$versionDat = curl_version();
$version = $versionDat['version_number'];
return $version < self::CURL_PROXY_QUIRK_VER;
}
}