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;
326 if (last && !strcmp(service, last->service))
328 free_discovery(last);
330 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
331 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
332 git_env_bool("GIT_SMART_HTTP", 1)) {
334 if (!strchr(url.buf, '?'))
335 strbuf_addch(&refs_url, '?');
337 strbuf_addch(&refs_url, '&');
338 strbuf_addf(&refs_url, "service=%s", service);
341 /* Add the extra Git-Protocol header */
342 if (get_protocol_http_header(get_protocol_version_config(), &protocol_header))
343 string_list_append(&extra_headers, protocol_header.buf);
345 memset(&http_options, 0, sizeof(http_options));
346 http_options.content_type = &type;
347 http_options.charset = &charset;
348 http_options.effective_url = &effective_url;
349 http_options.base_url = &url;
350 http_options.extra_headers = &extra_headers;
351 http_options.initial_request = 1;
352 http_options.no_cache = 1;
353 http_options.keep_error = 1;
355 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
359 case HTTP_MISSING_TARGET:
360 show_http_message(&type, &charset, &buffer);
361 die("repository '%s' not found", url.buf);
363 show_http_message(&type, &charset, &buffer);
364 die("Authentication failed for '%s'", url.buf);
366 show_http_message(&type, &charset, &buffer);
367 die("unable to access '%s': %s", url.buf, curl_errorstr);
370 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
371 warning(_("redirecting to %s"), url.buf);
373 last= xcalloc(1, sizeof(*last_discovery));
374 last->service = xstrdup(service);
375 last->buf_alloc = strbuf_detach(&buffer, &last->len);
376 last->buf = last->buf_alloc;
378 strbuf_addf(&exp, "application/x-%s-advertisement", service);
380 (5 <= last->len && last->buf[4] == '#') &&
381 !strbuf_cmp(&exp, &type)) {
385 * smart HTTP response; validate that the service
386 * pkt-line matches our request.
388 line = packet_read_line_buf(&last->buf, &last->len, NULL);
391 strbuf_addf(&exp, "# service=%s", service);
392 if (strcmp(line, exp.buf))
393 die("invalid server response; got '%s'", line);
394 strbuf_release(&exp);
396 /* The header can include additional metadata lines, up
397 * until a packet flush marker. Ignore these now, but
398 * in the future we might start to scan them.
400 while (packet_read_line_buf(&last->buf, &last->len, NULL))
404 } else if (maybe_smart &&
405 last->len > 5 && starts_with(last->buf + 4, "version 2")) {
410 last->refs = parse_git_refs(last, for_push);
412 last->refs = parse_info_refs(last);
414 strbuf_release(&refs_url);
415 strbuf_release(&exp);
416 strbuf_release(&type);
417 strbuf_release(&charset);
418 strbuf_release(&effective_url);
419 strbuf_release(&buffer);
420 strbuf_release(&protocol_header);
421 string_list_clear(&extra_headers, 0);
422 last_discovery = last;
426 static struct ref *get_refs(int for_push)
428 struct discovery *heads;
431 heads = discover_refs("git-receive-pack", for_push);
433 heads = discover_refs("git-upload-pack", for_push);
438 static void output_refs(struct ref *refs)
441 for (posn = refs; posn; posn = posn->next) {
443 printf("@%s %s\n", posn->symref, posn->name);
445 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
452 const char *service_name;
454 struct strbuf *stdin_preamble;
456 char *hdr_content_type;
458 char *protocol_header;
466 struct strbuf result;
467 unsigned gzip_request : 1;
468 unsigned initial_buffer : 1;
471 static size_t rpc_out(void *ptr, size_t eltsize,
472 size_t nmemb, void *buffer_)
474 size_t max = eltsize * nmemb;
475 struct rpc_state *rpc = buffer_;
476 size_t avail = rpc->len - rpc->pos;
479 rpc->initial_buffer = 0;
480 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
489 memcpy(ptr, rpc->buf + rpc->pos, avail);
494 #ifndef NO_CURL_IOCTL
495 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
497 struct rpc_state *rpc = clientp;
503 case CURLIOCMD_RESTARTREAD:
504 if (rpc->initial_buffer) {
508 error("unable to rewind rpc post data - try increasing http.postBuffer");
509 return CURLIOE_FAILRESTART;
512 return CURLIOE_UNKNOWNCMD;
517 static size_t rpc_in(char *ptr, size_t eltsize,
518 size_t nmemb, void *buffer_)
520 size_t size = eltsize * nmemb;
521 struct rpc_state *rpc = buffer_;
523 rpc->any_written = 1;
524 write_or_die(rpc->in, ptr, size);
528 static int run_slot(struct active_request_slot *slot,
529 struct slot_results *results)
532 struct slot_results results_buf;
535 results = &results_buf;
537 err = run_one_slot(slot, results);
539 if (err != HTTP_OK && err != HTTP_REAUTH) {
540 struct strbuf msg = STRBUF_INIT;
541 if (results->http_code && results->http_code != 200)
542 strbuf_addf(&msg, "HTTP %ld", results->http_code);
543 if (results->curl_result != CURLE_OK) {
545 strbuf_addch(&msg, ' ');
546 strbuf_addf(&msg, "curl %d", results->curl_result);
547 if (curl_errorstr[0]) {
548 strbuf_addch(&msg, ' ');
549 strbuf_addstr(&msg, curl_errorstr);
552 error("RPC failed; %s", msg.buf);
553 strbuf_release(&msg);
559 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
561 struct active_request_slot *slot;
562 struct curl_slist *headers = http_copy_default_headers();
563 struct strbuf buf = STRBUF_INIT;
566 slot = get_active_slot();
568 headers = curl_slist_append(headers, rpc->hdr_content_type);
569 headers = curl_slist_append(headers, rpc->hdr_accept);
571 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
572 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
573 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
574 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
575 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
576 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
577 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
578 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
579 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
581 err = run_slot(slot, results);
583 curl_slist_free_all(headers);
584 strbuf_release(&buf);
588 static curl_off_t xcurl_off_t(ssize_t len) {
589 if (len > maximum_signed_value_of_type(curl_off_t))
590 die("cannot handle pushes this big");
591 return (curl_off_t) len;
594 static int post_rpc(struct rpc_state *rpc)
596 struct active_request_slot *slot;
597 struct curl_slist *headers = http_copy_default_headers();
598 int use_gzip = rpc->gzip_request;
599 char *gzip_body = NULL;
600 size_t gzip_size = 0;
601 int err, large_request = 0;
602 int needs_100_continue = 0;
604 /* Try to load the entire request, if we can fit it into the
605 * allocated buffer space we can use HTTP/1.0 and avoid the
606 * chunked encoding mess.
609 size_t left = rpc->alloc - rpc->len;
610 char *buf = rpc->buf + rpc->len;
613 if (left < LARGE_PACKET_MAX) {
619 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
626 struct slot_results results;
629 err = probe_rpc(rpc, &results);
630 if (err == HTTP_REAUTH)
631 credential_fill(&http_auth);
632 } while (err == HTTP_REAUTH);
636 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
637 needs_100_continue = 1;
640 headers = curl_slist_append(headers, rpc->hdr_content_type);
641 headers = curl_slist_append(headers, rpc->hdr_accept);
642 headers = curl_slist_append(headers, needs_100_continue ?
643 "Expect: 100-continue" : "Expect:");
645 /* Add the extra Git-Protocol header */
646 if (rpc->protocol_header)
647 headers = curl_slist_append(headers, rpc->protocol_header);
650 slot = get_active_slot();
652 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
653 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
654 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
655 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
658 /* The request body is large and the size cannot be predicted.
659 * We must use chunked encoding to send it.
661 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
662 rpc->initial_buffer = 1;
663 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
664 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
665 #ifndef NO_CURL_IOCTL
666 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
667 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
669 if (options.verbosity > 1) {
670 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
674 } else if (gzip_body) {
676 * If we are looping to retry authentication, then the previous
677 * run will have set up the headers and gzip buffer already,
678 * and we just need to send it.
680 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
681 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
683 } else if (use_gzip && 1024 < rpc->len) {
684 /* The client backend isn't giving us compressed data so
685 * we can try to deflate it ourselves, this may save on.
691 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
692 gzip_size = git_deflate_bound(&stream, rpc->len);
693 gzip_body = xmalloc(gzip_size);
695 stream.next_in = (unsigned char *)rpc->buf;
696 stream.avail_in = rpc->len;
697 stream.next_out = (unsigned char *)gzip_body;
698 stream.avail_out = gzip_size;
700 ret = git_deflate(&stream, Z_FINISH);
701 if (ret != Z_STREAM_END)
702 die("cannot deflate request; zlib deflate error %d", ret);
704 ret = git_deflate_end_gently(&stream);
706 die("cannot deflate request; zlib end error %d", ret);
708 gzip_size = stream.total_out;
710 headers = curl_slist_append(headers, "Content-Encoding: gzip");
711 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
712 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
714 if (options.verbosity > 1) {
715 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
717 (unsigned long)rpc->len, (unsigned long)gzip_size);
721 /* We know the complete request size in advance, use the
722 * more normal Content-Length approach.
724 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
725 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
726 if (options.verbosity > 1) {
727 fprintf(stderr, "POST %s (%lu bytes)\n",
728 rpc->service_name, (unsigned long)rpc->len);
733 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
734 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
735 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
738 rpc->any_written = 0;
739 err = run_slot(slot, NULL);
740 if (err == HTTP_REAUTH && !large_request) {
741 credential_fill(&http_auth);
747 if (!rpc->any_written)
750 curl_slist_free_all(headers);
755 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
757 const char *svc = rpc->service_name;
758 struct strbuf buf = STRBUF_INIT;
759 struct strbuf *preamble = rpc->stdin_preamble;
760 struct child_process client = CHILD_PROCESS_INIT;
766 client.argv = rpc->argv;
767 if (start_command(&client))
770 write_or_die(client.in, preamble->buf, preamble->len);
772 write_or_die(client.in, heads->buf, heads->len);
774 rpc->alloc = http_post_buffer;
775 rpc->buf = xmalloc(rpc->alloc);
777 rpc->out = client.out;
778 strbuf_init(&rpc->result, 0);
780 strbuf_addf(&buf, "%s%s", url.buf, svc);
781 rpc->service_url = strbuf_detach(&buf, NULL);
783 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
784 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
786 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
787 rpc->hdr_accept = strbuf_detach(&buf, NULL);
789 if (get_protocol_http_header(heads->version, &buf))
790 rpc->protocol_header = strbuf_detach(&buf, NULL);
792 rpc->protocol_header = NULL;
795 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
800 err |= post_rpc(rpc);
806 strbuf_read(&rpc->result, client.out, 0);
810 if (xread(client.out, buf, sizeof(buf)) <= 0)
817 err |= finish_command(&client);
818 free(rpc->service_url);
819 free(rpc->hdr_content_type);
820 free(rpc->hdr_accept);
821 free(rpc->protocol_header);
823 strbuf_release(&buf);
827 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
829 struct walker *walker;
833 ALLOC_ARRAY(targets, nr_heads);
834 if (options.depth || options.deepen_since)
835 die("dumb http transport does not support shallow capabilities");
836 for (i = 0; i < nr_heads; i++)
837 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
839 walker = get_http_walker(url.buf);
841 walker->get_tree = 1;
842 walker->get_history = 1;
843 walker->get_verbosely = options.verbosity >= 3;
844 walker->get_recover = 0;
845 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
848 for (i = 0; i < nr_heads; i++)
852 return ret ? error("fetch failed.") : 0;
855 static int fetch_git(struct discovery *heads,
856 int nr_heads, struct ref **to_fetch)
858 struct rpc_state rpc;
859 struct strbuf preamble = STRBUF_INIT;
861 struct argv_array args = ARGV_ARRAY_INIT;
863 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
864 "--stdin", "--lock-pack", NULL);
865 if (options.followtags)
866 argv_array_push(&args, "--include-tag");
868 argv_array_push(&args, "--thin");
869 if (options.verbosity >= 3)
870 argv_array_pushl(&args, "-v", "-v", NULL);
871 if (options.check_self_contained_and_connected)
872 argv_array_push(&args, "--check-self-contained-and-connected");
874 argv_array_push(&args, "--cloning");
875 if (options.update_shallow)
876 argv_array_push(&args, "--update-shallow");
877 if (!options.progress)
878 argv_array_push(&args, "--no-progress");
880 argv_array_pushf(&args, "--depth=%lu", options.depth);
881 if (options.deepen_since)
882 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
883 for (i = 0; i < options.deepen_not.nr; i++)
884 argv_array_pushf(&args, "--shallow-exclude=%s",
885 options.deepen_not.items[i].string);
886 if (options.deepen_relative && options.depth)
887 argv_array_push(&args, "--deepen-relative");
888 argv_array_push(&args, url.buf);
890 for (i = 0; i < nr_heads; i++) {
891 struct ref *ref = to_fetch[i];
893 die("cannot fetch by sha1 over smart http");
894 packet_buf_write(&preamble, "%s %s\n",
895 oid_to_hex(&ref->old_oid), ref->name);
897 packet_buf_flush(&preamble);
899 memset(&rpc, 0, sizeof(rpc));
900 rpc.service_name = "git-upload-pack",
901 rpc.argv = args.argv;
902 rpc.stdin_preamble = &preamble;
903 rpc.gzip_request = 1;
905 err = rpc_service(&rpc, heads);
907 write_or_die(1, rpc.result.buf, rpc.result.len);
908 strbuf_release(&rpc.result);
909 strbuf_release(&preamble);
910 argv_array_clear(&args);
914 static int fetch(int nr_heads, struct ref **to_fetch)
916 struct discovery *d = discover_refs("git-upload-pack", 0);
918 return fetch_git(d, nr_heads, to_fetch);
920 return fetch_dumb(nr_heads, to_fetch);
923 static void parse_fetch(struct strbuf *buf)
925 struct ref **to_fetch = NULL;
926 struct ref *list_head = NULL;
927 struct ref **list = &list_head;
928 int alloc_heads = 0, nr_heads = 0;
932 if (skip_prefix(buf->buf, "fetch ", &p)) {
935 struct object_id old_oid;
937 if (get_oid_hex(p, &old_oid))
938 die("protocol error: expected sha/ref, got %s'", p);
939 if (p[GIT_SHA1_HEXSZ] == ' ')
940 name = p + GIT_SHA1_HEXSZ + 1;
941 else if (!p[GIT_SHA1_HEXSZ])
944 die("protocol error: expected sha/ref, got %s'", p);
946 ref = alloc_ref(name);
947 oidcpy(&ref->old_oid, &old_oid);
952 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
953 to_fetch[nr_heads++] = ref;
956 die("http transport does not support %s", buf->buf);
959 if (strbuf_getline_lf(buf, stdin) == EOF)
965 if (fetch(nr_heads, to_fetch))
966 exit(128); /* error already reported */
967 free_refs(list_head);
975 static int push_dav(int nr_spec, char **specs)
977 struct child_process child = CHILD_PROCESS_INIT;
981 argv_array_push(&child.args, "http-push");
982 argv_array_push(&child.args, "--helper-status");
984 argv_array_push(&child.args, "--dry-run");
985 if (options.verbosity > 1)
986 argv_array_push(&child.args, "--verbose");
987 argv_array_push(&child.args, url.buf);
988 for (i = 0; i < nr_spec; i++)
989 argv_array_push(&child.args, specs[i]);
991 if (run_command(&child))
992 die("git-http-push failed");
996 static int push_git(struct discovery *heads, int nr_spec, char **specs)
998 struct rpc_state rpc;
1000 struct argv_array args;
1001 struct string_list_item *cas_option;
1002 struct strbuf preamble = STRBUF_INIT;
1004 argv_array_init(&args);
1005 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1009 argv_array_push(&args, "--thin");
1010 if (options.dry_run)
1011 argv_array_push(&args, "--dry-run");
1012 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1013 argv_array_push(&args, "--signed=yes");
1014 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1015 argv_array_push(&args, "--signed=if-asked");
1016 if (options.verbosity == 0)
1017 argv_array_push(&args, "--quiet");
1018 else if (options.verbosity > 1)
1019 argv_array_push(&args, "--verbose");
1020 for (i = 0; i < options.push_options.nr; i++)
1021 argv_array_pushf(&args, "--push-option=%s",
1022 options.push_options.items[i].string);
1023 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1024 for_each_string_list_item(cas_option, &cas_options)
1025 argv_array_push(&args, cas_option->string);
1026 argv_array_push(&args, url.buf);
1028 argv_array_push(&args, "--stdin");
1029 for (i = 0; i < nr_spec; i++)
1030 packet_buf_write(&preamble, "%s\n", specs[i]);
1031 packet_buf_flush(&preamble);
1033 memset(&rpc, 0, sizeof(rpc));
1034 rpc.service_name = "git-receive-pack",
1035 rpc.argv = args.argv;
1036 rpc.stdin_preamble = &preamble;
1038 err = rpc_service(&rpc, heads);
1040 write_or_die(1, rpc.result.buf, rpc.result.len);
1041 strbuf_release(&rpc.result);
1042 strbuf_release(&preamble);
1043 argv_array_clear(&args);
1047 static int push(int nr_spec, char **specs)
1049 struct discovery *heads = discover_refs("git-receive-pack", 1);
1052 if (heads->proto_git)
1053 ret = push_git(heads, nr_spec, specs);
1055 ret = push_dav(nr_spec, specs);
1056 free_discovery(heads);
1060 static void parse_push(struct strbuf *buf)
1062 char **specs = NULL;
1063 int alloc_spec = 0, nr_spec = 0, i, ret;
1066 if (starts_with(buf->buf, "push ")) {
1067 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1068 specs[nr_spec++] = xstrdup(buf->buf + 5);
1071 die("http transport does not support %s", buf->buf);
1074 if (strbuf_getline_lf(buf, stdin) == EOF)
1080 ret = push(nr_spec, specs);
1085 exit(128); /* error already reported */
1088 for (i = 0; i < nr_spec; i++)
1094 * Used to represent the state of a connection to an HTTP server when
1095 * communicating using git's wire-protocol version 2.
1097 struct proxy_state {
1100 struct curl_slist *headers;
1101 struct strbuf request_buffer;
1104 struct packet_reader reader;
1109 static void proxy_state_init(struct proxy_state *p, const char *service_name,
1110 enum protocol_version version)
1112 struct strbuf buf = STRBUF_INIT;
1114 memset(p, 0, sizeof(*p));
1115 p->service_name = xstrdup(service_name);
1119 strbuf_init(&p->request_buffer, 0);
1121 strbuf_addf(&buf, "%s%s", url.buf, p->service_name);
1122 p->service_url = strbuf_detach(&buf, NULL);
1124 p->headers = http_copy_default_headers();
1126 strbuf_addf(&buf, "Content-Type: application/x-%s-request", p->service_name);
1127 p->headers = curl_slist_append(p->headers, buf.buf);
1130 strbuf_addf(&buf, "Accept: application/x-%s-result", p->service_name);
1131 p->headers = curl_slist_append(p->headers, buf.buf);
1134 p->headers = curl_slist_append(p->headers, "Transfer-Encoding: chunked");
1136 /* Add the Git-Protocol header */
1137 if (get_protocol_http_header(version, &buf))
1138 p->headers = curl_slist_append(p->headers, buf.buf);
1140 packet_reader_init(&p->reader, p->in, NULL, 0,
1141 PACKET_READ_GENTLE_ON_EOF);
1143 strbuf_release(&buf);
1146 static void proxy_state_clear(struct proxy_state *p)
1148 free(p->service_name);
1149 free(p->service_url);
1150 curl_slist_free_all(p->headers);
1151 strbuf_release(&p->request_buffer);
1155 * CURLOPT_READFUNCTION callback function.
1156 * Attempts to copy over a single packet-line at a time into the
1157 * curl provided buffer.
1159 static size_t proxy_in(char *buffer, size_t eltsize,
1160 size_t nmemb, void *userdata)
1163 struct proxy_state *p = userdata;
1164 size_t avail = p->request_buffer.len - p->pos;
1168 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1169 (uintmax_t)eltsize);
1173 if (p->seen_flush) {
1178 strbuf_reset(&p->request_buffer);
1179 switch (packet_reader_read(&p->reader)) {
1180 case PACKET_READ_EOF:
1181 die("unexpected EOF when reading from parent process");
1182 case PACKET_READ_NORMAL:
1183 packet_buf_write_len(&p->request_buffer, p->reader.line,
1186 case PACKET_READ_DELIM:
1187 packet_buf_delim(&p->request_buffer);
1189 case PACKET_READ_FLUSH:
1190 packet_buf_flush(&p->request_buffer);
1195 avail = p->request_buffer.len;
1200 memcpy(buffer, p->request_buffer.buf + p->pos, avail);
1205 static size_t proxy_out(char *buffer, size_t eltsize,
1206 size_t nmemb, void *userdata)
1209 struct proxy_state *p = userdata;
1212 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1213 (uintmax_t)eltsize);
1216 write_or_die(p->out, buffer, size);
1220 /* Issues a request to the HTTP server configured in `p` */
1221 static int proxy_request(struct proxy_state *p)
1223 struct active_request_slot *slot;
1225 slot = get_active_slot();
1227 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1228 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
1229 curl_easy_setopt(slot->curl, CURLOPT_URL, p->service_url);
1230 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, p->headers);
1232 /* Setup function to read request from client */
1233 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, proxy_in);
1234 curl_easy_setopt(slot->curl, CURLOPT_READDATA, p);
1236 /* Setup function to write server response to client */
1237 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, proxy_out);
1238 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, p);
1240 if (run_slot(slot, NULL) != HTTP_OK)
1246 static int stateless_connect(const char *service_name)
1248 struct discovery *discover;
1249 struct proxy_state p;
1252 * Run the info/refs request and see if the server supports protocol
1253 * v2. If and only if the server supports v2 can we successfully
1254 * establish a stateless connection, otherwise we need to tell the
1255 * client to fallback to using other transport helper functions to
1256 * complete their request.
1258 discover = discover_refs(service_name, 0);
1259 if (discover->version != protocol_v2) {
1260 printf("fallback\n");
1264 /* Stateless Connection established */
1269 proxy_state_init(&p, service_name, discover->version);
1272 * Dump the capability listing that we got from the server earlier
1273 * during the info/refs request.
1275 write_or_die(p.out, discover->buf, discover->len);
1277 /* Peek the next packet line. Until we see EOF keep sending POSTs */
1278 while (packet_reader_peek(&p.reader) != PACKET_READ_EOF) {
1279 if (proxy_request(&p)) {
1280 /* We would have an err here */
1285 proxy_state_clear(&p);
1289 int cmd_main(int argc, const char **argv)
1291 struct strbuf buf = STRBUF_INIT;
1294 setup_git_directory_gently(&nongit);
1296 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1300 options.verbosity = 1;
1301 options.progress = !!isatty(2);
1303 string_list_init(&options.deepen_not, 1);
1304 string_list_init(&options.push_options, 1);
1306 remote = remote_get(argv[1]);
1309 end_url_with_slash(&url, argv[2]);
1311 end_url_with_slash(&url, remote->url[0]);
1314 http_init(remote, url.buf, 0);
1319 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1321 error("remote-curl: error reading command stream from git");
1326 if (starts_with(buf.buf, "fetch ")) {
1328 die("remote-curl: fetch attempted without a local repo");
1331 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1332 int for_push = !!strstr(buf.buf + 4, "for-push");
1333 output_refs(get_refs(for_push));
1335 } else if (starts_with(buf.buf, "push ")) {
1338 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1339 char *value = strchr(arg, ' ');
1347 result = set_option(arg, value);
1350 else if (result < 0)
1351 printf("error invalid value\n");
1353 printf("unsupported\n");
1356 } else if (!strcmp(buf.buf, "capabilities")) {
1357 printf("stateless-connect\n");
1361 printf("check-connectivity\n");
1364 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1365 if (!stateless_connect(arg))
1368 error("remote-curl: unknown command '%s' from git", buf.buf);