The fifth batch
[alt-git.git] / remote-curl.c
blob1273507a96cae97eeaac9a95a47618e5e4f72850
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "git-curl-compat.h"
6 #include "config.h"
7 #include "environment.h"
8 #include "gettext.h"
9 #include "hex.h"
10 #include "remote.h"
11 #include "connect.h"
12 #include "strbuf.h"
13 #include "walker.h"
14 #include "http.h"
15 #include "run-command.h"
16 #include "pkt-line.h"
17 #include "string-list.h"
18 #include "strvec.h"
19 #include "credential.h"
20 #include "oid-array.h"
21 #include "send-pack.h"
22 #include "setup.h"
23 #include "protocol.h"
24 #include "quote.h"
25 #include "trace2.h"
26 #include "transport.h"
27 #include "url.h"
28 #include "write-or-die.h"
30 static struct remote *remote;
31 /* always ends with a trailing slash */
32 static struct strbuf url = STRBUF_INIT;
34 struct options {
35 int verbosity;
36 unsigned long depth;
37 char *deepen_since;
38 struct string_list deepen_not;
39 struct string_list push_options;
40 char *filter;
41 unsigned progress : 1,
42 check_self_contained_and_connected : 1,
43 cloning : 1,
44 update_shallow : 1,
45 followtags : 1,
46 dry_run : 1,
47 thin : 1,
48 /* One of the SEND_PACK_PUSH_CERT_* constants. */
49 push_cert : 2,
50 deepen_relative : 1,
52 /* see documentation of corresponding flag in fetch-pack.h */
53 from_promisor : 1,
55 refetch : 1,
56 atomic : 1,
57 object_format : 1,
58 force_if_includes : 1;
59 const struct git_hash_algo *hash_algo;
61 static struct options options;
62 static struct string_list cas_options = STRING_LIST_INIT_DUP;
64 static int set_option(const char *name, size_t namelen, const char *value)
66 if (!strncmp(name, "verbosity", namelen)) {
67 char *end;
68 int v = strtol(value, &end, 10);
69 if (value == end || *end)
70 return -1;
71 options.verbosity = v;
72 return 0;
74 else if (!strncmp(name, "progress", namelen)) {
75 if (!strcmp(value, "true"))
76 options.progress = 1;
77 else if (!strcmp(value, "false"))
78 options.progress = 0;
79 else
80 return -1;
81 return 0;
83 else if (!strncmp(name, "depth", namelen)) {
84 char *end;
85 unsigned long v = strtoul(value, &end, 10);
86 if (value == end || *end)
87 return -1;
88 options.depth = v;
89 return 0;
91 else if (!strncmp(name, "deepen-since", namelen)) {
92 options.deepen_since = xstrdup(value);
93 return 0;
95 else if (!strncmp(name, "deepen-not", namelen)) {
96 string_list_append(&options.deepen_not, value);
97 return 0;
99 else if (!strncmp(name, "deepen-relative", namelen)) {
100 if (!strcmp(value, "true"))
101 options.deepen_relative = 1;
102 else if (!strcmp(value, "false"))
103 options.deepen_relative = 0;
104 else
105 return -1;
106 return 0;
108 else if (!strncmp(name, "followtags", namelen)) {
109 if (!strcmp(value, "true"))
110 options.followtags = 1;
111 else if (!strcmp(value, "false"))
112 options.followtags = 0;
113 else
114 return -1;
115 return 0;
117 else if (!strncmp(name, "dry-run", namelen)) {
118 if (!strcmp(value, "true"))
119 options.dry_run = 1;
120 else if (!strcmp(value, "false"))
121 options.dry_run = 0;
122 else
123 return -1;
124 return 0;
126 else if (!strncmp(name, "check-connectivity", namelen)) {
127 if (!strcmp(value, "true"))
128 options.check_self_contained_and_connected = 1;
129 else if (!strcmp(value, "false"))
130 options.check_self_contained_and_connected = 0;
131 else
132 return -1;
133 return 0;
135 else if (!strncmp(name, "cas", namelen)) {
136 struct strbuf val = STRBUF_INIT;
137 strbuf_addstr(&val, "--force-with-lease=");
138 if (*value != '"')
139 strbuf_addstr(&val, value);
140 else if (unquote_c_style(&val, value, NULL))
141 return -1;
142 string_list_append(&cas_options, val.buf);
143 strbuf_release(&val);
144 return 0;
145 } else if (!strncmp(name, TRANS_OPT_FORCE_IF_INCLUDES, namelen)) {
146 if (!strcmp(value, "true"))
147 options.force_if_includes = 1;
148 else if (!strcmp(value, "false"))
149 options.force_if_includes = 0;
150 else
151 return -1;
152 return 0;
153 } else if (!strncmp(name, "cloning", namelen)) {
154 if (!strcmp(value, "true"))
155 options.cloning = 1;
156 else if (!strcmp(value, "false"))
157 options.cloning = 0;
158 else
159 return -1;
160 return 0;
161 } else if (!strncmp(name, "update-shallow", namelen)) {
162 if (!strcmp(value, "true"))
163 options.update_shallow = 1;
164 else if (!strcmp(value, "false"))
165 options.update_shallow = 0;
166 else
167 return -1;
168 return 0;
169 } else if (!strncmp(name, "pushcert", namelen)) {
170 if (!strcmp(value, "true"))
171 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
172 else if (!strcmp(value, "false"))
173 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
174 else if (!strcmp(value, "if-asked"))
175 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
176 else
177 return -1;
178 return 0;
179 } else if (!strncmp(name, "atomic", namelen)) {
180 if (!strcmp(value, "true"))
181 options.atomic = 1;
182 else if (!strcmp(value, "false"))
183 options.atomic = 0;
184 else
185 return -1;
186 return 0;
187 } else if (!strncmp(name, "push-option", namelen)) {
188 if (*value != '"')
189 string_list_append(&options.push_options, value);
190 else {
191 struct strbuf unquoted = STRBUF_INIT;
192 if (unquote_c_style(&unquoted, value, NULL) < 0)
193 die(_("invalid quoting in push-option value: '%s'"), value);
194 string_list_append_nodup(&options.push_options,
195 strbuf_detach(&unquoted, NULL));
197 return 0;
198 } else if (!strncmp(name, "family", namelen)) {
199 if (!strcmp(value, "ipv4"))
200 git_curl_ipresolve = CURL_IPRESOLVE_V4;
201 else if (!strcmp(value, "ipv6"))
202 git_curl_ipresolve = CURL_IPRESOLVE_V6;
203 else if (!strcmp(value, "all"))
204 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
205 else
206 return -1;
207 return 0;
208 } else if (!strncmp(name, "from-promisor", namelen)) {
209 options.from_promisor = 1;
210 return 0;
211 } else if (!strncmp(name, "refetch", namelen)) {
212 options.refetch = 1;
213 return 0;
214 } else if (!strncmp(name, "filter", namelen)) {
215 options.filter = xstrdup(value);
216 return 0;
217 } else if (!strncmp(name, "object-format", namelen)) {
218 options.object_format = 1;
219 if (strcmp(value, "true"))
220 die(_("unknown value for object-format: %s"), value);
221 return 0;
222 } else {
223 return 1 /* unsupported */;
227 struct discovery {
228 char *service;
229 char *buf_alloc;
230 char *buf;
231 size_t len;
232 struct ref *refs;
233 struct oid_array shallow;
234 enum protocol_version version;
235 unsigned proto_git : 1;
237 static struct discovery *last_discovery;
239 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
241 struct ref *list = NULL;
242 struct packet_reader reader;
244 packet_reader_init(&reader, -1, heads->buf, heads->len,
245 PACKET_READ_CHOMP_NEWLINE |
246 PACKET_READ_GENTLE_ON_EOF |
247 PACKET_READ_DIE_ON_ERR_PACKET);
249 heads->version = discover_version(&reader);
250 switch (heads->version) {
251 case protocol_v2:
253 * Do nothing. This isn't a list of refs but rather a
254 * capability advertisement. Client would have run
255 * 'stateless-connect' so we'll dump this capability listing
256 * and let them request the refs themselves.
258 break;
259 case protocol_v1:
260 case protocol_v0:
261 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
262 NULL, &heads->shallow);
263 options.hash_algo = reader.hash_algo;
264 break;
265 case protocol_unknown_version:
266 BUG("unknown protocol version");
269 return list;
273 * Try to detect the hash algorithm used by the remote repository when using
274 * the dumb HTTP transport. As dumb transports cannot tell us the object hash
275 * directly have to derive it from the advertised ref lengths.
277 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
279 const char *p = memchr(heads->buf, '\t', heads->len);
280 int algo;
283 * In case the remote has no refs we have no way to reliably determine
284 * the object hash used by that repository. In that case we simply fall
285 * back to SHA1, which may or may not be correct.
287 if (!p)
288 return &hash_algos[GIT_HASH_SHA1];
290 algo = hash_algo_by_length((p - heads->buf) / 2);
291 if (algo == GIT_HASH_UNKNOWN)
292 return NULL;
293 return &hash_algos[algo];
296 static struct ref *parse_info_refs(struct discovery *heads)
298 char *data, *start, *mid;
299 char *ref_name;
300 int i = 0;
302 struct ref *refs = NULL;
303 struct ref *ref = NULL;
304 struct ref *last_ref = NULL;
306 options.hash_algo = detect_hash_algo(heads);
307 if (!options.hash_algo)
308 die("%sinfo/refs not valid: could not determine hash algorithm; "
309 "is this a git repository?",
310 transport_anonymize_url(url.buf));
313 * Set the repository's hash algo to whatever we have just detected.
314 * This ensures that we can correctly parse the remote references.
316 repo_set_hash_algo(the_repository, hash_algo_by_ptr(options.hash_algo));
318 data = heads->buf;
319 start = NULL;
320 mid = data;
321 while (i < heads->len) {
322 if (!start) {
323 start = &data[i];
325 if (data[i] == '\t')
326 mid = &data[i];
327 if (data[i] == '\n') {
328 if (mid - start != options.hash_algo->hexsz)
329 die(_("%sinfo/refs not valid: is this a git repository?"),
330 transport_anonymize_url(url.buf));
331 data[i] = 0;
332 ref_name = mid + 1;
333 ref = alloc_ref(ref_name);
334 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
335 if (!refs)
336 refs = ref;
337 if (last_ref)
338 last_ref->next = ref;
339 last_ref = ref;
340 start = NULL;
342 i++;
345 ref = alloc_ref("HEAD");
346 if (!http_fetch_ref(url.buf, ref) &&
347 !resolve_remote_symref(ref, refs)) {
348 ref->next = refs;
349 refs = ref;
350 } else {
351 free_one_ref(ref);
354 return refs;
357 static void free_discovery(struct discovery *d)
359 if (d) {
360 if (d == last_discovery)
361 last_discovery = NULL;
362 free(d->shallow.oid);
363 free(d->buf_alloc);
364 free_refs(d->refs);
365 free(d->service);
366 free(d);
370 static int show_http_message(struct strbuf *type, struct strbuf *charset,
371 struct strbuf *msg)
373 const char *p, *eol;
376 * We only show text/plain parts, as other types are likely
377 * to be ugly to look at on the user's terminal.
379 if (strcmp(type->buf, "text/plain"))
380 return -1;
381 if (charset->len)
382 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
384 strbuf_trim(msg);
385 if (!msg->len)
386 return -1;
388 p = msg->buf;
389 do {
390 eol = strchrnul(p, '\n');
391 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
392 p = eol + 1;
393 } while(*eol);
394 return 0;
397 static int get_protocol_http_header(enum protocol_version version,
398 struct strbuf *header)
400 if (version > 0) {
401 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
402 version);
404 return 1;
407 return 0;
410 static void check_smart_http(struct discovery *d, const char *service,
411 struct strbuf *type)
413 const char *p;
414 struct packet_reader reader;
417 * If we don't see x-$service-advertisement, then it's not smart-http.
418 * But once we do, we commit to it and assume any other protocol
419 * violations are hard errors.
421 if (!skip_prefix(type->buf, "application/x-", &p) ||
422 !skip_prefix(p, service, &p) ||
423 strcmp(p, "-advertisement"))
424 return;
426 packet_reader_init(&reader, -1, d->buf, d->len,
427 PACKET_READ_CHOMP_NEWLINE |
428 PACKET_READ_DIE_ON_ERR_PACKET);
429 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
430 die(_("invalid server response; expected service, got flush packet"));
432 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
434 * The header can include additional metadata lines, up
435 * until a packet flush marker. Ignore these now, but
436 * in the future we might start to scan them.
438 for (;;) {
439 packet_reader_read(&reader);
440 if (reader.pktlen <= 0) {
441 break;
446 * v0 smart http; callers expect us to soak up the
447 * service and header packets
449 d->buf = reader.src_buffer;
450 d->len = reader.src_len;
451 d->proto_git = 1;
453 } else if (!strcmp(reader.line, "version 2")) {
455 * v2 smart http; do not consume version packet, which will
456 * be handled elsewhere.
458 d->proto_git = 1;
460 } else {
461 die(_("invalid server response; got '%s'"), reader.line);
465 static struct discovery *discover_refs(const char *service, int for_push)
467 struct strbuf type = STRBUF_INIT;
468 struct strbuf charset = STRBUF_INIT;
469 struct strbuf buffer = STRBUF_INIT;
470 struct strbuf refs_url = STRBUF_INIT;
471 struct strbuf effective_url = STRBUF_INIT;
472 struct strbuf protocol_header = STRBUF_INIT;
473 struct string_list extra_headers = STRING_LIST_INIT_DUP;
474 struct discovery *last = last_discovery;
475 int http_ret, maybe_smart = 0;
476 struct http_get_options http_options;
477 enum protocol_version version = get_protocol_version_config();
479 if (last && !strcmp(service, last->service))
480 return last;
481 free_discovery(last);
483 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
484 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
485 git_env_bool("GIT_SMART_HTTP", 1)) {
486 maybe_smart = 1;
487 if (!strchr(url.buf, '?'))
488 strbuf_addch(&refs_url, '?');
489 else
490 strbuf_addch(&refs_url, '&');
491 strbuf_addf(&refs_url, "service=%s", service);
495 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
496 * to perform any operation that doesn't involve upload-pack (i.e., a
497 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
498 * to do anything else (like push or remote archive) via v2.
500 if (version == protocol_v2 && strcmp("git-upload-pack", service))
501 version = protocol_v0;
503 /* Add the extra Git-Protocol header */
504 if (get_protocol_http_header(version, &protocol_header))
505 string_list_append(&extra_headers, protocol_header.buf);
507 memset(&http_options, 0, sizeof(http_options));
508 http_options.content_type = &type;
509 http_options.charset = &charset;
510 http_options.effective_url = &effective_url;
511 http_options.base_url = &url;
512 http_options.extra_headers = &extra_headers;
513 http_options.initial_request = 1;
514 http_options.no_cache = 1;
516 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
517 switch (http_ret) {
518 case HTTP_OK:
519 break;
520 case HTTP_MISSING_TARGET:
521 show_http_message(&type, &charset, &buffer);
522 die(_("repository '%s' not found"),
523 transport_anonymize_url(url.buf));
524 case HTTP_NOAUTH:
525 show_http_message(&type, &charset, &buffer);
526 die(_("Authentication failed for '%s'"),
527 transport_anonymize_url(url.buf));
528 case HTTP_NOMATCHPUBLICKEY:
529 show_http_message(&type, &charset, &buffer);
530 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
531 transport_anonymize_url(url.buf), curl_errorstr);
532 default:
533 show_http_message(&type, &charset, &buffer);
534 die(_("unable to access '%s': %s"),
535 transport_anonymize_url(url.buf), curl_errorstr);
538 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
539 char *u = transport_anonymize_url(url.buf);
540 warning(_("redirecting to %s"), u);
541 free(u);
544 last= xcalloc(1, sizeof(*last_discovery));
545 last->service = xstrdup(service);
546 last->buf_alloc = strbuf_detach(&buffer, &last->len);
547 last->buf = last->buf_alloc;
549 if (maybe_smart)
550 check_smart_http(last, service, &type);
552 if (last->proto_git)
553 last->refs = parse_git_refs(last, for_push);
554 else
555 last->refs = parse_info_refs(last);
557 strbuf_release(&refs_url);
558 strbuf_release(&type);
559 strbuf_release(&charset);
560 strbuf_release(&effective_url);
561 strbuf_release(&buffer);
562 strbuf_release(&protocol_header);
563 string_list_clear(&extra_headers, 0);
564 last_discovery = last;
565 return last;
568 static struct ref *get_refs(int for_push)
570 struct discovery *heads;
572 if (for_push)
573 heads = discover_refs("git-receive-pack", for_push);
574 else
575 heads = discover_refs("git-upload-pack", for_push);
577 return heads->refs;
580 static void output_refs(struct ref *refs)
582 struct ref *posn;
583 if (options.object_format && options.hash_algo) {
584 printf(":object-format %s\n", options.hash_algo->name);
585 repo_set_hash_algo(the_repository,
586 hash_algo_by_ptr(options.hash_algo));
588 for (posn = refs; posn; posn = posn->next) {
589 if (posn->symref)
590 printf("@%s %s\n", posn->symref, posn->name);
591 else
592 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
593 options.hash_algo),
594 posn->name);
596 printf("\n");
597 fflush(stdout);
600 struct rpc_state {
601 const char *service_name;
602 char *service_url;
603 char *hdr_content_type;
604 char *hdr_accept;
605 char *hdr_accept_language;
606 char *protocol_header;
607 char *buf;
608 size_t alloc;
609 size_t len;
610 size_t pos;
611 int in;
612 int out;
613 int any_written;
614 unsigned gzip_request : 1;
615 unsigned initial_buffer : 1;
618 * Whenever a pkt-line is read into buf, append the 4 characters
619 * denoting its length before appending the payload.
621 unsigned write_line_lengths : 1;
624 * Used by rpc_out; initialize to 0. This is true if a flush has been
625 * read, but the corresponding line length (if write_line_lengths is
626 * true) and EOF have not been sent to libcurl. Since each flush marks
627 * the end of a request, each flush must be completely sent before any
628 * further reading occurs.
630 unsigned flush_read_but_not_sent : 1;
633 #define RPC_STATE_INIT { 0 }
636 * Appends the result of reading from rpc->out to the string represented by
637 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
638 * enough space, 0 otherwise.
640 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
641 * hexadecimal string before appending the result described above.
643 * Writes the total number of bytes appended into appended.
645 static int rpc_read_from_out(struct rpc_state *rpc, int options,
646 size_t *appended,
647 enum packet_read_status *status) {
648 size_t left;
649 char *buf;
650 int pktlen_raw;
652 if (rpc->write_line_lengths) {
653 left = rpc->alloc - rpc->len - 4;
654 buf = rpc->buf + rpc->len + 4;
655 } else {
656 left = rpc->alloc - rpc->len;
657 buf = rpc->buf + rpc->len;
660 if (left < LARGE_PACKET_MAX)
661 return 0;
663 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
664 left, &pktlen_raw, options);
665 if (*status != PACKET_READ_EOF) {
666 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
667 rpc->len += *appended;
670 if (rpc->write_line_lengths) {
671 switch (*status) {
672 case PACKET_READ_EOF:
673 if (!(options & PACKET_READ_GENTLE_ON_EOF))
674 die(_("shouldn't have EOF when not gentle on EOF"));
675 break;
676 case PACKET_READ_NORMAL:
677 set_packet_header(buf - 4, *appended);
678 break;
679 case PACKET_READ_DELIM:
680 memcpy(buf - 4, "0001", 4);
681 break;
682 case PACKET_READ_FLUSH:
683 memcpy(buf - 4, "0000", 4);
684 break;
685 case PACKET_READ_RESPONSE_END:
686 die(_("remote server sent unexpected response end packet"));
690 return 1;
693 static size_t rpc_out(void *ptr, size_t eltsize,
694 size_t nmemb, void *buffer_)
696 size_t max = eltsize * nmemb;
697 struct rpc_state *rpc = buffer_;
698 size_t avail = rpc->len - rpc->pos;
699 enum packet_read_status status;
701 if (!avail) {
702 rpc->initial_buffer = 0;
703 rpc->len = 0;
704 rpc->pos = 0;
705 if (!rpc->flush_read_but_not_sent) {
706 if (!rpc_read_from_out(rpc, 0, &avail, &status))
707 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
708 if (status == PACKET_READ_FLUSH)
709 rpc->flush_read_but_not_sent = 1;
712 * If flush_read_but_not_sent is true, we have already read one
713 * full request but have not fully sent it + EOF, which is why
714 * we need to refrain from reading.
717 if (rpc->flush_read_but_not_sent) {
718 if (!avail) {
720 * The line length either does not need to be sent at
721 * all or has already been completely sent. Now we can
722 * return 0, indicating EOF, meaning that the flush has
723 * been fully sent.
725 rpc->flush_read_but_not_sent = 0;
726 return 0;
729 * If avail is non-zero, the line length for the flush still
730 * hasn't been fully sent. Proceed with sending the line
731 * length.
735 if (max < avail)
736 avail = max;
737 memcpy(ptr, rpc->buf + rpc->pos, avail);
738 rpc->pos += avail;
739 return avail;
742 static int rpc_seek(void *clientp, curl_off_t offset, int origin)
744 struct rpc_state *rpc = clientp;
746 if (origin != SEEK_SET)
747 BUG("rpc_seek only handles SEEK_SET, not %d", origin);
749 if (rpc->initial_buffer) {
750 if (offset < 0 || offset > rpc->len) {
751 error("curl seek would be outside of rpc buffer");
752 return CURL_SEEKFUNC_FAIL;
754 rpc->pos = offset;
755 return CURL_SEEKFUNC_OK;
757 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
758 return CURL_SEEKFUNC_FAIL;
761 struct check_pktline_state {
762 char len_buf[4];
763 int len_filled;
764 int remaining;
767 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
769 while (size) {
770 if (!state->remaining) {
771 int digits_remaining = 4 - state->len_filled;
772 if (digits_remaining > size)
773 digits_remaining = size;
774 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
775 state->len_filled += digits_remaining;
776 ptr += digits_remaining;
777 size -= digits_remaining;
779 if (state->len_filled == 4) {
780 state->remaining = packet_length(state->len_buf,
781 sizeof(state->len_buf));
782 if (state->remaining < 0) {
783 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
784 } else if (state->remaining == 2) {
785 die(_("remote-curl: unexpected response end packet"));
786 } else if (state->remaining < 4) {
787 state->remaining = 0;
788 } else {
789 state->remaining -= 4;
791 state->len_filled = 0;
795 if (state->remaining) {
796 int remaining = state->remaining;
797 if (remaining > size)
798 remaining = size;
799 ptr += remaining;
800 size -= remaining;
801 state->remaining -= remaining;
806 struct rpc_in_data {
807 struct rpc_state *rpc;
808 struct active_request_slot *slot;
809 int check_pktline;
810 struct check_pktline_state pktline_state;
814 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
815 * from ptr.
817 static size_t rpc_in(char *ptr, size_t eltsize,
818 size_t nmemb, void *buffer_)
820 size_t size = eltsize * nmemb;
821 struct rpc_in_data *data = buffer_;
822 long response_code;
824 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
825 &response_code) != CURLE_OK)
826 return size;
827 if (response_code >= 300)
828 return size;
829 if (size)
830 data->rpc->any_written = 1;
831 if (data->check_pktline)
832 check_pktline(&data->pktline_state, ptr, size);
833 write_or_die(data->rpc->in, ptr, size);
834 return size;
837 static int run_slot(struct active_request_slot *slot,
838 struct slot_results *results)
840 int err;
841 struct slot_results results_buf;
843 if (!results)
844 results = &results_buf;
846 err = run_one_slot(slot, results);
848 if (err != HTTP_OK && err != HTTP_REAUTH) {
849 struct strbuf msg = STRBUF_INIT;
850 if (results->http_code && results->http_code != 200)
851 strbuf_addf(&msg, "HTTP %ld", results->http_code);
852 if (results->curl_result != CURLE_OK) {
853 if (msg.len)
854 strbuf_addch(&msg, ' ');
855 strbuf_addf(&msg, "curl %d", results->curl_result);
856 if (curl_errorstr[0]) {
857 strbuf_addch(&msg, ' ');
858 strbuf_addstr(&msg, curl_errorstr);
861 error(_("RPC failed; %s"), msg.buf);
862 strbuf_release(&msg);
865 return err;
868 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
870 struct active_request_slot *slot;
871 struct curl_slist *headers = http_copy_default_headers();
872 struct strbuf buf = STRBUF_INIT;
873 int err;
875 slot = get_active_slot();
877 headers = curl_slist_append(headers, rpc->hdr_content_type);
878 headers = curl_slist_append(headers, rpc->hdr_accept);
880 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
881 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
882 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
883 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
884 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
885 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
886 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
887 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
888 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf);
890 err = run_slot(slot, results);
892 curl_slist_free_all(headers);
893 strbuf_release(&buf);
894 return err;
897 static curl_off_t xcurl_off_t(size_t len)
899 uintmax_t size = len;
900 if (size > maximum_signed_value_of_type(curl_off_t))
901 die(_("cannot handle pushes this big"));
902 return (curl_off_t)size;
906 * If flush_received is true, do not attempt to read any more; just use what's
907 * in rpc->buf.
909 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
911 struct active_request_slot *slot;
912 struct curl_slist *headers = NULL;
913 int use_gzip = rpc->gzip_request;
914 char *gzip_body = NULL;
915 size_t gzip_size = 0;
916 int err, large_request = 0;
917 int needs_100_continue = 0;
918 struct rpc_in_data rpc_in_data;
920 /* Try to load the entire request, if we can fit it into the
921 * allocated buffer space we can use HTTP/1.0 and avoid the
922 * chunked encoding mess.
924 if (!flush_received) {
925 while (1) {
926 size_t n;
927 enum packet_read_status status;
929 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
930 large_request = 1;
931 use_gzip = 0;
932 break;
934 if (status == PACKET_READ_FLUSH)
935 break;
939 if (large_request) {
940 struct slot_results results;
942 do {
943 err = probe_rpc(rpc, &results);
944 if (err == HTTP_REAUTH)
945 credential_fill(the_repository, &http_auth, 0);
946 } while (err == HTTP_REAUTH);
947 if (err != HTTP_OK)
948 return -1;
950 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE || http_auth.authtype)
951 needs_100_continue = 1;
954 retry:
955 headers = http_copy_default_headers();
956 headers = curl_slist_append(headers, rpc->hdr_content_type);
957 headers = curl_slist_append(headers, rpc->hdr_accept);
958 headers = curl_slist_append(headers, needs_100_continue ?
959 "Expect: 100-continue" : "Expect:");
961 headers = http_append_auth_header(&http_auth, headers);
963 /* Add Accept-Language header */
964 if (rpc->hdr_accept_language)
965 headers = curl_slist_append(headers, rpc->hdr_accept_language);
967 /* Add the extra Git-Protocol header */
968 if (rpc->protocol_header)
969 headers = curl_slist_append(headers, rpc->protocol_header);
971 slot = get_active_slot();
973 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
974 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
975 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
976 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
978 if (large_request) {
979 /* The request body is large and the size cannot be predicted.
980 * We must use chunked encoding to send it.
982 #ifdef GIT_CURL_NEED_TRANSFER_ENCODING_HEADER
983 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
984 #endif
985 rpc->initial_buffer = 1;
986 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
987 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
988 curl_easy_setopt(slot->curl, CURLOPT_SEEKFUNCTION, rpc_seek);
989 curl_easy_setopt(slot->curl, CURLOPT_SEEKDATA, rpc);
990 if (options.verbosity > 1) {
991 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
992 fflush(stderr);
995 } else if (gzip_body) {
997 * If we are looping to retry authentication, then the previous
998 * run will have set up the headers and gzip buffer already,
999 * and we just need to send it.
1001 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
1002 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
1004 } else if (use_gzip && 1024 < rpc->len) {
1005 /* The client backend isn't giving us compressed data so
1006 * we can try to deflate it ourselves, this may save on
1007 * the transfer time.
1009 git_zstream stream;
1010 int ret;
1012 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
1013 gzip_size = git_deflate_bound(&stream, rpc->len);
1014 gzip_body = xmalloc(gzip_size);
1016 stream.next_in = (unsigned char *)rpc->buf;
1017 stream.avail_in = rpc->len;
1018 stream.next_out = (unsigned char *)gzip_body;
1019 stream.avail_out = gzip_size;
1021 ret = git_deflate(&stream, Z_FINISH);
1022 if (ret != Z_STREAM_END)
1023 die(_("cannot deflate request; zlib deflate error %d"), ret);
1025 ret = git_deflate_end_gently(&stream);
1026 if (ret != Z_OK)
1027 die(_("cannot deflate request; zlib end error %d"), ret);
1029 gzip_size = stream.total_out;
1031 headers = curl_slist_append(headers, "Content-Encoding: gzip");
1032 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
1033 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
1035 if (options.verbosity > 1) {
1036 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
1037 rpc->service_name,
1038 (unsigned long)rpc->len, (unsigned long)gzip_size);
1039 fflush(stderr);
1041 } else {
1042 /* We know the complete request size in advance, use the
1043 * more normal Content-Length approach.
1045 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1046 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1047 if (options.verbosity > 1) {
1048 fprintf(stderr, "POST %s (%lu bytes)\n",
1049 rpc->service_name, (unsigned long)rpc->len);
1050 fflush(stderr);
1054 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1055 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
1056 rpc_in_data.rpc = rpc;
1057 rpc_in_data.slot = slot;
1058 rpc_in_data.check_pktline = stateless_connect;
1059 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
1060 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &rpc_in_data);
1061 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1064 rpc->any_written = 0;
1065 err = run_slot(slot, NULL);
1066 if (err == HTTP_REAUTH && !large_request) {
1067 credential_fill(the_repository, &http_auth, 0);
1068 curl_slist_free_all(headers);
1069 goto retry;
1071 if (err != HTTP_OK)
1072 err = -1;
1074 if (!rpc->any_written)
1075 err = -1;
1077 if (rpc_in_data.pktline_state.len_filled)
1078 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1079 if (rpc_in_data.pktline_state.remaining)
1080 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1082 if (stateless_connect)
1083 packet_response_end(rpc->in);
1085 curl_slist_free_all(headers);
1086 free(gzip_body);
1087 return err;
1090 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
1091 const char **client_argv, const struct strbuf *preamble,
1092 struct strbuf *rpc_result)
1094 const char *svc = rpc->service_name;
1095 struct strbuf buf = STRBUF_INIT;
1096 struct child_process client = CHILD_PROCESS_INIT;
1097 int err = 0;
1099 client.in = -1;
1100 client.out = -1;
1101 client.git_cmd = 1;
1102 strvec_pushv(&client.args, client_argv);
1103 if (start_command(&client))
1104 exit(1);
1105 write_or_die(client.in, preamble->buf, preamble->len);
1106 if (heads)
1107 write_or_die(client.in, heads->buf, heads->len);
1109 rpc->alloc = http_post_buffer;
1110 rpc->buf = xmalloc(rpc->alloc);
1111 rpc->in = client.in;
1112 rpc->out = client.out;
1114 strbuf_addf(&buf, "%s%s", url.buf, svc);
1115 rpc->service_url = strbuf_detach(&buf, NULL);
1117 rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
1119 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1120 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1122 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
1123 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1125 if (get_protocol_http_header(heads->version, &buf))
1126 rpc->protocol_header = strbuf_detach(&buf, NULL);
1127 else
1128 rpc->protocol_header = NULL;
1130 while (!err) {
1131 int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0);
1132 if (!n)
1133 break;
1134 rpc->pos = 0;
1135 rpc->len = n;
1136 err |= post_rpc(rpc, 0, 0);
1139 close(client.in);
1140 client.in = -1;
1141 if (!err) {
1142 strbuf_read(rpc_result, client.out, 0);
1143 } else {
1144 char buf[4096];
1145 for (;;)
1146 if (xread(client.out, buf, sizeof(buf)) <= 0)
1147 break;
1150 close(client.out);
1151 client.out = -1;
1153 err |= finish_command(&client);
1154 free(rpc->service_url);
1155 free(rpc->hdr_content_type);
1156 free(rpc->hdr_accept);
1157 free(rpc->hdr_accept_language);
1158 free(rpc->protocol_header);
1159 free(rpc->buf);
1160 strbuf_release(&buf);
1161 return err;
1164 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1166 struct walker *walker;
1167 char **targets;
1168 int ret, i;
1170 ALLOC_ARRAY(targets, nr_heads);
1171 if (options.depth || options.deepen_since)
1172 die(_("dumb http transport does not support shallow capabilities"));
1173 for (i = 0; i < nr_heads; i++)
1174 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1176 walker = get_http_walker(url.buf);
1177 walker->get_verbosely = options.verbosity >= 3;
1178 walker->get_progress = options.progress;
1179 walker->get_recover = 0;
1180 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1181 walker_free(walker);
1183 for (i = 0; i < nr_heads; i++)
1184 free(targets[i]);
1185 free(targets);
1187 return ret ? error(_("fetch failed.")) : 0;
1190 static int fetch_git(struct discovery *heads,
1191 int nr_heads, struct ref **to_fetch)
1193 struct rpc_state rpc = RPC_STATE_INIT;
1194 struct strbuf preamble = STRBUF_INIT;
1195 int i, err;
1196 struct strvec args = STRVEC_INIT;
1197 struct strbuf rpc_result = STRBUF_INIT;
1199 strvec_pushl(&args, "fetch-pack", "--stateless-rpc",
1200 "--stdin", "--lock-pack", NULL);
1201 if (options.followtags)
1202 strvec_push(&args, "--include-tag");
1203 if (options.thin)
1204 strvec_push(&args, "--thin");
1205 if (options.verbosity >= 3)
1206 strvec_pushl(&args, "-v", "-v", NULL);
1207 if (options.check_self_contained_and_connected)
1208 strvec_push(&args, "--check-self-contained-and-connected");
1209 if (options.cloning)
1210 strvec_push(&args, "--cloning");
1211 if (options.update_shallow)
1212 strvec_push(&args, "--update-shallow");
1213 if (!options.progress)
1214 strvec_push(&args, "--no-progress");
1215 if (options.depth)
1216 strvec_pushf(&args, "--depth=%lu", options.depth);
1217 if (options.deepen_since)
1218 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since);
1219 for (i = 0; i < options.deepen_not.nr; i++)
1220 strvec_pushf(&args, "--shallow-exclude=%s",
1221 options.deepen_not.items[i].string);
1222 if (options.deepen_relative && options.depth)
1223 strvec_push(&args, "--deepen-relative");
1224 if (options.from_promisor)
1225 strvec_push(&args, "--from-promisor");
1226 if (options.refetch)
1227 strvec_push(&args, "--refetch");
1228 if (options.filter)
1229 strvec_pushf(&args, "--filter=%s", options.filter);
1230 strvec_push(&args, url.buf);
1232 for (i = 0; i < nr_heads; i++) {
1233 struct ref *ref = to_fetch[i];
1234 if (!*ref->name)
1235 die(_("cannot fetch by sha1 over smart http"));
1236 packet_buf_write(&preamble, "%s %s\n",
1237 oid_to_hex(&ref->old_oid), ref->name);
1239 packet_buf_flush(&preamble);
1241 memset(&rpc, 0, sizeof(rpc));
1242 rpc.service_name = "git-upload-pack",
1243 rpc.gzip_request = 1;
1245 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1246 if (rpc_result.len)
1247 write_or_die(1, rpc_result.buf, rpc_result.len);
1248 strbuf_release(&rpc_result);
1249 strbuf_release(&preamble);
1250 strvec_clear(&args);
1251 return err;
1254 static int fetch(int nr_heads, struct ref **to_fetch)
1256 struct discovery *d = discover_refs("git-upload-pack", 0);
1257 if (d->proto_git)
1258 return fetch_git(d, nr_heads, to_fetch);
1259 else
1260 return fetch_dumb(nr_heads, to_fetch);
1263 static void parse_fetch(struct strbuf *buf)
1265 struct ref **to_fetch = NULL;
1266 struct ref *list_head = NULL;
1267 struct ref **list = &list_head;
1268 int alloc_heads = 0, nr_heads = 0;
1270 do {
1271 const char *p;
1272 if (skip_prefix(buf->buf, "fetch ", &p)) {
1273 const char *name;
1274 struct ref *ref;
1275 struct object_id old_oid;
1276 const char *q;
1278 if (parse_oid_hex(p, &old_oid, &q))
1279 die(_("protocol error: expected sha/ref, got '%s'"), p);
1280 if (*q == ' ')
1281 name = q + 1;
1282 else if (!*q)
1283 name = "";
1284 else
1285 die(_("protocol error: expected sha/ref, got '%s'"), p);
1287 ref = alloc_ref(name);
1288 oidcpy(&ref->old_oid, &old_oid);
1290 *list = ref;
1291 list = &ref->next;
1293 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1294 to_fetch[nr_heads++] = ref;
1296 else
1297 die(_("http transport does not support %s"), buf->buf);
1299 strbuf_reset(buf);
1300 if (strbuf_getline_lf(buf, stdin) == EOF)
1301 return;
1302 if (!*buf->buf)
1303 break;
1304 } while (1);
1306 if (fetch(nr_heads, to_fetch))
1307 exit(128); /* error already reported */
1308 free_refs(list_head);
1309 free(to_fetch);
1311 printf("\n");
1312 fflush(stdout);
1313 strbuf_reset(buf);
1316 static void parse_get(const char *arg)
1318 struct strbuf url = STRBUF_INIT;
1319 struct strbuf path = STRBUF_INIT;
1320 const char *space;
1322 space = strchr(arg, ' ');
1324 if (!space)
1325 die(_("protocol error: expected '<url> <path>', missing space"));
1327 strbuf_add(&url, arg, space - arg);
1328 strbuf_addstr(&path, space + 1);
1330 if (http_get_file(url.buf, path.buf, NULL))
1331 die(_("failed to download file at URL '%s'"), url.buf);
1333 strbuf_release(&url);
1334 strbuf_release(&path);
1335 printf("\n");
1336 fflush(stdout);
1339 static int push_dav(int nr_spec, const char **specs)
1341 struct child_process child = CHILD_PROCESS_INIT;
1342 size_t i;
1344 child.git_cmd = 1;
1345 strvec_push(&child.args, "http-push");
1346 strvec_push(&child.args, "--helper-status");
1347 if (options.dry_run)
1348 strvec_push(&child.args, "--dry-run");
1349 if (options.verbosity > 1)
1350 strvec_push(&child.args, "--verbose");
1351 strvec_push(&child.args, url.buf);
1352 for (i = 0; i < nr_spec; i++)
1353 strvec_push(&child.args, specs[i]);
1355 if (run_command(&child))
1356 die(_("git-http-push failed"));
1357 return 0;
1360 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1362 struct rpc_state rpc = RPC_STATE_INIT;
1363 int i, err;
1364 struct strvec args;
1365 struct string_list_item *cas_option;
1366 struct strbuf preamble = STRBUF_INIT;
1367 struct strbuf rpc_result = STRBUF_INIT;
1369 strvec_init(&args);
1370 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1371 NULL);
1373 if (options.thin)
1374 strvec_push(&args, "--thin");
1375 if (options.dry_run)
1376 strvec_push(&args, "--dry-run");
1377 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1378 strvec_push(&args, "--signed=yes");
1379 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1380 strvec_push(&args, "--signed=if-asked");
1381 if (options.atomic)
1382 strvec_push(&args, "--atomic");
1383 if (options.verbosity == 0)
1384 strvec_push(&args, "--quiet");
1385 else if (options.verbosity > 1)
1386 strvec_push(&args, "--verbose");
1387 for (i = 0; i < options.push_options.nr; i++)
1388 strvec_pushf(&args, "--push-option=%s",
1389 options.push_options.items[i].string);
1390 strvec_push(&args, options.progress ? "--progress" : "--no-progress");
1391 for_each_string_list_item(cas_option, &cas_options)
1392 strvec_push(&args, cas_option->string);
1393 strvec_push(&args, url.buf);
1395 if (options.force_if_includes)
1396 strvec_push(&args, "--force-if-includes");
1398 strvec_push(&args, "--stdin");
1399 for (i = 0; i < nr_spec; i++)
1400 packet_buf_write(&preamble, "%s\n", specs[i]);
1401 packet_buf_flush(&preamble);
1403 memset(&rpc, 0, sizeof(rpc));
1404 rpc.service_name = "git-receive-pack",
1406 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1407 if (rpc_result.len)
1408 write_or_die(1, rpc_result.buf, rpc_result.len);
1409 strbuf_release(&rpc_result);
1410 strbuf_release(&preamble);
1411 strvec_clear(&args);
1412 return err;
1415 static int push(int nr_spec, const char **specs)
1417 struct discovery *heads = discover_refs("git-receive-pack", 1);
1418 int ret;
1420 if (heads->proto_git)
1421 ret = push_git(heads, nr_spec, specs);
1422 else
1423 ret = push_dav(nr_spec, specs);
1424 free_discovery(heads);
1425 return ret;
1428 static void parse_push(struct strbuf *buf)
1430 struct strvec specs = STRVEC_INIT;
1431 int ret;
1433 do {
1434 const char *arg;
1435 if (skip_prefix(buf->buf, "push ", &arg))
1436 strvec_push(&specs, arg);
1437 else
1438 die(_("http transport does not support %s"), buf->buf);
1440 strbuf_reset(buf);
1441 if (strbuf_getline_lf(buf, stdin) == EOF)
1442 goto free_specs;
1443 if (!*buf->buf)
1444 break;
1445 } while (1);
1447 ret = push(specs.nr, specs.v);
1448 printf("\n");
1449 fflush(stdout);
1451 if (ret)
1452 exit(128); /* error already reported */
1454 free_specs:
1455 strvec_clear(&specs);
1458 static int stateless_connect(const char *service_name)
1460 struct discovery *discover;
1461 struct rpc_state rpc = RPC_STATE_INIT;
1462 struct strbuf buf = STRBUF_INIT;
1463 const char *accept_language;
1466 * Run the info/refs request and see if the server supports protocol
1467 * v2. If and only if the server supports v2 can we successfully
1468 * establish a stateless connection, otherwise we need to tell the
1469 * client to fallback to using other transport helper functions to
1470 * complete their request.
1472 * The "git-upload-archive" service is a read-only operation. Fallback
1473 * to use "git-upload-pack" service to discover protocol version.
1475 if (!strcmp(service_name, "git-upload-archive"))
1476 discover = discover_refs("git-upload-pack", 0);
1477 else
1478 discover = discover_refs(service_name, 0);
1479 if (discover->version != protocol_v2) {
1480 printf("fallback\n");
1481 fflush(stdout);
1482 return -1;
1483 } else {
1484 /* Stateless Connection established */
1485 printf("\n");
1486 fflush(stdout);
1488 accept_language = http_get_accept_language_header();
1489 if (accept_language)
1490 rpc.hdr_accept_language = xstrfmt("%s", accept_language);
1492 rpc.service_name = service_name;
1493 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1494 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1495 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1496 if (get_protocol_http_header(discover->version, &buf)) {
1497 rpc.protocol_header = strbuf_detach(&buf, NULL);
1498 } else {
1499 rpc.protocol_header = NULL;
1500 strbuf_release(&buf);
1502 rpc.buf = xmalloc(http_post_buffer);
1503 rpc.alloc = http_post_buffer;
1504 rpc.len = 0;
1505 rpc.pos = 0;
1506 rpc.in = 1;
1507 rpc.out = 0;
1508 rpc.any_written = 0;
1509 rpc.gzip_request = 1;
1510 rpc.initial_buffer = 0;
1511 rpc.write_line_lengths = 1;
1512 rpc.flush_read_but_not_sent = 0;
1515 * Dump the capability listing that we got from the server earlier
1516 * during the info/refs request. This does not work with the
1517 * "git-upload-archive" service.
1519 if (strcmp(service_name, "git-upload-archive"))
1520 write_or_die(rpc.in, discover->buf, discover->len);
1522 /* Until we see EOF keep sending POSTs */
1523 while (1) {
1524 size_t avail;
1525 enum packet_read_status status;
1527 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1528 &status))
1529 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1530 if (status == PACKET_READ_EOF)
1531 break;
1532 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
1533 /* We would have an err here */
1534 break;
1535 /* Reset the buffer for next request */
1536 rpc.len = 0;
1539 free(rpc.service_url);
1540 free(rpc.hdr_content_type);
1541 free(rpc.hdr_accept);
1542 free(rpc.hdr_accept_language);
1543 free(rpc.protocol_header);
1544 free(rpc.buf);
1545 strbuf_release(&buf);
1547 return 0;
1550 int cmd_main(int argc, const char **argv)
1552 struct strbuf buf = STRBUF_INIT;
1553 int nongit;
1554 int ret = 1;
1556 setup_git_directory_gently(&nongit);
1557 if (argc < 2) {
1558 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1559 goto cleanup;
1562 options.verbosity = 1;
1563 options.progress = !!isatty(2);
1564 options.thin = 1;
1565 string_list_init_dup(&options.deepen_not);
1566 string_list_init_dup(&options.push_options);
1569 * Just report "remote-curl" here (folding all the various aliases
1570 * ("git-remote-http", "git-remote-https", and etc.) here since they
1571 * are all just copies of the same actual executable.
1573 trace2_cmd_name("remote-curl");
1575 remote = remote_get(argv[1]);
1577 if (argc > 2) {
1578 end_url_with_slash(&url, argv[2]);
1579 } else {
1580 end_url_with_slash(&url, remote->url.v[0]);
1583 http_init(remote, url.buf, 0);
1585 do {
1586 const char *arg;
1588 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1589 if (ferror(stdin))
1590 error(_("remote-curl: error reading command stream from git"));
1591 goto cleanup;
1593 if (buf.len == 0)
1594 break;
1595 if (starts_with(buf.buf, "fetch ")) {
1596 if (nongit) {
1597 setup_git_directory_gently(&nongit);
1598 if (nongit)
1599 die(_("remote-curl: fetch attempted without a local repo"));
1601 parse_fetch(&buf);
1603 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1604 int for_push = !!strstr(buf.buf + 4, "for-push");
1605 output_refs(get_refs(for_push));
1607 } else if (starts_with(buf.buf, "push ")) {
1608 parse_push(&buf);
1610 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1611 const char *value = strchrnul(arg, ' ');
1612 size_t arglen = value - arg;
1613 int result;
1615 if (*value)
1616 value++; /* skip over SP */
1617 else
1618 value = "true";
1620 result = set_option(arg, arglen, value);
1621 if (!result)
1622 printf("ok\n");
1623 else if (result < 0)
1624 printf("error invalid value\n");
1625 else
1626 printf("unsupported\n");
1627 fflush(stdout);
1629 } else if (skip_prefix(buf.buf, "get ", &arg)) {
1630 parse_get(arg);
1631 fflush(stdout);
1633 } else if (!strcmp(buf.buf, "capabilities")) {
1634 printf("stateless-connect\n");
1635 printf("fetch\n");
1636 printf("get\n");
1637 printf("option\n");
1638 printf("push\n");
1639 printf("check-connectivity\n");
1640 printf("object-format\n");
1641 printf("\n");
1642 fflush(stdout);
1643 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1644 if (!stateless_connect(arg))
1645 break;
1646 } else {
1647 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1648 goto cleanup;
1650 strbuf_reset(&buf);
1651 } while (1);
1653 http_cleanup();
1654 ret = 0;
1655 cleanup:
1656 strbuf_release(&buf);
1658 return ret;