-
Notifications
You must be signed in to change notification settings - Fork 22
/
http-functions.php
1274 lines (1111 loc) · 27.7 KB
/
http-functions.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
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* HTTP request functions.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* This function collects headers when
* called as a callback function and returns
* the headers collected when not invoked as a callback.
*
* The difference is that the '$ch' argument is non-null
* when called as a callback.
*
* @param null|CurlHandle $ch cURL handle.
* @param null|string $header HTTP header to process, or null.
*
* @return int|array When collecting header, returns length of
* header saved. Returns array of headers otherwise.
*/
function vipgoci_curl_headers(
null|CurlHandle $ch,
null|string $header
) :int|array {
static $resp_headers = array();
if ( null === $ch ) {
/*
* If $ch is null, we are being called to
* return whatever headers we have collected.
*
* Make sure to empty the headers collected.
*/
$ret = $resp_headers;
$resp_headers = array();
/*
* 'Fix' the status header before returning;
* we want the value to be an array such as:
* array(
* 0 => 201, // Status-code
* 1 => 'Created' // Status-string
* )
*/
if ( isset( $ret['status'] ) ) {
$ret['status'] = explode(
' ',
$ret['status'][0]
);
}
return $ret;
}
/*
* Process callback requests.
*/
/*
* Get header length
*/
$header_len = strlen( $header );
/*
* Construct 'status' HTTP header based on the
* HTTP status code.
*/
if (
( strpos( $header, 'HTTP/' ) === 0 ) &&
( true === str_contains( $header, ' ' ) )
) {
$header = explode(
' ',
$header
);
$header = 'Status: ' . $header[1] . "\n\r";
}
/*
* Turn the header into an array
*/
$header = explode( ':', $header, 2 );
if ( count( $header ) < 2 ) {
/*
* Should there be less than two values
* in the array, simply return, as the header is
* invalid.
*/
return $header_len;
}
/*
* Save the header as a key => value
* in our associative array.
*/
$key = strtolower( trim( $header[0] ) );
if ( ! array_key_exists( $key, $resp_headers ) ) {
$resp_headers[ $key ] = array();
}
$resp_headers[ $key ][] = trim(
$header[1]
);
return $header_len;
}
/**
* Set a few options for cURL that enhance security.
*
* @param CurlHandle $ch cURL handle.
*
* @return void
*
* @codeCoverageIgnore
*/
function vipgoci_curl_set_security_options(
CurlHandle $ch
) :void {
/*
* Maximum number of redirects to zero.
*/
curl_setopt(
$ch,
CURLOPT_MAXREDIRS,
0
);
/*
* Do not follow any "Location" headers.
*/
curl_setopt(
$ch,
CURLOPT_FOLLOWLOCATION,
false
);
}
/**
* Log a warning if a Sunset HTTP header is
* found in array of response headers, as this indicates
* that the API feature will become deprecated in the
* future. Will log the URL called, but without query
* component, as it may contain sensitive information.
*
* Information on Sunset HTTP headers:
* https://datatracker.ietf.org/doc/html/draft-wilde-sunset-header-03
*
* @param string $http_url HTTP URL for identification.
* @param array $resp_headers HTTP response headers.
*
* @return void
*/
function vipgoci_http_resp_sunset_header_check(
string $http_url,
array $resp_headers
) :void {
/*
* Only do detection in 20% of cases, to limit
* amount of logging. In case of unit-testing this
* will be 100%.
*/
if ( ( ! defined( 'VIPGOCI_UNIT_TESTING' ) ) || ( true !== VIPGOCI_UNIT_TESTING ) ) {
if ( rand( 1, 5 ) > 1 ) {
return;
}
}
/*
* If no sunset header is found, do nothing.
*/
if (
( ! isset( $resp_headers['sunset'][0] ) ) ||
( strlen( $resp_headers['sunset'][0] ) <= 0 )
) {
return;
}
$sunset_date = $resp_headers['sunset'];
/*
* To minimize likelihood of data-leaks via the URL being
* logged, remove any query parameters and leave
* only the base URL.
*/
$http_url_parsed = parse_url( $http_url );
$http_url_clean = '';
if (
( ! isset( $http_url_parsed['scheme'] ) ) ||
( ! isset( $http_url_parsed['host'] ) )
) {
vipgoci_log(
'Warning: Invalid HTTP URL detected while processing sunset headers',
array(
'http_url' => $http_url,
)
);
}
if ( isset( $http_url_parsed['scheme'] ) ) {
$http_url_clean .=
$http_url_parsed['scheme'] . '://';
}
if ( isset( $http_url_parsed['host'] ) ) {
$http_url_clean .=
$http_url_parsed['host'];
}
if ( isset( $http_url_parsed['port'] ) ) {
$http_url_clean .= ':' . (int) $http_url_parsed['port'];
}
if ( isset( $http_url_parsed['path'] ) ) {
$http_url_clean .= '/' . $http_url_parsed['path'];
}
vipgoci_log(
'Warning: Sunset HTTP header detected, feature will become unavailable',
array(
'http_url_clean' => $http_url_clean,
'sunset_date' => $sunset_date,
),
0,
true // Log to IRC.
);
}
/**
* Detect if we exceeded the API rate limit,
* and if so, exit with error.
*
* @param string $http_api_url API URL used.
* @param array $resp_headers HTTP response headers.
*
* @return void
*/
function vipgoci_http_api_rate_limit_check(
string $http_api_url,
array $resp_headers
) :void {
/*
* Special case for WPScan API: Unlimited requests
* are indicated with a negative number for
* x-ratelimit-remaining header. Here we ignore
* such headers for WPScan API responses.
*/
if (
( true === str_starts_with(
$http_api_url,
VIPGOCI_WPSCAN_API_BASE_URL,
) ) &&
( isset( $resp_headers['x-ratelimit-remaining'][0] ) ) &&
( is_numeric( $resp_headers['x-ratelimit-remaining'][0] ) ) &&
( $resp_headers['x-ratelimit-remaining'][0] < 0 )
) {
return;
}
/*
* Look for ratelimit header.
*/
if (
( isset( $resp_headers['x-ratelimit-remaining'][0] ) ) &&
( is_numeric( $resp_headers['x-ratelimit-remaining'][0] ) ) &&
( $resp_headers['x-ratelimit-remaining'][0] <= 1 )
) {
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'http_api_request_limit_reached',
1
);
vipgoci_sysexit(
'Exceeded rate limit for HTTP API, unable to ' .
'continue without making further requests.',
array(
'http_api_url' => $http_api_url,
'x-ratelimit-remaining' => $resp_headers['x-ratelimit-remaining'][0],
'x-ratelimit-limit' => isset( $resp_headers['x-ratelimit-limit'][0] )
? $resp_headers['x-ratelimit-limit'][0] : null,
),
VIPGOCI_EXIT_HTTP_API_ERROR,
true // Log to IRC.
);
}
}
/**
* Save or get saved HTTP API rate limit information and return.
*
* @param string $http_api_url HTTP request URL.
* @param array $http_headers_response All HTTP headers from HTTP response as array.
*
* @return array|null Results as array, null when no results are cached or invalid HTTP URL is provided.
*/
function vipgoci_http_api_rate_limit_usage(
string $http_api_url = '',
array $http_headers_response = array()
) :array|null {
static $ratelimit_usage = array();
if (
( empty( $http_api_url ) ) ||
( empty( $http_headers_response ) )
) {
if ( empty( $ratelimit_usage ) ) {
return null;
} else {
return $ratelimit_usage;
}
}
if ( true === str_starts_with(
$http_api_url,
VIPGOCI_GITHUB_BASE_URL
) ) {
$service = 'github';
} elseif ( true === str_starts_with(
$http_api_url,
VIPGOCI_WPSCAN_API_BASE_URL
) ) {
$service = 'wpscan';
} else {
return null;
}
foreach ( array(
'x-ratelimit-limit',
'x-ratelimit-remaining',
'x-ratelimit-reset',
'x-ratelimit-used',
'x-ratelimit-resource',
) as $key ) {
if ( isset( $http_headers_response[ $key ][0] ) ) {
$key_short = str_replace(
'x-ratelimit-',
'',
$key
);
if ( is_numeric( $http_headers_response[ $key ][0] ) ) {
$ratelimit_usage[ $service ][ $key_short ] =
(int) $http_headers_response[ $key ][0];
} else {
$ratelimit_usage[ $service ][ $key_short ] =
$http_headers_response[ $key ][0];
}
}
}
return $ratelimit_usage;
}
/**
* Make sure to wait between requests to HTTP APIs,
* but only for certain APIs and when needed.
*
* This function should be called just before
* sending a request to a HTTP API -- that is the most
* effective usage. Will only wait if not enough time
* has passed between calls to this function and if the
* HTTP API URL specified matches one of the URLs in
* VIPGOCI_HTTP_API_WAIT_APIS_ARRAY.
*
* See here for background for GitHub API requests:
* https://developer.github.com/v3/guides/best-practices-for-integrators/#dealing-with-abuse-rate-limits
*
* @param string $http_api_url The HTTP API URL being called.
*
* @return void
*/
function vipgoci_http_api_wait( string $http_api_url ) :void {
static $last_request_time = null;
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'http_api_forced_wait' );
/*
* Only wait in case of certain APIs being called.
*/
$http_api_host = parse_url(
$http_api_url,
PHP_URL_HOST
);
$maybe_wait = false;
if ( ! empty( $http_api_host ) ) {
$maybe_wait = vipgoci_string_found_in_substrings_array(
VIPGOCI_HTTP_API_WAIT_APIS_ARRAY,
$http_api_host,
false
);
}
if ( false === $maybe_wait ) {
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'http_api_forced_wait' );
return;
}
/*
* We should maybe wait.
*/
if ( null !== $last_request_time ) {
/*
* Only sleep if less than specified time
* has elapsed from last request.
*/
if (
( time() - $last_request_time ) <
VIPGOCI_HTTP_API_WAIT_TIME_SECONDS
) {
sleep( VIPGOCI_HTTP_API_WAIT_TIME_SECONDS );
}
}
$last_request_time = time();
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'http_api_forced_wait' );
}
/**
* Make a GET request to HTTP API, for the URL
* provided, using the access-token specified.
*
* Will return the raw-data returned by the HTTP API,
* or halt execution on repeated errors.
*
* @param string $http_api_url HTTP request URL.
* @param null|string|array $http_api_token Access token to use as string or array, null to skip.
* @param bool $fatal_error_on_failure If to exit on failure or return.
* @param int $curl_retries_max How often to retry request in case of failure.
*
* @return string|null String containing results on success, null on failure (if set not to exit).
*/
function vipgoci_http_api_fetch_url(
string $http_api_url,
null|string|array $http_api_token,
bool $fatal_error_on_failure = true,
int $curl_retries_max = 4
) :string|null {
$curl_retries = 0;
/*
* Attempt to send request -- retry if
* it fails.
*/
do {
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_URL,
$http_api_url
);
curl_setopt(
$ch,
CURLOPT_RETURNTRANSFER,
1
);
curl_setopt(
$ch,
CURLOPT_CONNECTTIMEOUT,
VIPGOCI_HTTP_API_LONG_TIMEOUT
);
curl_setopt(
$ch,
CURLOPT_USERAGENT,
VIPGOCI_CLIENT_ID
);
curl_setopt(
$ch,
CURLOPT_HEADERFUNCTION,
'vipgoci_curl_headers'
);
/*
* Set HTTP headers.
*/
$tmp_http_headers_arr = array();
if (
( is_string( $http_api_token ) ) &&
( strlen( $http_api_token ) > 0 )
) {
$tmp_http_headers_arr[] = 'Authorization: token ' . $http_api_token;
} elseif (
( is_array( $http_api_token ) ) &&
( isset( $http_api_token['wpscan_token'] ) )
) {
$tmp_http_headers_arr[] = 'Authorization: Token token=' .
$http_api_token['wpscan_token'];
}
vipgoci_github_api_version_header_maybe_add(
$http_api_url,
$tmp_http_headers_arr
);
if ( ! empty( $tmp_http_headers_arr ) ) {
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
$tmp_http_headers_arr
);
}
vipgoci_curl_set_security_options(
$ch
);
// Make sure to wait if needed.
vipgoci_http_api_wait( $http_api_url );
/*
* Execute query to API, keep
* record of how long time it took,
* and also keep count of how many we do.
*/
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'http_api_request_get' );
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'http_api_request_get',
1
);
$resp_data = curl_exec( $ch );
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'http_api_request_get' );
$resp_headers = vipgoci_curl_headers(
null,
null
);
/*
* Detect and process possible errors
*/
if (
( false === $resp_data ) ||
( curl_errno( $ch ) ) ||
(
// Detect internal server errors (HTTP 50X).
( isset( $resp_headers['status'][0] ) ) &&
( 500 <= (int) $resp_headers['status'][0] ) &&
( 600 > (int) $resp_headers['status'][0] )
) ||
(
( isset( $resp_headers['retry-after'] ) ) &&
( intval( $resp_headers['retry-after'] ) > 0 )
)
) {
if (
( isset( $resp_headers['retry-after'] ) ) &&
( intval( $resp_headers['retry-after'] ) > 0 )
) {
$retry_sleep = intval( $resp_headers['retry-after'] ) + 1;
} else {
$retry_sleep = 10;
}
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'http_api_request_failure',
1
);
vipgoci_log(
'Sending GET request to HTTP API failed' .
( ( $curl_retries < $curl_retries_max ) ?
', will retry in ' . (string) $retry_sleep . ' second(s)' :
'' ),
array(
'http_api_url' => $http_api_url,
'curl_retries' => $curl_retries,
'curl_errno' => curl_errno(
$ch
),
'curl_errormsg' => curl_strerror(
curl_errno( $ch )
),
'http_status' =>
isset( $resp_headers['status'] ) ?
$resp_headers['status'] : null,
'x-github-request-id' =>
isset( $resp_headers['x-github-request-id'] ) ?
$resp_headers['x-github-request-id'] : null,
'http_response_headers' => $resp_headers,
'http_response_body' => $resp_data,
),
0,
true // Log to IRC.
);
$resp_data = false;
sleep( $retry_sleep );
}
vipgoci_http_api_rate_limit_usage(
$http_api_url,
$resp_headers
);
vipgoci_http_api_rate_limit_check(
$http_api_url,
$resp_headers
);
vipgoci_http_resp_sunset_header_check(
$http_api_url,
$resp_headers
);
curl_close( $ch );
} while (
( false === $resp_data ) &&
( $curl_retries++ < $curl_retries_max )
);
if (
( true === $fatal_error_on_failure ) &&
( false === $resp_data )
) {
vipgoci_sysexit(
'Gave up retrying request to HTTP API, can not continue',
array(),
VIPGOCI_EXIT_HTTP_API_ERROR,
true // Log to IRC.
);
} elseif (
( false === $fatal_error_on_failure ) &&
( false === $resp_data )
) {
return null;
}
return $resp_data;
}
/**
* Send a POST/DELETE request to HTTP API -- attempt
* to retry if errors were encountered.
*
* Note that the '$http_delete' parameter will determine
* if a POST or DELETE request will be sent.
*
* @param string $http_api_url HTTP request URL.
* @param array $http_api_postfields HTTP request postfields.
* @param null|string|array $http_api_token Access token to use as string or array, null to skip.
* @param bool $http_delete When true, performs HTTP DELETE instead of POST.
* @param bool $json_encode If true, will JSON encode $http_api_postfields using json_encode()
* before sending request, else uses http_build_query() to
* generate URL-encoded query-string from $http_api_postfields.
* @param int $http_version What HTTP protocol version to use with cURL, by default lets cURL decide.
* @param string $http_content_type The HTTP Content-Type header value to use. 'application/json' is the default.
* @param int $retry_max How often to retry request in case of failure.
* @param int $timeout Connection timeout, by default VIPGOCI_HTTP_API_LONG_TIMEOUT.
*
* @return string|int Request body as string on success, -1 on failure. Failures will be logged.
*
* @codeCoverageIgnore
*/
function vipgoci_http_api_post_url(
string $http_api_url,
array $http_api_postfields,
null|string|array $http_api_token,
bool $http_delete = false,
bool $json_encode = true,
int $http_version = CURL_HTTP_VERSION_NONE,
string $http_content_type = VIPGOCI_HTTP_API_CONTENT_TYPE_APPLICATION_JSON,
int $retry_max = 4,
int $timeout = VIPGOCI_HTTP_API_LONG_TIMEOUT
) :string|int {
/*
* Actually send a request to HTTP API -- make sure
* to retry if something fails.
*/
$retry_cnt = 0;
do {
/*
* By default, assume request went through okay.
*/
$ret_val = 0;
/*
* By default, do not retry the request.
*/
$retry_req = false;
/*
* Initialize and send request.
*/
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_URL,
$http_api_url
);
curl_setopt(
$ch,
CURLOPT_RETURNTRANSFER,
1
);
curl_setopt(
$ch,
CURLOPT_CONNECTTIMEOUT,
$timeout
);
curl_setopt(
$ch,
CURLOPT_USERAGENT,
VIPGOCI_CLIENT_ID
);
curl_setopt(
$ch,
CURLOPT_HTTP_VERSION,
$http_version
);
if ( false === $http_delete ) {
curl_setopt(
$ch,
CURLOPT_POST,
1
);
} else {
curl_setopt(
$ch,
CURLOPT_CUSTOMREQUEST,
'DELETE'
);
}
// Encode postfields as JSON if requested, else generate URL-encoded query string.
if ( true === $json_encode ) {
$tmp_postfields = json_encode(
$http_api_postfields
);
} else {
$tmp_postfields = http_build_query(
$http_api_postfields
);
}
curl_setopt(
$ch,
CURLOPT_POSTFIELDS,
$tmp_postfields
);
curl_setopt(
$ch,
CURLOPT_HEADERFUNCTION,
'vipgoci_curl_headers'
);
/*
* Set HTTP headers.
*/
$tmp_http_headers_arr = array();
if (
( is_string( $http_api_token ) ) &&
( strlen( $http_api_token ) > 0 )
) {
$tmp_http_headers_arr[] = 'Authorization: token ' . $http_api_token;
} elseif (
( is_array( $http_api_token ) ) &&
( isset( $http_api_token['bearer'] ) )
) {
$tmp_http_headers_arr[] = 'Authorization: Bearer ' . $http_api_token['bearer'];
}
if ( strlen( $http_content_type ) > 0 ) {
$tmp_http_headers_arr[] = 'Content-Type: ' . $http_content_type;
}
vipgoci_github_api_version_header_maybe_add(
$http_api_url,
$tmp_http_headers_arr
);
if ( ! empty( $tmp_http_headers_arr ) ) {
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
$tmp_http_headers_arr
);
}
unset( $tmp_http_headers_arr );
vipgoci_curl_set_security_options(
$ch
);
// Make sure to wait if needed.
vipgoci_http_api_wait( $http_api_url );
/*
* Execute query to HTTP API, keep
* record of how long time it took,
* and keep count of how many requests we do.
*/
if ( false === $http_delete ) {
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'http_api_request_post' );
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'http_api_request_post',
1
);
} else {
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'http_api_request_delete' );
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'http_api_request_delete',
1
);
}
$resp_data = curl_exec( $ch );
if ( false === $http_delete ) {
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'http_api_request_post' );
} else {
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'http_api_request_delete' );
}
$resp_headers = vipgoci_curl_headers(
null,
null
);
if ( false === $resp_data ) {
// Request failed, retry.
$retry_req = true;
// Retry in one second.
$retry_sleep = 1;
// Indicate request failed.
$ret_val = -1;
} elseif (
// Allow certain statuses, depending on type of request.
(
( false === $http_delete ) &&
( isset( $resp_headers['status'][0] ) ) &&
( intval( $resp_headers['status'][0] ) !== 200 ) &&
( intval( $resp_headers['status'][0] ) !== 201 ) &&
( intval( $resp_headers['status'][0] ) !== 100 )
)
||
(
( true === $http_delete ) &&
( isset( $resp_headers['status'][0] ) ) &&
( intval( $resp_headers['status'][0] ) !== 204 ) &&
( intval( $resp_headers['status'][0] ) !== 200 )
)
) {
// Indicate request failed.
$ret_val = -1;
// Set wait period between requests. May be altered.
$retry_sleep = 10;
/*
* Figure out if to retry.
*/
// Decode JSON.
$resp_data = json_decode( $resp_data );
if (
( false !== $resp_data ) &&
( isset( $resp_data->message ) )
) {
$resp_data->message = trim( $resp_data->message );
}
if (
( false !== $resp_data ) &&
( isset( $resp_headers['retry-after'] ) ) &&
( intval( $resp_headers['retry-after'] ) > 0 )
) {
$retry_req = true;
$retry_sleep = intval(
$resp_headers['retry-after']
) + 1;
} elseif (
( false !== $resp_data ) &&
( isset( $resp_data->message ) ) &&
( isset( $resp_data->errors[0] ) ) &&
( 'Validation Failed' === $resp_data->message ) &&
( 'was submitted too quickly after a previous comment' === $resp_data->errors[0] )
) {
/*
* Here we cannot retry, as submission
* has been labelled as "spam".
*/
$retry_req = false;
} elseif (
( false !== $resp_data ) &&
( isset( $resp_data->message ) ) &&
( 'Validation Failed' === $resp_data->message )
) {
$retry_req = false;
} elseif (
( false !== $resp_data ) &&
( isset( $resp_data->message ) ) &&
( 'Server Error' === $resp_data->message )
) {
$retry_req = false;
}
}
// On failure, log message.
if ( -1 === $ret_val ) {
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'http_api_request_failure',
1
);
vipgoci_log(
( false === $resp_data ?
'Sending POST request to HTTP API failed' :
'HTTP API reported an error during POST request'
) .
( ( true === $retry_req ) && ( $retry_cnt < $retry_max ) ?
', will retry request in ' . (string) $retry_sleep . ' second(s)' :
''
),
array(
'http_api_url' => $http_api_url,
'retry_cnt' => $retry_cnt,
'curl_errno' => curl_errno(
$ch
),
'curl_errormsg' => curl_strerror(
curl_errno( $ch )
),
'http_status' =>
isset( $resp_headers['status'] ) ?
$resp_headers['status'] : null,
'x-github-request-id' =>
isset( $resp_headers['x-github-request-id'] ) ?
$resp_headers['x-github-request-id'] : null,
'http_response_headers' => $resp_headers,
'http_response_body' => $resp_data,
),
0,
true // Log to IRC.
);
sleep( $retry_sleep );
}
vipgoci_http_api_rate_limit_check(
$http_api_url,
$resp_headers
);
vipgoci_http_api_rate_limit_usage(
$http_api_url,
$resp_headers
);
vipgoci_http_resp_sunset_header_check(
$http_api_url,
$resp_headers
);
curl_close( $ch );
} while (