remote-curl: unset CURLOPT_FAILONERROR
[git.git] / remote-curl.c
blob91b39ca0988c154a576f753179c79628d2305435
1 #include "cache.h"
2 #include "config.h"
3 #include "remote.h"
4 #include "connect.h"
5 #include "strbuf.h"
6 #include "walker.h"
7 #include "http.h"
8 #include "exec-cmd.h"
9 #include "run-command.h"
10 #include "pkt-line.h"
11 #include "string-list.h"
12 #include "sideband.h"
13 #include "argv-array.h"
14 #include "credential.h"
15 #include "sha1-array.h"
16 #include "send-pack.h"
17 #include "protocol.h"
18 #include "quote.h"
20 static struct remote *remote;
21 /* always ends with a trailing slash */
22 static struct strbuf url = STRBUF_INIT;
24 struct options {
25 int verbosity;
26 unsigned long depth;
27 char *deepen_since;
28 struct string_list deepen_not;
29 struct string_list push_options;
30 char *filter;
31 unsigned progress : 1,
32 check_self_contained_and_connected : 1,
33 cloning : 1,
34 update_shallow : 1,
35 followtags : 1,
36 dry_run : 1,
37 thin : 1,
38 /* One of the SEND_PACK_PUSH_CERT_* constants. */
39 push_cert : 2,
40 deepen_relative : 1,
41 from_promisor : 1,
42 no_dependents : 1;
44 static struct options options;
45 static struct string_list cas_options = STRING_LIST_INIT_DUP;
47 static int set_option(const char *name, const char *value)
49 if (!strcmp(name, "verbosity")) {
50 char *end;
51 int v = strtol(value, &end, 10);
52 if (value == end || *end)
53 return -1;
54 options.verbosity = v;
55 return 0;
57 else if (!strcmp(name, "progress")) {
58 if (!strcmp(value, "true"))
59 options.progress = 1;
60 else if (!strcmp(value, "false"))
61 options.progress = 0;
62 else
63 return -1;
64 return 0;
66 else if (!strcmp(name, "depth")) {
67 char *end;
68 unsigned long v = strtoul(value, &end, 10);
69 if (value == end || *end)
70 return -1;
71 options.depth = v;
72 return 0;
74 else if (!strcmp(name, "deepen-since")) {
75 options.deepen_since = xstrdup(value);
76 return 0;
78 else if (!strcmp(name, "deepen-not")) {
79 string_list_append(&options.deepen_not, value);
80 return 0;
82 else if (!strcmp(name, "deepen-relative")) {
83 if (!strcmp(value, "true"))
84 options.deepen_relative = 1;
85 else if (!strcmp(value, "false"))
86 options.deepen_relative = 0;
87 else
88 return -1;
89 return 0;
91 else if (!strcmp(name, "followtags")) {
92 if (!strcmp(value, "true"))
93 options.followtags = 1;
94 else if (!strcmp(value, "false"))
95 options.followtags = 0;
96 else
97 return -1;
98 return 0;
100 else if (!strcmp(name, "dry-run")) {
101 if (!strcmp(value, "true"))
102 options.dry_run = 1;
103 else if (!strcmp(value, "false"))
104 options.dry_run = 0;
105 else
106 return -1;
107 return 0;
109 else if (!strcmp(name, "check-connectivity")) {
110 if (!strcmp(value, "true"))
111 options.check_self_contained_and_connected = 1;
112 else if (!strcmp(value, "false"))
113 options.check_self_contained_and_connected = 0;
114 else
115 return -1;
116 return 0;
118 else if (!strcmp(name, "cas")) {
119 struct strbuf val = STRBUF_INIT;
120 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
121 string_list_append(&cas_options, val.buf);
122 strbuf_release(&val);
123 return 0;
124 } else if (!strcmp(name, "cloning")) {
125 if (!strcmp(value, "true"))
126 options.cloning = 1;
127 else if (!strcmp(value, "false"))
128 options.cloning = 0;
129 else
130 return -1;
131 return 0;
132 } else if (!strcmp(name, "update-shallow")) {
133 if (!strcmp(value, "true"))
134 options.update_shallow = 1;
135 else if (!strcmp(value, "false"))
136 options.update_shallow = 0;
137 else
138 return -1;
139 return 0;
140 } else if (!strcmp(name, "pushcert")) {
141 if (!strcmp(value, "true"))
142 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
143 else if (!strcmp(value, "false"))
144 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
145 else if (!strcmp(value, "if-asked"))
146 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
147 else
148 return -1;
149 return 0;
150 } else if (!strcmp(name, "push-option")) {
151 if (*value != '"')
152 string_list_append(&options.push_options, value);
153 else {
154 struct strbuf unquoted = STRBUF_INIT;
155 if (unquote_c_style(&unquoted, value, NULL) < 0)
156 die("invalid quoting in push-option value");
157 string_list_append_nodup(&options.push_options,
158 strbuf_detach(&unquoted, NULL));
160 return 0;
162 #if LIBCURL_VERSION_NUM >= 0x070a08
163 } else if (!strcmp(name, "family")) {
164 if (!strcmp(value, "ipv4"))
165 git_curl_ipresolve = CURL_IPRESOLVE_V4;
166 else if (!strcmp(value, "ipv6"))
167 git_curl_ipresolve = CURL_IPRESOLVE_V6;
168 else if (!strcmp(value, "all"))
169 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
170 else
171 return -1;
172 return 0;
173 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
174 } else if (!strcmp(name, "from-promisor")) {
175 options.from_promisor = 1;
176 return 0;
177 } else if (!strcmp(name, "no-dependents")) {
178 options.no_dependents = 1;
179 return 0;
180 } else if (!strcmp(name, "filter")) {
181 options.filter = xstrdup(value);
182 return 0;
183 } else {
184 return 1 /* unsupported */;
188 struct discovery {
189 char *service;
190 char *buf_alloc;
191 char *buf;
192 size_t len;
193 struct ref *refs;
194 struct oid_array shallow;
195 enum protocol_version version;
196 unsigned proto_git : 1;
198 static struct discovery *last_discovery;
200 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
202 struct ref *list = NULL;
203 struct packet_reader reader;
205 packet_reader_init(&reader, -1, heads->buf, heads->len,
206 PACKET_READ_CHOMP_NEWLINE |
207 PACKET_READ_GENTLE_ON_EOF);
209 heads->version = discover_version(&reader);
210 switch (heads->version) {
211 case protocol_v2:
213 * Do nothing. This isn't a list of refs but rather a
214 * capability advertisement. Client would have run
215 * 'stateless-connect' so we'll dump this capability listing
216 * and let them request the refs themselves.
218 break;
219 case protocol_v1:
220 case protocol_v0:
221 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
222 NULL, &heads->shallow);
223 break;
224 case protocol_unknown_version:
225 BUG("unknown protocol version");
228 return list;
231 static struct ref *parse_info_refs(struct discovery *heads)
233 char *data, *start, *mid;
234 char *ref_name;
235 int i = 0;
237 struct ref *refs = NULL;
238 struct ref *ref = NULL;
239 struct ref *last_ref = NULL;
241 data = heads->buf;
242 start = NULL;
243 mid = data;
244 while (i < heads->len) {
245 if (!start) {
246 start = &data[i];
248 if (data[i] == '\t')
249 mid = &data[i];
250 if (data[i] == '\n') {
251 if (mid - start != 40)
252 die("%sinfo/refs not valid: is this a git repository?",
253 url.buf);
254 data[i] = 0;
255 ref_name = mid + 1;
256 ref = alloc_ref(ref_name);
257 get_oid_hex(start, &ref->old_oid);
258 if (!refs)
259 refs = ref;
260 if (last_ref)
261 last_ref->next = ref;
262 last_ref = ref;
263 start = NULL;
265 i++;
268 ref = alloc_ref("HEAD");
269 if (!http_fetch_ref(url.buf, ref) &&
270 !resolve_remote_symref(ref, refs)) {
271 ref->next = refs;
272 refs = ref;
273 } else {
274 free(ref);
277 return refs;
280 static void free_discovery(struct discovery *d)
282 if (d) {
283 if (d == last_discovery)
284 last_discovery = NULL;
285 free(d->shallow.oid);
286 free(d->buf_alloc);
287 free_refs(d->refs);
288 free(d->service);
289 free(d);
293 static int show_http_message(struct strbuf *type, struct strbuf *charset,
294 struct strbuf *msg)
296 const char *p, *eol;
299 * We only show text/plain parts, as other types are likely
300 * to be ugly to look at on the user's terminal.
302 if (strcmp(type->buf, "text/plain"))
303 return -1;
304 if (charset->len)
305 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
307 strbuf_trim(msg);
308 if (!msg->len)
309 return -1;
311 p = msg->buf;
312 do {
313 eol = strchrnul(p, '\n');
314 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
315 p = eol + 1;
316 } while(*eol);
317 return 0;
320 static int get_protocol_http_header(enum protocol_version version,
321 struct strbuf *header)
323 if (version > 0) {
324 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
325 version);
327 return 1;
330 return 0;
333 static struct discovery *discover_refs(const char *service, int for_push)
335 struct strbuf exp = STRBUF_INIT;
336 struct strbuf type = STRBUF_INIT;
337 struct strbuf charset = STRBUF_INIT;
338 struct strbuf buffer = STRBUF_INIT;
339 struct strbuf refs_url = STRBUF_INIT;
340 struct strbuf effective_url = STRBUF_INIT;
341 struct strbuf protocol_header = STRBUF_INIT;
342 struct string_list extra_headers = STRING_LIST_INIT_DUP;
343 struct discovery *last = last_discovery;
344 int http_ret, maybe_smart = 0;
345 struct http_get_options http_options;
346 enum protocol_version version = get_protocol_version_config();
348 if (last && !strcmp(service, last->service))
349 return last;
350 free_discovery(last);
352 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
353 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
354 git_env_bool("GIT_SMART_HTTP", 1)) {
355 maybe_smart = 1;
356 if (!strchr(url.buf, '?'))
357 strbuf_addch(&refs_url, '?');
358 else
359 strbuf_addch(&refs_url, '&');
360 strbuf_addf(&refs_url, "service=%s", service);
364 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
365 * to perform a push, then fallback to v0 since the client doesn't know
366 * how to push yet using v2.
368 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
369 version = protocol_v0;
371 /* Add the extra Git-Protocol header */
372 if (get_protocol_http_header(version, &protocol_header))
373 string_list_append(&extra_headers, protocol_header.buf);
375 memset(&http_options, 0, sizeof(http_options));
376 http_options.content_type = &type;
377 http_options.charset = &charset;
378 http_options.effective_url = &effective_url;
379 http_options.base_url = &url;
380 http_options.extra_headers = &extra_headers;
381 http_options.initial_request = 1;
382 http_options.no_cache = 1;
384 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
385 switch (http_ret) {
386 case HTTP_OK:
387 break;
388 case HTTP_MISSING_TARGET:
389 show_http_message(&type, &charset, &buffer);
390 die("repository '%s' not found", url.buf);
391 case HTTP_NOAUTH:
392 show_http_message(&type, &charset, &buffer);
393 die("Authentication failed for '%s'", url.buf);
394 default:
395 show_http_message(&type, &charset, &buffer);
396 die("unable to access '%s': %s", url.buf, curl_errorstr);
399 if (options.verbosity && !starts_with(refs_url.buf, url.buf))
400 warning(_("redirecting to %s"), url.buf);
402 last= xcalloc(1, sizeof(*last_discovery));
403 last->service = xstrdup(service);
404 last->buf_alloc = strbuf_detach(&buffer, &last->len);
405 last->buf = last->buf_alloc;
407 strbuf_addf(&exp, "application/x-%s-advertisement", service);
408 if (maybe_smart &&
409 (5 <= last->len && last->buf[4] == '#') &&
410 !strbuf_cmp(&exp, &type)) {
411 char *line;
414 * smart HTTP response; validate that the service
415 * pkt-line matches our request.
417 line = packet_read_line_buf(&last->buf, &last->len, NULL);
418 if (!line)
419 die("invalid server response; expected service, got flush packet");
421 strbuf_reset(&exp);
422 strbuf_addf(&exp, "# service=%s", service);
423 if (strcmp(line, exp.buf))
424 die("invalid server response; got '%s'", line);
425 strbuf_release(&exp);
427 /* The header can include additional metadata lines, up
428 * until a packet flush marker. Ignore these now, but
429 * in the future we might start to scan them.
431 while (packet_read_line_buf(&last->buf, &last->len, NULL))
434 last->proto_git = 1;
435 } else if (maybe_smart &&
436 last->len > 5 && starts_with(last->buf + 4, "version 2")) {
437 last->proto_git = 1;
440 if (last->proto_git)
441 last->refs = parse_git_refs(last, for_push);
442 else
443 last->refs = parse_info_refs(last);
445 strbuf_release(&refs_url);
446 strbuf_release(&exp);
447 strbuf_release(&type);
448 strbuf_release(&charset);
449 strbuf_release(&effective_url);
450 strbuf_release(&buffer);
451 strbuf_release(&protocol_header);
452 string_list_clear(&extra_headers, 0);
453 last_discovery = last;
454 return last;
457 static struct ref *get_refs(int for_push)
459 struct discovery *heads;
461 if (for_push)
462 heads = discover_refs("git-receive-pack", for_push);
463 else
464 heads = discover_refs("git-upload-pack", for_push);
466 return heads->refs;
469 static void output_refs(struct ref *refs)
471 struct ref *posn;
472 for (posn = refs; posn; posn = posn->next) {
473 if (posn->symref)
474 printf("@%s %s\n", posn->symref, posn->name);
475 else
476 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
478 printf("\n");
479 fflush(stdout);
482 struct rpc_state {
483 const char *service_name;
484 const char **argv;
485 struct strbuf *stdin_preamble;
486 char *service_url;
487 char *hdr_content_type;
488 char *hdr_accept;
489 char *protocol_header;
490 char *buf;
491 size_t alloc;
492 size_t len;
493 size_t pos;
494 int in;
495 int out;
496 int any_written;
497 struct strbuf result;
498 unsigned gzip_request : 1;
499 unsigned initial_buffer : 1;
502 static size_t rpc_out(void *ptr, size_t eltsize,
503 size_t nmemb, void *buffer_)
505 size_t max = eltsize * nmemb;
506 struct rpc_state *rpc = buffer_;
507 size_t avail = rpc->len - rpc->pos;
509 if (!avail) {
510 rpc->initial_buffer = 0;
511 avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
512 if (!avail)
513 return 0;
514 rpc->pos = 0;
515 rpc->len = avail;
518 if (max < avail)
519 avail = max;
520 memcpy(ptr, rpc->buf + rpc->pos, avail);
521 rpc->pos += avail;
522 return avail;
525 #ifndef NO_CURL_IOCTL
526 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
528 struct rpc_state *rpc = clientp;
530 switch (cmd) {
531 case CURLIOCMD_NOP:
532 return CURLIOE_OK;
534 case CURLIOCMD_RESTARTREAD:
535 if (rpc->initial_buffer) {
536 rpc->pos = 0;
537 return CURLIOE_OK;
539 error("unable to rewind rpc post data - try increasing http.postBuffer");
540 return CURLIOE_FAILRESTART;
542 default:
543 return CURLIOE_UNKNOWNCMD;
546 #endif
548 struct rpc_in_data {
549 struct rpc_state *rpc;
550 struct active_request_slot *slot;
554 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
555 * from ptr.
557 static size_t rpc_in(char *ptr, size_t eltsize,
558 size_t nmemb, void *buffer_)
560 size_t size = eltsize * nmemb;
561 struct rpc_in_data *data = buffer_;
562 long response_code;
564 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
565 &response_code) != CURLE_OK)
566 return size;
567 if (response_code >= 300)
568 return size;
569 if (size)
570 data->rpc->any_written = 1;
571 write_or_die(data->rpc->in, ptr, size);
572 return size;
575 static int run_slot(struct active_request_slot *slot,
576 struct slot_results *results)
578 int err;
579 struct slot_results results_buf;
581 if (!results)
582 results = &results_buf;
584 err = run_one_slot(slot, results);
586 if (err != HTTP_OK && err != HTTP_REAUTH) {
587 struct strbuf msg = STRBUF_INIT;
588 if (results->http_code && results->http_code != 200)
589 strbuf_addf(&msg, "HTTP %ld", results->http_code);
590 if (results->curl_result != CURLE_OK) {
591 if (msg.len)
592 strbuf_addch(&msg, ' ');
593 strbuf_addf(&msg, "curl %d", results->curl_result);
594 if (curl_errorstr[0]) {
595 strbuf_addch(&msg, ' ');
596 strbuf_addstr(&msg, curl_errorstr);
599 error("RPC failed; %s", msg.buf);
600 strbuf_release(&msg);
603 return err;
606 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
608 struct active_request_slot *slot;
609 struct curl_slist *headers = http_copy_default_headers();
610 struct strbuf buf = STRBUF_INIT;
611 int err;
613 slot = get_active_slot();
615 headers = curl_slist_append(headers, rpc->hdr_content_type);
616 headers = curl_slist_append(headers, rpc->hdr_accept);
618 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
619 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
620 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
621 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
622 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
623 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
624 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
625 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
626 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
628 err = run_slot(slot, results);
630 curl_slist_free_all(headers);
631 strbuf_release(&buf);
632 return err;
635 static curl_off_t xcurl_off_t(size_t len) {
636 uintmax_t size = len;
637 if (size > maximum_signed_value_of_type(curl_off_t))
638 die("cannot handle pushes this big");
639 return (curl_off_t)size;
642 static int post_rpc(struct rpc_state *rpc)
644 struct active_request_slot *slot;
645 struct curl_slist *headers = http_copy_default_headers();
646 int use_gzip = rpc->gzip_request;
647 char *gzip_body = NULL;
648 size_t gzip_size = 0;
649 int err, large_request = 0;
650 int needs_100_continue = 0;
651 struct rpc_in_data rpc_in_data;
653 /* Try to load the entire request, if we can fit it into the
654 * allocated buffer space we can use HTTP/1.0 and avoid the
655 * chunked encoding mess.
657 while (1) {
658 size_t left = rpc->alloc - rpc->len;
659 char *buf = rpc->buf + rpc->len;
660 int n;
662 if (left < LARGE_PACKET_MAX) {
663 large_request = 1;
664 use_gzip = 0;
665 break;
668 n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
669 if (!n)
670 break;
671 rpc->len += n;
674 if (large_request) {
675 struct slot_results results;
677 do {
678 err = probe_rpc(rpc, &results);
679 if (err == HTTP_REAUTH)
680 credential_fill(&http_auth);
681 } while (err == HTTP_REAUTH);
682 if (err != HTTP_OK)
683 return -1;
685 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
686 needs_100_continue = 1;
689 headers = curl_slist_append(headers, rpc->hdr_content_type);
690 headers = curl_slist_append(headers, rpc->hdr_accept);
691 headers = curl_slist_append(headers, needs_100_continue ?
692 "Expect: 100-continue" : "Expect:");
694 /* Add the extra Git-Protocol header */
695 if (rpc->protocol_header)
696 headers = curl_slist_append(headers, rpc->protocol_header);
698 retry:
699 slot = get_active_slot();
701 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
702 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
703 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
704 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
706 if (large_request) {
707 /* The request body is large and the size cannot be predicted.
708 * We must use chunked encoding to send it.
710 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
711 rpc->initial_buffer = 1;
712 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
713 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
714 #ifndef NO_CURL_IOCTL
715 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
716 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
717 #endif
718 if (options.verbosity > 1) {
719 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
720 fflush(stderr);
723 } else if (gzip_body) {
725 * If we are looping to retry authentication, then the previous
726 * run will have set up the headers and gzip buffer already,
727 * and we just need to send it.
729 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
730 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
732 } else if (use_gzip && 1024 < rpc->len) {
733 /* The client backend isn't giving us compressed data so
734 * we can try to deflate it ourselves, this may save on
735 * the transfer time.
737 git_zstream stream;
738 int ret;
740 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
741 gzip_size = git_deflate_bound(&stream, rpc->len);
742 gzip_body = xmalloc(gzip_size);
744 stream.next_in = (unsigned char *)rpc->buf;
745 stream.avail_in = rpc->len;
746 stream.next_out = (unsigned char *)gzip_body;
747 stream.avail_out = gzip_size;
749 ret = git_deflate(&stream, Z_FINISH);
750 if (ret != Z_STREAM_END)
751 die("cannot deflate request; zlib deflate error %d", ret);
753 ret = git_deflate_end_gently(&stream);
754 if (ret != Z_OK)
755 die("cannot deflate request; zlib end error %d", ret);
757 gzip_size = stream.total_out;
759 headers = curl_slist_append(headers, "Content-Encoding: gzip");
760 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
761 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
763 if (options.verbosity > 1) {
764 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
765 rpc->service_name,
766 (unsigned long)rpc->len, (unsigned long)gzip_size);
767 fflush(stderr);
769 } else {
770 /* We know the complete request size in advance, use the
771 * more normal Content-Length approach.
773 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
774 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
775 if (options.verbosity > 1) {
776 fprintf(stderr, "POST %s (%lu bytes)\n",
777 rpc->service_name, (unsigned long)rpc->len);
778 fflush(stderr);
782 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
783 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
784 rpc_in_data.rpc = rpc;
785 rpc_in_data.slot = slot;
786 curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
787 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
790 rpc->any_written = 0;
791 err = run_slot(slot, NULL);
792 if (err == HTTP_REAUTH && !large_request) {
793 credential_fill(&http_auth);
794 goto retry;
796 if (err != HTTP_OK)
797 err = -1;
799 if (!rpc->any_written)
800 err = -1;
802 curl_slist_free_all(headers);
803 free(gzip_body);
804 return err;
807 static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
809 const char *svc = rpc->service_name;
810 struct strbuf buf = STRBUF_INIT;
811 struct strbuf *preamble = rpc->stdin_preamble;
812 struct child_process client = CHILD_PROCESS_INIT;
813 int err = 0;
815 client.in = -1;
816 client.out = -1;
817 client.git_cmd = 1;
818 client.argv = rpc->argv;
819 if (start_command(&client))
820 exit(1);
821 if (preamble)
822 write_or_die(client.in, preamble->buf, preamble->len);
823 if (heads)
824 write_or_die(client.in, heads->buf, heads->len);
826 rpc->alloc = http_post_buffer;
827 rpc->buf = xmalloc(rpc->alloc);
828 rpc->in = client.in;
829 rpc->out = client.out;
830 strbuf_init(&rpc->result, 0);
832 strbuf_addf(&buf, "%s%s", url.buf, svc);
833 rpc->service_url = strbuf_detach(&buf, NULL);
835 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
836 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
838 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
839 rpc->hdr_accept = strbuf_detach(&buf, NULL);
841 if (get_protocol_http_header(heads->version, &buf))
842 rpc->protocol_header = strbuf_detach(&buf, NULL);
843 else
844 rpc->protocol_header = NULL;
846 while (!err) {
847 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
848 if (!n)
849 break;
850 rpc->pos = 0;
851 rpc->len = n;
852 err |= post_rpc(rpc);
855 close(client.in);
856 client.in = -1;
857 if (!err) {
858 strbuf_read(&rpc->result, client.out, 0);
859 } else {
860 char buf[4096];
861 for (;;)
862 if (xread(client.out, buf, sizeof(buf)) <= 0)
863 break;
866 close(client.out);
867 client.out = -1;
869 err |= finish_command(&client);
870 free(rpc->service_url);
871 free(rpc->hdr_content_type);
872 free(rpc->hdr_accept);
873 free(rpc->protocol_header);
874 free(rpc->buf);
875 strbuf_release(&buf);
876 return err;
879 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
881 struct walker *walker;
882 char **targets;
883 int ret, i;
885 ALLOC_ARRAY(targets, nr_heads);
886 if (options.depth || options.deepen_since)
887 die("dumb http transport does not support shallow capabilities");
888 for (i = 0; i < nr_heads; i++)
889 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
891 walker = get_http_walker(url.buf);
892 walker->get_verbosely = options.verbosity >= 3;
893 walker->get_recover = 0;
894 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
895 walker_free(walker);
897 for (i = 0; i < nr_heads; i++)
898 free(targets[i]);
899 free(targets);
901 return ret ? error("fetch failed.") : 0;
904 static int fetch_git(struct discovery *heads,
905 int nr_heads, struct ref **to_fetch)
907 struct rpc_state rpc;
908 struct strbuf preamble = STRBUF_INIT;
909 int i, err;
910 struct argv_array args = ARGV_ARRAY_INIT;
912 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
913 "--stdin", "--lock-pack", NULL);
914 if (options.followtags)
915 argv_array_push(&args, "--include-tag");
916 if (options.thin)
917 argv_array_push(&args, "--thin");
918 if (options.verbosity >= 3)
919 argv_array_pushl(&args, "-v", "-v", NULL);
920 if (options.check_self_contained_and_connected)
921 argv_array_push(&args, "--check-self-contained-and-connected");
922 if (options.cloning)
923 argv_array_push(&args, "--cloning");
924 if (options.update_shallow)
925 argv_array_push(&args, "--update-shallow");
926 if (!options.progress)
927 argv_array_push(&args, "--no-progress");
928 if (options.depth)
929 argv_array_pushf(&args, "--depth=%lu", options.depth);
930 if (options.deepen_since)
931 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
932 for (i = 0; i < options.deepen_not.nr; i++)
933 argv_array_pushf(&args, "--shallow-exclude=%s",
934 options.deepen_not.items[i].string);
935 if (options.deepen_relative && options.depth)
936 argv_array_push(&args, "--deepen-relative");
937 if (options.from_promisor)
938 argv_array_push(&args, "--from-promisor");
939 if (options.no_dependents)
940 argv_array_push(&args, "--no-dependents");
941 if (options.filter)
942 argv_array_pushf(&args, "--filter=%s", options.filter);
943 argv_array_push(&args, url.buf);
945 for (i = 0; i < nr_heads; i++) {
946 struct ref *ref = to_fetch[i];
947 if (!*ref->name)
948 die("cannot fetch by sha1 over smart http");
949 packet_buf_write(&preamble, "%s %s\n",
950 oid_to_hex(&ref->old_oid), ref->name);
952 packet_buf_flush(&preamble);
954 memset(&rpc, 0, sizeof(rpc));
955 rpc.service_name = "git-upload-pack",
956 rpc.argv = args.argv;
957 rpc.stdin_preamble = &preamble;
958 rpc.gzip_request = 1;
960 err = rpc_service(&rpc, heads);
961 if (rpc.result.len)
962 write_or_die(1, rpc.result.buf, rpc.result.len);
963 strbuf_release(&rpc.result);
964 strbuf_release(&preamble);
965 argv_array_clear(&args);
966 return err;
969 static int fetch(int nr_heads, struct ref **to_fetch)
971 struct discovery *d = discover_refs("git-upload-pack", 0);
972 if (d->proto_git)
973 return fetch_git(d, nr_heads, to_fetch);
974 else
975 return fetch_dumb(nr_heads, to_fetch);
978 static void parse_fetch(struct strbuf *buf)
980 struct ref **to_fetch = NULL;
981 struct ref *list_head = NULL;
982 struct ref **list = &list_head;
983 int alloc_heads = 0, nr_heads = 0;
985 do {
986 const char *p;
987 if (skip_prefix(buf->buf, "fetch ", &p)) {
988 const char *name;
989 struct ref *ref;
990 struct object_id old_oid;
992 if (get_oid_hex(p, &old_oid))
993 die("protocol error: expected sha/ref, got %s'", p);
994 if (p[GIT_SHA1_HEXSZ] == ' ')
995 name = p + GIT_SHA1_HEXSZ + 1;
996 else if (!p[GIT_SHA1_HEXSZ])
997 name = "";
998 else
999 die("protocol error: expected sha/ref, got %s'", p);
1001 ref = alloc_ref(name);
1002 oidcpy(&ref->old_oid, &old_oid);
1004 *list = ref;
1005 list = &ref->next;
1007 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1008 to_fetch[nr_heads++] = ref;
1010 else
1011 die("http transport does not support %s", buf->buf);
1013 strbuf_reset(buf);
1014 if (strbuf_getline_lf(buf, stdin) == EOF)
1015 return;
1016 if (!*buf->buf)
1017 break;
1018 } while (1);
1020 if (fetch(nr_heads, to_fetch))
1021 exit(128); /* error already reported */
1022 free_refs(list_head);
1023 free(to_fetch);
1025 printf("\n");
1026 fflush(stdout);
1027 strbuf_reset(buf);
1030 static int push_dav(int nr_spec, char **specs)
1032 struct child_process child = CHILD_PROCESS_INIT;
1033 size_t i;
1035 child.git_cmd = 1;
1036 argv_array_push(&child.args, "http-push");
1037 argv_array_push(&child.args, "--helper-status");
1038 if (options.dry_run)
1039 argv_array_push(&child.args, "--dry-run");
1040 if (options.verbosity > 1)
1041 argv_array_push(&child.args, "--verbose");
1042 argv_array_push(&child.args, url.buf);
1043 for (i = 0; i < nr_spec; i++)
1044 argv_array_push(&child.args, specs[i]);
1046 if (run_command(&child))
1047 die("git-http-push failed");
1048 return 0;
1051 static int push_git(struct discovery *heads, int nr_spec, char **specs)
1053 struct rpc_state rpc;
1054 int i, err;
1055 struct argv_array args;
1056 struct string_list_item *cas_option;
1057 struct strbuf preamble = STRBUF_INIT;
1059 argv_array_init(&args);
1060 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1061 NULL);
1063 if (options.thin)
1064 argv_array_push(&args, "--thin");
1065 if (options.dry_run)
1066 argv_array_push(&args, "--dry-run");
1067 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1068 argv_array_push(&args, "--signed=yes");
1069 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1070 argv_array_push(&args, "--signed=if-asked");
1071 if (options.verbosity == 0)
1072 argv_array_push(&args, "--quiet");
1073 else if (options.verbosity > 1)
1074 argv_array_push(&args, "--verbose");
1075 for (i = 0; i < options.push_options.nr; i++)
1076 argv_array_pushf(&args, "--push-option=%s",
1077 options.push_options.items[i].string);
1078 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1079 for_each_string_list_item(cas_option, &cas_options)
1080 argv_array_push(&args, cas_option->string);
1081 argv_array_push(&args, url.buf);
1083 argv_array_push(&args, "--stdin");
1084 for (i = 0; i < nr_spec; i++)
1085 packet_buf_write(&preamble, "%s\n", specs[i]);
1086 packet_buf_flush(&preamble);
1088 memset(&rpc, 0, sizeof(rpc));
1089 rpc.service_name = "git-receive-pack",
1090 rpc.argv = args.argv;
1091 rpc.stdin_preamble = &preamble;
1093 err = rpc_service(&rpc, heads);
1094 if (rpc.result.len)
1095 write_or_die(1, rpc.result.buf, rpc.result.len);
1096 strbuf_release(&rpc.result);
1097 strbuf_release(&preamble);
1098 argv_array_clear(&args);
1099 return err;
1102 static int push(int nr_spec, char **specs)
1104 struct discovery *heads = discover_refs("git-receive-pack", 1);
1105 int ret;
1107 if (heads->proto_git)
1108 ret = push_git(heads, nr_spec, specs);
1109 else
1110 ret = push_dav(nr_spec, specs);
1111 free_discovery(heads);
1112 return ret;
1115 static void parse_push(struct strbuf *buf)
1117 char **specs = NULL;
1118 int alloc_spec = 0, nr_spec = 0, i, ret;
1120 do {
1121 if (starts_with(buf->buf, "push ")) {
1122 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1123 specs[nr_spec++] = xstrdup(buf->buf + 5);
1125 else
1126 die("http transport does not support %s", buf->buf);
1128 strbuf_reset(buf);
1129 if (strbuf_getline_lf(buf, stdin) == EOF)
1130 goto free_specs;
1131 if (!*buf->buf)
1132 break;
1133 } while (1);
1135 ret = push(nr_spec, specs);
1136 printf("\n");
1137 fflush(stdout);
1139 if (ret)
1140 exit(128); /* error already reported */
1142 free_specs:
1143 for (i = 0; i < nr_spec; i++)
1144 free(specs[i]);
1145 free(specs);
1149 * Used to represent the state of a connection to an HTTP server when
1150 * communicating using git's wire-protocol version 2.
1152 struct proxy_state {
1153 char *service_name;
1154 char *service_url;
1155 struct curl_slist *headers;
1156 struct strbuf request_buffer;
1157 int in;
1158 int out;
1159 struct packet_reader reader;
1160 size_t pos;
1161 int seen_flush;
1164 static void proxy_state_init(struct proxy_state *p, const char *service_name,
1165 enum protocol_version version)
1167 struct strbuf buf = STRBUF_INIT;
1169 memset(p, 0, sizeof(*p));
1170 p->service_name = xstrdup(service_name);
1172 p->in = 0;
1173 p->out = 1;
1174 strbuf_init(&p->request_buffer, 0);
1176 strbuf_addf(&buf, "%s%s", url.buf, p->service_name);
1177 p->service_url = strbuf_detach(&buf, NULL);
1179 p->headers = http_copy_default_headers();
1181 strbuf_addf(&buf, "Content-Type: application/x-%s-request", p->service_name);
1182 p->headers = curl_slist_append(p->headers, buf.buf);
1183 strbuf_reset(&buf);
1185 strbuf_addf(&buf, "Accept: application/x-%s-result", p->service_name);
1186 p->headers = curl_slist_append(p->headers, buf.buf);
1187 strbuf_reset(&buf);
1189 p->headers = curl_slist_append(p->headers, "Transfer-Encoding: chunked");
1191 /* Add the Git-Protocol header */
1192 if (get_protocol_http_header(version, &buf))
1193 p->headers = curl_slist_append(p->headers, buf.buf);
1195 packet_reader_init(&p->reader, p->in, NULL, 0,
1196 PACKET_READ_GENTLE_ON_EOF);
1198 strbuf_release(&buf);
1201 static void proxy_state_clear(struct proxy_state *p)
1203 free(p->service_name);
1204 free(p->service_url);
1205 curl_slist_free_all(p->headers);
1206 strbuf_release(&p->request_buffer);
1210 * CURLOPT_READFUNCTION callback function.
1211 * Attempts to copy over a single packet-line at a time into the
1212 * curl provided buffer.
1214 static size_t proxy_in(char *buffer, size_t eltsize,
1215 size_t nmemb, void *userdata)
1217 size_t max;
1218 struct proxy_state *p = userdata;
1219 size_t avail = p->request_buffer.len - p->pos;
1222 if (eltsize != 1)
1223 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1224 (uintmax_t)eltsize);
1225 max = nmemb;
1227 if (!avail) {
1228 if (p->seen_flush) {
1229 p->seen_flush = 0;
1230 return 0;
1233 strbuf_reset(&p->request_buffer);
1234 switch (packet_reader_read(&p->reader)) {
1235 case PACKET_READ_EOF:
1236 die("unexpected EOF when reading from parent process");
1237 case PACKET_READ_NORMAL:
1238 packet_buf_write_len(&p->request_buffer, p->reader.line,
1239 p->reader.pktlen);
1240 break;
1241 case PACKET_READ_DELIM:
1242 packet_buf_delim(&p->request_buffer);
1243 break;
1244 case PACKET_READ_FLUSH:
1245 packet_buf_flush(&p->request_buffer);
1246 p->seen_flush = 1;
1247 break;
1249 p->pos = 0;
1250 avail = p->request_buffer.len;
1253 if (max < avail)
1254 avail = max;
1255 memcpy(buffer, p->request_buffer.buf + p->pos, avail);
1256 p->pos += avail;
1257 return avail;
1260 static size_t proxy_out(char *buffer, size_t eltsize,
1261 size_t nmemb, void *userdata)
1263 size_t size;
1264 struct proxy_state *p = userdata;
1266 if (eltsize != 1)
1267 BUG("curl read callback called with size = %"PRIuMAX" != 1",
1268 (uintmax_t)eltsize);
1269 size = nmemb;
1271 write_or_die(p->out, buffer, size);
1272 return size;
1275 /* Issues a request to the HTTP server configured in `p` */
1276 static int proxy_request(struct proxy_state *p)
1278 struct active_request_slot *slot;
1280 slot = get_active_slot();
1282 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
1283 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1284 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
1285 curl_easy_setopt(slot->curl, CURLOPT_URL, p->service_url);
1286 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, p->headers);
1288 /* Setup function to read request from client */
1289 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, proxy_in);
1290 curl_easy_setopt(slot->curl, CURLOPT_READDATA, p);
1292 /* Setup function to write server response to client */
1293 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, proxy_out);
1294 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, p);
1296 if (run_slot(slot, NULL) != HTTP_OK)
1297 return -1;
1299 return 0;
1302 static int stateless_connect(const char *service_name)
1304 struct discovery *discover;
1305 struct proxy_state p;
1308 * Run the info/refs request and see if the server supports protocol
1309 * v2. If and only if the server supports v2 can we successfully
1310 * establish a stateless connection, otherwise we need to tell the
1311 * client to fallback to using other transport helper functions to
1312 * complete their request.
1314 discover = discover_refs(service_name, 0);
1315 if (discover->version != protocol_v2) {
1316 printf("fallback\n");
1317 fflush(stdout);
1318 return -1;
1319 } else {
1320 /* Stateless Connection established */
1321 printf("\n");
1322 fflush(stdout);
1325 proxy_state_init(&p, service_name, discover->version);
1328 * Dump the capability listing that we got from the server earlier
1329 * during the info/refs request.
1331 write_or_die(p.out, discover->buf, discover->len);
1333 /* Peek the next packet line. Until we see EOF keep sending POSTs */
1334 while (packet_reader_peek(&p.reader) != PACKET_READ_EOF) {
1335 if (proxy_request(&p)) {
1336 /* We would have an err here */
1337 break;
1341 proxy_state_clear(&p);
1342 return 0;
1345 int cmd_main(int argc, const char **argv)
1347 struct strbuf buf = STRBUF_INIT;
1348 int nongit;
1350 setup_git_directory_gently(&nongit);
1351 if (argc < 2) {
1352 error("remote-curl: usage: git remote-curl <remote> [<url>]");
1353 return 1;
1356 options.verbosity = 1;
1357 options.progress = !!isatty(2);
1358 options.thin = 1;
1359 string_list_init(&options.deepen_not, 1);
1360 string_list_init(&options.push_options, 1);
1362 remote = remote_get(argv[1]);
1364 if (argc > 2) {
1365 end_url_with_slash(&url, argv[2]);
1366 } else {
1367 end_url_with_slash(&url, remote->url[0]);
1370 http_init(remote, url.buf, 0);
1372 do {
1373 const char *arg;
1375 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1376 if (ferror(stdin))
1377 error("remote-curl: error reading command stream from git");
1378 return 1;
1380 if (buf.len == 0)
1381 break;
1382 if (starts_with(buf.buf, "fetch ")) {
1383 if (nongit)
1384 die("remote-curl: fetch attempted without a local repo");
1385 parse_fetch(&buf);
1387 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1388 int for_push = !!strstr(buf.buf + 4, "for-push");
1389 output_refs(get_refs(for_push));
1391 } else if (starts_with(buf.buf, "push ")) {
1392 parse_push(&buf);
1394 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1395 char *value = strchr(arg, ' ');
1396 int result;
1398 if (value)
1399 *value++ = '\0';
1400 else
1401 value = "true";
1403 result = set_option(arg, value);
1404 if (!result)
1405 printf("ok\n");
1406 else if (result < 0)
1407 printf("error invalid value\n");
1408 else
1409 printf("unsupported\n");
1410 fflush(stdout);
1412 } else if (!strcmp(buf.buf, "capabilities")) {
1413 printf("stateless-connect\n");
1414 printf("fetch\n");
1415 printf("option\n");
1416 printf("push\n");
1417 printf("check-connectivity\n");
1418 printf("\n");
1419 fflush(stdout);
1420 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1421 if (!stateless_connect(arg))
1422 break;
1423 } else {
1424 error("remote-curl: unknown command '%s' from git", buf.buf);
1425 return 1;
1427 strbuf_reset(&buf);
1428 } while (1);
1430 http_cleanup();
1432 return 0;