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"
19 static struct remote *remote;
20 /* always ends with a trailing slash */
21 static struct strbuf url = STRBUF_INIT;
27 struct string_list deepen_not;
28 struct string_list push_options;
29 unsigned progress : 1,
30 check_self_contained_and_connected : 1,
36 /* One of the SEND_PACK_PUSH_CERT_* constants. */
40 static struct options options;
41 static struct string_list cas_options = STRING_LIST_INIT_DUP;
43 static int set_option(const char *name, const char *value)
45 if (!strcmp(name, "verbosity")) {
47 int v = strtol(value, &end, 10);
48 if (value == end || *end)
50 options.verbosity = v;
53 else if (!strcmp(name, "progress")) {
54 if (!strcmp(value, "true"))
56 else if (!strcmp(value, "false"))
62 else if (!strcmp(name, "depth")) {
64 unsigned long v = strtoul(value, &end, 10);
65 if (value == end || *end)
70 else if (!strcmp(name, "deepen-since")) {
71 options.deepen_since = xstrdup(value);
74 else if (!strcmp(name, "deepen-not")) {
75 string_list_append(&options.deepen_not, value);
78 else if (!strcmp(name, "deepen-relative")) {
79 if (!strcmp(value, "true"))
80 options.deepen_relative = 1;
81 else if (!strcmp(value, "false"))
82 options.deepen_relative = 0;
87 else if (!strcmp(name, "followtags")) {
88 if (!strcmp(value, "true"))
89 options.followtags = 1;
90 else if (!strcmp(value, "false"))
91 options.followtags = 0;
96 else if (!strcmp(name, "dry-run")) {
97 if (!strcmp(value, "true"))
99 else if (!strcmp(value, "false"))
105 else if (!strcmp(name, "check-connectivity")) {
106 if (!strcmp(value, "true"))
107 options.check_self_contained_and_connected = 1;
108 else if (!strcmp(value, "false"))
109 options.check_self_contained_and_connected = 0;
114 else if (!strcmp(name, "cas")) {
115 struct strbuf val = STRBUF_INIT;
116 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
117 string_list_append(&cas_options, val.buf);
118 strbuf_release(&val);
120 } else if (!strcmp(name, "cloning")) {
121 if (!strcmp(value, "true"))
123 else if (!strcmp(value, "false"))
128 } else if (!strcmp(name, "update-shallow")) {
129 if (!strcmp(value, "true"))
130 options.update_shallow = 1;
131 else if (!strcmp(value, "false"))
132 options.update_shallow = 0;
136 } else if (!strcmp(name, "pushcert")) {
137 if (!strcmp(value, "true"))
138 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
139 else if (!strcmp(value, "false"))
140 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
141 else if (!strcmp(value, "if-asked"))
142 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
146 } else if (!strcmp(name, "push-option")) {
147 string_list_append(&options.push_options, value);
150 #if LIBCURL_VERSION_NUM >= 0x070a08
151 } else if (!strcmp(name, "family")) {
152 if (!strcmp(value, "ipv4"))
153 git_curl_ipresolve = CURL_IPRESOLVE_V4;
154 else if (!strcmp(value, "ipv6"))
155 git_curl_ipresolve = CURL_IPRESOLVE_V6;
156 else if (!strcmp(value, "all"))
157 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
161 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
163 return 1 /* unsupported */;
173 struct oid_array shallow;
174 enum protocol_version version;
175 unsigned proto_git : 1;
177 static struct discovery *last_discovery;
179 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
181 struct ref *list = NULL;
182 struct packet_reader reader;
184 packet_reader_init(&reader, -1, heads->buf, heads->len,
185 PACKET_READ_CHOMP_NEWLINE |
186 PACKET_READ_GENTLE_ON_EOF);
188 heads->version = discover_version(&reader);
189 switch (heads->version) {
192 * Do nothing. This isn't a list of refs but rather a
193 * capability advertisement. Client would have run
194 * 'stateless-connect' so we'll dump this capability listing
195 * and let them request the refs themselves.
200 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
201 NULL, &heads->shallow);
203 case protocol_unknown_version:
204 BUG("unknown protocol version");
210 static struct ref *parse_info_refs(struct discovery *heads)
212 char *data, *start, *mid;
216 struct ref *refs = NULL;
217 struct ref *ref = NULL;
218 struct ref *last_ref = NULL;
223 while (i < heads->len) {
229 if (data[i] == '\n') {
230 if (mid - start != 40)
231 die("%sinfo/refs not valid: is this a git repository?",
235 ref = alloc_ref(ref_name);
236 get_oid_hex(start, &ref->old_oid);
240 last_ref->next = ref;
247 ref = alloc_ref("HEAD");
248 if (!http_fetch_ref(url.buf, ref) &&
249 !resolve_remote_symref(ref, refs)) {
259 static void free_discovery(struct discovery *d)
262 if (d == last_discovery)
263 last_discovery = NULL;
264 free(d->shallow.oid);
272 static int show_http_message(struct strbuf *type, struct strbuf *charset,
278 * We only show text/plain parts, as other types are likely
279 * to be ugly to look at on the user's terminal.
281 if (strcmp(type->buf, "text/plain"))
284 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
292 eol = strchrnul(p, '\n');
293 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
299 static int get_protocol_http_header(enum protocol_version version,
300 struct strbuf *header)
303 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
312 static struct discovery *discover_refs(const char *service, int for_push)
314 struct strbuf exp = STRBUF_INIT;
315 struct strbuf type = STRBUF_INIT;
316 struct strbuf charset = STRBUF_INIT;
317 struct strbuf buffer = STRBUF_INIT;
318 struct strbuf refs_url = STRBUF_INIT;
319 struct strbuf effective_url = STRBUF_INIT;
320 struct strbuf protocol_header = STRBUF_INIT;
321 struct string_list extra_headers = STRING_LIST_INIT_DUP;
322 struct discovery *last = last_discovery;
323 int http_ret, maybe_smart = 0;
324 struct http_get_options http_options;
325 enum protocol_version version = get_protocol_version_config();
327 if (last && !strcmp(service, last->service))
329 free_discovery(last);
331 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
332 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
333 git_env_bool("GIT_SMART_HTTP", 1)) {
335 if (!strchr(url.buf, '?'))
336 strbuf_addch(&refs_url, '?');
338 strbuf_addch(&refs_url, '&');
339 strbuf_addf(&refs_url, "service=%s", service);
343 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
344 * to perform a push, then fallback to v0 since the client doesn't know
345 * how to push yet using v2.
347 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
348 version = protocol_v0;
350 /* Add the extra Git-Protocol header */
351 if (get_protocol_http_header(version, &protocol_header))
352 string_list_append(&extra_headers, protocol_header.buf);
354 memset(&http_options, 0, sizeof(http_options));
355 http_options.content_type = &type;
356 http_options.charset = &charset;
357 http_options.effective_url = &effective_url;
358 http_options.base_url = &url;
359 http_options.extra_headers = &extra_headers;
360 http_options.initial_request = 1;
361 http_options.no_cache = 1;
362 http_options.keep_error = 1;
364 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
368 case HTTP_MISSING_TARGET:
369 show_http_message(&type, &charset, &buffer);
370 die("repository '%s' not found", url.buf);
372 show_http_message(&type, &charset, &buffer);
373 die("Authentication failed for '%s'", url.buf);
375 show_http_message(&type, &charset, &buffer);
376 die("unable to access '%s': %s", url.buf, curl_errorstr);
379 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
380 warning(_("redirecting to %s"), url.buf);
382 last= xcalloc(1, sizeof(*last_discovery));
383 last->service = xstrdup(service);
384 last->buf_alloc = strbuf_detach(&buffer, &last->len);
385 last->buf = last->buf_alloc;
387 strbuf_addf(&exp, "application/x-%s-advertisement", service);
389 (5 <= last->len && last->buf[4] == '#') &&
390 !strbuf_cmp(&exp, &type)) {
394 * smart HTTP response; validate that the service
395 * pkt-line matches our request.
397 line = packet_read_line_buf(&last->buf, &last->len, NULL);
400 strbuf_addf(&exp, "# service=%s", service);
401 if (strcmp(line, exp.buf))
402 die("invalid server response; got '%s'", line);
403 strbuf_release(&exp);
405 /* The header can include additional metadata lines, up
406 * until a packet flush marker. Ignore these now, but
407 * in the future we might start to scan them.
409 while (packet_read_line_buf(&last->buf, &last->len, NULL))
413 } else if (maybe_smart &&
414 last->len > 5 && starts_with(last->buf + 4, "version 2")) {
419 last->refs = parse_git_refs(last, for_push);
421 last->refs = parse_info_refs(last);
423 strbuf_release(&refs_url);
424 strbuf_release(&exp);
425 strbuf_release(&type);
426 strbuf_release(&charset);
427 strbuf_release(&effective_url);
428 strbuf_release(&buffer);
429 strbuf_release(&protocol_header);
430 string_list_clear(&extra_headers, 0);
431 last_discovery = last;
435 static struct ref *get_refs(int for_push)
437 struct discovery *heads;
440 heads = discover_refs("git-receive-pack", for_push);
442 heads = discover_refs("git-upload-pack", for_push);
447 static void output_refs(struct ref *refs)
450 for (posn = refs; posn; posn = posn->next) {
452 printf("@%s %s\n", posn->symref, posn->name);
454 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
461 const char *service_name;
463 struct strbuf *stdin_preamble;
465 char *hdr_content_type;
467 char *protocol_header;
475 struct strbuf result;
476 unsigned gzip_request : 1;
477 unsigned initial_buffer : 1;
480 static size_t rpc_out(void *ptr, size_t eltsize,
481 size_t nmemb, void *buffer_)
483 size_t max = eltsize * nmemb;
484 struct rpc_state *rpc = buffer_;
485 size_t avail = rpc->len - rpc->pos;
488 rpc->initial_buffer = 0;
489 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
498 memcpy(ptr, rpc->buf + rpc->pos, avail);
503 #ifndef NO_CURL_IOCTL
504 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
506 struct rpc_state *rpc = clientp;
512 case CURLIOCMD_RESTARTREAD:
513 if (rpc->initial_buffer) {
517 error("unable to rewind rpc post data - try increasing http.postBuffer");
518 return CURLIOE_FAILRESTART;
521 return CURLIOE_UNKNOWNCMD;
526 static size_t rpc_in(char *ptr, size_t eltsize,
527 size_t nmemb, void *buffer_)
529 size_t size = eltsize * nmemb;
530 struct rpc_state *rpc = buffer_;
532 rpc->any_written = 1;
533 write_or_die(rpc->in, ptr, size);
537 static int run_slot(struct active_request_slot *slot,
538 struct slot_results *results)
541 struct slot_results results_buf;
544 results = &results_buf;
546 err = run_one_slot(slot, results);
548 if (err != HTTP_OK && err != HTTP_REAUTH) {
549 struct strbuf msg = STRBUF_INIT;
550 if (results->http_code && results->http_code != 200)
551 strbuf_addf(&msg, "HTTP %ld", results->http_code);
552 if (results->curl_result != CURLE_OK) {
554 strbuf_addch(&msg, ' ');
555 strbuf_addf(&msg, "curl %d", results->curl_result);
556 if (curl_errorstr[0]) {
557 strbuf_addch(&msg, ' ');
558 strbuf_addstr(&msg, curl_errorstr);
561 error("RPC failed; %s", msg.buf);
562 strbuf_release(&msg);
568 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
570 struct active_request_slot *slot;
571 struct curl_slist *headers = http_copy_default_headers();
572 struct strbuf buf = STRBUF_INIT;
575 slot = get_active_slot();
577 headers = curl_slist_append(headers, rpc->hdr_content_type);
578 headers = curl_slist_append(headers, rpc->hdr_accept);
580 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
581 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
582 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
583 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
584 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
585 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
586 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
587 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
588 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
590 err = run_slot(slot, results);
592 curl_slist_free_all(headers);
593 strbuf_release(&buf);
597 static curl_off_t xcurl_off_t(ssize_t len) {
598 if (len > maximum_signed_value_of_type(curl_off_t))
599 die("cannot handle pushes this big");
600 return (curl_off_t) len;
603 static int post_rpc(struct rpc_state *rpc)
605 struct active_request_slot *slot;
606 struct curl_slist *headers = http_copy_default_headers();
607 int use_gzip = rpc->gzip_request;
608 char *gzip_body = NULL;
609 size_t gzip_size = 0;
610 int err, large_request = 0;
611 int needs_100_continue = 0;
613 /* Try to load the entire request, if we can fit it into the
614 * allocated buffer space we can use HTTP/1.0 and avoid the
615 * chunked encoding mess.
618 size_t left = rpc->alloc - rpc->len;
619 char *buf = rpc->buf + rpc->len;
622 if (left < LARGE_PACKET_MAX) {
628 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
635 struct slot_results results;
638 err = probe_rpc(rpc, &results);
639 if (err == HTTP_REAUTH)
640 credential_fill(&http_auth);
641 } while (err == HTTP_REAUTH);
645 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
646 needs_100_continue = 1;
649 headers = curl_slist_append(headers, rpc->hdr_content_type);
650 headers = curl_slist_append(headers, rpc->hdr_accept);
651 headers = curl_slist_append(headers, needs_100_continue ?
652 "Expect: 100-continue" : "Expect:");
654 /* Add the extra Git-Protocol header */
655 if (rpc->protocol_header)
656 headers = curl_slist_append(headers, rpc->protocol_header);
659 slot = get_active_slot();
661 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
662 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
663 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
664 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
667 /* The request body is large and the size cannot be predicted.
668 * We must use chunked encoding to send it.
670 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
671 rpc->initial_buffer = 1;
672 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
673 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
674 #ifndef NO_CURL_IOCTL
675 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
676 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
678 if (options.verbosity > 1) {
679 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
683 } else if (gzip_body) {
685 * If we are looping to retry authentication, then the previous
686 * run will have set up the headers and gzip buffer already,
687 * and we just need to send it.
689 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
690 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
692 } else if (use_gzip && 1024 < rpc->len) {
693 /* The client backend isn't giving us compressed data so
694 * we can try to deflate it ourselves, this may save on.
700 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
701 gzip_size = git_deflate_bound(&stream, rpc->len);
702 gzip_body = xmalloc(gzip_size);
704 stream.next_in = (unsigned char *)rpc->buf;
705 stream.avail_in = rpc->len;
706 stream.next_out = (unsigned char *)gzip_body;
707 stream.avail_out = gzip_size;
709 ret = git_deflate(&stream, Z_FINISH);
710 if (ret != Z_STREAM_END)
711 die("cannot deflate request; zlib deflate error %d", ret);
713 ret = git_deflate_end_gently(&stream);
715 die("cannot deflate request; zlib end error %d", ret);
717 gzip_size = stream.total_out;
719 headers = curl_slist_append(headers, "Content-Encoding: gzip");
720 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
721 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
723 if (options.verbosity > 1) {
724 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
726 (unsigned long)rpc->len, (unsigned long)gzip_size);
730 /* We know the complete request size in advance, use the
731 * more normal Content-Length approach.
733 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
734 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
735 if (options.verbosity > 1) {
736 fprintf(stderr, "POST %s (%lu bytes)\n",
737 rpc->service_name, (unsigned long)rpc->len);
742 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
743 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
744 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
747 rpc->any_written = 0;
748 err = run_slot(slot, NULL);
749 if (err == HTTP_REAUTH && !large_request) {
750 credential_fill(&http_auth);
756 if (!rpc->any_written)
759 curl_slist_free_all(headers);
764 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
766 const char *svc = rpc->service_name;
767 struct strbuf buf = STRBUF_INIT;
768 struct strbuf *preamble = rpc->stdin_preamble;
769 struct child_process client = CHILD_PROCESS_INIT;
775 client.argv = rpc->argv;
776 if (start_command(&client))
779 write_or_die(client.in, preamble->buf, preamble->len);
781 write_or_die(client.in, heads->buf, heads->len);
783 rpc->alloc = http_post_buffer;
784 rpc->buf = xmalloc(rpc->alloc);
786 rpc->out = client.out;
787 strbuf_init(&rpc->result, 0);
789 strbuf_addf(&buf, "%s%s", url.buf, svc);
790 rpc->service_url = strbuf_detach(&buf, NULL);
792 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
793 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
795 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
796 rpc->hdr_accept = strbuf_detach(&buf, NULL);
798 if (get_protocol_http_header(heads->version, &buf))
799 rpc->protocol_header = strbuf_detach(&buf, NULL);
801 rpc->protocol_header = NULL;
804 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
809 err |= post_rpc(rpc);
815 strbuf_read(&rpc->result, client.out, 0);
819 if (xread(client.out, buf, sizeof(buf)) <= 0)
826 err |= finish_command(&client);
827 free(rpc->service_url);
828 free(rpc->hdr_content_type);
829 free(rpc->hdr_accept);
830 free(rpc->protocol_header);
832 strbuf_release(&buf);
836 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
838 struct walker *walker;
842 ALLOC_ARRAY(targets, nr_heads);
843 if (options.depth || options.deepen_since)
844 die("dumb http transport does not support shallow capabilities");
845 for (i = 0; i < nr_heads; i++)
846 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
848 walker = get_http_walker(url.buf);
850 walker->get_tree = 1;
851 walker->get_history = 1;
852 walker->get_verbosely = options.verbosity >= 3;
853 walker->get_recover = 0;
854 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
857 for (i = 0; i < nr_heads; i++)
861 return ret ? error("fetch failed.") : 0;
864 static int fetch_git(struct discovery *heads,
865 int nr_heads, struct ref **to_fetch)
867 struct rpc_state rpc;
868 struct strbuf preamble = STRBUF_INIT;
870 struct argv_array args = ARGV_ARRAY_INIT;
872 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
873 "--stdin", "--lock-pack", NULL);
874 if (options.followtags)
875 argv_array_push(&args, "--include-tag");
877 argv_array_push(&args, "--thin");
878 if (options.verbosity >= 3)
879 argv_array_pushl(&args, "-v", "-v", NULL);
880 if (options.check_self_contained_and_connected)
881 argv_array_push(&args, "--check-self-contained-and-connected");
883 argv_array_push(&args, "--cloning");
884 if (options.update_shallow)
885 argv_array_push(&args, "--update-shallow");
886 if (!options.progress)
887 argv_array_push(&args, "--no-progress");
889 argv_array_pushf(&args, "--depth=%lu", options.depth);
890 if (options.deepen_since)
891 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
892 for (i = 0; i < options.deepen_not.nr; i++)
893 argv_array_pushf(&args, "--shallow-exclude=%s",
894 options.deepen_not.items[i].string);
895 if (options.deepen_relative && options.depth)
896 argv_array_push(&args, "--deepen-relative");
897 argv_array_push(&args, url.buf);
899 for (i = 0; i < nr_heads; i++) {
900 struct ref *ref = to_fetch[i];
902 die("cannot fetch by sha1 over smart http");
903 packet_buf_write(&preamble, "%s %s\n",
904 oid_to_hex(&ref->old_oid), ref->name);
906 packet_buf_flush(&preamble);
908 memset(&rpc, 0, sizeof(rpc));
909 rpc.service_name = "git-upload-pack",
910 rpc.argv = args.argv;
911 rpc.stdin_preamble = &preamble;
912 rpc.gzip_request = 1;
914 err = rpc_service(&rpc, heads);
916 write_or_die(1, rpc.result.buf, rpc.result.len);
917 strbuf_release(&rpc.result);
918 strbuf_release(&preamble);
919 argv_array_clear(&args);
923 static int fetch(int nr_heads, struct ref **to_fetch)
925 struct discovery *d = discover_refs("git-upload-pack", 0);
927 return fetch_git(d, nr_heads, to_fetch);
929 return fetch_dumb(nr_heads, to_fetch);
932 static void parse_fetch(struct strbuf *buf)
934 struct ref **to_fetch = NULL;
935 struct ref *list_head = NULL;
936 struct ref **list = &list_head;
937 int alloc_heads = 0, nr_heads = 0;
941 if (skip_prefix(buf->buf, "fetch ", &p)) {
944 struct object_id old_oid;
946 if (get_oid_hex(p, &old_oid))
947 die("protocol error: expected sha/ref, got %s'", p);
948 if (p[GIT_SHA1_HEXSZ] == ' ')
949 name = p + GIT_SHA1_HEXSZ + 1;
950 else if (!p[GIT_SHA1_HEXSZ])
953 die("protocol error: expected sha/ref, got %s'", p);
955 ref = alloc_ref(name);
956 oidcpy(&ref->old_oid, &old_oid);
961 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
962 to_fetch[nr_heads++] = ref;
965 die("http transport does not support %s", buf->buf);
968 if (strbuf_getline_lf(buf, stdin) == EOF)
974 if (fetch(nr_heads, to_fetch))
975 exit(128); /* error already reported */
976 free_refs(list_head);
984 static int push_dav(int nr_spec, char **specs)
986 struct child_process child = CHILD_PROCESS_INIT;
990 argv_array_push(&child.args, "http-push");
991 argv_array_push(&child.args, "--helper-status");
993 argv_array_push(&child.args, "--dry-run");
994 if (options.verbosity > 1)
995 argv_array_push(&child.args, "--verbose");
996 argv_array_push(&child.args, url.buf);
997 for (i = 0; i < nr_spec; i++)
998 argv_array_push(&child.args, specs[i]);
1000 if (run_command(&child))
1001 die("git-http-push failed");
1005 static int push_git(struct discovery *heads, int nr_spec, char **specs)
1007 struct rpc_state rpc;
1009 struct argv_array args;
1010 struct string_list_item *cas_option;
1011 struct strbuf preamble = STRBUF_INIT;
1013 argv_array_init(&args);
1014 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1018 argv_array_push(&args, "--thin");
1019 if (options.dry_run)
1020 argv_array_push(&args, "--dry-run");
1021 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1022 argv_array_push(&args, "--signed=yes");
1023 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1024 argv_array_push(&args, "--signed=if-asked");
1025 if (options.verbosity == 0)
1026 argv_array_push(&args, "--quiet");
1027 else if (options.verbosity > 1)
1028 argv_array_push(&args, "--verbose");
1029 for (i = 0; i < options.push_options.nr; i++)
1030 argv_array_pushf(&args, "--push-option=%s",
1031 options.push_options.items[i].string);
1032 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1033 for_each_string_list_item(cas_option, &cas_options)
1034 argv_array_push(&args, cas_option->string);
1035 argv_array_push(&args, url.buf);
1037 argv_array_push(&args, "--stdin");
1038 for (i = 0; i < nr_spec; i++)
1039 packet_buf_write(&preamble, "%s\n", specs[i]);
1040 packet_buf_flush(&preamble);
1042 memset(&rpc, 0, sizeof(rpc));
1043 rpc.service_name = "git-receive-pack",
1044 rpc.argv = args.argv;
1045 rpc.stdin_preamble = &preamble;
1047 err = rpc_service(&rpc, heads);
1049 write_or_die(1, rpc.result.buf, rpc.result.len);
1050 strbuf_release(&rpc.result);
1051 strbuf_release(&preamble);
1052 argv_array_clear(&args);
1056 static int push(int nr_spec, char **specs)
1058 struct discovery *heads = discover_refs("git-receive-pack", 1);
1061 if (heads->proto_git)
1062 ret = push_git(heads, nr_spec, specs);
1064 ret = push_dav(nr_spec, specs);
1065 free_discovery(heads);
1069 static void parse_push(struct strbuf *buf)
1071 char **specs = NULL;
1072 int alloc_spec = 0, nr_spec = 0, i, ret;
1075 if (starts_with(buf->buf, "push ")) {
1076 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1077 specs[nr_spec++] = xstrdup(buf->buf + 5);
1080 die("http transport does not support %s", buf->buf);
1083 if (strbuf_getline_lf(buf, stdin) == EOF)
1089 ret = push(nr_spec, specs);
1094 exit(128); /* error already reported */
1097 for (i = 0; i < nr_spec; i++)
1103 * Used to represent the state of a connection to an HTTP server when
1104 * communicating using git's wire-protocol version 2.
1106 struct proxy_state {
1109 struct curl_slist *headers;
1110 struct strbuf request_buffer;
1113 struct packet_reader reader;
1118 static void proxy_state_init(struct proxy_state *p, const char *service_name,
1119 enum protocol_version version)
1121 struct strbuf buf = STRBUF_INIT;
1123 memset(p, 0, sizeof(*p));
1124 p->service_name = xstrdup(service_name);
1128 strbuf_init(&p->request_buffer, 0);
1130 strbuf_addf(&buf, "%s%s", url.buf, p->service_name);
1131 p->service_url = strbuf_detach(&buf, NULL);
1133 p->headers = http_copy_default_headers();
1135 strbuf_addf(&buf, "Content-Type: application/x-%s-request", p->service_name);
1136 p->headers = curl_slist_append(p->headers, buf.buf);
1139 strbuf_addf(&buf, "Accept: application/x-%s-result", p->service_name);
1140 p->headers = curl_slist_append(p->headers, buf.buf);
1143 p->headers = curl_slist_append(p->headers, "Transfer-Encoding: chunked");
1145 /* Add the Git-Protocol header */
1146 if (get_protocol_http_header(version, &buf))
1147 p->headers = curl_slist_append(p->headers, buf.buf);
1149 packet_reader_init(&p->reader, p->in, NULL, 0,
1150 PACKET_READ_GENTLE_ON_EOF);
1152 strbuf_release(&buf);
1155 static void proxy_state_clear(struct proxy_state *p)
1157 free(p->service_name);
1158 free(p->service_url);
1159 curl_slist_free_all(p->headers);
1160 strbuf_release(&p->request_buffer);
1164 * CURLOPT_READFUNCTION callback function.
1165 * Attempts to copy over a single packet-line at a time into the
1166 * curl provided buffer.
1168 static size_t proxy_in(char *buffer, size_t eltsize,
1169 size_t nmemb, void *userdata)
1172 struct proxy_state *p = userdata;
1173 size_t avail = p->request_buffer.len - p->pos;
1177 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1178 (uintmax_t)eltsize);
1182 if (p->seen_flush) {
1187 strbuf_reset(&p->request_buffer);
1188 switch (packet_reader_read(&p->reader)) {
1189 case PACKET_READ_EOF:
1190 die("unexpected EOF when reading from parent process");
1191 case PACKET_READ_NORMAL:
1192 packet_buf_write_len(&p->request_buffer, p->reader.line,
1195 case PACKET_READ_DELIM:
1196 packet_buf_delim(&p->request_buffer);
1198 case PACKET_READ_FLUSH:
1199 packet_buf_flush(&p->request_buffer);
1204 avail = p->request_buffer.len;
1209 memcpy(buffer, p->request_buffer.buf + p->pos, avail);
1214 static size_t proxy_out(char *buffer, size_t eltsize,
1215 size_t nmemb, void *userdata)
1218 struct proxy_state *p = userdata;
1221 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1222 (uintmax_t)eltsize);
1225 write_or_die(p->out, buffer, size);
1229 /* Issues a request to the HTTP server configured in `p` */
1230 static int proxy_request(struct proxy_state *p)
1232 struct active_request_slot *slot;
1234 slot = get_active_slot();
1236 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1237 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
1238 curl_easy_setopt(slot->curl, CURLOPT_URL, p->service_url);
1239 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, p->headers);
1241 /* Setup function to read request from client */
1242 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, proxy_in);
1243 curl_easy_setopt(slot->curl, CURLOPT_READDATA, p);
1245 /* Setup function to write server response to client */
1246 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, proxy_out);
1247 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, p);
1249 if (run_slot(slot, NULL) != HTTP_OK)
1255 static int stateless_connect(const char *service_name)
1257 struct discovery *discover;
1258 struct proxy_state p;
1261 * Run the info/refs request and see if the server supports protocol
1262 * v2. If and only if the server supports v2 can we successfully
1263 * establish a stateless connection, otherwise we need to tell the
1264 * client to fallback to using other transport helper functions to
1265 * complete their request.
1267 discover = discover_refs(service_name, 0);
1268 if (discover->version != protocol_v2) {
1269 printf("fallback\n");
1273 /* Stateless Connection established */
1278 proxy_state_init(&p, service_name, discover->version);
1281 * Dump the capability listing that we got from the server earlier
1282 * during the info/refs request.
1284 write_or_die(p.out, discover->buf, discover->len);
1286 /* Peek the next packet line. Until we see EOF keep sending POSTs */
1287 while (packet_reader_peek(&p.reader) != PACKET_READ_EOF) {
1288 if (proxy_request(&p)) {
1289 /* We would have an err here */
1294 proxy_state_clear(&p);
1298 int cmd_main(int argc, const char **argv)
1300 struct strbuf buf = STRBUF_INIT;
1303 setup_git_directory_gently(&nongit);
1305 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1309 options.verbosity = 1;
1310 options.progress = !!isatty(2);
1312 string_list_init(&options.deepen_not, 1);
1313 string_list_init(&options.push_options, 1);
1315 remote = remote_get(argv[1]);
1318 end_url_with_slash(&url, argv[2]);
1320 end_url_with_slash(&url, remote->url[0]);
1323 http_init(remote, url.buf, 0);
1328 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1330 error("remote-curl: error reading command stream from git");
1335 if (starts_with(buf.buf, "fetch ")) {
1337 die("remote-curl: fetch attempted without a local repo");
1340 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1341 int for_push = !!strstr(buf.buf + 4, "for-push");
1342 output_refs(get_refs(for_push));
1344 } else if (starts_with(buf.buf, "push ")) {
1347 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1348 char *value = strchr(arg, ' ');
1356 result = set_option(arg, value);
1359 else if (result < 0)
1360 printf("error invalid value\n");
1362 printf("unsupported\n");
1365 } else if (!strcmp(buf.buf, "capabilities")) {
1366 printf("stateless-connect\n");
1370 printf("check-connectivity\n");
1373 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1374 if (!stateless_connect(arg))
1377 error("remote-curl: unknown command '%s' from git", buf.buf);