pack-bitmap.h: remove magic number
[git.git] / remote-curl.c
blob051f26629d7b9f6679e47f6dc353f09c16c377ae
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"
19 #include "transport.h"
21 static struct remote *remote;
22 /* always ends with a trailing slash */
23 static struct strbuf url = STRBUF_INIT;
25 struct options {
26 int verbosity;
27 unsigned long depth;
28 char *deepen_since;
29 struct string_list deepen_not;
30 struct string_list push_options;
31 char *filter;
32 unsigned progress : 1,
33 check_self_contained_and_connected : 1,
34 cloning : 1,
35 update_shallow : 1,
36 followtags : 1,
37 dry_run : 1,
38 thin : 1,
39 /* One of the SEND_PACK_PUSH_CERT_* constants. */
40 push_cert : 2,
41 deepen_relative : 1,
42 from_promisor : 1,
43 no_dependents : 1;
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")) {
51 char *end;
52 int v = strtol(value, &end, 10);
53 if (value == end || *end)
54 return -1;
55 options.verbosity = v;
56 return 0;
58 else if (!strcmp(name, "progress")) {
59 if (!strcmp(value, "true"))
60 options.progress = 1;
61 else if (!strcmp(value, "false"))
62 options.progress = 0;
63 else
64 return -1;
65 return 0;
67 else if (!strcmp(name, "depth")) {
68 char *end;
69 unsigned long v = strtoul(value, &end, 10);
70 if (value == end || *end)
71 return -1;
72 options.depth = v;
73 return 0;
75 else if (!strcmp(name, "deepen-since")) {
76 options.deepen_since = xstrdup(value);
77 return 0;
79 else if (!strcmp(name, "deepen-not")) {
80 string_list_append(&options.deepen_not, value);
81 return 0;
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;
88 else
89 return -1;
90 return 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;
97 else
98 return -1;
99 return 0;
101 else if (!strcmp(name, "dry-run")) {
102 if (!strcmp(value, "true"))
103 options.dry_run = 1;
104 else if (!strcmp(value, "false"))
105 options.dry_run = 0;
106 else
107 return -1;
108 return 0;
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;
115 else
116 return -1;
117 return 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);
124 return 0;
125 } else if (!strcmp(name, "cloning")) {
126 if (!strcmp(value, "true"))
127 options.cloning = 1;
128 else if (!strcmp(value, "false"))
129 options.cloning = 0;
130 else
131 return -1;
132 return 0;
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;
138 else
139 return -1;
140 return 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;
148 else
149 return -1;
150 return 0;
151 } else if (!strcmp(name, "push-option")) {
152 if (*value != '"')
153 string_list_append(&options.push_options, value);
154 else {
155 struct strbuf unquoted = STRBUF_INIT;
156 if (unquote_c_style(&unquoted, value, NULL) < 0)
157 die(_("invalid quoting in push-option value: '%s'"), value);
158 string_list_append_nodup(&options.push_options,
159 strbuf_detach(&unquoted, NULL));
161 return 0;
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;
171 else
172 return -1;
173 return 0;
174 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
175 } else if (!strcmp(name, "from-promisor")) {
176 options.from_promisor = 1;
177 return 0;
178 } else if (!strcmp(name, "no-dependents")) {
179 options.no_dependents = 1;
180 return 0;
181 } else if (!strcmp(name, "filter")) {
182 options.filter = xstrdup(value);
183 return 0;
184 } else {
185 return 1 /* unsupported */;
189 struct discovery {
190 char *service;
191 char *buf_alloc;
192 char *buf;
193 size_t len;
194 struct ref *refs;
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 |
209 PACKET_READ_DIE_ON_ERR_PACKET);
211 heads->version = discover_version(&reader);
212 switch (heads->version) {
213 case protocol_v2:
215 * Do nothing. This isn't a list of refs but rather a
216 * capability advertisement. Client would have run
217 * 'stateless-connect' so we'll dump this capability listing
218 * and let them request the refs themselves.
220 break;
221 case protocol_v1:
222 case protocol_v0:
223 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
224 NULL, &heads->shallow);
225 break;
226 case protocol_unknown_version:
227 BUG("unknown protocol version");
230 return list;
233 static struct ref *parse_info_refs(struct discovery *heads)
235 char *data, *start, *mid;
236 char *ref_name;
237 int i = 0;
239 struct ref *refs = NULL;
240 struct ref *ref = NULL;
241 struct ref *last_ref = NULL;
243 data = heads->buf;
244 start = NULL;
245 mid = data;
246 while (i < heads->len) {
247 if (!start) {
248 start = &data[i];
250 if (data[i] == '\t')
251 mid = &data[i];
252 if (data[i] == '\n') {
253 if (mid - start != the_hash_algo->hexsz)
254 die(_("%sinfo/refs not valid: is this a git repository?"),
255 transport_anonymize_url(url.buf));
256 data[i] = 0;
257 ref_name = mid + 1;
258 ref = alloc_ref(ref_name);
259 get_oid_hex(start, &ref->old_oid);
260 if (!refs)
261 refs = ref;
262 if (last_ref)
263 last_ref->next = ref;
264 last_ref = ref;
265 start = NULL;
267 i++;
270 ref = alloc_ref("HEAD");
271 if (!http_fetch_ref(url.buf, ref) &&
272 !resolve_remote_symref(ref, refs)) {
273 ref->next = refs;
274 refs = ref;
275 } else {
276 free(ref);
279 return refs;
282 static void free_discovery(struct discovery *d)
284 if (d) {
285 if (d == last_discovery)
286 last_discovery = NULL;
287 free(d->shallow.oid);
288 free(d->buf_alloc);
289 free_refs(d->refs);
290 free(d->service);
291 free(d);
295 static int show_http_message(struct strbuf *type, struct strbuf *charset,
296 struct strbuf *msg)
298 const char *p, *eol;
301 * We only show text/plain parts, as other types are likely
302 * to be ugly to look at on the user's terminal.
304 if (strcmp(type->buf, "text/plain"))
305 return -1;
306 if (charset->len)
307 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
309 strbuf_trim(msg);
310 if (!msg->len)
311 return -1;
313 p = msg->buf;
314 do {
315 eol = strchrnul(p, '\n');
316 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
317 p = eol + 1;
318 } while(*eol);
319 return 0;
322 static int get_protocol_http_header(enum protocol_version version,
323 struct strbuf *header)
325 if (version > 0) {
326 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
327 version);
329 return 1;
332 return 0;
335 static void check_smart_http(struct discovery *d, const char *service,
336 struct strbuf *type)
338 const char *p;
339 struct packet_reader reader;
342 * If we don't see x-$service-advertisement, then it's not smart-http.
343 * But once we do, we commit to it and assume any other protocol
344 * violations are hard errors.
346 if (!skip_prefix(type->buf, "application/x-", &p) ||
347 !skip_prefix(p, service, &p) ||
348 strcmp(p, "-advertisement"))
349 return;
351 packet_reader_init(&reader, -1, d->buf, d->len,
352 PACKET_READ_CHOMP_NEWLINE |
353 PACKET_READ_DIE_ON_ERR_PACKET);
354 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
355 die(_("invalid server response; expected service, got flush packet"));
357 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
359 * The header can include additional metadata lines, up
360 * until a packet flush marker. Ignore these now, but
361 * in the future we might start to scan them.
363 for (;;) {
364 packet_reader_read(&reader);
365 if (reader.pktlen <= 0) {
366 break;
371 * v0 smart http; callers expect us to soak up the
372 * service and header packets
374 d->buf = reader.src_buffer;
375 d->len = reader.src_len;
376 d->proto_git = 1;
378 } else if (!strcmp(reader.line, "version 2")) {
380 * v2 smart http; do not consume version packet, which will
381 * be handled elsewhere.
383 d->proto_git = 1;
385 } else {
386 die(_("invalid server response; got '%s'"), reader.line);
390 static struct discovery *discover_refs(const char *service, int for_push)
392 struct strbuf type = STRBUF_INIT;
393 struct strbuf charset = STRBUF_INIT;
394 struct strbuf buffer = STRBUF_INIT;
395 struct strbuf refs_url = STRBUF_INIT;
396 struct strbuf effective_url = STRBUF_INIT;
397 struct strbuf protocol_header = STRBUF_INIT;
398 struct string_list extra_headers = STRING_LIST_INIT_DUP;
399 struct discovery *last = last_discovery;
400 int http_ret, maybe_smart = 0;
401 struct http_get_options http_options;
402 enum protocol_version version = get_protocol_version_config();
404 if (last && !strcmp(service, last->service))
405 return last;
406 free_discovery(last);
408 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
409 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
410 git_env_bool("GIT_SMART_HTTP", 1)) {
411 maybe_smart = 1;
412 if (!strchr(url.buf, '?'))
413 strbuf_addch(&refs_url, '?');
414 else
415 strbuf_addch(&refs_url, '&');
416 strbuf_addf(&refs_url, "service=%s", service);
420 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
421 * to perform a push, then fallback to v0 since the client doesn't know
422 * how to push yet using v2.
424 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
425 version = protocol_v0;
427 /* Add the extra Git-Protocol header */
428 if (get_protocol_http_header(version, &protocol_header))
429 string_list_append(&extra_headers, protocol_header.buf);
431 memset(&http_options, 0, sizeof(http_options));
432 http_options.content_type = &type;
433 http_options.charset = &charset;
434 http_options.effective_url = &effective_url;
435 http_options.base_url = &url;
436 http_options.extra_headers = &extra_headers;
437 http_options.initial_request = 1;
438 http_options.no_cache = 1;
440 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
441 switch (http_ret) {
442 case HTTP_OK:
443 break;
444 case HTTP_MISSING_TARGET:
445 show_http_message(&type, &charset, &buffer);
446 die(_("repository '%s' not found"),
447 transport_anonymize_url(url.buf));
448 case HTTP_NOAUTH:
449 show_http_message(&type, &charset, &buffer);
450 die(_("Authentication failed for '%s'"),
451 transport_anonymize_url(url.buf));
452 default:
453 show_http_message(&type, &charset, &buffer);
454 die(_("unable to access '%s': %s"),
455 transport_anonymize_url(url.buf), curl_errorstr);
458 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
459 char *u = transport_anonymize_url(url.buf);
460 warning(_("redirecting to %s"), u);
461 free(u);
464 last= xcalloc(1, sizeof(*last_discovery));
465 last->service = xstrdup(service);
466 last->buf_alloc = strbuf_detach(&buffer, &last->len);
467 last->buf = last->buf_alloc;
469 if (maybe_smart)
470 check_smart_http(last, service, &type);
472 if (last->proto_git)
473 last->refs = parse_git_refs(last, for_push);
474 else
475 last->refs = parse_info_refs(last);
477 strbuf_release(&refs_url);
478 strbuf_release(&type);
479 strbuf_release(&charset);
480 strbuf_release(&effective_url);
481 strbuf_release(&buffer);
482 strbuf_release(&protocol_header);
483 string_list_clear(&extra_headers, 0);
484 last_discovery = last;
485 return last;
488 static struct ref *get_refs(int for_push)
490 struct discovery *heads;
492 if (for_push)
493 heads = discover_refs("git-receive-pack", for_push);
494 else
495 heads = discover_refs("git-upload-pack", for_push);
497 return heads->refs;
500 static void output_refs(struct ref *refs)
502 struct ref *posn;
503 for (posn = refs; posn; posn = posn->next) {
504 if (posn->symref)
505 printf("@%s %s\n", posn->symref, posn->name);
506 else
507 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
509 printf("\n");
510 fflush(stdout);
513 struct rpc_state {
514 const char *service_name;
515 char *service_url;
516 char *hdr_content_type;
517 char *hdr_accept;
518 char *protocol_header;
519 char *buf;
520 size_t alloc;
521 size_t len;
522 size_t pos;
523 int in;
524 int out;
525 int any_written;
526 unsigned gzip_request : 1;
527 unsigned initial_buffer : 1;
530 * Whenever a pkt-line is read into buf, append the 4 characters
531 * denoting its length before appending the payload.
533 unsigned write_line_lengths : 1;
536 * Used by rpc_out; initialize to 0. This is true if a flush has been
537 * read, but the corresponding line length (if write_line_lengths is
538 * true) and EOF have not been sent to libcurl. Since each flush marks
539 * the end of a request, each flush must be completely sent before any
540 * further reading occurs.
542 unsigned flush_read_but_not_sent : 1;
546 * Appends the result of reading from rpc->out to the string represented by
547 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
548 * enough space, 0 otherwise.
550 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
551 * hexadecimal string before appending the result described above.
553 * Writes the total number of bytes appended into appended.
555 static int rpc_read_from_out(struct rpc_state *rpc, int options,
556 size_t *appended,
557 enum packet_read_status *status) {
558 size_t left;
559 char *buf;
560 int pktlen_raw;
562 if (rpc->write_line_lengths) {
563 left = rpc->alloc - rpc->len - 4;
564 buf = rpc->buf + rpc->len + 4;
565 } else {
566 left = rpc->alloc - rpc->len;
567 buf = rpc->buf + rpc->len;
570 if (left < LARGE_PACKET_MAX)
571 return 0;
573 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
574 left, &pktlen_raw, options);
575 if (*status != PACKET_READ_EOF) {
576 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
577 rpc->len += *appended;
580 if (rpc->write_line_lengths) {
581 switch (*status) {
582 case PACKET_READ_EOF:
583 if (!(options & PACKET_READ_GENTLE_ON_EOF))
584 die(_("shouldn't have EOF when not gentle on EOF"));
585 break;
586 case PACKET_READ_NORMAL:
587 set_packet_header(buf - 4, *appended);
588 break;
589 case PACKET_READ_DELIM:
590 memcpy(buf - 4, "0001", 4);
591 break;
592 case PACKET_READ_FLUSH:
593 memcpy(buf - 4, "0000", 4);
594 break;
598 return 1;
601 static size_t rpc_out(void *ptr, size_t eltsize,
602 size_t nmemb, void *buffer_)
604 size_t max = eltsize * nmemb;
605 struct rpc_state *rpc = buffer_;
606 size_t avail = rpc->len - rpc->pos;
607 enum packet_read_status status;
609 if (!avail) {
610 rpc->initial_buffer = 0;
611 rpc->len = 0;
612 rpc->pos = 0;
613 if (!rpc->flush_read_but_not_sent) {
614 if (!rpc_read_from_out(rpc, 0, &avail, &status))
615 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
616 if (status == PACKET_READ_FLUSH)
617 rpc->flush_read_but_not_sent = 1;
620 * If flush_read_but_not_sent is true, we have already read one
621 * full request but have not fully sent it + EOF, which is why
622 * we need to refrain from reading.
625 if (rpc->flush_read_but_not_sent) {
626 if (!avail) {
628 * The line length either does not need to be sent at
629 * all or has already been completely sent. Now we can
630 * return 0, indicating EOF, meaning that the flush has
631 * been fully sent.
633 rpc->flush_read_but_not_sent = 0;
634 return 0;
637 * If avail is non-zerp, the line length for the flush still
638 * hasn't been fully sent. Proceed with sending the line
639 * length.
643 if (max < avail)
644 avail = max;
645 memcpy(ptr, rpc->buf + rpc->pos, avail);
646 rpc->pos += avail;
647 return avail;
650 #ifndef NO_CURL_IOCTL
651 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
653 struct rpc_state *rpc = clientp;
655 switch (cmd) {
656 case CURLIOCMD_NOP:
657 return CURLIOE_OK;
659 case CURLIOCMD_RESTARTREAD:
660 if (rpc->initial_buffer) {
661 rpc->pos = 0;
662 return CURLIOE_OK;
664 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
665 return CURLIOE_FAILRESTART;
667 default:
668 return CURLIOE_UNKNOWNCMD;
671 #endif
673 struct rpc_in_data {
674 struct rpc_state *rpc;
675 struct active_request_slot *slot;
679 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
680 * from ptr.
682 static size_t rpc_in(char *ptr, size_t eltsize,
683 size_t nmemb, void *buffer_)
685 size_t size = eltsize * nmemb;
686 struct rpc_in_data *data = buffer_;
687 long response_code;
689 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
690 &response_code) != CURLE_OK)
691 return size;
692 if (response_code >= 300)
693 return size;
694 if (size)
695 data->rpc->any_written = 1;
696 write_or_die(data->rpc->in, ptr, size);
697 return size;
700 static int run_slot(struct active_request_slot *slot,
701 struct slot_results *results)
703 int err;
704 struct slot_results results_buf;
706 if (!results)
707 results = &results_buf;
709 err = run_one_slot(slot, results);
711 if (err != HTTP_OK && err != HTTP_REAUTH) {
712 struct strbuf msg = STRBUF_INIT;
713 if (results->http_code && results->http_code != 200)
714 strbuf_addf(&msg, "HTTP %ld", results->http_code);
715 if (results->curl_result != CURLE_OK) {
716 if (msg.len)
717 strbuf_addch(&msg, ' ');
718 strbuf_addf(&msg, "curl %d", results->curl_result);
719 if (curl_errorstr[0]) {
720 strbuf_addch(&msg, ' ');
721 strbuf_addstr(&msg, curl_errorstr);
724 error(_("RPC failed; %s"), msg.buf);
725 strbuf_release(&msg);
728 return err;
731 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
733 struct active_request_slot *slot;
734 struct curl_slist *headers = http_copy_default_headers();
735 struct strbuf buf = STRBUF_INIT;
736 int err;
738 slot = get_active_slot();
740 headers = curl_slist_append(headers, rpc->hdr_content_type);
741 headers = curl_slist_append(headers, rpc->hdr_accept);
743 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
744 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
745 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
746 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
747 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
748 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
749 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
750 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
751 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
753 err = run_slot(slot, results);
755 curl_slist_free_all(headers);
756 strbuf_release(&buf);
757 return err;
760 static curl_off_t xcurl_off_t(size_t len)
762 uintmax_t size = len;
763 if (size > maximum_signed_value_of_type(curl_off_t))
764 die(_("cannot handle pushes this big"));
765 return (curl_off_t)size;
769 * If flush_received is true, do not attempt to read any more; just use what's
770 * in rpc->buf.
772 static int post_rpc(struct rpc_state *rpc, int flush_received)
774 struct active_request_slot *slot;
775 struct curl_slist *headers = http_copy_default_headers();
776 int use_gzip = rpc->gzip_request;
777 char *gzip_body = NULL;
778 size_t gzip_size = 0;
779 int err, large_request = 0;
780 int needs_100_continue = 0;
781 struct rpc_in_data rpc_in_data;
783 /* Try to load the entire request, if we can fit it into the
784 * allocated buffer space we can use HTTP/1.0 and avoid the
785 * chunked encoding mess.
787 if (!flush_received) {
788 while (1) {
789 size_t n;
790 enum packet_read_status status;
792 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
793 large_request = 1;
794 use_gzip = 0;
795 break;
797 if (status == PACKET_READ_FLUSH)
798 break;
802 if (large_request) {
803 struct slot_results results;
805 do {
806 err = probe_rpc(rpc, &results);
807 if (err == HTTP_REAUTH)
808 credential_fill(&http_auth);
809 } while (err == HTTP_REAUTH);
810 if (err != HTTP_OK)
811 return -1;
813 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
814 needs_100_continue = 1;
817 headers = curl_slist_append(headers, rpc->hdr_content_type);
818 headers = curl_slist_append(headers, rpc->hdr_accept);
819 headers = curl_slist_append(headers, needs_100_continue ?
820 "Expect: 100-continue" : "Expect:");
822 /* Add the extra Git-Protocol header */
823 if (rpc->protocol_header)
824 headers = curl_slist_append(headers, rpc->protocol_header);
826 retry:
827 slot = get_active_slot();
829 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
830 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
831 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
832 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
834 if (large_request) {
835 /* The request body is large and the size cannot be predicted.
836 * We must use chunked encoding to send it.
838 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
839 rpc->initial_buffer = 1;
840 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
841 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
842 #ifndef NO_CURL_IOCTL
843 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
844 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
845 #endif
846 if (options.verbosity > 1) {
847 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
848 fflush(stderr);
851 } else if (gzip_body) {
853 * If we are looping to retry authentication, then the previous
854 * run will have set up the headers and gzip buffer already,
855 * and we just need to send it.
857 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
858 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
860 } else if (use_gzip && 1024 < rpc->len) {
861 /* The client backend isn't giving us compressed data so
862 * we can try to deflate it ourselves, this may save on
863 * the transfer time.
865 git_zstream stream;
866 int ret;
868 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
869 gzip_size = git_deflate_bound(&stream, rpc->len);
870 gzip_body = xmalloc(gzip_size);
872 stream.next_in = (unsigned char *)rpc->buf;
873 stream.avail_in = rpc->len;
874 stream.next_out = (unsigned char *)gzip_body;
875 stream.avail_out = gzip_size;
877 ret = git_deflate(&stream, Z_FINISH);
878 if (ret != Z_STREAM_END)
879 die(_("cannot deflate request; zlib deflate error %d"), ret);
881 ret = git_deflate_end_gently(&stream);
882 if (ret != Z_OK)
883 die(_("cannot deflate request; zlib end error %d"), ret);
885 gzip_size = stream.total_out;
887 headers = curl_slist_append(headers, "Content-Encoding: gzip");
888 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
889 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
891 if (options.verbosity > 1) {
892 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
893 rpc->service_name,
894 (unsigned long)rpc->len, (unsigned long)gzip_size);
895 fflush(stderr);
897 } else {
898 /* We know the complete request size in advance, use the
899 * more normal Content-Length approach.
901 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
902 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
903 if (options.verbosity > 1) {
904 fprintf(stderr, "POST %s (%lu bytes)\n",
905 rpc->service_name, (unsigned long)rpc->len);
906 fflush(stderr);
910 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
911 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
912 rpc_in_data.rpc = rpc;
913 rpc_in_data.slot = slot;
914 curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
915 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
918 rpc->any_written = 0;
919 err = run_slot(slot, NULL);
920 if (err == HTTP_REAUTH && !large_request) {
921 credential_fill(&http_auth);
922 goto retry;
924 if (err != HTTP_OK)
925 err = -1;
927 if (!rpc->any_written)
928 err = -1;
930 curl_slist_free_all(headers);
931 free(gzip_body);
932 return err;
935 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
936 const char **client_argv, const struct strbuf *preamble,
937 struct strbuf *rpc_result)
939 const char *svc = rpc->service_name;
940 struct strbuf buf = STRBUF_INIT;
941 struct child_process client = CHILD_PROCESS_INIT;
942 int err = 0;
944 client.in = -1;
945 client.out = -1;
946 client.git_cmd = 1;
947 client.argv = client_argv;
948 if (start_command(&client))
949 exit(1);
950 write_or_die(client.in, preamble->buf, preamble->len);
951 if (heads)
952 write_or_die(client.in, heads->buf, heads->len);
954 rpc->alloc = http_post_buffer;
955 rpc->buf = xmalloc(rpc->alloc);
956 rpc->in = client.in;
957 rpc->out = client.out;
959 strbuf_addf(&buf, "%s%s", url.buf, svc);
960 rpc->service_url = strbuf_detach(&buf, NULL);
962 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
963 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
965 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
966 rpc->hdr_accept = strbuf_detach(&buf, NULL);
968 if (get_protocol_http_header(heads->version, &buf))
969 rpc->protocol_header = strbuf_detach(&buf, NULL);
970 else
971 rpc->protocol_header = NULL;
973 while (!err) {
974 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
975 if (!n)
976 break;
977 rpc->pos = 0;
978 rpc->len = n;
979 err |= post_rpc(rpc, 0);
982 close(client.in);
983 client.in = -1;
984 if (!err) {
985 strbuf_read(rpc_result, client.out, 0);
986 } else {
987 char buf[4096];
988 for (;;)
989 if (xread(client.out, buf, sizeof(buf)) <= 0)
990 break;
993 close(client.out);
994 client.out = -1;
996 err |= finish_command(&client);
997 free(rpc->service_url);
998 free(rpc->hdr_content_type);
999 free(rpc->hdr_accept);
1000 free(rpc->protocol_header);
1001 free(rpc->buf);
1002 strbuf_release(&buf);
1003 return err;
1006 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1008 struct walker *walker;
1009 char **targets;
1010 int ret, i;
1012 ALLOC_ARRAY(targets, nr_heads);
1013 if (options.depth || options.deepen_since)
1014 die(_("dumb http transport does not support shallow capabilities"));
1015 for (i = 0; i < nr_heads; i++)
1016 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1018 walker = get_http_walker(url.buf);
1019 walker->get_verbosely = options.verbosity >= 3;
1020 walker->get_recover = 0;
1021 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1022 walker_free(walker);
1024 for (i = 0; i < nr_heads; i++)
1025 free(targets[i]);
1026 free(targets);
1028 return ret ? error(_("fetch failed.")) : 0;
1031 static int fetch_git(struct discovery *heads,
1032 int nr_heads, struct ref **to_fetch)
1034 struct rpc_state rpc;
1035 struct strbuf preamble = STRBUF_INIT;
1036 int i, err;
1037 struct argv_array args = ARGV_ARRAY_INIT;
1038 struct strbuf rpc_result = STRBUF_INIT;
1040 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
1041 "--stdin", "--lock-pack", NULL);
1042 if (options.followtags)
1043 argv_array_push(&args, "--include-tag");
1044 if (options.thin)
1045 argv_array_push(&args, "--thin");
1046 if (options.verbosity >= 3)
1047 argv_array_pushl(&args, "-v", "-v", NULL);
1048 if (options.check_self_contained_and_connected)
1049 argv_array_push(&args, "--check-self-contained-and-connected");
1050 if (options.cloning)
1051 argv_array_push(&args, "--cloning");
1052 if (options.update_shallow)
1053 argv_array_push(&args, "--update-shallow");
1054 if (!options.progress)
1055 argv_array_push(&args, "--no-progress");
1056 if (options.depth)
1057 argv_array_pushf(&args, "--depth=%lu", options.depth);
1058 if (options.deepen_since)
1059 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
1060 for (i = 0; i < options.deepen_not.nr; i++)
1061 argv_array_pushf(&args, "--shallow-exclude=%s",
1062 options.deepen_not.items[i].string);
1063 if (options.deepen_relative && options.depth)
1064 argv_array_push(&args, "--deepen-relative");
1065 if (options.from_promisor)
1066 argv_array_push(&args, "--from-promisor");
1067 if (options.no_dependents)
1068 argv_array_push(&args, "--no-dependents");
1069 if (options.filter)
1070 argv_array_pushf(&args, "--filter=%s", options.filter);
1071 argv_array_push(&args, url.buf);
1073 for (i = 0; i < nr_heads; i++) {
1074 struct ref *ref = to_fetch[i];
1075 if (!*ref->name)
1076 die(_("cannot fetch by sha1 over smart http"));
1077 packet_buf_write(&preamble, "%s %s\n",
1078 oid_to_hex(&ref->old_oid), ref->name);
1080 packet_buf_flush(&preamble);
1082 memset(&rpc, 0, sizeof(rpc));
1083 rpc.service_name = "git-upload-pack",
1084 rpc.gzip_request = 1;
1086 err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1087 if (rpc_result.len)
1088 write_or_die(1, rpc_result.buf, rpc_result.len);
1089 strbuf_release(&rpc_result);
1090 strbuf_release(&preamble);
1091 argv_array_clear(&args);
1092 return err;
1095 static int fetch(int nr_heads, struct ref **to_fetch)
1097 struct discovery *d = discover_refs("git-upload-pack", 0);
1098 if (d->proto_git)
1099 return fetch_git(d, nr_heads, to_fetch);
1100 else
1101 return fetch_dumb(nr_heads, to_fetch);
1104 static void parse_fetch(struct strbuf *buf)
1106 struct ref **to_fetch = NULL;
1107 struct ref *list_head = NULL;
1108 struct ref **list = &list_head;
1109 int alloc_heads = 0, nr_heads = 0;
1111 do {
1112 const char *p;
1113 if (skip_prefix(buf->buf, "fetch ", &p)) {
1114 const char *name;
1115 struct ref *ref;
1116 struct object_id old_oid;
1117 const char *q;
1119 if (parse_oid_hex(p, &old_oid, &q))
1120 die(_("protocol error: expected sha/ref, got '%s'"), p);
1121 if (*q == ' ')
1122 name = q + 1;
1123 else if (!*q)
1124 name = "";
1125 else
1126 die(_("protocol error: expected sha/ref, got '%s'"), p);
1128 ref = alloc_ref(name);
1129 oidcpy(&ref->old_oid, &old_oid);
1131 *list = ref;
1132 list = &ref->next;
1134 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1135 to_fetch[nr_heads++] = ref;
1137 else
1138 die(_("http transport does not support %s"), buf->buf);
1140 strbuf_reset(buf);
1141 if (strbuf_getline_lf(buf, stdin) == EOF)
1142 return;
1143 if (!*buf->buf)
1144 break;
1145 } while (1);
1147 if (fetch(nr_heads, to_fetch))
1148 exit(128); /* error already reported */
1149 free_refs(list_head);
1150 free(to_fetch);
1152 printf("\n");
1153 fflush(stdout);
1154 strbuf_reset(buf);
1157 static int push_dav(int nr_spec, char **specs)
1159 struct child_process child = CHILD_PROCESS_INIT;
1160 size_t i;
1162 child.git_cmd = 1;
1163 argv_array_push(&child.args, "http-push");
1164 argv_array_push(&child.args, "--helper-status");
1165 if (options.dry_run)
1166 argv_array_push(&child.args, "--dry-run");
1167 if (options.verbosity > 1)
1168 argv_array_push(&child.args, "--verbose");
1169 argv_array_push(&child.args, url.buf);
1170 for (i = 0; i < nr_spec; i++)
1171 argv_array_push(&child.args, specs[i]);
1173 if (run_command(&child))
1174 die(_("git-http-push failed"));
1175 return 0;
1178 static int push_git(struct discovery *heads, int nr_spec, char **specs)
1180 struct rpc_state rpc;
1181 int i, err;
1182 struct argv_array args;
1183 struct string_list_item *cas_option;
1184 struct strbuf preamble = STRBUF_INIT;
1185 struct strbuf rpc_result = STRBUF_INIT;
1187 argv_array_init(&args);
1188 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1189 NULL);
1191 if (options.thin)
1192 argv_array_push(&args, "--thin");
1193 if (options.dry_run)
1194 argv_array_push(&args, "--dry-run");
1195 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1196 argv_array_push(&args, "--signed=yes");
1197 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1198 argv_array_push(&args, "--signed=if-asked");
1199 if (options.verbosity == 0)
1200 argv_array_push(&args, "--quiet");
1201 else if (options.verbosity > 1)
1202 argv_array_push(&args, "--verbose");
1203 for (i = 0; i < options.push_options.nr; i++)
1204 argv_array_pushf(&args, "--push-option=%s",
1205 options.push_options.items[i].string);
1206 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1207 for_each_string_list_item(cas_option, &cas_options)
1208 argv_array_push(&args, cas_option->string);
1209 argv_array_push(&args, url.buf);
1211 argv_array_push(&args, "--stdin");
1212 for (i = 0; i < nr_spec; i++)
1213 packet_buf_write(&preamble, "%s\n", specs[i]);
1214 packet_buf_flush(&preamble);
1216 memset(&rpc, 0, sizeof(rpc));
1217 rpc.service_name = "git-receive-pack",
1219 err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1220 if (rpc_result.len)
1221 write_or_die(1, rpc_result.buf, rpc_result.len);
1222 strbuf_release(&rpc_result);
1223 strbuf_release(&preamble);
1224 argv_array_clear(&args);
1225 return err;
1228 static int push(int nr_spec, char **specs)
1230 struct discovery *heads = discover_refs("git-receive-pack", 1);
1231 int ret;
1233 if (heads->proto_git)
1234 ret = push_git(heads, nr_spec, specs);
1235 else
1236 ret = push_dav(nr_spec, specs);
1237 free_discovery(heads);
1238 return ret;
1241 static void parse_push(struct strbuf *buf)
1243 char **specs = NULL;
1244 int alloc_spec = 0, nr_spec = 0, i, ret;
1246 do {
1247 if (starts_with(buf->buf, "push ")) {
1248 ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1249 specs[nr_spec++] = xstrdup(buf->buf + 5);
1251 else
1252 die(_("http transport does not support %s"), buf->buf);
1254 strbuf_reset(buf);
1255 if (strbuf_getline_lf(buf, stdin) == EOF)
1256 goto free_specs;
1257 if (!*buf->buf)
1258 break;
1259 } while (1);
1261 ret = push(nr_spec, specs);
1262 printf("\n");
1263 fflush(stdout);
1265 if (ret)
1266 exit(128); /* error already reported */
1268 free_specs:
1269 for (i = 0; i < nr_spec; i++)
1270 free(specs[i]);
1271 free(specs);
1274 static int stateless_connect(const char *service_name)
1276 struct discovery *discover;
1277 struct rpc_state rpc;
1278 struct strbuf buf = STRBUF_INIT;
1281 * Run the info/refs request and see if the server supports protocol
1282 * v2. If and only if the server supports v2 can we successfully
1283 * establish a stateless connection, otherwise we need to tell the
1284 * client to fallback to using other transport helper functions to
1285 * complete their request.
1287 discover = discover_refs(service_name, 0);
1288 if (discover->version != protocol_v2) {
1289 printf("fallback\n");
1290 fflush(stdout);
1291 return -1;
1292 } else {
1293 /* Stateless Connection established */
1294 printf("\n");
1295 fflush(stdout);
1298 rpc.service_name = service_name;
1299 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1300 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1301 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1302 if (get_protocol_http_header(discover->version, &buf)) {
1303 rpc.protocol_header = strbuf_detach(&buf, NULL);
1304 } else {
1305 rpc.protocol_header = NULL;
1306 strbuf_release(&buf);
1308 rpc.buf = xmalloc(http_post_buffer);
1309 rpc.alloc = http_post_buffer;
1310 rpc.len = 0;
1311 rpc.pos = 0;
1312 rpc.in = 1;
1313 rpc.out = 0;
1314 rpc.any_written = 0;
1315 rpc.gzip_request = 1;
1316 rpc.initial_buffer = 0;
1317 rpc.write_line_lengths = 1;
1318 rpc.flush_read_but_not_sent = 0;
1321 * Dump the capability listing that we got from the server earlier
1322 * during the info/refs request.
1324 write_or_die(rpc.in, discover->buf, discover->len);
1326 /* Until we see EOF keep sending POSTs */
1327 while (1) {
1328 size_t avail;
1329 enum packet_read_status status;
1331 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1332 &status))
1333 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1334 if (status == PACKET_READ_EOF)
1335 break;
1336 if (post_rpc(&rpc, status == PACKET_READ_FLUSH))
1337 /* We would have an err here */
1338 break;
1339 /* Reset the buffer for next request */
1340 rpc.len = 0;
1343 free(rpc.service_url);
1344 free(rpc.hdr_content_type);
1345 free(rpc.hdr_accept);
1346 free(rpc.protocol_header);
1347 free(rpc.buf);
1348 strbuf_release(&buf);
1350 return 0;
1353 int cmd_main(int argc, const char **argv)
1355 struct strbuf buf = STRBUF_INIT;
1356 int nongit;
1358 setup_git_directory_gently(&nongit);
1359 if (argc < 2) {
1360 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1361 return 1;
1364 options.verbosity = 1;
1365 options.progress = !!isatty(2);
1366 options.thin = 1;
1367 string_list_init(&options.deepen_not, 1);
1368 string_list_init(&options.push_options, 1);
1371 * Just report "remote-curl" here (folding all the various aliases
1372 * ("git-remote-http", "git-remote-https", and etc.) here since they
1373 * are all just copies of the same actual executable.
1375 trace2_cmd_name("remote-curl");
1377 remote = remote_get(argv[1]);
1379 if (argc > 2) {
1380 end_url_with_slash(&url, argv[2]);
1381 } else {
1382 end_url_with_slash(&url, remote->url[0]);
1385 http_init(remote, url.buf, 0);
1387 do {
1388 const char *arg;
1390 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1391 if (ferror(stdin))
1392 error(_("remote-curl: error reading command stream from git"));
1393 return 1;
1395 if (buf.len == 0)
1396 break;
1397 if (starts_with(buf.buf, "fetch ")) {
1398 if (nongit)
1399 die(_("remote-curl: fetch attempted without a local repo"));
1400 parse_fetch(&buf);
1402 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1403 int for_push = !!strstr(buf.buf + 4, "for-push");
1404 output_refs(get_refs(for_push));
1406 } else if (starts_with(buf.buf, "push ")) {
1407 parse_push(&buf);
1409 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1410 char *value = strchr(arg, ' ');
1411 int result;
1413 if (value)
1414 *value++ = '\0';
1415 else
1416 value = "true";
1418 result = set_option(arg, value);
1419 if (!result)
1420 printf("ok\n");
1421 else if (result < 0)
1422 printf("error invalid value\n");
1423 else
1424 printf("unsupported\n");
1425 fflush(stdout);
1427 } else if (!strcmp(buf.buf, "capabilities")) {
1428 printf("stateless-connect\n");
1429 printf("fetch\n");
1430 printf("option\n");
1431 printf("push\n");
1432 printf("check-connectivity\n");
1433 printf("\n");
1434 fflush(stdout);
1435 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1436 if (!stateless_connect(arg))
1437 break;
1438 } else {
1439 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1440 return 1;
1442 strbuf_reset(&buf);
1443 } while (1);
1445 http_cleanup();
1447 return 0;