forked from git-for-windows/git
-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathgvfs-helper.c
4210 lines (3605 loc) · 119 KB
/
gvfs-helper.c
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
// TODO Write a man page. Here are some notes for dogfooding.
// TODO
//
// Usage: git gvfs-helper [<main_options>] <sub-command> [<sub-command-options>]
//
// <main_options>:
//
// --remote=<remote-name> // defaults to "origin"
//
// --fallback // boolean. defaults to off
//
// When a fetch from the cache-server fails, automatically
// fallback to the main Git server. This option has no effect
// if no cache-server is defined.
//
// --cache-server=<use> // defaults to "verify"
//
// verify := lookup the set of defined cache-servers using
// "gvfs/config" and confirm that the selected
// cache-server is well-known. Silently disable the
// cache-server if not. (See security notes later.)
//
// error := verify cache-server and abort if not well-known.
//
// trust := do not verify cache-server. just use it, if set.
//
// disable := disable the cache-server and always use the main
// Git server.
//
// --shared-cache=<odb-directory-pathname>
//
// A relative or absolute pathname to the ODB directory to store
// fetched objects.
//
// If this option is not specified, we default to the value
// in the "gvfs.sharedcache" config setting and then to the
// local ".git/objects" directory.
//
// <sub-command>:
//
// config
//
// Fetch the "gvfs/config" string from the main Git server.
// (The cache-server setting is ignored because cache-servers
// do not support this REST API.)
//
// get
//
// Fetch 1 or more objects one at a time using a "/gvfs/objects"
// GET request.
//
// If a cache-server is configured,
// try it first. Optionally fallback to the main Git server.
//
// The set of objects is given on stdin and is assumed to be
// a list of <oid>, one per line.
//
// <get-options>:
//
// --max-retries=<n> // defaults to "6"
//
// Number of retries after transient network errors.
// Set to zero to disable such retries.
//
// post
//
// Fetch 1 or more objects in bulk using a "/gvfs/objects" POST
// request.
//
// If a cache-server is configured,
// try it first. Optionally fallback to the main Git server.
//
// The set of objects is given on stdin and is assumed to be
// a list of <oid>, one per line.
//
// <get-options>:
//
// --block-size=<n> // defaults to "4000"
//
// Request objects from server in batches of at
// most n objects (not bytes).
//
// --depth=<depth> // defaults to "1"
//
// --max-retries=<n> // defaults to "6"
//
// Number of retries after transient network errors.
// Set to zero to disable such retries.
//
// prefetch
//
// Use "/gvfs/prefetch" REST API to fetch 1 or more commits-and-trees
// prefetch packs from the server.
//
// <prefetch-options>:
//
// --since=<t> // defaults to "0"
//
// Time in seconds since the epoch. If omitted or
// zero, the timestamp from the newest prefetch
// packfile found in the shared-cache ODB is used.
// (This is based upon the packfile name, not the
// mtime.)
//
// The GVFS Protocol defines this value as a way to
// request cached packfiles NEWER THAN this timestamp.
//
// server
//
// Interactive/sub-process mode. Listen for a series of commands
// and data on stdin and return results on stdout. This command
// uses pkt-line format [1] and implements the long-running process
// protocol [2] to communicate with the foreground/parent process.
//
// <server-options>:
//
// --block-size=<n> // defaults to "4000"
//
// Request objects from server in batches of at
// most n objects (not bytes) when using POST
// requests.
//
// --depth=<depth> // defaults to "1"
//
// --max-retries=<n> // defaults to "6"
//
// Number of retries after transient network errors.
// Set to zero to disable such retries.
//
// Interactive verb: objects.get
//
// Fetch 1 or more objects, one at a time, using a
// "/gvfs/objects" GET requests.
//
// Each object will be created as a loose object in the ODB.
//
// Create 1 or more loose objects in the shared-cache ODB.
// (The pathname of the selected ODB is reported at the
// beginning of the response; this should match the pathname
// given on the command line).
//
// git> objects.get
// git> <oid>
// git> <oid>
// git> ...
// git> <oid>
// git> 0000
//
// git< odb <directory>
// git< loose <oid>
// git< loose <oid>
// git< ...
// git< loose <oid>
// git< ok | partial | error <message>
// git< 0000
//
// Interactive verb: objects.post
//
// Fetch 1 or more objects, in bulk, using one or more
// "/gvfs/objects" POST requests.
//
// Create 1 or more loose objects and/or packfiles in the
// shared-cache ODB. A POST is allowed to respond with
// either loose or packed objects.
//
// git> objects.post
// git> <oid>
// git> <oid>
// git> ...
// git> <oid>
// git> 0000
//
// git< odb <directory>
// git< loose <oid> | packfile <filename.pack>
// git< loose <oid> | packfile <filename.pack>
// git< ...
// git< loose <oid> | packfile <filename.pack>
// git< ok | partial | error <message>
// git< 0000
//
// Interactive verb: object.prefetch
//
// Fetch 1 or more prefetch packs using a "/gvfs/prefetch"
// request.
//
// git> objects.prefetch
// git> <timestamp> // optional
// git> 0000
//
// git< odb <directory>
// git< packfile <filename.pack>
// git< packfile <filename.pack>
// git< ...
// git< packfile <filename.pack>
// git< ok | error <message>
// git< 0000
//
// If a cache-server is configured, try it first.
// Optionally fallback to the main Git server.
//
// [1] Documentation/technical/protocol-common.txt
// [2] Documentation/technical/long-running-process-protocol.txt
// [3] See GIT_TRACE_PACKET
//
// endpoint
//
// Fetch the given endpoint from the main Git server (specifying
// `gvfs/config` as endpoint is idempotent to the `config`
// command mentioned above).
//
//////////////////////////////////////////////////////////////////
#include "cache.h"
#include "config.h"
#include "remote.h"
#include "connect.h"
#include "strbuf.h"
#include "walker.h"
#include "http.h"
#include "exec-cmd.h"
#include "run-command.h"
#include "pkt-line.h"
#include "string-list.h"
#include "sideband.h"
#include "strvec.h"
#include "credential.h"
#include "oid-array.h"
#include "send-pack.h"
#include "protocol.h"
#include "quote.h"
#include "transport.h"
#include "parse-options.h"
#include "object-store.h"
#include "json-writer.h"
#include "tempfile.h"
#include "oidset.h"
#include "dir.h"
#include "progress.h"
#include "packfile.h"
#define TR2_CAT "gvfs-helper"
static const char * const main_usage[] = {
N_("git gvfs-helper [<main_options>] config [<options>]"),
N_("git gvfs-helper [<main_options>] get [<options>]"),
N_("git gvfs-helper [<main_options>] post [<options>]"),
N_("git gvfs-helper [<main_options>] prefetch [<options>]"),
N_("git gvfs-helper [<main_options>] server [<options>]"),
NULL
};
static const char *const objects_get_usage[] = {
N_("git gvfs-helper [<main_options>] get [<options>]"),
NULL
};
static const char *const objects_post_usage[] = {
N_("git gvfs-helper [<main_options>] post [<options>]"),
NULL
};
static const char *const prefetch_usage[] = {
N_("git gvfs-helper [<main_options>] prefetch [<options>]"),
NULL
};
static const char *const server_usage[] = {
N_("git gvfs-helper [<main_options>] server [<options>]"),
NULL
};
/*
* "commitDepth" field in gvfs protocol
*/
#define GH__DEFAULT__OBJECTS_POST__COMMIT_DEPTH 1
/*
* Chunk/block size in number of objects we request in each packfile
*/
#define GH__DEFAULT__OBJECTS_POST__BLOCK_SIZE 4000
/*
* Retry attempts (after the initial request) for transient errors and 429s.
*/
#define GH__DEFAULT_MAX_RETRIES 6
/*
* Maximum delay in seconds for transient (network) error retries.
*/
#define GH__DEFAULT_MAX_TRANSIENT_BACKOFF_SEC 300
/*
* Our exit-codes.
*/
enum gh__error_code {
GH__ERROR_CODE__USAGE = -1, /* will be mapped to usage() */
GH__ERROR_CODE__OK = 0,
GH__ERROR_CODE__ERROR = 1, /* unspecified */
GH__ERROR_CODE__CURL_ERROR = 2,
GH__ERROR_CODE__HTTP_401 = 3,
GH__ERROR_CODE__HTTP_404 = 4,
GH__ERROR_CODE__HTTP_429 = 5,
GH__ERROR_CODE__HTTP_503 = 6,
GH__ERROR_CODE__HTTP_OTHER = 7,
GH__ERROR_CODE__UNEXPECTED_CONTENT_TYPE = 8,
GH__ERROR_CODE__COULD_NOT_CREATE_TEMPFILE = 8,
GH__ERROR_CODE__COULD_NOT_INSTALL_LOOSE = 10,
GH__ERROR_CODE__COULD_NOT_INSTALL_PACKFILE = 11,
GH__ERROR_CODE__SUBPROCESS_SYNTAX = 12,
GH__ERROR_CODE__INDEX_PACK_FAILED = 13,
GH__ERROR_CODE__COULD_NOT_INSTALL_PREFETCH = 14,
};
enum gh__cache_server_mode {
/* verify URL. disable if unknown. */
GH__CACHE_SERVER_MODE__VERIFY_DISABLE = 0,
/* verify URL. error if unknown. */
GH__CACHE_SERVER_MODE__VERIFY_ERROR,
/* disable the cache-server, if defined */
GH__CACHE_SERVER_MODE__DISABLE,
/* trust any cache-server */
GH__CACHE_SERVER_MODE__TRUST_WITHOUT_VERIFY,
};
/*
* The set of command line, config, and environment variables
* that we use as input to decide how we should operate.
*/
static struct gh__cmd_opts {
const char *remote_name;
int try_fallback; /* to git server if cache-server fails */
int show_progress;
int depth;
int block_size;
int max_retries;
int max_transient_backoff_sec;
enum gh__cache_server_mode cache_server_mode;
} gh__cmd_opts;
/*
* The chosen global state derrived from the inputs in gh__cmd_opts.
*/
static struct gh__global {
struct remote *remote;
struct credential main_creds;
struct credential cache_creds;
const char *main_url;
const char *cache_server_url;
struct strbuf buf_odb_path;
int http_is_initialized;
int cache_server_is_initialized; /* did sub-command look for one */
int main_creds_need_approval; /* try to only approve them once */
} gh__global;
enum gh__server_type {
GH__SERVER_TYPE__MAIN = 0,
GH__SERVER_TYPE__CACHE = 1,
GH__SERVER_TYPE__NR,
};
static const char *gh__server_type_label[GH__SERVER_TYPE__NR] = {
"(main)",
"(cs)"
};
enum gh__objects_mode {
GH__OBJECTS_MODE__NONE = 0,
/*
* Bulk fetch objects.
*
* But also, force the use of HTTP POST regardless of how many
* objects we are requesting.
*
* The GVFS Protocol treats requests for commit objects
* differently in GET and POST requests WRT whether it
* automatically also fetches the referenced trees.
*/
GH__OBJECTS_MODE__POST,
/*
* Fetch objects one at a time using HTTP GET.
*
* Force the use of GET (primarily because of the commit
* object treatment).
*/
GH__OBJECTS_MODE__GET,
/*
* Fetch one or more pre-computed "prefetch packs" containing
* commits and trees.
*/
GH__OBJECTS_MODE__PREFETCH,
};
struct gh__azure_throttle
{
unsigned long tstu_limit;
unsigned long tstu_remaining;
unsigned long reset_sec;
unsigned long retry_after_sec;
};
static void gh__azure_throttle__zero(struct gh__azure_throttle *azure)
{
azure->tstu_limit = 0;
azure->tstu_remaining = 0;
azure->reset_sec = 0;
azure->retry_after_sec = 0;
}
#define GH__AZURE_THROTTLE_INIT { \
.tstu_limit = 0, \
.tstu_remaining = 0, \
.reset_sec = 0, \
.retry_after_sec = 0, \
}
static struct gh__azure_throttle gh__global_throttle[GH__SERVER_TYPE__NR] = {
GH__AZURE_THROTTLE_INIT,
GH__AZURE_THROTTLE_INIT,
};
/*
* Stolen from http.c
*/
static CURLcode gh__curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
{
char *ptr;
CURLcode ret;
strbuf_reset(buf);
ret = curl_easy_getinfo(curl, info, &ptr);
if (!ret && ptr)
strbuf_addstr(buf, ptr);
return ret;
}
enum gh__progress_state {
GH__PROGRESS_STATE__START = 0,
GH__PROGRESS_STATE__PHASE1,
GH__PROGRESS_STATE__PHASE2,
GH__PROGRESS_STATE__PHASE3,
};
/*
* Parameters to drive an HTTP request (with any necessary retries).
*/
struct gh__request_params {
/*
* b_is_post indicates if the current HTTP request is a POST=1 or
* a GET=0. This is a lower level field used to setup CURL and
* the tempfile used to receive the content.
*
* It is related to, but different from the GH__OBJECTS_MODE__
* field that we present to the gvfs-helper client or in the CLI
* (which only concerns the semantics of the /gvfs/objects protocol
* on the set of requested OIDs).
*
* For example, we use an HTTP GET to get the /gvfs/config data
* into a buffer.
*/
int b_is_post;
int b_write_to_file; /* write to file=1 or strbuf=0 */
int b_permit_cache_server_if_defined;
enum gh__objects_mode objects_mode;
enum gh__server_type server_type;
int k_attempt; /* robust retry attempt */
int k_transient_delay_sec; /* delay before transient error retries */
unsigned long object_count; /* number of objects being fetched */
const struct strbuf *post_payload; /* POST body to send */
struct curl_slist *headers; /* additional http headers to send */
struct tempfile *tempfile; /* for response content when file */
struct strbuf *buffer; /* for response content when strbuf */
struct strbuf tr2_label; /* for trace2 regions */
struct object_id loose_oid;
/*
* Note that I am putting all of the progress-related instance data
* inside the request-params in the hope that we can eventually
* do multi-threaded/concurrent HTTP requests when chunking
* large requests. However, the underlying "struct progress" API
* is not thread safe (that is, it doesn't allow concurrent progress
* reports (since that might require multiple lines on the screen
* or something)).
*/
enum gh__progress_state progress_state;
struct strbuf progress_base_phase2_msg;
struct strbuf progress_base_phase3_msg;
/*
* The buffer for the formatted progress message is shared by the
* "struct progress" API and must remain valid for the duration of
* the start_progress..stop_progress lifespan.
*/
struct strbuf progress_msg;
struct progress *progress;
struct strbuf e2eid;
struct string_list *result_list; /* we do not own this */
};
#define GH__REQUEST_PARAMS_INIT { \
.b_is_post = 0, \
.b_write_to_file = 0, \
.b_permit_cache_server_if_defined = 1, \
.server_type = GH__SERVER_TYPE__MAIN, \
.k_attempt = 0, \
.k_transient_delay_sec = 0, \
.object_count = 0, \
.post_payload = NULL, \
.headers = NULL, \
.tempfile = NULL, \
.buffer = NULL, \
.tr2_label = STRBUF_INIT, \
.loose_oid = {{0}}, \
.progress_state = GH__PROGRESS_STATE__START, \
.progress_base_phase2_msg = STRBUF_INIT, \
.progress_base_phase3_msg = STRBUF_INIT, \
.progress_msg = STRBUF_INIT, \
.progress = NULL, \
.e2eid = STRBUF_INIT, \
.result_list = NULL, \
}
static void gh__request_params__release(struct gh__request_params *params)
{
if (!params)
return;
params->post_payload = NULL; /* we do not own this */
curl_slist_free_all(params->headers);
params->headers = NULL;
delete_tempfile(¶ms->tempfile);
params->buffer = NULL; /* we do not own this */
strbuf_release(¶ms->tr2_label);
strbuf_release(¶ms->progress_base_phase2_msg);
strbuf_release(¶ms->progress_base_phase3_msg);
strbuf_release(¶ms->progress_msg);
stop_progress(¶ms->progress);
params->progress = NULL;
strbuf_release(¶ms->e2eid);
params->result_list = NULL; /* we do not own this */
}
/*
* How we handle retries for various unexpected network errors.
*/
enum gh__retry_mode {
/*
* The operation was successful, so no retry is needed.
* Use this for HTTP 200, for example.
*/
GH__RETRY_MODE__SUCCESS = 0,
/*
* Retry using the normal 401 Auth mechanism.
*/
GH__RETRY_MODE__HTTP_401,
/*
* Fail because at least one of the requested OIDs does not exist.
*/
GH__RETRY_MODE__FAIL_404,
/*
* A transient network error, such as dropped connection
* or network IO error. Our belief is that a retry MAY
* succeed. (See Gremlins and Cosmic Rays....)
*/
GH__RETRY_MODE__TRANSIENT,
/*
* Request was blocked completely because of a 429.
*/
GH__RETRY_MODE__HTTP_429,
/*
* Request failed because the server was (temporarily?) offline.
*/
GH__RETRY_MODE__HTTP_503,
/*
* The operation had a hard failure and we have no
* expectation that a second attempt will give a different
* answer, such as a bad hostname or a mal-formed URL.
*/
GH__RETRY_MODE__HARD_FAIL,
};
/*
* Bucket to describe the results of an HTTP requests (may be
* overwritten during retries so that it describes the final attempt).
*/
struct gh__response_status {
struct strbuf error_message;
struct strbuf content_type;
enum gh__error_code ec;
enum gh__retry_mode retry;
intmax_t bytes_received;
struct gh__azure_throttle *azure;
};
#define GH__RESPONSE_STATUS_INIT { \
.error_message = STRBUF_INIT, \
.content_type = STRBUF_INIT, \
.ec = GH__ERROR_CODE__OK, \
.retry = GH__RETRY_MODE__SUCCESS, \
.bytes_received = 0, \
.azure = NULL, \
}
static void gh__response_status__zero(struct gh__response_status *s)
{
strbuf_setlen(&s->error_message, 0);
strbuf_setlen(&s->content_type, 0);
s->ec = GH__ERROR_CODE__OK;
s->retry = GH__RETRY_MODE__SUCCESS;
s->bytes_received = 0;
s->azure = NULL;
}
static void install_result(struct gh__request_params *params,
struct gh__response_status *status);
/*
* Log the E2EID for the current request.
*
* Since every HTTP request to the cache-server and to the main Git server
* will send back a unique E2EID (probably a GUID), we don't want to overload
* telemetry with each ID -- rather, only the ones for which there was a
* problem and that may be helpful in a post mortem.
*/
static void log_e2eid(struct gh__request_params *params,
struct gh__response_status *status)
{
if (!params->e2eid.len)
return;
switch (status->retry) {
default:
case GH__RETRY_MODE__SUCCESS:
case GH__RETRY_MODE__HTTP_401:
case GH__RETRY_MODE__FAIL_404:
return;
case GH__RETRY_MODE__HARD_FAIL:
case GH__RETRY_MODE__TRANSIENT:
case GH__RETRY_MODE__HTTP_429:
case GH__RETRY_MODE__HTTP_503:
break;
}
if (trace2_is_enabled()) {
struct strbuf key = STRBUF_INIT;
strbuf_addstr(&key, "e2eid");
strbuf_addstr(&key, gh__server_type_label[params->server_type]);
trace2_data_string(TR2_CAT, NULL, key.buf,
params->e2eid.buf);
strbuf_release(&key);
}
}
/*
* Normalize a few HTTP response codes before we try to decide
* how to dispatch on them.
*/
static long gh__normalize_odd_codes(struct gh__request_params *params,
long http_response_code)
{
if (params->server_type == GH__SERVER_TYPE__CACHE &&
http_response_code == 400) {
/*
* The cache-server sends a somewhat bogus 400 instead of
* the normal 401 when AUTH is required. Fixup the status
* to hide that.
*
* TODO Technically, the cache-server could send a 400
* TODO for many reasons, not just for their bogus
* TODO pseudo-401, but we're going to assume it is a
* TODO 401 for now. We should confirm the expected
* TODO error message in the response-body.
*/
return 401;
}
if (http_response_code == 203) {
/*
* A proxy server transformed a 200 from the origin server
* into a 203. We don't care about the subtle distinction.
*/
return 200;
}
return http_response_code;
}
/*
* Map HTTP response codes into a retry strategy.
* See https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
*
* https://docs.microsoft.com/en-us/azure/devops/integrate/concepts/rate-limits?view=azure-devops
*/
static void compute_retry_mode_from_http_response(
struct gh__response_status *status,
long http_response_code)
{
switch (http_response_code) {
case 200:
status->retry = GH__RETRY_MODE__SUCCESS;
status->ec = GH__ERROR_CODE__OK;
return;
case 301: /* all the various flavors of HTTP Redirect */
case 302:
case 303:
case 304:
case 305:
case 306:
case 307:
case 308:
/*
* TODO Consider a redirected-retry (with or without
* TODO a Retry-After header).
*/
goto hard_fail;
case 401:
strbuf_addstr(&status->error_message,
"(http:401) Not Authorized");
status->retry = GH__RETRY_MODE__HTTP_401;
status->ec = GH__ERROR_CODE__HTTP_401;
return;
case 404:
/*
* TODO if params->object_count > 1, consider
* TODO splitting the request into 2 halves
* TODO and retrying each half in series.
*/
strbuf_addstr(&status->error_message,
"(http:404) Not Found");
status->retry = GH__RETRY_MODE__FAIL_404;
status->ec = GH__ERROR_CODE__HTTP_404;
return;
case 429:
/*
* This is a hard block because we've been bad.
*/
strbuf_addstr(&status->error_message,
"(http:429) Too Many Requests [throttled]");
status->retry = GH__RETRY_MODE__HTTP_429;
status->ec = GH__ERROR_CODE__HTTP_429;
trace2_data_string(TR2_CAT, NULL, "error/http",
status->error_message.buf);
return;
case 503:
/*
* We assume that this comes with a "Retry-After" header like 429s.
*/
strbuf_addstr(&status->error_message,
"(http:503) Server Unavailable [throttled]");
status->retry = GH__RETRY_MODE__HTTP_503;
status->ec = GH__ERROR_CODE__HTTP_503;
trace2_data_string(TR2_CAT, NULL, "error/http",
status->error_message.buf);
return;
default:
goto hard_fail;
}
hard_fail:
strbuf_addf(&status->error_message, "(http:%d) Other [hard_fail]",
(int)http_response_code);
status->retry = GH__RETRY_MODE__HARD_FAIL;
status->ec = GH__ERROR_CODE__HTTP_OTHER;
trace2_data_string(TR2_CAT, NULL, "error/http",
status->error_message.buf);
return;
}
/*
* Map CURLE errors code to a retry strategy.
* See <curl/curl.h> and
* https://curl.haxx.se/libcurl/c/libcurl-errors.html
*
* This could be a static table rather than a switch, but
* that is harder to debug and we may want to selectively
* log errors.
*
* I've commented out all of the hard-fail cases for now
* and let the default handle them. This is to indicate
* that I considered them and found them to be not actionable.
* Also, the spelling of some of the CURLE_ symbols seem
* to change between curl releases on different platforms,
* so I'm not going to fight that.
*/
static void compute_retry_mode_from_curl_error(
struct gh__response_status *status,
CURLcode curl_code)
{
switch (curl_code) {
case CURLE_OK:
status->retry = GH__RETRY_MODE__SUCCESS;
status->ec = GH__ERROR_CODE__OK;
return;
//se CURLE_UNSUPPORTED_PROTOCOL: goto hard_fail;
//se CURLE_FAILED_INIT: goto hard_fail;
//se CURLE_URL_MALFORMAT: goto hard_fail;
//se CURLE_NOT_BUILT_IN: goto hard_fail;
//se CURLE_COULDNT_RESOLVE_PROXY: goto hard_fail;
//se CURLE_COULDNT_RESOLVE_HOST: goto hard_fail;
case CURLE_COULDNT_CONNECT: goto transient;
//se CURLE_WEIRD_SERVER_REPLY: goto hard_fail;
//se CURLE_REMOTE_ACCESS_DENIED: goto hard_fail;
//se CURLE_FTP_ACCEPT_FAILED: goto hard_fail;
//se CURLE_FTP_WEIRD_PASS_REPLY: goto hard_fail;
//se CURLE_FTP_ACCEPT_TIMEOUT: goto hard_fail;
//se CURLE_FTP_WEIRD_PASV_REPLY: goto hard_fail;
//se CURLE_FTP_WEIRD_227_FORMAT: goto hard_fail;
//se CURLE_FTP_CANT_GET_HOST: goto hard_fail;
case CURLE_HTTP2: goto transient;
//se CURLE_FTP_COULDNT_SET_TYPE: goto hard_fail;
case CURLE_PARTIAL_FILE: goto transient;
//se CURLE_FTP_COULDNT_RETR_FILE: goto hard_fail;
//se CURLE_OBSOLETE20: goto hard_fail;
//se CURLE_QUOTE_ERROR: goto hard_fail;
//se CURLE_HTTP_RETURNED_ERROR: goto hard_fail;
case CURLE_WRITE_ERROR: goto transient;
//se CURLE_OBSOLETE24: goto hard_fail;
case CURLE_UPLOAD_FAILED: goto transient;
//se CURLE_READ_ERROR: goto hard_fail;
//se CURLE_OUT_OF_MEMORY: goto hard_fail;
case CURLE_OPERATION_TIMEDOUT: goto transient;
//se CURLE_OBSOLETE29: goto hard_fail;
//se CURLE_FTP_PORT_FAILED: goto hard_fail;
//se CURLE_FTP_COULDNT_USE_REST: goto hard_fail;
//se CURLE_OBSOLETE32: goto hard_fail;
//se CURLE_RANGE_ERROR: goto hard_fail;
case CURLE_HTTP_POST_ERROR: goto transient;
//se CURLE_SSL_CONNECT_ERROR: goto hard_fail;
//se CURLE_BAD_DOWNLOAD_RESUME: goto hard_fail;
//se CURLE_FILE_COULDNT_READ_FILE: goto hard_fail;
//se CURLE_LDAP_CANNOT_BIND: goto hard_fail;
//se CURLE_LDAP_SEARCH_FAILED: goto hard_fail;
//se CURLE_OBSOLETE40: goto hard_fail;
//se CURLE_FUNCTION_NOT_FOUND: goto hard_fail;
//se CURLE_ABORTED_BY_CALLBACK: goto hard_fail;
//se CURLE_BAD_FUNCTION_ARGUMENT: goto hard_fail;
//se CURLE_OBSOLETE44: goto hard_fail;
//se CURLE_INTERFACE_FAILED: goto hard_fail;
//se CURLE_OBSOLETE46: goto hard_fail;
//se CURLE_TOO_MANY_REDIRECTS: goto hard_fail;
//se CURLE_UNKNOWN_OPTION: goto hard_fail;
//se CURLE_TELNET_OPTION_SYNTAX: goto hard_fail;
//se CURLE_OBSOLETE50: goto hard_fail;
//se CURLE_PEER_FAILED_VERIFICATION: goto hard_fail;
//se CURLE_GOT_NOTHING: goto hard_fail;
//se CURLE_SSL_ENGINE_NOTFOUND: goto hard_fail;
//se CURLE_SSL_ENGINE_SETFAILED: goto hard_fail;
case CURLE_SEND_ERROR: goto transient;
case CURLE_RECV_ERROR: goto transient;
//se CURLE_OBSOLETE57: goto hard_fail;
//se CURLE_SSL_CERTPROBLEM: goto hard_fail;
//se CURLE_SSL_CIPHER: goto hard_fail;
//se CURLE_SSL_CACERT: goto hard_fail;
//se CURLE_BAD_CONTENT_ENCODING: goto hard_fail;
//se CURLE_LDAP_INVALID_URL: goto hard_fail;
//se CURLE_FILESIZE_EXCEEDED: goto hard_fail;
//se CURLE_USE_SSL_FAILED: goto hard_fail;
//se CURLE_SEND_FAIL_REWIND: goto hard_fail;
//se CURLE_SSL_ENGINE_INITFAILED: goto hard_fail;
//se CURLE_LOGIN_DENIED: goto hard_fail;
//se CURLE_TFTP_NOTFOUND: goto hard_fail;
//se CURLE_TFTP_PERM: goto hard_fail;
//se CURLE_REMOTE_DISK_FULL: goto hard_fail;
//se CURLE_TFTP_ILLEGAL: goto hard_fail;
//se CURLE_TFTP_UNKNOWNID: goto hard_fail;
//se CURLE_REMOTE_FILE_EXISTS: goto hard_fail;
//se CURLE_TFTP_NOSUCHUSER: goto hard_fail;
//se CURLE_CONV_FAILED: goto hard_fail;
//se CURLE_CONV_REQD: goto hard_fail;
//se CURLE_SSL_CACERT_BADFILE: goto hard_fail;
//se CURLE_REMOTE_FILE_NOT_FOUND: goto hard_fail;
//se CURLE_SSH: goto hard_fail;
//se CURLE_SSL_SHUTDOWN_FAILED: goto hard_fail;
case CURLE_AGAIN: goto transient;
//se CURLE_SSL_CRL_BADFILE: goto hard_fail;
//se CURLE_SSL_ISSUER_ERROR: goto hard_fail;
//se CURLE_FTP_PRET_FAILED: goto hard_fail;
//se CURLE_RTSP_CSEQ_ERROR: goto hard_fail;
//se CURLE_RTSP_SESSION_ERROR: goto hard_fail;
//se CURLE_FTP_BAD_FILE_LIST: goto hard_fail;
//se CURLE_CHUNK_FAILED: goto hard_fail;
//se CURLE_NO_CONNECTION_AVAILABLE: goto hard_fail;
//se CURLE_SSL_PINNEDPUBKEYNOTMATCH: goto hard_fail;
//se CURLE_SSL_INVALIDCERTSTATUS: goto hard_fail;
#ifdef CURLE_HTTP2_STREAM
case CURLE_HTTP2_STREAM: goto transient;
#endif
default: goto hard_fail;
}
hard_fail:
strbuf_addf(&status->error_message, "(curl:%d) %s [hard_fail]",
curl_code, curl_easy_strerror(curl_code));
status->retry = GH__RETRY_MODE__HARD_FAIL;
status->ec = GH__ERROR_CODE__CURL_ERROR;
trace2_data_string(TR2_CAT, NULL, "error/curl",
status->error_message.buf);
return;
transient:
strbuf_addf(&status->error_message, "(curl:%d) %s [transient]",
curl_code, curl_easy_strerror(curl_code));
status->retry = GH__RETRY_MODE__TRANSIENT;
status->ec = GH__ERROR_CODE__CURL_ERROR;
trace2_data_string(TR2_CAT, NULL, "error/curl",
status->error_message.buf);
return;
}
/*
* Create a single normalized 'ec' error-code from the status we
* received from the HTTP request. Map a few of the expected HTTP
* status code to 'ec', but don't get too crazy here.
*/
static void gh__response_status__set_from_slot(
struct gh__request_params *params,
struct gh__response_status *status,
const struct active_request_slot *slot)
{
long http_response_code;
CURLcode curl_code;
curl_code = slot->results->curl_result;
gh__curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE,
&status->content_type);
curl_easy_getinfo(slot->curl, CURLINFO_RESPONSE_CODE,
&http_response_code);
strbuf_setlen(&status->error_message, 0);
http_response_code = gh__normalize_odd_codes(params,
http_response_code);
/*
* Use normalized response/status codes form curl/http to decide
* how to set the error-code we propagate *AND* to decide if we
* we should retry because of transient network problems.
*/
if (curl_code == CURLE_OK ||
curl_code == CURLE_HTTP_RETURNED_ERROR)
compute_retry_mode_from_http_response(status,
http_response_code);
else
compute_retry_mode_from_curl_error(status, curl_code);
if (status->ec != GH__ERROR_CODE__OK)
status->bytes_received = 0;
else if (params->b_write_to_file)
status->bytes_received = (intmax_t)ftell(params->tempfile->fp);