9 #include "run-command.h"
11 #include "string-list.h"
13 #include "argv-array.h"
14 #include "credential.h"
15 #include "sha1-array.h"
16 #include "send-pack.h"
20 static struct remote *remote;
21 /* always ends with a trailing slash */
22 static struct strbuf url = STRBUF_INIT;
28 struct string_list deepen_not;
29 struct string_list push_options;
31 unsigned progress : 1,
32 check_self_contained_and_connected : 1,
38 /* One of the SEND_PACK_PUSH_CERT_* constants. */
44 static struct options options;
45 static struct string_list cas_options = STRING_LIST_INIT_DUP;
47 static int set_option(const char *name, const char *value)
49 if (!strcmp(name, "verbosity")) {
51 int v = strtol(value, &end, 10);
52 if (value == end || *end)
54 options.verbosity = v;
57 else if (!strcmp(name, "progress")) {
58 if (!strcmp(value, "true"))
60 else if (!strcmp(value, "false"))
66 else if (!strcmp(name, "depth")) {
68 unsigned long v = strtoul(value, &end, 10);
69 if (value == end || *end)
74 else if (!strcmp(name, "deepen-since")) {
75 options.deepen_since = xstrdup(value);
78 else if (!strcmp(name, "deepen-not")) {
79 string_list_append(&options.deepen_not, value);
82 else if (!strcmp(name, "deepen-relative")) {
83 if (!strcmp(value, "true"))
84 options.deepen_relative = 1;
85 else if (!strcmp(value, "false"))
86 options.deepen_relative = 0;
91 else if (!strcmp(name, "followtags")) {
92 if (!strcmp(value, "true"))
93 options.followtags = 1;
94 else if (!strcmp(value, "false"))
95 options.followtags = 0;
100 else if (!strcmp(name, "dry-run")) {
101 if (!strcmp(value, "true"))
103 else if (!strcmp(value, "false"))
109 else if (!strcmp(name, "check-connectivity")) {
110 if (!strcmp(value, "true"))
111 options.check_self_contained_and_connected = 1;
112 else if (!strcmp(value, "false"))
113 options.check_self_contained_and_connected = 0;
118 else if (!strcmp(name, "cas")) {
119 struct strbuf val = STRBUF_INIT;
120 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
121 string_list_append(&cas_options, val.buf);
122 strbuf_release(&val);
124 } else if (!strcmp(name, "cloning")) {
125 if (!strcmp(value, "true"))
127 else if (!strcmp(value, "false"))
132 } else if (!strcmp(name, "update-shallow")) {
133 if (!strcmp(value, "true"))
134 options.update_shallow = 1;
135 else if (!strcmp(value, "false"))
136 options.update_shallow = 0;
140 } else if (!strcmp(name, "pushcert")) {
141 if (!strcmp(value, "true"))
142 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
143 else if (!strcmp(value, "false"))
144 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
145 else if (!strcmp(value, "if-asked"))
146 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
150 } else if (!strcmp(name, "push-option")) {
152 string_list_append(&options.push_options, value);
154 struct strbuf unquoted = STRBUF_INIT;
155 if (unquote_c_style(&unquoted, value, NULL) < 0)
156 die("invalid quoting in push-option value");
157 string_list_append_nodup(&options.push_options,
158 strbuf_detach(&unquoted, NULL));
161 } else if (!strcmp(name, "family")) {
162 if (!strcmp(value, "ipv4"))
163 git_curl_ipresolve = CURL_IPRESOLVE_V4;
164 else if (!strcmp(value, "ipv6"))
165 git_curl_ipresolve = CURL_IPRESOLVE_V6;
166 else if (!strcmp(value, "all"))
167 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
171 } else if (!strcmp(name, "from-promisor")) {
172 options.from_promisor = 1;
174 } else if (!strcmp(name, "no-dependents")) {
175 options.no_dependents = 1;
177 } else if (!strcmp(name, "filter")) {
178 options.filter = xstrdup(value);;
181 return 1 /* unsupported */;
191 struct oid_array shallow;
192 enum protocol_version version;
193 unsigned proto_git : 1;
195 static struct discovery *last_discovery;
197 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
199 struct ref *list = NULL;
200 struct packet_reader reader;
202 packet_reader_init(&reader, -1, heads->buf, heads->len,
203 PACKET_READ_CHOMP_NEWLINE |
204 PACKET_READ_GENTLE_ON_EOF);
206 heads->version = discover_version(&reader);
207 switch (heads->version) {
210 * Do nothing. Client should run 'stateless-connect' and
211 * request the refs themselves.
216 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
217 NULL, &heads->shallow);
219 case protocol_unknown_version:
220 BUG("unknown protocol version");
226 static struct ref *parse_info_refs(struct discovery *heads)
228 char *data, *start, *mid;
232 struct ref *refs = NULL;
233 struct ref *ref = NULL;
234 struct ref *last_ref = NULL;
239 while (i < heads->len) {
245 if (data[i] == '\n') {
246 if (mid - start != 40)
247 die("%sinfo/refs not valid: is this a git repository?",
251 ref = alloc_ref(ref_name);
252 get_oid_hex(start, &ref->old_oid);
256 last_ref->next = ref;
263 ref = alloc_ref("HEAD");
264 if (!http_fetch_ref(url.buf, ref) &&
265 !resolve_remote_symref(ref, refs)) {
275 static void free_discovery(struct discovery *d)
278 if (d == last_discovery)
279 last_discovery = NULL;
280 free(d->shallow.oid);
288 static int show_http_message(struct strbuf *type, struct strbuf *charset,
294 * We only show text/plain parts, as other types are likely
295 * to be ugly to look at on the user's terminal.
297 if (strcmp(type->buf, "text/plain"))
300 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
308 eol = strchrnul(p, '\n');
309 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
315 static int get_protocol_http_header(enum protocol_version version,
316 struct strbuf *header)
319 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
328 static struct discovery *discover_refs(const char *service, int for_push)
330 struct strbuf exp = STRBUF_INIT;
331 struct strbuf type = STRBUF_INIT;
332 struct strbuf charset = STRBUF_INIT;
333 struct strbuf buffer = STRBUF_INIT;
334 struct strbuf refs_url = STRBUF_INIT;
335 struct strbuf effective_url = STRBUF_INIT;
336 struct strbuf protocol_header = STRBUF_INIT;
337 struct string_list extra_headers = STRING_LIST_INIT_DUP;
338 struct discovery *last = last_discovery;
339 int http_ret, maybe_smart = 0;
340 struct http_get_options http_options;
341 enum protocol_version version = get_protocol_version_config();
343 if (last && !strcmp(service, last->service))
345 free_discovery(last);
347 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
348 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
349 git_env_bool("GIT_SMART_HTTP", 1)) {
351 if (!strchr(url.buf, '?'))
352 strbuf_addch(&refs_url, '?');
354 strbuf_addch(&refs_url, '&');
355 strbuf_addf(&refs_url, "service=%s", service);
359 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
360 * to perform a push, then fallback to v0 since the client doesn't know
361 * how to push yet using v2.
363 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
364 version = protocol_v0;
366 /* Add the extra Git-Protocol header */
367 if (get_protocol_http_header(version, &protocol_header))
368 string_list_append(&extra_headers, protocol_header.buf);
370 memset(&http_options, 0, sizeof(http_options));
371 http_options.content_type = &type;
372 http_options.charset = &charset;
373 http_options.effective_url = &effective_url;
374 http_options.base_url = &url;
375 http_options.extra_headers = &extra_headers;
376 http_options.initial_request = 1;
377 http_options.no_cache = 1;
378 http_options.keep_error = 1;
380 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
384 case HTTP_MISSING_TARGET:
385 show_http_message(&type, &charset, &buffer);
386 die("repository '%s' not found", url.buf);
388 show_http_message(&type, &charset, &buffer);
389 die("Authentication failed for '%s'", url.buf);
391 show_http_message(&type, &charset, &buffer);
392 die("unable to access '%s': %s", url.buf, curl_errorstr);
395 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
396 warning(_("redirecting to %s"), url.buf);
398 last= xcalloc(1, sizeof(*last_discovery));
399 last->service = xstrdup(service);
400 last->buf_alloc = strbuf_detach(&buffer, &last->len);
401 last->buf = last->buf_alloc;
403 strbuf_addf(&exp, "application/x-%s-advertisement", service);
405 (5 <= last->len && last->buf[4] == '#') &&
406 !strbuf_cmp(&exp, &type)) {
410 * smart HTTP response; validate that the service
411 * pkt-line matches our request.
413 line = packet_read_line_buf(&last->buf, &last->len, NULL);
415 die("invalid server response; expected service, got flush packet");
418 strbuf_addf(&exp, "# service=%s", service);
419 if (strcmp(line, exp.buf))
420 die("invalid server response; got '%s'", line);
421 strbuf_release(&exp);
423 /* The header can include additional metadata lines, up
424 * until a packet flush marker. Ignore these now, but
425 * in the future we might start to scan them.
427 while (packet_read_line_buf(&last->buf, &last->len, NULL))
434 last->refs = parse_git_refs(last, for_push);
436 last->refs = parse_info_refs(last);
438 strbuf_release(&refs_url);
439 strbuf_release(&exp);
440 strbuf_release(&type);
441 strbuf_release(&charset);
442 strbuf_release(&effective_url);
443 strbuf_release(&buffer);
444 strbuf_release(&protocol_header);
445 string_list_clear(&extra_headers, 0);
446 last_discovery = last;
450 static struct ref *get_refs(int for_push)
452 struct discovery *heads;
455 heads = discover_refs("git-receive-pack", for_push);
457 heads = discover_refs("git-upload-pack", for_push);
462 static void output_refs(struct ref *refs)
465 for (posn = refs; posn; posn = posn->next) {
467 printf("@%s %s\n", posn->symref, posn->name);
469 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
476 const char *service_name;
478 struct strbuf *stdin_preamble;
480 char *hdr_content_type;
482 char *protocol_header;
490 struct strbuf result;
491 unsigned gzip_request : 1;
492 unsigned initial_buffer : 1;
495 static size_t rpc_out(void *ptr, size_t eltsize,
496 size_t nmemb, void *buffer_)
498 size_t max = eltsize * nmemb;
499 struct rpc_state *rpc = buffer_;
500 size_t avail = rpc->len - rpc->pos;
503 rpc->initial_buffer = 0;
504 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
513 memcpy(ptr, rpc->buf + rpc->pos, avail);
518 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
520 struct rpc_state *rpc = clientp;
526 case CURLIOCMD_RESTARTREAD:
527 if (rpc->initial_buffer) {
531 error("unable to rewind rpc post data - try increasing http.postBuffer");
532 return CURLIOE_FAILRESTART;
535 return CURLIOE_UNKNOWNCMD;
539 static size_t rpc_in(char *ptr, size_t eltsize,
540 size_t nmemb, void *buffer_)
542 size_t size = eltsize * nmemb;
543 struct rpc_state *rpc = buffer_;
545 rpc->any_written = 1;
546 write_or_die(rpc->in, ptr, size);
550 static int run_slot(struct active_request_slot *slot,
551 struct slot_results *results)
554 struct slot_results results_buf;
557 results = &results_buf;
559 err = run_one_slot(slot, results);
561 if (err != HTTP_OK && err != HTTP_REAUTH) {
562 struct strbuf msg = STRBUF_INIT;
563 if (results->http_code && results->http_code != 200)
564 strbuf_addf(&msg, "HTTP %ld", results->http_code);
565 if (results->curl_result != CURLE_OK) {
567 strbuf_addch(&msg, ' ');
568 strbuf_addf(&msg, "curl %d", results->curl_result);
569 if (curl_errorstr[0]) {
570 strbuf_addch(&msg, ' ');
571 strbuf_addstr(&msg, curl_errorstr);
574 error("RPC failed; %s", msg.buf);
575 strbuf_release(&msg);
581 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
583 struct active_request_slot *slot;
584 struct curl_slist *headers = http_copy_default_headers();
585 struct strbuf buf = STRBUF_INIT;
588 slot = get_active_slot();
590 headers = curl_slist_append(headers, rpc->hdr_content_type);
591 headers = curl_slist_append(headers, rpc->hdr_accept);
593 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
594 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
595 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
596 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
597 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
598 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
599 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
600 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
601 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
603 err = run_slot(slot, results);
605 curl_slist_free_all(headers);
606 strbuf_release(&buf);
610 static curl_off_t xcurl_off_t(ssize_t len) {
611 if (len > maximum_signed_value_of_type(curl_off_t))
612 die("cannot handle pushes this big");
613 return (curl_off_t) len;
616 static int post_rpc(struct rpc_state *rpc)
618 struct active_request_slot *slot;
619 struct curl_slist *headers = http_copy_default_headers();
620 int use_gzip = rpc->gzip_request;
621 char *gzip_body = NULL;
622 size_t gzip_size = 0;
623 int err, large_request = 0;
624 int needs_100_continue = 0;
626 /* Try to load the entire request, if we can fit it into the
627 * allocated buffer space we can use HTTP/1.0 and avoid the
628 * chunked encoding mess.
631 size_t left = rpc->alloc - rpc->len;
632 char *buf = rpc->buf + rpc->len;
635 if (left < LARGE_PACKET_MAX) {
641 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
648 struct slot_results results;
651 err = probe_rpc(rpc, &results);
652 if (err == HTTP_REAUTH)
653 credential_fill(&http_auth);
654 } while (err == HTTP_REAUTH);
658 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
659 needs_100_continue = 1;
662 headers = curl_slist_append(headers, rpc->hdr_content_type);
663 headers = curl_slist_append(headers, rpc->hdr_accept);
664 headers = curl_slist_append(headers, needs_100_continue ?
665 "Expect: 100-continue" : "Expect:");
667 /* Add the extra Git-Protocol header */
668 if (rpc->protocol_header)
669 headers = curl_slist_append(headers, rpc->protocol_header);
672 slot = get_active_slot();
674 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
675 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
676 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
677 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
680 /* The request body is large and the size cannot be predicted.
681 * We must use chunked encoding to send it.
683 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
684 rpc->initial_buffer = 1;
685 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
686 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
687 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
688 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
689 if (options.verbosity > 1) {
690 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
694 } else if (gzip_body) {
696 * If we are looping to retry authentication, then the previous
697 * run will have set up the headers and gzip buffer already,
698 * and we just need to send it.
700 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
701 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
703 } else if (use_gzip && 1024 < rpc->len) {
704 /* The client backend isn't giving us compressed data so
705 * we can try to deflate it ourselves, this may save on.
711 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
712 gzip_size = git_deflate_bound(&stream, rpc->len);
713 gzip_body = xmalloc(gzip_size);
715 stream.next_in = (unsigned char *)rpc->buf;
716 stream.avail_in = rpc->len;
717 stream.next_out = (unsigned char *)gzip_body;
718 stream.avail_out = gzip_size;
720 ret = git_deflate(&stream, Z_FINISH);
721 if (ret != Z_STREAM_END)
722 die("cannot deflate request; zlib deflate error %d", ret);
724 ret = git_deflate_end_gently(&stream);
726 die("cannot deflate request; zlib end error %d", ret);
728 gzip_size = stream.total_out;
730 headers = curl_slist_append(headers, "Content-Encoding: gzip");
731 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
732 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
734 if (options.verbosity > 1) {
735 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
737 (unsigned long)rpc->len, (unsigned long)gzip_size);
741 /* We know the complete request size in advance, use the
742 * more normal Content-Length approach.
744 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
745 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
746 if (options.verbosity > 1) {
747 fprintf(stderr, "POST %s (%lu bytes)\n",
748 rpc->service_name, (unsigned long)rpc->len);
753 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
754 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
755 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
758 rpc->any_written = 0;
759 err = run_slot(slot, NULL);
760 if (err == HTTP_REAUTH && !large_request) {
761 credential_fill(&http_auth);
767 if (!rpc->any_written)
770 curl_slist_free_all(headers);
775 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
777 const char *svc = rpc->service_name;
778 struct strbuf buf = STRBUF_INIT;
779 struct strbuf *preamble = rpc->stdin_preamble;
780 struct child_process client = CHILD_PROCESS_INIT;
786 client.argv = rpc->argv;
787 if (start_command(&client))
790 write_or_die(client.in, preamble->buf, preamble->len);
792 write_or_die(client.in, heads->buf, heads->len);
794 rpc->alloc = http_post_buffer;
795 rpc->buf = xmalloc(rpc->alloc);
797 rpc->out = client.out;
798 strbuf_init(&rpc->result, 0);
800 strbuf_addf(&buf, "%s%s", url.buf, svc);
801 rpc->service_url = strbuf_detach(&buf, NULL);
803 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
804 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
806 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
807 rpc->hdr_accept = strbuf_detach(&buf, NULL);
809 if (get_protocol_http_header(heads->version, &buf))
810 rpc->protocol_header = strbuf_detach(&buf, NULL);
812 rpc->protocol_header = NULL;
815 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
820 err |= post_rpc(rpc);
826 strbuf_read(&rpc->result, client.out, 0);
830 if (xread(client.out, buf, sizeof(buf)) <= 0)
837 err |= finish_command(&client);
838 free(rpc->service_url);
839 free(rpc->hdr_content_type);
840 free(rpc->hdr_accept);
841 free(rpc->protocol_header);
843 strbuf_release(&buf);
847 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
849 struct walker *walker;
853 ALLOC_ARRAY(targets, nr_heads);
854 if (options.depth || options.deepen_since)
855 die("dumb http transport does not support shallow capabilities");
856 for (i = 0; i < nr_heads; i++)
857 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
859 walker = get_http_walker(url.buf);
861 walker->get_tree = 1;
862 walker->get_history = 1;
863 walker->get_verbosely = options.verbosity >= 3;
864 walker->get_recover = 0;
865 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
868 for (i = 0; i < nr_heads; i++)
872 return ret ? error("fetch failed.") : 0;
875 static int fetch_git(struct discovery *heads,
876 int nr_heads, struct ref **to_fetch)
878 struct rpc_state rpc;
879 struct strbuf preamble = STRBUF_INIT;
881 struct argv_array args = ARGV_ARRAY_INIT;
883 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
884 "--stdin", "--lock-pack", NULL);
885 if (options.followtags)
886 argv_array_push(&args, "--include-tag");
888 argv_array_push(&args, "--thin");
889 if (options.verbosity >= 3)
890 argv_array_pushl(&args, "-v", "-v", NULL);
891 if (options.check_self_contained_and_connected)
892 argv_array_push(&args, "--check-self-contained-and-connected");
894 argv_array_push(&args, "--cloning");
895 if (options.update_shallow)
896 argv_array_push(&args, "--update-shallow");
897 if (!options.progress)
898 argv_array_push(&args, "--no-progress");
900 argv_array_pushf(&args, "--depth=%lu", options.depth);
901 if (options.deepen_since)
902 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
903 for (i = 0; i < options.deepen_not.nr; i++)
904 argv_array_pushf(&args, "--shallow-exclude=%s",
905 options.deepen_not.items[i].string);
906 if (options.deepen_relative && options.depth)
907 argv_array_push(&args, "--deepen-relative");
908 if (options.from_promisor)
909 argv_array_push(&args, "--from-promisor");
910 if (options.no_dependents)
911 argv_array_push(&args, "--no-dependents");
913 argv_array_pushf(&args, "--filter=%s", options.filter);
914 argv_array_push(&args, url.buf);
916 for (i = 0; i < nr_heads; i++) {
917 struct ref *ref = to_fetch[i];
919 die("cannot fetch by sha1 over smart http");
920 packet_buf_write(&preamble, "%s %s\n",
921 oid_to_hex(&ref->old_oid), ref->name);
923 packet_buf_flush(&preamble);
925 memset(&rpc, 0, sizeof(rpc));
926 rpc.service_name = "git-upload-pack",
927 rpc.argv = args.argv;
928 rpc.stdin_preamble = &preamble;
929 rpc.gzip_request = 1;
931 err = rpc_service(&rpc, heads);
933 write_or_die(1, rpc.result.buf, rpc.result.len);
934 strbuf_release(&rpc.result);
935 strbuf_release(&preamble);
936 argv_array_clear(&args);
940 static int fetch(int nr_heads, struct ref **to_fetch)
942 struct discovery *d = discover_refs("git-upload-pack", 0);
944 return fetch_git(d, nr_heads, to_fetch);
946 return fetch_dumb(nr_heads, to_fetch);
949 static void parse_fetch(struct strbuf *buf)
951 struct ref **to_fetch = NULL;
952 struct ref *list_head = NULL;
953 struct ref **list = &list_head;
954 int alloc_heads = 0, nr_heads = 0;
958 if (skip_prefix(buf->buf, "fetch ", &p)) {
961 struct object_id old_oid;
963 if (get_oid_hex(p, &old_oid))
964 die("protocol error: expected sha/ref, got %s'", p);
965 if (p[GIT_SHA1_HEXSZ] == ' ')
966 name = p + GIT_SHA1_HEXSZ + 1;
967 else if (!p[GIT_SHA1_HEXSZ])
970 die("protocol error: expected sha/ref, got %s'", p);
972 ref = alloc_ref(name);
973 oidcpy(&ref->old_oid, &old_oid);
978 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
979 to_fetch[nr_heads++] = ref;
982 die("http transport does not support %s", buf->buf);
985 if (strbuf_getline_lf(buf, stdin) == EOF)
991 if (fetch(nr_heads, to_fetch))
992 exit(128); /* error already reported */
993 free_refs(list_head);
1001 static int push_dav(int nr_spec, char **specs)
1003 struct child_process child = CHILD_PROCESS_INIT;
1007 argv_array_push(&child.args, "http-push");
1008 argv_array_push(&child.args, "--helper-status");
1009 if (options.dry_run)
1010 argv_array_push(&child.args, "--dry-run");
1011 if (options.verbosity > 1)
1012 argv_array_push(&child.args, "--verbose");
1013 argv_array_push(&child.args, url.buf);
1014 for (i = 0; i < nr_spec; i++)
1015 argv_array_push(&child.args, specs[i]);
1017 if (run_command(&child))
1018 die("git-http-push failed");
1022 static int push_git(struct discovery *heads, int nr_spec, char **specs)
1024 struct rpc_state rpc;
1026 struct argv_array args;
1027 struct string_list_item *cas_option;
1028 struct strbuf preamble = STRBUF_INIT;
1030 argv_array_init(&args);
1031 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1035 argv_array_push(&args, "--thin");
1036 if (options.dry_run)
1037 argv_array_push(&args, "--dry-run");
1038 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1039 argv_array_push(&args, "--signed=yes");
1040 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1041 argv_array_push(&args, "--signed=if-asked");
1042 if (options.verbosity == 0)
1043 argv_array_push(&args, "--quiet");
1044 else if (options.verbosity > 1)
1045 argv_array_push(&args, "--verbose");
1046 for (i = 0; i < options.push_options.nr; i++)
1047 argv_array_pushf(&args, "--push-option=%s",
1048 options.push_options.items[i].string);
1049 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1050 for_each_string_list_item(cas_option, &cas_options)
1051 argv_array_push(&args, cas_option->string);
1052 argv_array_push(&args, url.buf);
1054 argv_array_push(&args, "--stdin");
1055 for (i = 0; i < nr_spec; i++)
1056 packet_buf_write(&preamble, "%s\n", specs[i]);
1057 packet_buf_flush(&preamble);
1059 memset(&rpc, 0, sizeof(rpc));
1060 rpc.service_name = "git-receive-pack",
1061 rpc.argv = args.argv;
1062 rpc.stdin_preamble = &preamble;
1064 err = rpc_service(&rpc, heads);
1066 write_or_die(1, rpc.result.buf, rpc.result.len);
1067 strbuf_release(&rpc.result);
1068 strbuf_release(&preamble);
1069 argv_array_clear(&args);
1073 static int push(int nr_spec, char **specs)
1075 struct discovery *heads = discover_refs("git-receive-pack", 1);
1078 if (heads->proto_git)
1079 ret = push_git(heads, nr_spec, specs);
1081 ret = push_dav(nr_spec, specs);
1082 free_discovery(heads);
1086 static void parse_push(struct strbuf *buf)
1088 char **specs = NULL;
1089 int alloc_spec = 0, nr_spec = 0, i, ret;
1092 if (starts_with(buf->buf, "push ")) {
1093 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1094 specs[nr_spec++] = xstrdup(buf->buf + 5);
1097 die("http transport does not support %s", buf->buf);
1100 if (strbuf_getline_lf(buf, stdin) == EOF)
1106 ret = push(nr_spec, specs);
1111 exit(128); /* error already reported */
1114 for (i = 0; i < nr_spec; i++)
1119 struct proxy_state {
1122 struct curl_slist *headers;
1123 struct strbuf request_buffer;
1126 struct packet_reader reader;
1131 static void proxy_state_init(struct proxy_state *p, const char *service_name,
1132 enum protocol_version version)
1134 struct strbuf buf = STRBUF_INIT;
1136 memset(p, 0, sizeof(*p));
1137 p->service_name = xstrdup(service_name);
1141 strbuf_init(&p->request_buffer, 0);
1143 strbuf_addf(&buf, "%s%s", url.buf, p->service_name);
1144 p->service_url = strbuf_detach(&buf, NULL);
1146 p->headers = http_copy_default_headers();
1148 strbuf_addf(&buf, "Content-Type: application/x-%s-request", p->service_name);
1149 p->headers = curl_slist_append(p->headers, buf.buf);
1152 strbuf_addf(&buf, "Accept: application/x-%s-result", p->service_name);
1153 p->headers = curl_slist_append(p->headers, buf.buf);
1156 p->headers = curl_slist_append(p->headers, "Transfer-Encoding: chunked");
1158 /* Add the Git-Protocol header */
1159 if (get_protocol_http_header(version, &buf))
1160 p->headers = curl_slist_append(p->headers, buf.buf);
1162 packet_reader_init(&p->reader, p->in, NULL, 0,
1163 PACKET_READ_GENTLE_ON_EOF);
1165 strbuf_release(&buf);
1168 static void proxy_state_clear(struct proxy_state *p)
1170 free(p->service_name);
1171 free(p->service_url);
1172 curl_slist_free_all(p->headers);
1173 strbuf_release(&p->request_buffer);
1176 static size_t proxy_in(char *buffer, size_t eltsize,
1177 size_t nmemb, void *userdata)
1179 size_t max = eltsize * nmemb;
1180 struct proxy_state *p = userdata;
1181 size_t avail = p->request_buffer.len - p->pos;
1184 if (p->seen_flush) {
1189 strbuf_reset(&p->request_buffer);
1190 switch (packet_reader_read(&p->reader)) {
1191 case PACKET_READ_EOF:
1192 die("unexpected EOF when reading from parent process");
1193 case PACKET_READ_NORMAL:
1194 packet_buf_write_len(&p->request_buffer, p->reader.line,
1197 case PACKET_READ_DELIM:
1198 packet_buf_delim(&p->request_buffer);
1200 case PACKET_READ_FLUSH:
1201 packet_buf_flush(&p->request_buffer);
1206 avail = p->request_buffer.len;
1211 memcpy(buffer, p->request_buffer.buf + p->pos, avail);
1216 static size_t proxy_out(char *buffer, size_t eltsize,
1217 size_t nmemb, void *userdata)
1219 size_t size = eltsize * nmemb;
1220 struct proxy_state *p = userdata;
1222 write_or_die(p->out, buffer, size);
1226 static int proxy_post(struct proxy_state *p)
1228 struct active_request_slot *slot;
1231 slot = get_active_slot();
1233 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1234 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
1235 curl_easy_setopt(slot->curl, CURLOPT_URL, p->service_url);
1236 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, p->headers);
1238 /* Setup function to read request from client */
1239 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, proxy_in);
1240 curl_easy_setopt(slot->curl, CURLOPT_READDATA, p);
1242 /* Setup function to write server response to client */
1243 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, proxy_out);
1244 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, p);
1246 err = run_slot(slot, NULL);
1254 static int stateless_connect(const char *service_name)
1256 struct discovery *discover;
1257 struct proxy_state p;
1260 * Run the info/refs request and see if the server supports protocol
1261 * v2. If and only if the server supports v2 can we successfully
1262 * establish a stateless connection, otherwise we need to tell the
1263 * client to fallback to using other transport helper functions to
1264 * complete their request.
1266 discover = discover_refs(service_name, 0);
1267 if (discover->version != protocol_v2) {
1268 printf("fallback\n");
1272 /* Stateless Connection established */
1277 proxy_state_init(&p, service_name, discover->version);
1280 * Dump the capability listing that we got from the server earlier
1281 * during the info/refs request.
1283 write_or_die(p.out, discover->buf, discover->len);
1285 /* Peek the next packet line. Until we see EOF keep sending POSTs */
1286 while (packet_reader_peek(&p.reader) != PACKET_READ_EOF) {
1287 if (proxy_post(&p)) {
1288 /* We would have an err here */
1293 proxy_state_clear(&p);
1297 int cmd_main(int argc, const char **argv)
1299 struct strbuf buf = STRBUF_INIT;
1302 setup_git_directory_gently(&nongit);
1304 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1308 options.verbosity = 1;
1309 options.progress = !!isatty(2);
1311 string_list_init(&options.deepen_not, 1);
1312 string_list_init(&options.push_options, 1);
1314 remote = remote_get(argv[1]);
1317 end_url_with_slash(&url, argv[2]);
1319 end_url_with_slash(&url, remote->url[0]);
1322 http_init(remote, url.buf, 0);
1327 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1329 error("remote-curl: error reading command stream from git");
1334 if (starts_with(buf.buf, "fetch ")) {
1336 die("remote-curl: fetch attempted without a local repo");
1339 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1340 int for_push = !!strstr(buf.buf + 4, "for-push");
1341 output_refs(get_refs(for_push));
1343 } else if (starts_with(buf.buf, "push ")) {
1346 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1347 char *value = strchr(arg, ' ');
1355 result = set_option(arg, value);
1358 else if (result < 0)
1359 printf("error invalid value\n");
1361 printf("unsupported\n");
1364 } else if (!strcmp(buf.buf, "capabilities")) {
1365 printf("stateless-connect\n");
1369 printf("check-connectivity\n");
1372 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1373 if (!stateless_connect(arg))
1376 error("remote-curl: unknown command '%s' from git", buf.buf);