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/remote-curl.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 / remote-curl.c` 1668 lines ยท 44803 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 "git-curl-compat.h" 6 #include "config.h" 7 #include "environment.h" 8 #include "gettext.h" 9 #include "hex.h" 10 #include "remote.h" 11 #include "connect.h" 12 #include "strbuf.h" 13 #include "walker.h" 14 #include "http.h" 15 #include "run-command.h" 16 #include "pkt-line.h" 17 #include "string-list.h" 18 #include "strvec.h" 19 #include "credential.h" 20 #include "oid-array.h" 21 #include "send-pack.h" 22 #include "setup.h" 23 #include "protocol.h" 24 #include "quote.h" 25 #include "trace2.h" 26 #include "transport.h" 27 #include "url.h" 28 #include "write-or-die.h" 29 30 static struct remote *remote; 31 /* always ends with a trailing slash */ 32 static struct strbuf url = STRBUF_INIT; 33 34 struct options { 35 int verbosity; 36 unsigned long depth; 37 char *deepen_since; 38 struct string_list deepen_not; 39 struct string_list push_options; 40 char *filter; 41 unsigned progress : 1, 42 check_self_contained_and_connected : 1, 43 cloning : 1, 44 update_shallow : 1, 45 followtags : 1, 46 dry_run : 1, 47 thin : 1, 48 /* One of the SEND_PACK_PUSH_CERT_* constants. */ 49 push_cert : 2, 50 deepen_relative : 1, 51 52 /* see documentation of corresponding flag in fetch-pack.h */ 53 from_promisor : 1, 54 55 refetch : 1, 56 atomic : 1, 57 object_format : 1, 58 force_if_includes : 1; 59 const struct git_hash_algo *hash_algo; 60 }; 61 static struct options options; 62 static struct string_list cas_options = STRING_LIST_INIT_DUP; 63 64 static int set_option(const char *name, size_t namelen, const char *value) 65 { 66 if (!strncmp(name, "verbosity", namelen)) { 67 char *end; 68 int v = strtol(value, &end, 10); 69 if (value == end || *end) 70 return -1; 71 options.verbosity = v; 72 return 0; 73 } 74 else if (!strncmp(name, "progress", namelen)) { 75 if (!strcmp(value, "true")) 76 options.progress = 1; 77 else if (!strcmp(value, "false")) 78 options.progress = 0; 79 else 80 return -1; 81 return 0; 82 } 83 else if (!strncmp(name, "depth", namelen)) { 84 char *end; 85 unsigned long v = strtoul(value, &end, 10); 86 if (value == end || *end) 87 return -1; 88 options.depth = v; 89 return 0; 90 } 91 else if (!strncmp(name, "deepen-since", namelen)) { 92 options.deepen_since = xstrdup(value); 93 return 0; 94 } 95 else if (!strncmp(name, "deepen-not", namelen)) { 96 string_list_append(&options.deepen_not, value); 97 return 0; 98 } 99 else if (!strncmp(name, "deepen-relative", namelen)) { 100 if (!strcmp(value, "true")) 101 options.deepen_relative = 1; 102 else if (!strcmp(value, "false")) 103 options.deepen_relative = 0; 104 else 105 return -1; 106 return 0; 107 } 108 else if (!strncmp(name, "followtags", namelen)) { 109 if (!strcmp(value, "true")) 110 options.followtags = 1; 111 else if (!strcmp(value, "false")) 112 options.followtags = 0; 113 else 114 return -1; 115 return 0; 116 } 117 else if (!strncmp(name, "dry-run", namelen)) { 118 if (!strcmp(value, "true")) 119 options.dry_run = 1; 120 else if (!strcmp(value, "false")) 121 options.dry_run = 0; 122 else 123 return -1; 124 return 0; 125 } 126 else if (!strncmp(name, "check-connectivity", namelen)) { 127 if (!strcmp(value, "true")) 128 options.check_self_contained_and_connected = 1; 129 else if (!strcmp(value, "false")) 130 options.check_self_contained_and_connected = 0; 131 else 132 return -1; 133 return 0; 134 } 135 else if (!strncmp(name, "cas", namelen)) { 136 struct strbuf val = STRBUF_INIT; 137 strbuf_addstr(&val, "--force-with-lease="); 138 if (*value != '"') 139 strbuf_addstr(&val, value); 140 else if (unquote_c_style(&val, value, NULL)) 141 return -1; 142 string_list_append(&cas_options, val.buf); 143 strbuf_release(&val); 144 return 0; 145 } else if (!strncmp(name, TRANS_OPT_FORCE_IF_INCLUDES, namelen)) { 146 if (!strcmp(value, "true")) 147 options.force_if_includes = 1; 148 else if (!strcmp(value, "false")) 149 options.force_if_includes = 0; 150 else 151 return -1; 152 return 0; 153 } else if (!strncmp(name, "cloning", namelen)) { 154 if (!strcmp(value, "true")) 155 options.cloning = 1; 156 else if (!strcmp(value, "false")) 157 options.cloning = 0; 158 else 159 return -1; 160 return 0; 161 } else if (!strncmp(name, "update-shallow", namelen)) { 162 if (!strcmp(value, "true")) 163 options.update_shallow = 1; 164 else if (!strcmp(value, "false")) 165 options.update_shallow = 0; 166 else 167 return -1; 168 return 0; 169 } else if (!strncmp(name, "pushcert", namelen)) { 170 if (!strcmp(value, "true")) 171 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS; 172 else if (!strcmp(value, "false")) 173 options.push_cert = SEND_PACK_PUSH_CERT_NEVER; 174 else if (!strcmp(value, "if-asked")) 175 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED; 176 else 177 return -1; 178 return 0; 179 } else if (!strncmp(name, "atomic", namelen)) { 180 if (!strcmp(value, "true")) 181 options.atomic = 1; 182 else if (!strcmp(value, "false")) 183 options.atomic = 0; 184 else 185 return -1; 186 return 0; 187 } else if (!strncmp(name, "push-option", namelen)) { 188 if (*value != '"') 189 string_list_append(&options.push_options, value); 190 else { 191 struct strbuf unquoted = STRBUF_INIT; 192 if (unquote_c_style(&unquoted, value, NULL) < 0) 193 die(_("invalid quoting in push-option value: '%s'"), value); 194 string_list_append_nodup(&options.push_options, 195 strbuf_detach(&unquoted, NULL)); 196 } 197 return 0; 198 } else if (!strncmp(name, "family", namelen)) { 199 if (!strcmp(value, "ipv4")) 200 git_curl_ipresolve = CURL_IPRESOLVE_V4; 201 else if (!strcmp(value, "ipv6")) 202 git_curl_ipresolve = CURL_IPRESOLVE_V6; 203 else if (!strcmp(value, "all")) 204 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER; 205 else 206 return -1; 207 return 0; 208 } else if (!strncmp(name, "from-promisor", namelen)) { 209 options.from_promisor = 1; 210 return 0; 211 } else if (!strncmp(name, "refetch", namelen)) { 212 options.refetch = 1; 213 return 0; 214 } else if (!strncmp(name, "filter", namelen)) { 215 options.filter = xstrdup(value); 216 return 0; 217 } else if (!strncmp(name, "object-format", namelen)) { 218 options.object_format = 1; 219 if (strcmp(value, "true")) 220 die(_("unknown value for object-format: %s"), value); 221 return 0; 222 } else { 223 return 1 /* unsupported */; 224 } 225 } 226 227 struct discovery { 228 char *service; 229 char *buf_alloc; 230 char *buf; 231 size_t len; 232 struct ref *refs; 233 struct oid_array shallow; 234 enum protocol_version version; 235 unsigned proto_git : 1; 236 }; 237 static struct discovery *last_discovery; 238 239 static struct ref *parse_git_refs(struct discovery *heads, int for_push) 240 { 241 struct ref *list = NULL; 242 struct packet_reader reader; 243 244 packet_reader_init(&reader, -1, heads->buf, heads->len, 245 PACKET_READ_CHOMP_NEWLINE | 246 PACKET_READ_GENTLE_ON_EOF | 247 PACKET_READ_DIE_ON_ERR_PACKET); 248 249 heads->version = discover_version(&reader); 250 switch (heads->version) { 251 case protocol_v2: 252 /* 253 * Do nothing. This isn't a list of refs but rather a 254 * capability advertisement. Client would have run 255 * 'stateless-connect' so we'll dump this capability listing 256 * and let them request the refs themselves. 257 */ 258 break; 259 case protocol_v1: 260 case protocol_v0: 261 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0, 262 NULL, &heads->shallow); 263 options.hash_algo = reader.hash_algo; 264 break; 265 case protocol_unknown_version: 266 BUG("unknown protocol version"); 267 } 268 269 return list; 270 } 271 272 /* 273 * Try to detect the hash algorithm used by the remote repository when using 274 * the dumb HTTP transport. As dumb transports cannot tell us the object hash 275 * directly have to derive it from the advertised ref lengths. 276 */ 277 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads) 278 { 279 const char *p = memchr(heads->buf, '\t', heads->len); 280 int algo; 281 282 /* 283 * In case the remote has no refs we have no way to reliably determine 284 * the object hash used by that repository. In that case we simply fall 285 * back to SHA1, which may or may not be correct. 286 */ 287 if (!p) 288 return &hash_algos[GIT_HASH_SHA1_LEGACY]; 289 290 algo = hash_algo_by_length((p - heads->buf) / 2); 291 if (algo == GIT_HASH_UNKNOWN) 292 return NULL; 293 return &hash_algos[algo]; 294 } 295 296 static struct ref *parse_info_refs(struct discovery *heads) 297 { 298 char *data, *start, *mid; 299 char *ref_name; 300 int i = 0; 301 302 struct ref *refs = NULL; 303 struct ref *ref = NULL; 304 struct ref *last_ref = NULL; 305 306 options.hash_algo = detect_hash_algo(heads); 307 if (!options.hash_algo) 308 die("%sinfo/refs not valid: could not determine hash algorithm; " 309 "is this a git repository?", 310 transport_anonymize_url(url.buf)); 311 312 /* 313 * Set the repository's hash algo to whatever we have just detected. 314 * This ensures that we can correctly parse the remote references. 315 */ 316 repo_set_hash_algo(the_repository, hash_algo_by_ptr(options.hash_algo)); 317 318 data = heads->buf; 319 start = NULL; 320 mid = data; 321 while (i < heads->len) { 322 if (!start) { 323 start = &data[i]; 324 } 325 if (data[i] == '\t') 326 mid = &data[i]; 327 if (data[i] == '\n') { 328 if (mid - start != options.hash_algo->hexsz) 329 die(_("%sinfo/refs not valid: is this a git repository?"), 330 transport_anonymize_url(url.buf)); 331 data[i] = 0; 332 ref_name = mid + 1; 333 ref = alloc_ref(ref_name); 334 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo); 335 if (!refs) 336 refs = ref; 337 if (last_ref) 338 last_ref->next = ref; 339 last_ref = ref; 340 start = NULL; 341 } 342 i++; 343 } 344 345 ref = alloc_ref("HEAD"); 346 if (!http_fetch_ref(url.buf, ref) && 347 !resolve_remote_symref(ref, refs)) { 348 ref->next = refs; 349 refs = ref; 350 } else { 351 free_one_ref(ref); 352 } 353 354 return refs; 355 } 356 357 static void free_discovery(struct discovery *d) 358 { 359 if (d) { 360 if (d == last_discovery) 361 last_discovery = NULL; 362 free(d->shallow.oid); 363 free(d->buf_alloc); 364 free_refs(d->refs); 365 free(d->service); 366 free(d); 367 } 368 } 369 370 static int show_http_message(struct strbuf *type, struct strbuf *charset, 371 struct strbuf *msg) 372 { 373 const char *p, *eol; 374 375 /* 376 * We only show text/plain parts, as other types are likely 377 * to be ugly to look at on the user's terminal. 378 */ 379 if (strcmp(type->buf, "text/plain")) 380 return -1; 381 if (charset->len) 382 strbuf_reencode(msg, charset->buf, get_log_output_encoding()); 383 384 strbuf_trim(msg); 385 if (!msg->len) 386 return -1; 387 388 p = msg->buf; 389 do { 390 eol = strchrnul(p, '\n'); 391 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p); 392 p = eol + 1; 393 } while(*eol); 394 return 0; 395 } 396 397 static int get_protocol_http_header(enum protocol_version version, 398 struct strbuf *header) 399 { 400 if (version > 0) { 401 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d", 402 version); 403 404 return 1; 405 } 406 407 return 0; 408 } 409 410 static void check_smart_http(struct discovery *d, const char *service, 411 struct strbuf *type) 412 { 413 const char *p; 414 struct packet_reader reader; 415 416 /* 417 * If we don't see x-$service-advertisement, then it's not smart-http. 418 * But once we do, we commit to it and assume any other protocol 419 * violations are hard errors. 420 */ 421 if (!skip_prefix(type->buf, "application/x-", &p) || 422 !skip_prefix(p, service, &p) || 423 strcmp(p, "-advertisement")) 424 return; 425 426 packet_reader_init(&reader, -1, d->buf, d->len, 427 PACKET_READ_CHOMP_NEWLINE | 428 PACKET_READ_DIE_ON_ERR_PACKET); 429 if (packet_reader_read(&reader) != PACKET_READ_NORMAL) 430 die(_("invalid server response; expected service, got flush packet")); 431 432 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) { 433 /* 434 * The header can include additional metadata lines, up 435 * until a packet flush marker. Ignore these now, but 436 * in the future we might start to scan them. 437 */ 438 for (;;) { 439 packet_reader_read(&reader); 440 if (reader.pktlen <= 0) { 441 break; 442 } 443 } 444 445 /* 446 * v0 smart http; callers expect us to soak up the 447 * service and header packets 448 */ 449 d->buf = reader.src_buffer; 450 d->len = reader.src_len; 451 d->proto_git = 1; 452 453 } else if (!strcmp(reader.line, "version 2")) { 454 /* 455 * v2 smart http; do not consume version packet, which will 456 * be handled elsewhere. 457 */ 458 d->proto_git = 1; 459 460 } else { 461 die(_("invalid server response; got '%s'"), reader.line); 462 } 463 } 464 465 static struct discovery *discover_refs(const char *service, int for_push) 466 { 467 struct strbuf type = STRBUF_INIT; 468 struct strbuf charset = STRBUF_INIT; 469 struct strbuf buffer = STRBUF_INIT; 470 struct strbuf refs_url = STRBUF_INIT; 471 struct strbuf effective_url = STRBUF_INIT; 472 struct strbuf protocol_header = STRBUF_INIT; 473 struct string_list extra_headers = STRING_LIST_INIT_DUP; 474 struct discovery *last = last_discovery; 475 int http_ret, maybe_smart = 0; 476 struct http_get_options http_options; 477 enum protocol_version version = get_protocol_version_config(); 478 479 if (last && !strcmp(service, last->service)) 480 return last; 481 free_discovery(last); 482 483 strbuf_addf(&refs_url, "%sinfo/refs", url.buf); 484 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) && 485 git_env_bool("GIT_SMART_HTTP", 1)) { 486 maybe_smart = 1; 487 if (!strchr(url.buf, '?')) 488 strbuf_addch(&refs_url, '?'); 489 else 490 strbuf_addch(&refs_url, '&'); 491 strbuf_addf(&refs_url, "service=%s", service); 492 } 493 494 /* 495 * NEEDSWORK: If we are trying to use protocol v2 and we are planning 496 * to perform any operation that doesn't involve upload-pack (i.e., a 497 * fetch, ls-remote, etc), then fallback to v0 since we don't know how 498 * to do anything else (like push or remote archive) via v2. 499 */ 500 if (version == protocol_v2 && strcmp("git-upload-pack", service)) 501 version = protocol_v0; 502 503 /* Add the extra Git-Protocol header */ 504 if (get_protocol_http_header(version, &protocol_header)) 505 string_list_append(&extra_headers, protocol_header.buf); 506 507 memset(&http_options, 0, sizeof(http_options)); 508 http_options.content_type = &type; 509 http_options.charset = &charset; 510 http_options.effective_url = &effective_url; 511 http_options.base_url = &url; 512 http_options.extra_headers = &extra_headers; 513 http_options.initial_request = 1; 514 http_options.no_cache = 1; 515 516 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options); 517 switch (http_ret) { 518 case HTTP_OK: 519 break; 520 case HTTP_MISSING_TARGET: 521 show_http_message(&type, &charset, &buffer); 522 die(_("repository '%s' not found"), 523 transport_anonymize_url(url.buf)); 524 case HTTP_NOAUTH: 525 show_http_message(&type, &charset, &buffer); 526 die(_("Authentication failed for '%s'"), 527 transport_anonymize_url(url.buf)); 528 case HTTP_NOMATCHPUBLICKEY: 529 show_http_message(&type, &charset, &buffer); 530 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"), 531 transport_anonymize_url(url.buf), curl_errorstr); 532 case HTTP_RATE_LIMITED: 533 if (http_options.retry_after > 0) { 534 show_http_message(&type, &charset, &buffer); 535 die(_("rate limited by '%s', please try again in %ld seconds"), 536 transport_anonymize_url(url.buf), 537 http_options.retry_after); 538 } else { 539 show_http_message(&type, &charset, &buffer); 540 die(_("rate limited by '%s', please try again later"), 541 transport_anonymize_url(url.buf)); 542 } 543 default: 544 show_http_message(&type, &charset, &buffer); 545 die(_("unable to access '%s': %s"), 546 transport_anonymize_url(url.buf), curl_errorstr); 547 } 548 549 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) { 550 char *u = transport_anonymize_url(url.buf); 551 warning(_("redirecting to %s"), u); 552 free(u); 553 } 554 555 last= xcalloc(1, sizeof(*last_discovery)); 556 last->service = xstrdup(service); 557 last->buf_alloc = strbuf_detach(&buffer, &last->len); 558 last->buf = last->buf_alloc; 559 560 if (maybe_smart) 561 check_smart_http(last, service, &type); 562 563 if (last->proto_git) 564 last->refs = parse_git_refs(last, for_push); 565 else 566 last->refs = parse_info_refs(last); 567 568 strbuf_release(&refs_url); 569 strbuf_release(&type); 570 strbuf_release(&charset); 571 strbuf_release(&effective_url); 572 strbuf_release(&buffer); 573 strbuf_release(&protocol_header); 574 string_list_clear(&extra_headers, 0); 575 last_discovery = last; 576 return last; 577 } 578 579 static struct ref *get_refs(int for_push) 580 { 581 struct discovery *heads; 582 583 if (for_push) 584 heads = discover_refs("git-receive-pack", for_push); 585 else 586 heads = discover_refs("git-upload-pack", for_push); 587 588 return heads->refs; 589 } 590 591 static void output_refs(struct ref *refs) 592 { 593 struct ref *posn; 594 if (options.object_format && options.hash_algo) { 595 printf(":object-format %s\n", options.hash_algo->name); 596 repo_set_hash_algo(the_repository, 597 hash_algo_by_ptr(options.hash_algo)); 598 } 599 for (posn = refs; posn; posn = posn->next) { 600 if (posn->symref) 601 printf("@%s %s\n", posn->symref, posn->name); 602 else 603 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash, 604 options.hash_algo), 605 posn->name); 606 } 607 printf("\n"); 608 fflush(stdout); 609 } 610 611 struct rpc_state { 612 const char *service_name; 613 char *service_url; 614 char *hdr_content_type; 615 char *hdr_accept; 616 char *hdr_accept_language; 617 char *protocol_header; 618 char *buf; 619 size_t alloc; 620 size_t len; 621 size_t pos; 622 int in; 623 int out; 624 int any_written; 625 unsigned gzip_request : 1; 626 unsigned initial_buffer : 1; 627 628 /* 629 * Whenever a pkt-line is read into buf, append the 4 characters 630 * denoting its length before appending the payload. 631 */ 632 unsigned write_line_lengths : 1; 633 634 /* 635 * Used by rpc_out; initialize to 0. This is true if a flush has been 636 * read, but the corresponding line length (if write_line_lengths is 637 * true) and EOF have not been sent to libcurl. Since each flush marks 638 * the end of a request, each flush must be completely sent before any 639 * further reading occurs. 640 */ 641 unsigned flush_read_but_not_sent : 1; 642 }; 643 644 #define RPC_STATE_INIT { 0 } 645 646 /* 647 * Appends the result of reading from rpc->out to the string represented by 648 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was 649 * enough space, 0 otherwise. 650 * 651 * If rpc->write_line_lengths is true, appends the line length as a 4-byte 652 * hexadecimal string before appending the result described above. 653 * 654 * Writes the total number of bytes appended into appended. 655 */ 656 static int rpc_read_from_out(struct rpc_state *rpc, int options, 657 size_t *appended, 658 enum packet_read_status *status) { 659 size_t left; 660 char *buf; 661 int pktlen_raw; 662 663 if (rpc->write_line_lengths) { 664 left = rpc->alloc - rpc->len - 4; 665 buf = rpc->buf + rpc->len + 4; 666 } else { 667 left = rpc->alloc - rpc->len; 668 buf = rpc->buf + rpc->len; 669 } 670 671 if (left < LARGE_PACKET_MAX) 672 return 0; 673 674 *status = packet_read_with_status(rpc->out, NULL, NULL, buf, 675 left, &pktlen_raw, options); 676 if (*status != PACKET_READ_EOF) { 677 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0); 678 rpc->len += *appended; 679 } 680 681 if (rpc->write_line_lengths) { 682 switch (*status) { 683 case PACKET_READ_EOF: 684 if (!(options & PACKET_READ_GENTLE_ON_EOF)) 685 die(_("shouldn't have EOF when not gentle on EOF")); 686 break; 687 case PACKET_READ_NORMAL: 688 set_packet_header(buf - 4, *appended); 689 break; 690 case PACKET_READ_DELIM: 691 memcpy(buf - 4, "0001", 4); 692 break; 693 case PACKET_READ_FLUSH: 694 memcpy(buf - 4, "0000", 4); 695 break; 696 case PACKET_READ_RESPONSE_END: 697 die(_("remote server sent unexpected response end packet")); 698 } 699 } 700 701 return 1; 702 } 703 704 static size_t rpc_out(void *ptr, size_t eltsize, 705 size_t nmemb, void *buffer_) 706 { 707 size_t max = eltsize * nmemb; 708 struct rpc_state *rpc = buffer_; 709 size_t avail = rpc->len - rpc->pos; 710 enum packet_read_status status; 711 712 if (!avail) { 713 rpc->initial_buffer = 0; 714 rpc->len = 0; 715 rpc->pos = 0; 716 if (!rpc->flush_read_but_not_sent) { 717 if (!rpc_read_from_out(rpc, 0, &avail, &status)) 718 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX"); 719 if (status == PACKET_READ_FLUSH) 720 rpc->flush_read_but_not_sent = 1; 721 } 722 /* 723 * If flush_read_but_not_sent is true, we have already read one 724 * full request but have not fully sent it + EOF, which is why 725 * we need to refrain from reading. 726 */ 727 } 728 if (rpc->flush_read_but_not_sent) { 729 if (!avail) { 730 /* 731 * The line length either does not need to be sent at 732 * all or has already been completely sent. Now we can 733 * return 0, indicating EOF, meaning that the flush has 734 * been fully sent. 735 */ 736 rpc->flush_read_but_not_sent = 0; 737 return 0; 738 } 739 /* 740 * If avail is non-zero, the line length for the flush still 741 * hasn't been fully sent. Proceed with sending the line 742 * length. 743 */ 744 } 745 746 if (max < avail) 747 avail = max; 748 memcpy(ptr, rpc->buf + rpc->pos, avail); 749 rpc->pos += avail; 750 return avail; 751 } 752 753 static int rpc_seek(void *clientp, curl_off_t offset, int origin) 754 { 755 struct rpc_state *rpc = clientp; 756 757 if (origin != SEEK_SET) 758 BUG("rpc_seek only handles SEEK_SET, not %d", origin); 759 760 if (rpc->initial_buffer) { 761 if (offset < 0 || offset > rpc->len) { 762 error("curl seek would be outside of rpc buffer"); 763 return CURL_SEEKFUNC_FAIL; 764 } 765 rpc->pos = offset; 766 return CURL_SEEKFUNC_OK; 767 } 768 error(_("unable to rewind rpc post data - try increasing http.postBuffer")); 769 return CURL_SEEKFUNC_FAIL; 770 } 771 772 struct check_pktline_state { 773 char len_buf[4]; 774 int len_filled; 775 int remaining; 776 }; 777 778 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size) 779 { 780 while (size) { 781 if (!state->remaining) { 782 int digits_remaining = 4 - state->len_filled; 783 if (digits_remaining > size) 784 digits_remaining = size; 785 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining); 786 state->len_filled += digits_remaining; 787 ptr += digits_remaining; 788 size -= digits_remaining; 789 790 if (state->len_filled == 4) { 791 state->remaining = packet_length(state->len_buf, 792 sizeof(state->len_buf)); 793 if (state->remaining < 0) { 794 die(_("remote-curl: bad line length character: %.4s"), state->len_buf); 795 } else if (state->remaining == 2) { 796 die(_("remote-curl: unexpected response end packet")); 797 } else if (state->remaining < 4) { 798 state->remaining = 0; 799 } else { 800 state->remaining -= 4; 801 } 802 state->len_filled = 0; 803 } 804 } 805 806 if (state->remaining) { 807 int remaining = state->remaining; 808 if (remaining > size) 809 remaining = size; 810 ptr += remaining; 811 size -= remaining; 812 state->remaining -= remaining; 813 } 814 } 815 } 816 817 struct rpc_in_data { 818 struct rpc_state *rpc; 819 struct active_request_slot *slot; 820 int check_pktline; 821 struct check_pktline_state pktline_state; 822 }; 823 824 /* 825 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed 826 * from ptr. 827 */ 828 static size_t rpc_in(char *ptr, size_t eltsize, 829 size_t nmemb, void *buffer_) 830 { 831 size_t size = eltsize * nmemb; 832 struct rpc_in_data *data = buffer_; 833 long response_code; 834 835 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE, 836 &response_code) != CURLE_OK) 837 return size; 838 if (response_code >= 300) 839 return size; 840 if (size) 841 data->rpc->any_written = 1; 842 if (data->check_pktline) 843 check_pktline(&data->pktline_state, ptr, size); 844 write_or_die(data->rpc->in, ptr, size); 845 return size; 846 } 847 848 static int run_slot(struct active_request_slot *slot, 849 struct slot_results *results) 850 { 851 int err; 852 struct slot_results results_buf; 853 854 if (!results) 855 results = &results_buf; 856 857 err = run_one_slot(slot, results); 858 859 if (err != HTTP_OK && err != HTTP_REAUTH) { 860 struct strbuf msg = STRBUF_INIT; 861 if (results->http_code && results->http_code != 200) 862 strbuf_addf(&msg, "HTTP %ld", results->http_code); 863 if (results->curl_result != CURLE_OK) { 864 if (msg.len) 865 strbuf_addch(&msg, ' '); 866 strbuf_addf(&msg, "curl %d", results->curl_result); 867 if (curl_errorstr[0]) { 868 strbuf_addch(&msg, ' '); 869 strbuf_addstr(&msg, curl_errorstr); 870 } 871 } 872 error(_("RPC failed; %s"), msg.buf); 873 strbuf_release(&msg); 874 } 875 876 return err; 877 } 878 879 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results) 880 { 881 struct active_request_slot *slot; 882 struct curl_slist *headers = http_copy_default_headers(); 883 struct strbuf buf = STRBUF_INIT; 884 int err; 885 886 slot = get_active_slot(); 887 888 headers = curl_slist_append(headers, rpc->hdr_content_type); 889 headers = curl_slist_append(headers, rpc->hdr_accept); 890 headers = http_append_auth_header(&http_auth, headers); 891 892 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0L); 893 curl_easy_setopt(slot->curl, CURLOPT_POST, 1L); 894 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); 895 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL); 896 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000"); 897 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4L); 898 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); 899 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer); 900 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf); 901 902 err = run_slot(slot, results); 903 904 curl_slist_free_all(headers); 905 strbuf_release(&buf); 906 return err; 907 } 908 909 /* 910 * If flush_received is true, do not attempt to read any more; just use what's 911 * in rpc->buf. 912 */ 913 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received) 914 { 915 struct active_request_slot *slot; 916 struct curl_slist *headers = NULL; 917 int use_gzip = rpc->gzip_request; 918 char *gzip_body = NULL; 919 size_t gzip_size = 0; 920 int err, large_request = 0; 921 int needs_100_continue = 0; 922 struct rpc_in_data rpc_in_data; 923 924 /* Try to load the entire request, if we can fit it into the 925 * allocated buffer space we can use HTTP/1.0 and avoid the 926 * chunked encoding mess. 927 */ 928 if (!flush_received) { 929 while (1) { 930 size_t n; 931 enum packet_read_status status; 932 933 if (!rpc_read_from_out(rpc, 0, &n, &status)) { 934 large_request = 1; 935 use_gzip = 0; 936 break; 937 } 938 if (status == PACKET_READ_FLUSH) 939 break; 940 } 941 } 942 943 if (large_request) { 944 struct slot_results results; 945 946 do { 947 err = probe_rpc(rpc, &results); 948 if (err == HTTP_REAUTH) 949 http_reauth_prepare(0); 950 } while (err == HTTP_REAUTH); 951 if (err != HTTP_OK) 952 return -1; 953 954 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE || http_auth.authtype) 955 needs_100_continue = 1; 956 } 957 958 retry: 959 headers = http_copy_default_headers(); 960 headers = curl_slist_append(headers, rpc->hdr_content_type); 961 headers = curl_slist_append(headers, rpc->hdr_accept); 962 headers = curl_slist_append(headers, needs_100_continue ? 963 "Expect: 100-continue" : "Expect:"); 964 965 headers = http_append_auth_header(&http_auth, headers); 966 967 /* Add Accept-Language header */ 968 if (rpc->hdr_accept_language) 969 headers = curl_slist_append(headers, rpc->hdr_accept_language); 970 971 /* Add the extra Git-Protocol header */ 972 if (rpc->protocol_header) 973 headers = curl_slist_append(headers, rpc->protocol_header); 974 975 slot = get_active_slot(); 976 977 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0L); 978 curl_easy_setopt(slot->curl, CURLOPT_POST, 1L); 979 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url); 980 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, ""); 981 982 if (large_request) { 983 /* The request body is large and the size cannot be predicted. 984 * We must use chunked encoding to send it. 985 */ 986 #ifdef GIT_CURL_NEED_TRANSFER_ENCODING_HEADER 987 headers = curl_slist_append(headers, "Transfer-Encoding: chunked"); 988 #endif 989 rpc->initial_buffer = 1; 990 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out); 991 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc); 992 curl_easy_setopt(slot->curl, CURLOPT_SEEKFUNCTION, rpc_seek); 993 curl_easy_setopt(slot->curl, CURLOPT_SEEKDATA, rpc); 994 if (options.verbosity > 1) { 995 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name); 996 fflush(stderr); 997 } 998 999 } else if (gzip_body) { 1000 /* 1001 * If we are looping to retry authentication, then the previous 1002 * run will have set up the headers and gzip buffer already, 1003 * and we just need to send it. 1004 */ 1005 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); 1006 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, cast_size_t_to_curl_off_t(gzip_size)); 1007 1008 } else if (use_gzip && 1024 < rpc->len) { 1009 /* The client backend isn't giving us compressed data so 1010 * we can try to deflate it ourselves, this may save on 1011 * the transfer time. 1012 */ 1013 git_zstream stream; 1014 int ret; 1015 1016 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION); 1017 gzip_size = git_deflate_bound(&stream, rpc->len); 1018 gzip_body = xmalloc(gzip_size); 1019 1020 stream.next_in = (unsigned char *)rpc->buf; 1021 stream.avail_in = rpc->len; 1022 stream.next_out = (unsigned char *)gzip_body; 1023 stream.avail_out = gzip_size; 1024 1025 ret = git_deflate(&stream, Z_FINISH); 1026 if (ret != Z_STREAM_END) 1027 die(_("cannot deflate request; zlib deflate error %d"), ret); 1028 1029 ret = git_deflate_end_gently(&stream); 1030 if (ret != Z_OK) 1031 die(_("cannot deflate request; zlib end error %d"), ret); 1032 1033 gzip_size = stream.total_out; 1034 1035 headers = curl_slist_append(headers, "Content-Encoding: gzip"); 1036 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body); 1037 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, cast_size_t_to_curl_off_t(gzip_size)); 1038 1039 if (options.verbosity > 1) { 1040 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n", 1041 rpc->service_name, 1042 (unsigned long)rpc->len, (unsigned long)gzip_size); 1043 fflush(stderr); 1044 } 1045 } else { 1046 /* We know the complete request size in advance, use the 1047 * more normal Content-Length approach. 1048 */ 1049 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf); 1050 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, cast_size_t_to_curl_off_t(rpc->len)); 1051 if (options.verbosity > 1) { 1052 fprintf(stderr, "POST %s (%lu bytes)\n", 1053 rpc->service_name, (unsigned long)rpc->len); 1054 fflush(stderr); 1055 } 1056 } 1057 1058 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers); 1059 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in); 1060 rpc_in_data.rpc = rpc; 1061 rpc_in_data.slot = slot; 1062 rpc_in_data.check_pktline = stateless_connect; 1063 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state)); 1064 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &rpc_in_data); 1065 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0L); 1066 1067 1068 rpc->any_written = 0; 1069 err = run_slot(slot, NULL); 1070 if (err == HTTP_REAUTH && !large_request) { 1071 http_reauth_prepare(0); 1072 curl_slist_free_all(headers); 1073 goto retry; 1074 } 1075 if (err != HTTP_OK) 1076 err = -1; 1077 1078 if (!rpc->any_written) 1079 err = -1; 1080 1081 if (rpc_in_data.pktline_state.len_filled) 1082 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled); 1083 if (rpc_in_data.pktline_state.remaining) 1084 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining); 1085 1086 if (stateless_connect) 1087 packet_response_end(rpc->in); 1088 1089 curl_slist_free_all(headers); 1090 free(gzip_body); 1091 return err; 1092 } 1093 1094 static int rpc_service(struct rpc_state *rpc, struct discovery *heads, 1095 const char **client_argv, const struct strbuf *preamble, 1096 struct strbuf *rpc_result) 1097 { 1098 const char *svc = rpc->service_name; 1099 struct strbuf buf = STRBUF_INIT; 1100 struct child_process client = CHILD_PROCESS_INIT; 1101 int err = 0; 1102 1103 client.in = -1; 1104 client.out = -1; 1105 client.git_cmd = 1; 1106 strvec_pushv(&client.args, client_argv); 1107 if (start_command(&client)) 1108 exit(1); 1109 write_or_die(client.in, preamble->buf, preamble->len); 1110 if (heads) 1111 write_or_die(client.in, heads->buf, heads->len); 1112 1113 rpc->alloc = http_post_buffer; 1114 rpc->buf = xmalloc(rpc->alloc); 1115 rpc->in = client.in; 1116 rpc->out = client.out; 1117 1118 strbuf_addf(&buf, "%s%s", url.buf, svc); 1119 rpc->service_url = strbuf_detach(&buf, NULL); 1120 1121 rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header()); 1122 1123 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc); 1124 rpc->hdr_content_type = strbuf_detach(&buf, NULL); 1125 1126 strbuf_addf(&buf, "Accept: application/x-%s-result", svc); 1127 rpc->hdr_accept = strbuf_detach(&buf, NULL); 1128 1129 if (get_protocol_http_header(heads->version, &buf)) 1130 rpc->protocol_header = strbuf_detach(&buf, NULL); 1131 else 1132 rpc->protocol_header = NULL; 1133 1134 while (!err) { 1135 int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0); 1136 if (!n) 1137 break; 1138 rpc->pos = 0; 1139 rpc->len = n; 1140 err |= post_rpc(rpc, 0, 0); 1141 } 1142 1143 close(client.in); 1144 client.in = -1; 1145 if (!err) { 1146 strbuf_read(rpc_result, client.out, 0); 1147 } else { 1148 char buf[4096]; 1149 for (;;) 1150 if (xread(client.out, buf, sizeof(buf)) <= 0) 1151 break; 1152 } 1153 1154 close(client.out); 1155 client.out = -1; 1156 1157 err |= finish_command(&client); 1158 free(rpc->service_url); 1159 free(rpc->hdr_content_type); 1160 free(rpc->hdr_accept); 1161 free(rpc->hdr_accept_language); 1162 free(rpc->protocol_header); 1163 free(rpc->buf); 1164 strbuf_release(&buf); 1165 return err; 1166 } 1167 1168 static int fetch_dumb(int nr_heads, struct ref **to_fetch) 1169 { 1170 struct walker *walker; 1171 char **targets; 1172 int ret, i; 1173 1174 ALLOC_ARRAY(targets, nr_heads); 1175 if (options.depth || options.deepen_since) 1176 die(_("dumb http transport does not support shallow capabilities")); 1177 for (i = 0; i < nr_heads; i++) 1178 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid)); 1179 1180 walker = get_http_walker(url.buf); 1181 walker->get_verbosely = options.verbosity >= 3; 1182 walker->get_progress = options.progress; 1183 walker->get_recover = 0; 1184 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL); 1185 walker_free(walker); 1186 1187 for (i = 0; i < nr_heads; i++) 1188 free(targets[i]); 1189 free(targets); 1190 1191 return ret ? error(_("fetch failed.")) : 0; 1192 } 1193 1194 static int fetch_git(struct discovery *heads, 1195 int nr_heads, struct ref **to_fetch) 1196 { 1197 struct rpc_state rpc = RPC_STATE_INIT; 1198 struct strbuf preamble = STRBUF_INIT; 1199 int i, err; 1200 struct strvec args = STRVEC_INIT; 1201 struct strbuf rpc_result = STRBUF_INIT; 1202 1203 strvec_pushl(&args, "fetch-pack", "--stateless-rpc", 1204 "--stdin", "--lock-pack", NULL); 1205 if (options.followtags) 1206 strvec_push(&args, "--include-tag"); 1207 if (options.thin) 1208 strvec_push(&args, "--thin"); 1209 if (options.verbosity >= 3) 1210 strvec_pushl(&args, "-v", "-v", NULL); 1211 if (options.check_self_contained_and_connected) 1212 strvec_push(&args, "--check-self-contained-and-connected"); 1213 if (options.cloning) 1214 strvec_push(&args, "--cloning"); 1215 if (options.update_shallow) 1216 strvec_push(&args, "--update-shallow"); 1217 if (!options.progress) 1218 strvec_push(&args, "--no-progress"); 1219 if (options.depth) 1220 strvec_pushf(&args, "--depth=%lu", options.depth); 1221 if (options.deepen_since) 1222 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since); 1223 for (i = 0; i < options.deepen_not.nr; i++) 1224 strvec_pushf(&args, "--shallow-exclude=%s", 1225 options.deepen_not.items[i].string); 1226 if (options.deepen_relative && options.depth) 1227 strvec_push(&args, "--deepen-relative"); 1228 if (options.from_promisor) 1229 strvec_push(&args, "--from-promisor"); 1230 if (options.refetch) 1231 strvec_push(&args, "--refetch"); 1232 if (options.filter) 1233 strvec_pushf(&args, "--filter=%s", options.filter); 1234 strvec_push(&args, url.buf); 1235 1236 for (i = 0; i < nr_heads; i++) { 1237 struct ref *ref = to_fetch[i]; 1238 if (!*ref->name) 1239 die(_("cannot fetch by sha1 over smart http")); 1240 packet_buf_write(&preamble, "%s %s\n", 1241 oid_to_hex(&ref->old_oid), ref->name); 1242 } 1243 packet_buf_flush(&preamble); 1244 1245 memset(&rpc, 0, sizeof(rpc)); 1246 rpc.service_name = "git-upload-pack"; 1247 rpc.gzip_request = 1; 1248 1249 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result); 1250 if (rpc_result.len) 1251 write_or_die(1, rpc_result.buf, rpc_result.len); 1252 strbuf_release(&rpc_result); 1253 strbuf_release(&preamble); 1254 strvec_clear(&args); 1255 return err; 1256 } 1257 1258 static int fetch(int nr_heads, struct ref **to_fetch) 1259 { 1260 struct discovery *d = discover_refs("git-upload-pack", 0); 1261 if (d->proto_git) 1262 return fetch_git(d, nr_heads, to_fetch); 1263 else 1264 return fetch_dumb(nr_heads, to_fetch); 1265 } 1266 1267 static void parse_fetch(struct strbuf *buf) 1268 { 1269 struct ref **to_fetch = NULL; 1270 struct ref *list_head = NULL; 1271 struct ref **list = &list_head; 1272 int alloc_heads = 0, nr_heads = 0; 1273 1274 do { 1275 const char *p; 1276 if (skip_prefix(buf->buf, "fetch ", &p)) { 1277 const char *name; 1278 struct ref *ref; 1279 struct object_id old_oid; 1280 const char *q; 1281 1282 if (parse_oid_hex(p, &old_oid, &q)) 1283 die(_("protocol error: expected sha/ref, got '%s'"), p); 1284 if (*q == ' ') 1285 name = q + 1; 1286 else if (!*q) 1287 name = ""; 1288 else 1289 die(_("protocol error: expected sha/ref, got '%s'"), p); 1290 1291 ref = alloc_ref(name); 1292 oidcpy(&ref->old_oid, &old_oid); 1293 1294 *list = ref; 1295 list = &ref->next; 1296 1297 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads); 1298 to_fetch[nr_heads++] = ref; 1299 } 1300 else 1301 die(_("http transport does not support %s"), buf->buf); 1302 1303 strbuf_reset(buf); 1304 if (strbuf_getline_lf(buf, stdin) == EOF) 1305 return; 1306 if (!*buf->buf) 1307 break; 1308 } while (1); 1309 1310 if (fetch(nr_heads, to_fetch)) 1311 exit(128); /* error already reported */ 1312 free_refs(list_head); 1313 free(to_fetch); 1314 1315 printf("\n"); 1316 fflush(stdout); 1317 strbuf_reset(buf); 1318 } 1319 1320 static void parse_get(const char *arg) 1321 { 1322 struct strbuf url = STRBUF_INIT; 1323 struct strbuf path = STRBUF_INIT; 1324 const char *space; 1325 1326 space = strchr(arg, ' '); 1327 1328 if (!space) 1329 die(_("protocol error: expected ' ', missing space")); 1330 1331 strbuf_add(&url, arg, space - arg); 1332 strbuf_addstr(&path, space + 1); 1333 1334 if (http_get_file(url.buf, path.buf, NULL)) 1335 die(_("failed to download file at URL '%s'"), url.buf); 1336 1337 strbuf_release(&url); 1338 strbuf_release(&path); 1339 printf("\n"); 1340 fflush(stdout); 1341 } 1342 1343 static int push_dav(const char **specs) 1344 { 1345 struct child_process child = CHILD_PROCESS_INIT; 1346 1347 child.git_cmd = 1; 1348 strvec_push(&child.args, "http-push"); 1349 strvec_push(&child.args, "--helper-status"); 1350 if (options.dry_run) 1351 strvec_push(&child.args, "--dry-run"); 1352 if (options.verbosity > 1) 1353 strvec_push(&child.args, "--verbose"); 1354 strvec_push(&child.args, url.buf); 1355 strvec_pushv(&child.args, specs); 1356 1357 if (run_command(&child)) 1358 die(_("git-http-push failed")); 1359 return 0; 1360 } 1361 1362 static int push_git(struct discovery *heads, const char **specs) 1363 { 1364 struct rpc_state rpc = RPC_STATE_INIT; 1365 int i, err; 1366 struct strvec args; 1367 struct string_list_item *cas_option; 1368 struct strbuf preamble = STRBUF_INIT; 1369 struct strbuf rpc_result = STRBUF_INIT; 1370 1371 strvec_init(&args); 1372 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status", 1373 NULL); 1374 1375 if (options.thin) 1376 strvec_push(&args, "--thin"); 1377 if (options.dry_run) 1378 strvec_push(&args, "--dry-run"); 1379 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS) 1380 strvec_push(&args, "--signed=yes"); 1381 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED) 1382 strvec_push(&args, "--signed=if-asked"); 1383 if (options.atomic) 1384 strvec_push(&args, "--atomic"); 1385 if (options.verbosity == 0) 1386 strvec_push(&args, "--quiet"); 1387 else if (options.verbosity > 1) 1388 strvec_push(&args, "--verbose"); 1389 for (i = 0; i < options.push_options.nr; i++) 1390 strvec_pushf(&args, "--push-option=%s", 1391 options.push_options.items[i].string); 1392 strvec_push(&args, options.progress ? "--progress" : "--no-progress"); 1393 for_each_string_list_item(cas_option, &cas_options) 1394 strvec_push(&args, cas_option->string); 1395 strvec_push(&args, url.buf); 1396 1397 if (options.force_if_includes) 1398 strvec_push(&args, "--force-if-includes"); 1399 1400 strvec_push(&args, "--stdin"); 1401 for (; *specs; specs++) 1402 packet_buf_write(&preamble, "%s\n", *specs); 1403 packet_buf_flush(&preamble); 1404 1405 memset(&rpc, 0, sizeof(rpc)); 1406 rpc.service_name = "git-receive-pack"; 1407 1408 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result); 1409 if (rpc_result.len) 1410 write_or_die(1, rpc_result.buf, rpc_result.len); 1411 strbuf_release(&rpc_result); 1412 strbuf_release(&preamble); 1413 strvec_clear(&args); 1414 return err; 1415 } 1416 1417 static int push(const char **specs) 1418 { 1419 struct discovery *heads = discover_refs("git-receive-pack", 1); 1420 int ret; 1421 1422 if (heads->proto_git) 1423 ret = push_git(heads, specs); 1424 else 1425 ret = push_dav(specs); 1426 free_discovery(heads); 1427 return ret; 1428 } 1429 1430 static void parse_push(struct strbuf *buf) 1431 { 1432 struct strvec specs = STRVEC_INIT; 1433 int ret; 1434 1435 do { 1436 const char *arg; 1437 if (skip_prefix(buf->buf, "push ", &arg)) 1438 strvec_push(&specs, arg); 1439 else 1440 die(_("http transport does not support %s"), buf->buf); 1441 1442 strbuf_reset(buf); 1443 if (strbuf_getline_lf(buf, stdin) == EOF) 1444 goto free_specs; 1445 if (!*buf->buf) 1446 break; 1447 } while (1); 1448 1449 ret = push(specs.v); 1450 printf("\n"); 1451 fflush(stdout); 1452 1453 if (ret) 1454 exit(128); /* error already reported */ 1455 1456 free_specs: 1457 strvec_clear(&specs); 1458 } 1459 1460 static int stateless_connect(const char *service_name) 1461 { 1462 struct discovery *discover; 1463 struct rpc_state rpc = RPC_STATE_INIT; 1464 struct strbuf buf = STRBUF_INIT; 1465 const char *accept_language; 1466 1467 /* 1468 * Run the info/refs request and see if the server supports protocol 1469 * v2. If and only if the server supports v2 can we successfully 1470 * establish a stateless connection, otherwise we need to tell the 1471 * client to fallback to using other transport helper functions to 1472 * complete their request. 1473 * 1474 * The "git-upload-archive" service is a read-only operation. Fallback 1475 * to use "git-upload-pack" service to discover protocol version. 1476 */ 1477 if (!strcmp(service_name, "git-upload-archive")) 1478 discover = discover_refs("git-upload-pack", 0); 1479 else 1480 discover = discover_refs(service_name, 0); 1481 if (discover->version != protocol_v2) { 1482 printf("fallback\n"); 1483 fflush(stdout); 1484 return -1; 1485 } else { 1486 /* Stateless Connection established */ 1487 printf("\n"); 1488 fflush(stdout); 1489 } 1490 accept_language = http_get_accept_language_header(); 1491 if (accept_language) 1492 rpc.hdr_accept_language = xstrfmt("%s", accept_language); 1493 1494 rpc.service_name = service_name; 1495 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name); 1496 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name); 1497 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name); 1498 if (get_protocol_http_header(discover->version, &buf)) { 1499 rpc.protocol_header = strbuf_detach(&buf, NULL); 1500 } else { 1501 rpc.protocol_header = NULL; 1502 strbuf_release(&buf); 1503 } 1504 rpc.buf = xmalloc(http_post_buffer); 1505 rpc.alloc = http_post_buffer; 1506 rpc.len = 0; 1507 rpc.pos = 0; 1508 rpc.in = 1; 1509 rpc.out = 0; 1510 rpc.any_written = 0; 1511 rpc.gzip_request = 1; 1512 rpc.initial_buffer = 0; 1513 rpc.write_line_lengths = 1; 1514 rpc.flush_read_but_not_sent = 0; 1515 1516 /* 1517 * Dump the capability listing that we got from the server earlier 1518 * during the info/refs request. This does not work with the 1519 * "git-upload-archive" service. 1520 */ 1521 if (strcmp(service_name, "git-upload-archive")) 1522 write_or_die(rpc.in, discover->buf, discover->len); 1523 1524 /* Until we see EOF keep sending POSTs */ 1525 while (1) { 1526 size_t avail; 1527 enum packet_read_status status; 1528 1529 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail, 1530 &status)) 1531 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX"); 1532 if (status == PACKET_READ_EOF) 1533 break; 1534 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH)) 1535 /* We would have an err here */ 1536 break; 1537 /* Reset the buffer for next request */ 1538 rpc.len = 0; 1539 } 1540 1541 free(rpc.service_url); 1542 free(rpc.hdr_content_type); 1543 free(rpc.hdr_accept); 1544 free(rpc.hdr_accept_language); 1545 free(rpc.protocol_header); 1546 free(rpc.buf); 1547 strbuf_release(&buf); 1548 1549 return 0; 1550 } 1551 1552 int cmd_main(int argc, const char **argv) 1553 { 1554 struct strbuf buf = STRBUF_INIT; 1555 int nongit; 1556 int ret = 1; 1557 1558 setup_git_directory_gently(the_repository, &nongit); 1559 if (argc < 2) { 1560 error(_("remote-curl: usage: git remote-curl []")); 1561 goto cleanup; 1562 } 1563 1564 /* 1565 * yuck, see `9e89dcb66a` (builtin/ls-remote: fall back to SHA1 outside 1566 * of a repo, 2024-08-02) 1567 */ 1568 if (nongit) 1569 repo_set_hash_algo(the_repository, GIT_HASH_DEFAULT); 1570 1571 options.verbosity = 1; 1572 options.progress = !!isatty(2); 1573 options.thin = 1; 1574 string_list_init_dup(&options.deepen_not); 1575 string_list_init_dup(&options.push_options); 1576 1577 /* 1578 * Just report "remote-curl" here (folding all the various aliases 1579 * ("git-remote-http", "git-remote-https", and etc.) here since they 1580 * are all just copies of the same actual executable. 1581 */ 1582 trace2_cmd_name("remote-curl"); 1583 1584 remote = remote_get(argv[1]); 1585 1586 if (argc > 2) { 1587 end_url_with_slash(&url, argv[2]); 1588 } else { 1589 end_url_with_slash(&url, remote->url.v[0]); 1590 } 1591 1592 http_init(remote, url.buf, 0); 1593 1594 do { 1595 const char *arg; 1596 1597 if (strbuf_getline_lf(&buf, stdin) == EOF) { 1598 if (ferror(stdin)) 1599 error(_("remote-curl: error reading command stream from git")); 1600 goto cleanup; 1601 } 1602 if (buf.len == 0) 1603 break; 1604 if (starts_with(buf.buf, "fetch ")) { 1605 if (nongit) { 1606 setup_git_directory_gently(the_repository, &nongit); 1607 if (nongit) 1608 die(_("remote-curl: fetch attempted without a local repo")); 1609 } 1610 parse_fetch(&buf); 1611 1612 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) { 1613 int for_push = !!strstr(buf.buf + 4, "for-push"); 1614 output_refs(get_refs(for_push)); 1615 1616 } else if (starts_with(buf.buf, "push ")) { 1617 parse_push(&buf); 1618 1619 } else if (skip_prefix(buf.buf, "option ", &arg)) { 1620 const char *value = strchrnul(arg, ' '); 1621 size_t arglen = value - arg; 1622 int result; 1623 1624 if (*value) 1625 value++; /* skip over SP */ 1626 else 1627 value = "true"; 1628 1629 result = set_option(arg, arglen, value); 1630 if (!result) 1631 printf("ok\n"); 1632 else if (result < 0) 1633 printf("error invalid value\n"); 1634 else 1635 printf("unsupported\n"); 1636 fflush(stdout); 1637 1638 } else if (skip_prefix(buf.buf, "get ", &arg)) { 1639 parse_get(arg); 1640 fflush(stdout); 1641 1642 } else if (!strcmp(buf.buf, "capabilities")) { 1643 printf("stateless-connect\n"); 1644 printf("fetch\n"); 1645 printf("get\n"); 1646 printf("option\n"); 1647 printf("push\n"); 1648 printf("check-connectivity\n"); 1649 printf("object-format\n"); 1650 printf("\n"); 1651 fflush(stdout); 1652 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) { 1653 if (!stateless_connect(arg)) 1654 break; 1655 } else { 1656 error(_("remote-curl: unknown command '%s' from git"), buf.buf); 1657 goto cleanup; 1658 } 1659 strbuf_reset(&buf); 1660 } while (1); 1661 1662 http_cleanup(); 1663 ret = 0; 1664 cleanup: 1665 strbuf_release(&buf); 1666 1667 return ret; 1668 }