-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPendingRequest.php
More file actions
481 lines (406 loc) · 14.4 KB
/
Copy pathPendingRequest.php
File metadata and controls
481 lines (406 loc) · 14.4 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
declare(strict_types=1);
namespace Zero\Lib\Http;
use CURLFile;
use Zero\Lib\Session;
class PendingRequest
{
/** @var array<string, string> */
private array $headers = [];
/** @var array<string, string> */
private array $cookies = [];
/** @var array<string, mixed> */
private array $query = [];
/** @var array<int, array{name:string, contents:mixed, filename:?string, headers:array<string,string>}> */
private array $multipart = [];
/** @var array<int, mixed> */
private array $curlOptions = [];
private ?int $timeout = null;
private ?int $connectTimeout = null;
private string $bodyFormat = 'json'; // json | form | multipart | body
private ?string $baseUrl = null;
private bool $verify = true;
private int $retries = 0;
private int $retryDelayMs = 0;
private ?\Closure $retryWhen = null;
private bool $debug = false;
private bool $debugDie = false;
private bool $throwOnFailure = false;
public function timeout(int $seconds): self
{
$this->timeout = max(1, $seconds);
return $this;
}
public function connectTimeout(int $seconds): self
{
$this->connectTimeout = max(1, $seconds);
return $this;
}
public function withHeaders(array $headers): self
{
foreach ($headers as $name => $value) {
$this->headers[$this->normalizeHeaderName((string) $name)] = (string) $value;
}
return $this;
}
public function withHeader(string $name, string $value): self
{
$this->headers[$this->normalizeHeaderName($name)] = $value;
return $this;
}
public function acceptJson(): self
{
return $this->withHeader('Accept', 'application/json');
}
public function accept(string $contentType): self
{
return $this->withHeader('Accept', $contentType);
}
public function asJson(): self
{
$this->bodyFormat = 'json';
return $this->contentType('application/json');
}
public function asForm(): self
{
$this->bodyFormat = 'form';
return $this->contentType('application/x-www-form-urlencoded');
}
public function asMultipart(): self
{
$this->bodyFormat = 'multipart';
return $this;
}
public function bodyFormat(string $format): self
{
$this->bodyFormat = $format;
return $this;
}
public function contentType(string $type): self
{
return $this->withHeader('Content-Type', $type);
}
public function withToken(string $token, string $type = 'Bearer'): self
{
return $this->withHeader('Authorization', trim($type . ' ' . $token));
}
public function withBasicAuth(string $username, string $password): self
{
return $this->withHeader('Authorization', 'Basic ' . base64_encode($username . ':' . $password));
}
public function withQueryParameters(array $query): self
{
$this->query = array_replace($this->query, $query);
return $this;
}
public function withCookies(array $cookies): self
{
foreach ($cookies as $name => $value) {
$this->cookies[(string) $name] = (string) $value;
}
return $this;
}
public function withUserAgent(string $userAgent): self
{
return $this->withHeader('User-Agent', $userAgent);
}
public function withoutVerifying(): self
{
$this->verify = false;
return $this;
}
public function baseUrl(string $url): self
{
$this->baseUrl = rtrim($url, '/');
return $this;
}
public function withOptions(array $options): self
{
foreach ($options as $key => $value) {
$this->curlOptions[$key] = $value;
}
return $this;
}
public function attach(string $name, mixed $contents, ?string $filename = null, array $headers = []): self
{
$this->bodyFormat = 'multipart';
$this->multipart[] = [
'name' => $name,
'contents' => $contents,
'filename' => $filename,
'headers' => $headers,
];
return $this;
}
public function retry(int $times, int $sleepMs = 0, ?callable $when = null): self
{
$this->retries = max(0, $times);
$this->retryDelayMs = max(0, $sleepMs);
$this->retryWhen = $when !== null ? \Closure::fromCallable($when) : null;
return $this;
}
public function throw(bool $throw = true): self
{
$this->throwOnFailure = $throw;
return $this;
}
public function dump(): self
{
$this->debug = true;
return $this;
}
public function dd(): self
{
$this->debug = true;
$this->debugDie = true;
return $this;
}
// ----- Terminal methods -----
public function get(string $url, array $query = []): ClientResponse
{
return $this->send('GET', $url, null, $query);
}
public function head(string $url, array $query = []): ClientResponse
{
return $this->send('HEAD', $url, null, $query);
}
public function post(string $url, mixed $data = null, array $query = []): ClientResponse
{
return $this->send('POST', $url, $data, $query);
}
public function put(string $url, mixed $data = null, array $query = []): ClientResponse
{
return $this->send('PUT', $url, $data, $query);
}
public function patch(string $url, mixed $data = null, array $query = []): ClientResponse
{
return $this->send('PATCH', $url, $data, $query);
}
public function delete(string $url, mixed $data = null, array $query = []): ClientResponse
{
return $this->send('DELETE', $url, $data, $query);
}
public function send(string $method, string $url, mixed $data = null, array $query = []): ClientResponse
{
$attempt = 0;
$maxAttempts = 1 + $this->retries;
$lastResponse = null;
while ($attempt < $maxAttempts) {
$attempt++;
$lastResponse = $this->dispatch($method, $url, $data, $query);
if ($lastResponse->successful()) {
break;
}
$shouldRetry = $attempt < $maxAttempts;
if ($shouldRetry && $this->retryWhen !== null) {
$shouldRetry = (bool) ($this->retryWhen)($lastResponse, $attempt);
}
if (! $shouldRetry) {
break;
}
if ($this->retryDelayMs > 0) {
usleep($this->retryDelayMs * 1000);
}
}
if ($this->throwOnFailure) {
$lastResponse->throw();
}
return $lastResponse;
}
private function dispatch(string $method, string $url, mixed $data, array $extraQuery): ClientResponse
{
$resolvedUrl = $this->resolveUrl($url, $extraQuery);
$headers = $this->buildHeaders();
$payload = $this->preparePayload($data, $headers);
$options = [
CURLOPT_URL => $resolvedUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => strtoupper($method),
CURLOPT_HEADER => true,
CURLOPT_SSL_VERIFYPEER => $this->verify,
CURLOPT_SSL_VERIFYHOST => $this->verify ? 2 : 0,
];
if ($this->timeout !== null) {
$options[CURLOPT_TIMEOUT] = $this->timeout;
}
if ($this->connectTimeout !== null) {
$options[CURLOPT_CONNECTTIMEOUT] = $this->connectTimeout;
}
if ($payload !== null) {
$options[CURLOPT_POSTFIELDS] = $payload;
}
if ($this->cookies !== []) {
$options[CURLOPT_COOKIE] = http_build_query($this->cookies, '', '; ');
}
$options[CURLOPT_HTTPHEADER] = $this->headersToWire($headers);
if (strtoupper($method) === 'HEAD') {
$options[CURLOPT_NOBODY] = true;
}
foreach ($this->curlOptions as $key => $value) {
$options[$key] = $value;
}
if ($this->debug) {
$this->emitDebug($method, $resolvedUrl, $headers, $payload);
}
$handle = curl_init();
curl_setopt_array($handle, $options);
$rawResponse = curl_exec($handle);
$statusCode = (int) curl_getinfo($handle, CURLINFO_HTTP_CODE);
$headerSize = (int) curl_getinfo($handle, CURLINFO_HEADER_SIZE);
$error = curl_errno($handle) ? curl_error($handle) : null;
// curl_close() is a no-op since PHP 8.0 (the handle is a CurlHandle
// object freed by the garbage collector) and is deprecated in PHP 8.5,
// where this app's error handler promotes the deprecation to an
// exception. Unset the handle instead to release it deterministically.
unset($handle);
$rawHeaders = '';
$body = '';
if (is_string($rawResponse)) {
$rawHeaders = substr($rawResponse, 0, $headerSize);
$body = (string) substr($rawResponse, $headerSize);
}
if ($statusCode === 0 && $error !== null) {
$statusCode = 0;
}
$parsedHeaders = $this->parseHeaders($rawHeaders);
return new ClientResponse($statusCode, $body, $parsedHeaders, $error);
}
private function resolveUrl(string $url, array $extraQuery): string
{
$absolute = (bool) preg_match('/^https?:\/\//i', $url);
if (! $absolute) {
$base = $this->baseUrl ?? ($_ENV['CONFIG']['API_HOST'] ?? '');
if ($base !== '') {
$url = rtrim((string) $base, '/') . '/' . ltrim($url, '/');
}
}
$combinedQuery = array_replace($this->query, $extraQuery);
if ($combinedQuery !== []) {
$separator = str_contains($url, '?') ? '&' : '?';
$url .= $separator . http_build_query($combinedQuery);
}
return $url;
}
/**
* @return array<string, string>
*/
private function buildHeaders(): array
{
$headers = $this->headers;
// Auto-attach session token if no explicit Authorization header.
if (! isset($headers['Authorization']) && class_exists(Session::class) && Session::has('token')) {
$headers['Authorization'] = 'Bearer ' . Session::get('token');
}
return $headers;
}
private function preparePayload(mixed $data, array &$headers): mixed
{
// Multipart wins if files are attached or asMultipart() was called.
if ($this->bodyFormat === 'multipart' || $this->multipart !== []) {
$fields = is_array($data) ? $data : [];
foreach ($this->multipart as $part) {
$contents = $part['contents'];
if (is_string($contents) && is_file($contents)) {
$fields[$part['name']] = new CURLFile($contents, null, $part['filename'] ?? basename($contents));
} elseif (is_string($contents)) {
// Inline string contents — write to tmp so curl can attach.
$tmp = tempnam(sys_get_temp_dir(), 'zero_http_');
if ($tmp !== false) {
file_put_contents($tmp, $contents);
$fields[$part['name']] = new CURLFile($tmp, null, $part['filename'] ?? $part['name']);
}
} else {
$fields[$part['name']] = $contents;
}
}
// libcurl sets multipart Content-Type with boundary automatically.
unset($headers['Content-Type']);
return $fields === [] ? null : $fields;
}
if ($data === null) {
return null;
}
if ($this->bodyFormat === 'form') {
if (! isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/x-www-form-urlencoded';
}
return is_array($data) ? http_build_query($data) : (string) $data;
}
if ($this->bodyFormat === 'body') {
return is_string($data) ? $data : (string) $data;
}
// Default: JSON
if (! isset($headers['Content-Type'])) {
$headers['Content-Type'] = 'application/json';
}
if (is_string($data)) {
return $data;
}
$encoded = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $encoded === false ? '' : $encoded;
}
/**
* @param array<string, string> $headers
* @return array<int, string>
*/
private function headersToWire(array $headers): array
{
$wire = [];
foreach ($headers as $name => $value) {
$wire[] = $name . ': ' . $value;
}
return $wire;
}
/**
* @return array<string, array<int, string>>
*/
private function parseHeaders(string $raw): array
{
$headers = [];
$blocks = preg_split('/\r?\n\r?\n/', trim($raw)) ?: [];
// Take the last block (handles redirects/100-continue intermediate responses).
$last = end($blocks);
if ($last === false) {
return $headers;
}
$lines = preg_split('/\r?\n/', $last) ?: [];
foreach ($lines as $line) {
if (! str_contains($line, ':')) continue;
[$name, $value] = explode(':', $line, 2);
$name = trim($name);
$value = trim($value);
if ($name === '') continue;
$headers[$name][] = $value;
}
return $headers;
}
private function normalizeHeaderName(string $name): string
{
$parts = explode('-', $name);
return implode('-', array_map(static fn($p) => ucfirst(strtolower($p)), $parts));
}
private function emitDebug(string $method, string $url, array $headers, mixed $payload): void
{
$lines = [
'> ' . strtoupper($method) . ' ' . $url,
];
foreach ($headers as $name => $value) {
$lines[] = '> ' . $name . ': ' . $value;
}
if ($payload !== null) {
$lines[] = '>';
$lines[] = '> ' . (is_string($payload) ? $payload : print_r($payload, true));
}
$output = implode("\n", $lines) . "\n";
if (PHP_SAPI === 'cli') {
fwrite(STDERR, $output);
} else {
echo '<pre>' . htmlspecialchars($output) . '</pre>';
}
if ($this->debugDie) {
exit(1);
}
}
}