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 #include "transport.h"
21 static struct remote *remote;
22 /* always ends with a trailing slash */
23 static struct strbuf url = STRBUF_INIT;
29 struct string_list deepen_not;
30 struct string_list push_options;
32 unsigned progress : 1,
33 check_self_contained_and_connected : 1,
39 /* One of the SEND_PACK_PUSH_CERT_* constants. */
45 static struct options options;
46 static struct string_list cas_options = STRING_LIST_INIT_DUP;
48 static int set_option(const char *name, const char *value)
50 if (!strcmp(name, "verbosity")) {
52 int v = strtol(value, &end, 10);
53 if (value == end || *end)
55 options.verbosity = v;
58 else if (!strcmp(name, "progress")) {
59 if (!strcmp(value, "true"))
61 else if (!strcmp(value, "false"))
67 else if (!strcmp(name, "depth")) {
69 unsigned long v = strtoul(value, &end, 10);
70 if (value == end || *end)
75 else if (!strcmp(name, "deepen-since")) {
76 options.deepen_since = xstrdup(value);
79 else if (!strcmp(name, "deepen-not")) {
80 string_list_append(&options.deepen_not, value);
83 else if (!strcmp(name, "deepen-relative")) {
84 if (!strcmp(value, "true"))
85 options.deepen_relative = 1;
86 else if (!strcmp(value, "false"))
87 options.deepen_relative = 0;
92 else if (!strcmp(name, "followtags")) {
93 if (!strcmp(value, "true"))
94 options.followtags = 1;
95 else if (!strcmp(value, "false"))
96 options.followtags = 0;
101 else if (!strcmp(name, "dry-run")) {
102 if (!strcmp(value, "true"))
104 else if (!strcmp(value, "false"))
110 else if (!strcmp(name, "check-connectivity")) {
111 if (!strcmp(value, "true"))
112 options.check_self_contained_and_connected = 1;
113 else if (!strcmp(value, "false"))
114 options.check_self_contained_and_connected = 0;
119 else if (!strcmp(name, "cas")) {
120 struct strbuf val = STRBUF_INIT;
121 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
122 string_list_append(&cas_options, val.buf);
123 strbuf_release(&val);
125 } else if (!strcmp(name, "cloning")) {
126 if (!strcmp(value, "true"))
128 else if (!strcmp(value, "false"))
133 } else if (!strcmp(name, "update-shallow")) {
134 if (!strcmp(value, "true"))
135 options.update_shallow = 1;
136 else if (!strcmp(value, "false"))
137 options.update_shallow = 0;
141 } else if (!strcmp(name, "pushcert")) {
142 if (!strcmp(value, "true"))
143 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
144 else if (!strcmp(value, "false"))
145 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
146 else if (!strcmp(value, "if-asked"))
147 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
151 } else if (!strcmp(name, "push-option")) {
153 string_list_append(&options.push_options, value);
155 struct strbuf unquoted = STRBUF_INIT;
156 if (unquote_c_style(&unquoted, value, NULL) < 0)
157 die("invalid quoting in push-option value");
158 string_list_append_nodup(&options.push_options,
159 strbuf_detach(&unquoted, NULL));
163 #if LIBCURL_VERSION_NUM >= 0x070a08
164 } else if (!strcmp(name, "family")) {
165 if (!strcmp(value, "ipv4"))
166 git_curl_ipresolve = CURL_IPRESOLVE_V4;
167 else if (!strcmp(value, "ipv6"))
168 git_curl_ipresolve = CURL_IPRESOLVE_V6;
169 else if (!strcmp(value, "all"))
170 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
174 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
175 } else if (!strcmp(name, "from-promisor")) {
176 options.from_promisor = 1;
178 } else if (!strcmp(name, "no-dependents")) {
179 options.no_dependents = 1;
181 } else if (!strcmp(name, "filter")) {
182 options.filter = xstrdup(value);
185 return 1 /* unsupported */;
195 struct oid_array shallow;
196 enum protocol_version version;
197 unsigned proto_git : 1;
199 static struct discovery *last_discovery;
201 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
203 struct ref *list = NULL;
204 struct packet_reader reader;
206 packet_reader_init(&reader, -1, heads->buf, heads->len,
207 PACKET_READ_CHOMP_NEWLINE |
208 PACKET_READ_GENTLE_ON_EOF);
210 heads->version = discover_version(&reader);
211 switch (heads->version) {
214 * Do nothing. This isn't a list of refs but rather a
215 * capability advertisement. Client would have run
216 * 'stateless-connect' so we'll dump this capability listing
217 * and let them request the refs themselves.
222 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
223 NULL, &heads->shallow);
225 case protocol_unknown_version:
226 BUG("unknown protocol version");
232 static struct ref *parse_info_refs(struct discovery *heads)
234 char *data, *start, *mid;
238 struct ref *refs = NULL;
239 struct ref *ref = NULL;
240 struct ref *last_ref = NULL;
245 while (i < heads->len) {
251 if (data[i] == '\n') {
252 if (mid - start != 40)
253 die("%sinfo/refs not valid: is this a git repository?",
254 transport_anonymize_url(url.buf));
257 ref = alloc_ref(ref_name);
258 get_oid_hex(start, &ref->old_oid);
262 last_ref->next = ref;
269 ref = alloc_ref("HEAD");
270 if (!http_fetch_ref(url.buf, ref) &&
271 !resolve_remote_symref(ref, refs)) {
281 static void free_discovery(struct discovery *d)
284 if (d == last_discovery)
285 last_discovery = NULL;
286 free(d->shallow.oid);
294 static int show_http_message(struct strbuf *type, struct strbuf *charset,
300 * We only show text/plain parts, as other types are likely
301 * to be ugly to look at on the user's terminal.
303 if (strcmp(type->buf, "text/plain"))
306 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
314 eol = strchrnul(p, '\n');
315 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
321 static int get_protocol_http_header(enum protocol_version version,
322 struct strbuf *header)
325 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
334 static struct discovery *discover_refs(const char *service, int for_push)
336 struct strbuf exp = STRBUF_INIT;
337 struct strbuf type = STRBUF_INIT;
338 struct strbuf charset = STRBUF_INIT;
339 struct strbuf buffer = STRBUF_INIT;
340 struct strbuf refs_url = STRBUF_INIT;
341 struct strbuf effective_url = STRBUF_INIT;
342 struct strbuf protocol_header = STRBUF_INIT;
343 struct string_list extra_headers = STRING_LIST_INIT_DUP;
344 struct discovery *last = last_discovery;
345 int http_ret, maybe_smart = 0;
346 struct http_get_options http_options;
347 enum protocol_version version = get_protocol_version_config();
349 if (last && !strcmp(service, last->service))
351 free_discovery(last);
353 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
354 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
355 git_env_bool("GIT_SMART_HTTP", 1)) {
357 if (!strchr(url.buf, '?'))
358 strbuf_addch(&refs_url, '?');
360 strbuf_addch(&refs_url, '&');
361 strbuf_addf(&refs_url, "service=%s", service);
365 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
366 * to perform a push, then fallback to v0 since the client doesn't know
367 * how to push yet using v2.
369 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
370 version = protocol_v0;
372 /* Add the extra Git-Protocol header */
373 if (get_protocol_http_header(version, &protocol_header))
374 string_list_append(&extra_headers, protocol_header.buf);
376 memset(&http_options, 0, sizeof(http_options));
377 http_options.content_type = &type;
378 http_options.charset = &charset;
379 http_options.effective_url = &effective_url;
380 http_options.base_url = &url;
381 http_options.extra_headers = &extra_headers;
382 http_options.initial_request = 1;
383 http_options.no_cache = 1;
384 http_options.keep_error = 1;
386 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
390 case HTTP_MISSING_TARGET:
391 show_http_message(&type, &charset, &buffer);
392 die("repository '%s' not found",
393 transport_anonymize_url(url.buf));
395 show_http_message(&type, &charset, &buffer);
396 die("Authentication failed for '%s'",
397 transport_anonymize_url(url.buf));
399 show_http_message(&type, &charset, &buffer);
400 die("unable to access '%s': %s",
401 transport_anonymize_url(url.buf), curl_errorstr);
404 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
405 char *u = transport_anonymize_url(url.buf);
406 warning(_("redirecting to %s"), u);
410 last= xcalloc(1, sizeof(*last_discovery));
411 last->service = xstrdup(service);
412 last->buf_alloc = strbuf_detach(&buffer, &last->len);
413 last->buf = last->buf_alloc;
415 strbuf_addf(&exp, "application/x-%s-advertisement", service);
417 (5 <= last->len && last->buf[4] == '#') &&
418 !strbuf_cmp(&exp, &type)) {
422 * smart HTTP response; validate that the service
423 * pkt-line matches our request.
425 line = packet_read_line_buf(&last->buf, &last->len, NULL);
427 die("invalid server response; expected service, got flush packet");
430 strbuf_addf(&exp, "# service=%s", service);
431 if (strcmp(line, exp.buf))
432 die("invalid server response; got '%s'", line);
433 strbuf_release(&exp);
435 /* The header can include additional metadata lines, up
436 * until a packet flush marker. Ignore these now, but
437 * in the future we might start to scan them.
439 while (packet_read_line_buf(&last->buf, &last->len, NULL))
443 } else if (maybe_smart &&
444 last->len > 5 && starts_with(last->buf + 4, "version 2")) {
449 last->refs = parse_git_refs(last, for_push);
451 last->refs = parse_info_refs(last);
453 strbuf_release(&refs_url);
454 strbuf_release(&exp);
455 strbuf_release(&type);
456 strbuf_release(&charset);
457 strbuf_release(&effective_url);
458 strbuf_release(&buffer);
459 strbuf_release(&protocol_header);
460 string_list_clear(&extra_headers, 0);
461 last_discovery = last;
465 static struct ref *get_refs(int for_push)
467 struct discovery *heads;
470 heads = discover_refs("git-receive-pack", for_push);
472 heads = discover_refs("git-upload-pack", for_push);
477 static void output_refs(struct ref *refs)
480 for (posn = refs; posn; posn = posn->next) {
482 printf("@%s %s\n", posn->symref, posn->name);
484 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
491 const char *service_name;
493 struct strbuf *stdin_preamble;
495 char *hdr_content_type;
497 char *protocol_header;
505 struct strbuf result;
506 unsigned gzip_request : 1;
507 unsigned initial_buffer : 1;
510 static size_t rpc_out(void *ptr, size_t eltsize,
511 size_t nmemb, void *buffer_)
513 size_t max = eltsize * nmemb;
514 struct rpc_state *rpc = buffer_;
515 size_t avail = rpc->len - rpc->pos;
518 rpc->initial_buffer = 0;
519 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
528 memcpy(ptr, rpc->buf + rpc->pos, avail);
533 #ifndef NO_CURL_IOCTL
534 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
536 struct rpc_state *rpc = clientp;
542 case CURLIOCMD_RESTARTREAD:
543 if (rpc->initial_buffer) {
547 error("unable to rewind rpc post data - try increasing http.postBuffer");
548 return CURLIOE_FAILRESTART;
551 return CURLIOE_UNKNOWNCMD;
556 static size_t rpc_in(char *ptr, size_t eltsize,
557 size_t nmemb, void *buffer_)
559 size_t size = eltsize * nmemb;
560 struct rpc_state *rpc = buffer_;
562 rpc->any_written = 1;
563 write_or_die(rpc->in, ptr, size);
567 static int run_slot(struct active_request_slot *slot,
568 struct slot_results *results)
571 struct slot_results results_buf;
574 results = &results_buf;
576 err = run_one_slot(slot, results);
578 if (err != HTTP_OK && err != HTTP_REAUTH) {
579 struct strbuf msg = STRBUF_INIT;
580 if (results->http_code && results->http_code != 200)
581 strbuf_addf(&msg, "HTTP %ld", results->http_code);
582 if (results->curl_result != CURLE_OK) {
584 strbuf_addch(&msg, ' ');
585 strbuf_addf(&msg, "curl %d", results->curl_result);
586 if (curl_errorstr[0]) {
587 strbuf_addch(&msg, ' ');
588 strbuf_addstr(&msg, curl_errorstr);
591 error("RPC failed; %s", msg.buf);
592 strbuf_release(&msg);
598 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
600 struct active_request_slot *slot;
601 struct curl_slist *headers = http_copy_default_headers();
602 struct strbuf buf = STRBUF_INIT;
605 slot = get_active_slot();
607 headers = curl_slist_append(headers, rpc->hdr_content_type);
608 headers = curl_slist_append(headers, rpc->hdr_accept);
610 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
611 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
612 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
613 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
614 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
615 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
616 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
617 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
618 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
620 err = run_slot(slot, results);
622 curl_slist_free_all(headers);
623 strbuf_release(&buf);
627 static curl_off_t xcurl_off_t(ssize_t len) {
628 if (len > maximum_signed_value_of_type(curl_off_t))
629 die("cannot handle pushes this big");
630 return (curl_off_t) len;
633 static int post_rpc(struct rpc_state *rpc)
635 struct active_request_slot *slot;
636 struct curl_slist *headers = http_copy_default_headers();
637 int use_gzip = rpc->gzip_request;
638 char *gzip_body = NULL;
639 size_t gzip_size = 0;
640 int err, large_request = 0;
641 int needs_100_continue = 0;
643 /* Try to load the entire request, if we can fit it into the
644 * allocated buffer space we can use HTTP/1.0 and avoid the
645 * chunked encoding mess.
648 size_t left = rpc->alloc - rpc->len;
649 char *buf = rpc->buf + rpc->len;
652 if (left < LARGE_PACKET_MAX) {
658 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
665 struct slot_results results;
668 err = probe_rpc(rpc, &results);
669 if (err == HTTP_REAUTH)
670 credential_fill(&http_auth);
671 } while (err == HTTP_REAUTH);
675 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
676 needs_100_continue = 1;
679 headers = curl_slist_append(headers, rpc->hdr_content_type);
680 headers = curl_slist_append(headers, rpc->hdr_accept);
681 headers = curl_slist_append(headers, needs_100_continue ?
682 "Expect: 100-continue" : "Expect:");
684 /* Add the extra Git-Protocol header */
685 if (rpc->protocol_header)
686 headers = curl_slist_append(headers, rpc->protocol_header);
689 slot = get_active_slot();
691 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
692 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
693 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
694 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
697 /* The request body is large and the size cannot be predicted.
698 * We must use chunked encoding to send it.
700 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
701 rpc->initial_buffer = 1;
702 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
703 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
704 #ifndef NO_CURL_IOCTL
705 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
706 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
708 if (options.verbosity > 1) {
709 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
713 } else if (gzip_body) {
715 * If we are looping to retry authentication, then the previous
716 * run will have set up the headers and gzip buffer already,
717 * and we just need to send it.
719 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
720 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
722 } else if (use_gzip && 1024 < rpc->len) {
723 /* The client backend isn't giving us compressed data so
724 * we can try to deflate it ourselves, this may save on
730 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
731 gzip_size = git_deflate_bound(&stream, rpc->len);
732 gzip_body = xmalloc(gzip_size);
734 stream.next_in = (unsigned char *)rpc->buf;
735 stream.avail_in = rpc->len;
736 stream.next_out = (unsigned char *)gzip_body;
737 stream.avail_out = gzip_size;
739 ret = git_deflate(&stream, Z_FINISH);
740 if (ret != Z_STREAM_END)
741 die("cannot deflate request; zlib deflate error %d", ret);
743 ret = git_deflate_end_gently(&stream);
745 die("cannot deflate request; zlib end error %d", ret);
747 gzip_size = stream.total_out;
749 headers = curl_slist_append(headers, "Content-Encoding: gzip");
750 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
751 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
753 if (options.verbosity > 1) {
754 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
756 (unsigned long)rpc->len, (unsigned long)gzip_size);
760 /* We know the complete request size in advance, use the
761 * more normal Content-Length approach.
763 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
764 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
765 if (options.verbosity > 1) {
766 fprintf(stderr, "POST %s (%lu bytes)\n",
767 rpc->service_name, (unsigned long)rpc->len);
772 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
773 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
774 curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
777 rpc->any_written = 0;
778 err = run_slot(slot, NULL);
779 if (err == HTTP_REAUTH && !large_request) {
780 credential_fill(&http_auth);
786 if (!rpc->any_written)
789 curl_slist_free_all(headers);
794 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
796 const char *svc = rpc->service_name;
797 struct strbuf buf = STRBUF_INIT;
798 struct strbuf *preamble = rpc->stdin_preamble;
799 struct child_process client = CHILD_PROCESS_INIT;
805 client.argv = rpc->argv;
806 if (start_command(&client))
809 write_or_die(client.in, preamble->buf, preamble->len);
811 write_or_die(client.in, heads->buf, heads->len);
813 rpc->alloc = http_post_buffer;
814 rpc->buf = xmalloc(rpc->alloc);
816 rpc->out = client.out;
817 strbuf_init(&rpc->result, 0);
819 strbuf_addf(&buf, "%s%s", url.buf, svc);
820 rpc->service_url = strbuf_detach(&buf, NULL);
822 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
823 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
825 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
826 rpc->hdr_accept = strbuf_detach(&buf, NULL);
828 if (get_protocol_http_header(heads->version, &buf))
829 rpc->protocol_header = strbuf_detach(&buf, NULL);
831 rpc->protocol_header = NULL;
834 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
839 err |= post_rpc(rpc);
845 strbuf_read(&rpc->result, client.out, 0);
849 if (xread(client.out, buf, sizeof(buf)) <= 0)
856 err |= finish_command(&client);
857 free(rpc->service_url);
858 free(rpc->hdr_content_type);
859 free(rpc->hdr_accept);
860 free(rpc->protocol_header);
862 strbuf_release(&buf);
866 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
868 struct walker *walker;
872 ALLOC_ARRAY(targets, nr_heads);
873 if (options.depth || options.deepen_since)
874 die("dumb http transport does not support shallow capabilities");
875 for (i = 0; i < nr_heads; i++)
876 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
878 walker = get_http_walker(url.buf);
879 walker->get_verbosely = options.verbosity >= 3;
880 walker->get_recover = 0;
881 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
884 for (i = 0; i < nr_heads; i++)
888 return ret ? error("fetch failed.") : 0;
891 static int fetch_git(struct discovery *heads,
892 int nr_heads, struct ref **to_fetch)
894 struct rpc_state rpc;
895 struct strbuf preamble = STRBUF_INIT;
897 struct argv_array args = ARGV_ARRAY_INIT;
899 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
900 "--stdin", "--lock-pack", NULL);
901 if (options.followtags)
902 argv_array_push(&args, "--include-tag");
904 argv_array_push(&args, "--thin");
905 if (options.verbosity >= 3)
906 argv_array_pushl(&args, "-v", "-v", NULL);
907 if (options.check_self_contained_and_connected)
908 argv_array_push(&args, "--check-self-contained-and-connected");
910 argv_array_push(&args, "--cloning");
911 if (options.update_shallow)
912 argv_array_push(&args, "--update-shallow");
913 if (!options.progress)
914 argv_array_push(&args, "--no-progress");
916 argv_array_pushf(&args, "--depth=%lu", options.depth);
917 if (options.deepen_since)
918 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
919 for (i = 0; i < options.deepen_not.nr; i++)
920 argv_array_pushf(&args, "--shallow-exclude=%s",
921 options.deepen_not.items[i].string);
922 if (options.deepen_relative && options.depth)
923 argv_array_push(&args, "--deepen-relative");
924 if (options.from_promisor)
925 argv_array_push(&args, "--from-promisor");
926 if (options.no_dependents)
927 argv_array_push(&args, "--no-dependents");
929 argv_array_pushf(&args, "--filter=%s", options.filter);
930 argv_array_push(&args, url.buf);
932 for (i = 0; i < nr_heads; i++) {
933 struct ref *ref = to_fetch[i];
935 die("cannot fetch by sha1 over smart http");
936 packet_buf_write(&preamble, "%s %s\n",
937 oid_to_hex(&ref->old_oid), ref->name);
939 packet_buf_flush(&preamble);
941 memset(&rpc, 0, sizeof(rpc));
942 rpc.service_name = "git-upload-pack",
943 rpc.argv = args.argv;
944 rpc.stdin_preamble = &preamble;
945 rpc.gzip_request = 1;
947 err = rpc_service(&rpc, heads);
949 write_or_die(1, rpc.result.buf, rpc.result.len);
950 strbuf_release(&rpc.result);
951 strbuf_release(&preamble);
952 argv_array_clear(&args);
956 static int fetch(int nr_heads, struct ref **to_fetch)
958 struct discovery *d = discover_refs("git-upload-pack", 0);
960 return fetch_git(d, nr_heads, to_fetch);
962 return fetch_dumb(nr_heads, to_fetch);
965 static void parse_fetch(struct strbuf *buf)
967 struct ref **to_fetch = NULL;
968 struct ref *list_head = NULL;
969 struct ref **list = &list_head;
970 int alloc_heads = 0, nr_heads = 0;
974 if (skip_prefix(buf->buf, "fetch ", &p)) {
977 struct object_id old_oid;
979 if (get_oid_hex(p, &old_oid))
980 die("protocol error: expected sha/ref, got %s'", p);
981 if (p[GIT_SHA1_HEXSZ] == ' ')
982 name = p + GIT_SHA1_HEXSZ + 1;
983 else if (!p[GIT_SHA1_HEXSZ])
986 die("protocol error: expected sha/ref, got %s'", p);
988 ref = alloc_ref(name);
989 oidcpy(&ref->old_oid, &old_oid);
994 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
995 to_fetch[nr_heads++] = ref;
998 die("http transport does not support %s", buf->buf);
1001 if (strbuf_getline_lf(buf, stdin) == EOF)
1007 if (fetch(nr_heads, to_fetch))
1008 exit(128); /* error already reported */
1009 free_refs(list_head);
1017 static int push_dav(int nr_spec, char **specs)
1019 struct child_process child = CHILD_PROCESS_INIT;
1023 argv_array_push(&child.args, "http-push");
1024 argv_array_push(&child.args, "--helper-status");
1025 if (options.dry_run)
1026 argv_array_push(&child.args, "--dry-run");
1027 if (options.verbosity > 1)
1028 argv_array_push(&child.args, "--verbose");
1029 argv_array_push(&child.args, url.buf);
1030 for (i = 0; i < nr_spec; i++)
1031 argv_array_push(&child.args, specs[i]);
1033 if (run_command(&child))
1034 die("git-http-push failed");
1038 static int push_git(struct discovery *heads, int nr_spec, char **specs)
1040 struct rpc_state rpc;
1042 struct argv_array args;
1043 struct string_list_item *cas_option;
1044 struct strbuf preamble = STRBUF_INIT;
1046 argv_array_init(&args);
1047 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1051 argv_array_push(&args, "--thin");
1052 if (options.dry_run)
1053 argv_array_push(&args, "--dry-run");
1054 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1055 argv_array_push(&args, "--signed=yes");
1056 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1057 argv_array_push(&args, "--signed=if-asked");
1058 if (options.verbosity == 0)
1059 argv_array_push(&args, "--quiet");
1060 else if (options.verbosity > 1)
1061 argv_array_push(&args, "--verbose");
1062 for (i = 0; i < options.push_options.nr; i++)
1063 argv_array_pushf(&args, "--push-option=%s",
1064 options.push_options.items[i].string);
1065 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1066 for_each_string_list_item(cas_option, &cas_options)
1067 argv_array_push(&args, cas_option->string);
1068 argv_array_push(&args, url.buf);
1070 argv_array_push(&args, "--stdin");
1071 for (i = 0; i < nr_spec; i++)
1072 packet_buf_write(&preamble, "%s\n", specs[i]);
1073 packet_buf_flush(&preamble);
1075 memset(&rpc, 0, sizeof(rpc));
1076 rpc.service_name = "git-receive-pack",
1077 rpc.argv = args.argv;
1078 rpc.stdin_preamble = &preamble;
1080 err = rpc_service(&rpc, heads);
1082 write_or_die(1, rpc.result.buf, rpc.result.len);
1083 strbuf_release(&rpc.result);
1084 strbuf_release(&preamble);
1085 argv_array_clear(&args);
1089 static int push(int nr_spec, char **specs)
1091 struct discovery *heads = discover_refs("git-receive-pack", 1);
1094 if (heads->proto_git)
1095 ret = push_git(heads, nr_spec, specs);
1097 ret = push_dav(nr_spec, specs);
1098 free_discovery(heads);
1102 static void parse_push(struct strbuf *buf)
1104 char **specs = NULL;
1105 int alloc_spec = 0, nr_spec = 0, i, ret;
1108 if (starts_with(buf->buf, "push ")) {
1109 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1110 specs[nr_spec++] = xstrdup(buf->buf + 5);
1113 die("http transport does not support %s", buf->buf);
1116 if (strbuf_getline_lf(buf, stdin) == EOF)
1122 ret = push(nr_spec, specs);
1127 exit(128); /* error already reported */
1130 for (i = 0; i < nr_spec; i++)
1136 * Used to represent the state of a connection to an HTTP server when
1137 * communicating using git's wire-protocol version 2.
1139 struct proxy_state {
1142 struct curl_slist *headers;
1143 struct strbuf request_buffer;
1146 struct packet_reader reader;
1151 static void proxy_state_init(struct proxy_state *p, const char *service_name,
1152 enum protocol_version version)
1154 struct strbuf buf = STRBUF_INIT;
1156 memset(p, 0, sizeof(*p));
1157 p->service_name = xstrdup(service_name);
1161 strbuf_init(&p->request_buffer, 0);
1163 strbuf_addf(&buf, "%s%s", url.buf, p->service_name);
1164 p->service_url = strbuf_detach(&buf, NULL);
1166 p->headers = http_copy_default_headers();
1168 strbuf_addf(&buf, "Content-Type: application/x-%s-request", p->service_name);
1169 p->headers = curl_slist_append(p->headers, buf.buf);
1172 strbuf_addf(&buf, "Accept: application/x-%s-result", p->service_name);
1173 p->headers = curl_slist_append(p->headers, buf.buf);
1176 p->headers = curl_slist_append(p->headers, "Transfer-Encoding: chunked");
1178 /* Add the Git-Protocol header */
1179 if (get_protocol_http_header(version, &buf))
1180 p->headers = curl_slist_append(p->headers, buf.buf);
1182 packet_reader_init(&p->reader, p->in, NULL, 0,
1183 PACKET_READ_GENTLE_ON_EOF);
1185 strbuf_release(&buf);
1188 static void proxy_state_clear(struct proxy_state *p)
1190 free(p->service_name);
1191 free(p->service_url);
1192 curl_slist_free_all(p->headers);
1193 strbuf_release(&p->request_buffer);
1197 * CURLOPT_READFUNCTION callback function.
1198 * Attempts to copy over a single packet-line at a time into the
1199 * curl provided buffer.
1201 static size_t proxy_in(char *buffer, size_t eltsize,
1202 size_t nmemb, void *userdata)
1205 struct proxy_state *p = userdata;
1206 size_t avail = p->request_buffer.len - p->pos;
1210 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1211 (uintmax_t)eltsize);
1215 if (p->seen_flush) {
1220 strbuf_reset(&p->request_buffer);
1221 switch (packet_reader_read(&p->reader)) {
1222 case PACKET_READ_EOF:
1223 die("unexpected EOF when reading from parent process");
1224 case PACKET_READ_NORMAL:
1225 packet_buf_write_len(&p->request_buffer, p->reader.line,
1228 case PACKET_READ_DELIM:
1229 packet_buf_delim(&p->request_buffer);
1231 case PACKET_READ_FLUSH:
1232 packet_buf_flush(&p->request_buffer);
1237 avail = p->request_buffer.len;
1242 memcpy(buffer, p->request_buffer.buf + p->pos, avail);
1247 static size_t proxy_out(char *buffer, size_t eltsize,
1248 size_t nmemb, void *userdata)
1251 struct proxy_state *p = userdata;
1254 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1255 (uintmax_t)eltsize);
1258 write_or_die(p->out, buffer, size);
1262 /* Issues a request to the HTTP server configured in `p` */
1263 static int proxy_request(struct proxy_state *p)
1265 struct active_request_slot *slot;
1267 slot = get_active_slot();
1269 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
1270 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1271 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
1272 curl_easy_setopt(slot->curl, CURLOPT_URL, p->service_url);
1273 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, p->headers);
1275 /* Setup function to read request from client */
1276 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, proxy_in);
1277 curl_easy_setopt(slot->curl, CURLOPT_READDATA, p);
1279 /* Setup function to write server response to client */
1280 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, proxy_out);
1281 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, p);
1283 if (run_slot(slot, NULL) != HTTP_OK)
1289 static int stateless_connect(const char *service_name)
1291 struct discovery *discover;
1292 struct proxy_state p;
1295 * Run the info/refs request and see if the server supports protocol
1296 * v2. If and only if the server supports v2 can we successfully
1297 * establish a stateless connection, otherwise we need to tell the
1298 * client to fallback to using other transport helper functions to
1299 * complete their request.
1301 discover = discover_refs(service_name, 0);
1302 if (discover->version != protocol_v2) {
1303 printf("fallback\n");
1307 /* Stateless Connection established */
1312 proxy_state_init(&p, service_name, discover->version);
1315 * Dump the capability listing that we got from the server earlier
1316 * during the info/refs request.
1318 write_or_die(p.out, discover->buf, discover->len);
1320 /* Peek the next packet line. Until we see EOF keep sending POSTs */
1321 while (packet_reader_peek(&p.reader) != PACKET_READ_EOF) {
1322 if (proxy_request(&p)) {
1323 /* We would have an err here */
1328 proxy_state_clear(&p);
1332 int cmd_main(int argc, const char **argv)
1334 struct strbuf buf = STRBUF_INIT;
1337 setup_git_directory_gently(&nongit);
1339 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1343 options.verbosity = 1;
1344 options.progress = !!isatty(2);
1346 string_list_init(&options.deepen_not, 1);
1347 string_list_init(&options.push_options, 1);
1349 remote = remote_get(argv[1]);
1352 end_url_with_slash(&url, argv[2]);
1354 end_url_with_slash(&url, remote->url[0]);
1357 http_init(remote, url.buf, 0);
1362 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1364 error("remote-curl: error reading command stream from git");
1369 if (starts_with(buf.buf, "fetch ")) {
1371 die("remote-curl: fetch attempted without a local repo");
1374 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1375 int for_push = !!strstr(buf.buf + 4, "for-push");
1376 output_refs(get_refs(for_push));
1378 } else if (starts_with(buf.buf, "push ")) {
1381 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1382 char *value = strchr(arg, ' ');
1390 result = set_option(arg, value);
1393 else if (result < 0)
1394 printf("error invalid value\n");
1396 printf("unsupported\n");
1399 } else if (!strcmp(buf.buf, "capabilities")) {
1400 printf("stateless-connect\n");
1404 printf("check-connectivity\n");
1407 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1408 if (!stateless_connect(arg))
1411 error("remote-curl: unknown command '%s' from git", buf.buf);