Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/http-walker.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / http-walker.c` 629 lines ยท 15603 bytes Raw Blame History 1 #define USE_THE_REPOSITORY_VARIABLE 2 #define DISABLE_SIGN_COMPARE_WARNINGS 3 4 #include "git-compat-util.h" 5 #include "repository.h" 6 #include "hex.h" 7 #include "walker.h" 8 #include "http.h" 9 #include "list.h" 10 #include "transport.h" 11 #include "packfile.h" 12 #include "object-file.h" 13 #include "odb.h" 14 15 struct alt_base { 16 char *base; 17 int got_indices; 18 struct packfile_list packs; 19 struct alt_base *next; 20 }; 21 22 enum object_request_state { 23 WAITING, 24 ABORTED, 25 ACTIVE, 26 COMPLETE 27 }; 28 29 struct object_request { 30 struct walker *walker; 31 struct object_id oid; 32 struct alt_base *repo; 33 enum object_request_state state; 34 struct http_object_request *req; 35 struct list_head node; 36 }; 37 38 struct alternates_request { 39 struct walker *walker; 40 const char *base; 41 struct strbuf *url; 42 struct strbuf *buffer; 43 struct active_request_slot *slot; 44 int http_specific; 45 }; 46 47 struct walker_data { 48 const char *url; 49 int got_alternates; 50 struct alt_base *alt; 51 }; 52 53 static LIST_HEAD(object_queue_head); 54 55 static void fetch_alternates(struct walker *walker, const char *base); 56 57 static void process_object_response(void *callback_data); 58 59 static void start_object_request(struct object_request *obj_req) 60 { 61 struct active_request_slot *slot; 62 struct http_object_request *req; 63 64 req = new_http_object_request(obj_req->repo->base, &obj_req->oid); 65 if (!req) { 66 obj_req->state = ABORTED; 67 return; 68 } 69 obj_req->req = req; 70 71 slot = req->slot; 72 slot->callback_func = process_object_response; 73 slot->callback_data = obj_req; 74 75 /* Try to get the request started, abort the request on error */ 76 obj_req->state = ACTIVE; 77 if (!start_active_slot(slot)) { 78 obj_req->state = ABORTED; 79 release_http_object_request(&req); 80 return; 81 } 82 } 83 84 static void finish_object_request(struct object_request *obj_req) 85 { 86 if (finish_http_object_request(obj_req->req)) 87 return; 88 89 if (obj_req->req->rename == 0) 90 walker_say(obj_req->walker, "got %s\n", oid_to_hex(&obj_req->oid)); 91 } 92 93 static void process_object_response(void *callback_data) 94 { 95 struct object_request *obj_req = 96 (struct object_request *)callback_data; 97 struct walker *walker = obj_req->walker; 98 struct walker_data *data = walker->data; 99 struct alt_base *alt = data->alt; 100 101 process_http_object_request(obj_req->req); 102 obj_req->state = COMPLETE; 103 104 normalize_curl_result(&obj_req->req->curl_result, 105 obj_req->req->http_code, 106 obj_req->req->errorstr, 107 sizeof(obj_req->req->errorstr)); 108 109 /* Use alternates if necessary */ 110 if (missing_target(obj_req->req)) { 111 fetch_alternates(walker, alt->base); 112 if (obj_req->repo->next) { 113 obj_req->repo = 114 obj_req->repo->next; 115 release_http_object_request(&obj_req->req); 116 start_object_request(obj_req); 117 return; 118 } 119 } 120 121 finish_object_request(obj_req); 122 } 123 124 static void release_object_request(struct object_request *obj_req) 125 { 126 if (obj_req->req !=NULL && obj_req->req->localfile != -1) 127 error("fd leakage in release: %d", obj_req->req->localfile); 128 129 list_del(&obj_req->node); 130 free(obj_req); 131 } 132 133 static int fill_active_slot(void *data UNUSED) 134 { 135 struct object_request *obj_req; 136 struct list_head *pos, *tmp, *head = &object_queue_head; 137 138 list_for_each_safe(pos, tmp, head) { 139 obj_req = list_entry(pos, struct object_request, node); 140 if (obj_req->state == WAITING) { 141 if (odb_has_object(the_repository->objects, &obj_req->oid, 142 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR)) 143 obj_req->state = COMPLETE; 144 else { 145 start_object_request(obj_req); 146 return 1; 147 } 148 } 149 } 150 return 0; 151 } 152 153 static void prefetch(struct walker *walker, const struct object_id *oid) 154 { 155 struct object_request *newreq; 156 struct walker_data *data = walker->data; 157 158 newreq = xmalloc(sizeof(*newreq)); 159 newreq->walker = walker; 160 oidcpy(&newreq->oid, oid); 161 newreq->repo = data->alt; 162 newreq->state = WAITING; 163 newreq->req = NULL; 164 165 http_is_verbose = walker->get_verbosely; 166 list_add_tail(&newreq->node, &object_queue_head); 167 168 fill_active_slots(); 169 step_active_slots(); 170 } 171 172 static int is_alternate_allowed(const char *url) 173 { 174 const char *protocols[] = { 175 "http", "https", "ftp", "ftps" 176 }; 177 int i; 178 179 if (http_follow_config != HTTP_FOLLOW_ALWAYS) { 180 warning("alternate disabled by http.followRedirects: %s", url); 181 return 0; 182 } 183 184 for (i = 0; i < ARRAY_SIZE(protocols); i++) { 185 const char *end; 186 if (skip_prefix(url, protocols[i], &end) && 187 starts_with(end, "://")) 188 break; 189 } 190 191 if (i >= ARRAY_SIZE(protocols)) { 192 warning("ignoring alternate with unknown protocol: %s", url); 193 return 0; 194 } 195 if (!is_transport_allowed(protocols[i], 0)) { 196 warning("ignoring alternate with restricted protocol: %s", url); 197 return 0; 198 } 199 200 return 1; 201 } 202 203 static void process_alternates_response(void *callback_data) 204 { 205 struct alternates_request *alt_req = 206 (struct alternates_request *)callback_data; 207 struct walker *walker = alt_req->walker; 208 struct walker_data *cdata = walker->data; 209 struct active_request_slot *slot = alt_req->slot; 210 struct alt_base *tail = cdata->alt; 211 const char *base = alt_req->base; 212 const char null_byte = '\0'; 213 char *data; 214 int i = 0; 215 216 normalize_curl_result(&slot->curl_result, slot->http_code, 217 curl_errorstr, sizeof(curl_errorstr)); 218 219 if (alt_req->http_specific) { 220 if (slot->curl_result != CURLE_OK || 221 !alt_req->buffer->len) { 222 223 /* Try reusing the slot to get non-http alternates */ 224 alt_req->http_specific = 0; 225 strbuf_reset(alt_req->url); 226 strbuf_addf(alt_req->url, "%s/objects/info/alternates", 227 base); 228 curl_easy_setopt(slot->curl, CURLOPT_URL, 229 alt_req->url->buf); 230 active_requests++; 231 slot->in_use = 1; 232 if (slot->finished) 233 (*slot->finished) = 0; 234 if (!start_active_slot(slot)) { 235 cdata->got_alternates = -1; 236 slot->in_use = 0; 237 if (slot->finished) 238 (*slot->finished) = 1; 239 } 240 return; 241 } 242 } else if (slot->curl_result != CURLE_OK) { 243 if (!missing_target(slot)) { 244 cdata->got_alternates = -1; 245 return; 246 } 247 } 248 249 fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer); 250 alt_req->buffer->len--; 251 data = alt_req->buffer->buf; 252 253 while (i < alt_req->buffer->len) { 254 int posn = i; 255 while (posn < alt_req->buffer->len && data[posn] != '\n') 256 posn++; 257 if (data[posn] == '\n') { 258 int okay = 0; 259 int serverlen = 0; 260 struct alt_base *newalt; 261 if (data[i] == '/') { 262 /* 263 * This counts 264 * http://git.host/pub/scm/linux.git/ 265 * -----------here^ 266 * so memcpy(dst, base, serverlen) will 267 * copy up to "...git.host". 268 */ 269 const char *colon_ss = strstr(base,"://"); 270 if (colon_ss) { 271 serverlen = (strchrnul(colon_ss + 3, '/') 272 - base); 273 okay = 1; 274 } 275 } else if (!memcmp(data + i, "../", 3)) { 276 /* 277 * Relative URL; chop the corresponding 278 * number of subpath from base (and ../ 279 * from data), and concatenate the result. 280 * 281 * The code first drops ../ from data, and 282 * then drops one ../ from data and one path 283 * from base. IOW, one extra ../ is dropped 284 * from data than path is dropped from base. 285 * 286 * This is not wrong. The alternate in 287 * http://git.host/pub/scm/linux.git/ 288 * to borrow from 289 * http://git.host/pub/scm/linus.git/ 290 * is ../../linus.git/objects/. You need 291 * two ../../ to borrow from your direct 292 * neighbour. 293 */ 294 i += 3; 295 serverlen = strlen(base); 296 while (i + 2 < posn && 297 !memcmp(data + i, "../", 3)) { 298 do { 299 serverlen--; 300 } while (serverlen && 301 base[serverlen - 1] != '/'); 302 i += 3; 303 } 304 /* If the server got removed, give up. */ 305 okay = strchr(base, ':') - base + 3 < 306 serverlen; 307 } else if (alt_req->http_specific) { 308 char *colon = strchr(data + i, ':'); 309 char *slash = strchr(data + i, '/'); 310 if (colon && slash && colon < data + posn && 311 slash < data + posn && colon < slash) { 312 okay = 1; 313 } 314 } 315 if (okay) { 316 struct strbuf target = STRBUF_INIT; 317 strbuf_add(&target, base, serverlen); 318 strbuf_add(&target, data + i, posn - i); 319 if (!strbuf_strip_suffix(&target, "objects")) { 320 warning("ignoring alternate that does" 321 " not end in 'objects': %s", 322 target.buf); 323 strbuf_release(&target); 324 } else if (is_alternate_allowed(target.buf)) { 325 warning("adding alternate object store: %s", 326 target.buf); 327 CALLOC_ARRAY(newalt, 1); 328 newalt->base = strbuf_detach(&target, NULL); 329 330 while (tail->next != NULL) 331 tail = tail->next; 332 tail->next = newalt; 333 } else { 334 strbuf_release(&target); 335 } 336 } 337 } 338 i = posn + 1; 339 } 340 341 cdata->got_alternates = 1; 342 } 343 344 static void fetch_alternates(struct walker *walker, const char *base) 345 { 346 struct strbuf buffer = STRBUF_INIT; 347 struct strbuf url = STRBUF_INIT; 348 struct active_request_slot *slot; 349 struct alternates_request alt_req; 350 struct walker_data *cdata = walker->data; 351 352 /* 353 * If another request has already started fetching alternates, 354 * wait for them to arrive and return to processing this request's 355 * curl message 356 */ 357 while (cdata->got_alternates == 0) { 358 step_active_slots(); 359 } 360 361 /* Nothing to do if they've already been fetched */ 362 if (cdata->got_alternates == 1) 363 return; 364 365 /* Start the fetch */ 366 cdata->got_alternates = 0; 367 368 if (walker->get_verbosely) 369 fprintf(stderr, "Getting alternates list for %s\n", base); 370 371 strbuf_addf(&url, "%s/objects/info/http-alternates", base); 372 373 /* 374 * Use a callback to process the result, since another request 375 * may fail and need to have alternates loaded before continuing 376 */ 377 slot = get_active_slot(); 378 slot->callback_func = process_alternates_response; 379 alt_req.walker = walker; 380 slot->callback_data = &alt_req; 381 382 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buffer); 383 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); 384 curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf); 385 386 alt_req.base = base; 387 alt_req.url = &url; 388 alt_req.buffer = &buffer; 389 alt_req.http_specific = 1; 390 alt_req.slot = slot; 391 392 if (start_active_slot(slot)) 393 run_active_slot(slot); 394 else 395 cdata->got_alternates = -1; 396 397 strbuf_release(&buffer); 398 strbuf_release(&url); 399 } 400 401 static int fetch_indices(struct walker *walker, struct alt_base *repo) 402 { 403 int ret; 404 405 if (repo->got_indices) 406 return 0; 407 408 if (walker->get_verbosely) 409 fprintf(stderr, "Getting pack list for %s\n", repo->base); 410 411 switch (http_get_info_packs(repo->base, &repo->packs)) { 412 case HTTP_OK: 413 case HTTP_MISSING_TARGET: 414 repo->got_indices = 1; 415 ret = 0; 416 break; 417 default: 418 repo->got_indices = 0; 419 ret = -1; 420 } 421 422 return ret; 423 } 424 425 static int http_fetch_pack(struct walker *walker, struct alt_base *repo, 426 const struct object_id *oid) 427 { 428 struct packed_git *target; 429 int ret; 430 struct slot_results results; 431 struct http_pack_request *preq; 432 433 if (fetch_indices(walker, repo)) 434 return -1; 435 target = packfile_list_find_oid(repo->packs.head, oid); 436 if (!target) 437 return -1; 438 close_pack_index(target); 439 440 if (walker->get_verbosely) { 441 fprintf(stderr, "Getting pack %s\n", 442 hash_to_hex(target->hash)); 443 fprintf(stderr, " which contains %s\n", 444 oid_to_hex(oid)); 445 } 446 447 preq = new_http_pack_request(target->hash, repo->base); 448 if (!preq) 449 goto abort; 450 preq->slot->results = &results; 451 452 if (start_active_slot(preq->slot)) { 453 run_active_slot(preq->slot); 454 if (results.curl_result != CURLE_OK && 455 results.http_code != 416) { 456 error("Unable to get pack file %s\n%s", preq->url, 457 curl_errorstr); 458 goto abort; 459 } 460 } else { 461 error("Unable to start request"); 462 goto abort; 463 } 464 465 ret = finish_http_pack_request(preq); 466 release_http_pack_request(preq); 467 if (ret) 468 return ret; 469 http_install_packfile(target, &repo->packs); 470 471 return 0; 472 473 abort: 474 return -1; 475 } 476 477 static void abort_object_request(struct object_request *obj_req) 478 { 479 release_object_request(obj_req); 480 } 481 482 static int fetch_object(struct walker *walker, const struct object_id *oid) 483 { 484 char *hex = oid_to_hex(oid); 485 int ret = 0; 486 struct object_request *obj_req = NULL; 487 struct http_object_request *req; 488 struct list_head *pos, *head = &object_queue_head; 489 490 list_for_each(pos, head) { 491 obj_req = list_entry(pos, struct object_request, node); 492 if (oideq(&obj_req->oid, oid)) 493 break; 494 } 495 if (!obj_req) 496 return error("Couldn't find request for %s in the queue", hex); 497 498 if (odb_has_object(the_repository->objects, &obj_req->oid, 499 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR)) { 500 if (obj_req->req) 501 abort_http_object_request(&obj_req->req); 502 abort_object_request(obj_req); 503 return 0; 504 } 505 506 while (obj_req->state == WAITING) 507 step_active_slots(); 508 509 /* 510 * obj_req->req might change when fetching alternates in the callback 511 * process_object_response; therefore, the "shortcut" variable, req, 512 * is used only after we're done with slots. 513 */ 514 while (obj_req->state == ACTIVE) 515 run_active_slot(obj_req->req->slot); 516 517 req = obj_req->req; 518 519 if (req->localfile != -1) { 520 close(req->localfile); 521 req->localfile = -1; 522 } 523 524 normalize_curl_result(&req->curl_result, req->http_code, 525 req->errorstr, sizeof(req->errorstr)); 526 527 if (obj_req->state == ABORTED) { 528 ret = error("Request for %s aborted", hex); 529 } else if (req->curl_result != CURLE_OK && 530 req->http_code != 416) { 531 if (missing_target(req)) 532 ret = -1; /* Be silent, it is probably in a pack. */ 533 else 534 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)", 535 req->errorstr, req->curl_result, 536 req->http_code, hex); 537 } else if (req->zret != Z_STREAM_END) { 538 walker->corrupt_object_found++; 539 ret = error("File %s (%s) corrupt", hex, req->url); 540 } else if (!oideq(&obj_req->oid, &req->real_oid)) { 541 ret = error("File %s has bad hash", hex); 542 } else if (req->rename < 0) { 543 struct odb_source_files *files = odb_source_files_downcast(the_repository->objects->source); 544 struct strbuf buf = STRBUF_INIT; 545 odb_loose_path(files->dirs->loose, &buf, &req->oid); 546 ret = error("unable to write sha1 filename %s", buf.buf); 547 strbuf_release(&buf); 548 } 549 550 release_http_object_request(&obj_req->req); 551 release_object_request(obj_req); 552 return ret; 553 } 554 555 static int fetch(struct walker *walker, const struct object_id *oid) 556 { 557 struct walker_data *data = walker->data; 558 struct alt_base *altbase = data->alt; 559 560 if (!fetch_object(walker, oid)) 561 return 0; 562 while (altbase) { 563 if (!http_fetch_pack(walker, altbase, oid)) 564 return 0; 565 fetch_alternates(walker, data->alt->base); 566 altbase = altbase->next; 567 } 568 return error("Unable to find %s under %s", oid_to_hex(oid), 569 data->alt->base); 570 } 571 572 static int fetch_ref(struct walker *walker, struct ref *ref) 573 { 574 struct walker_data *data = walker->data; 575 return http_fetch_ref(data->alt->base, ref); 576 } 577 578 static void cleanup(struct walker *walker) 579 { 580 struct walker_data *data = walker->data; 581 struct alt_base *alt, *alt_next; 582 583 if (data) { 584 alt = data->alt; 585 while (alt) { 586 struct packfile_list_entry *e; 587 588 alt_next = alt->next; 589 590 for (e = alt->packs.head; e; e = e->next) { 591 close_pack(e->pack); 592 free(e->pack); 593 } 594 packfile_list_clear(&alt->packs); 595 596 free(alt->base); 597 free(alt); 598 599 alt = alt_next; 600 } 601 free(data); 602 walker->data = NULL; 603 } 604 } 605 606 struct walker *get_http_walker(const char *url) 607 { 608 char *s; 609 struct walker_data *data = xmalloc(sizeof(struct walker_data)); 610 struct walker *walker = xmalloc(sizeof(struct walker)); 611 612 CALLOC_ARRAY(data->alt, 1); 613 data->alt->base = xstrdup(url); 614 for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s) 615 *s = 0; 616 617 data->got_alternates = -1; 618 619 walker->corrupt_object_found = 0; 620 walker->fetch = fetch; 621 walker->fetch_ref = fetch_ref; 622 walker->prefetch = prefetch; 623 walker->cleanup = cleanup; 624 walker->data = data; 625 626 add_fill_function(NULL, fill_active_slot); 627 628 return walker; 629 }