Git 2.43.4
[git.git] / remote-curl.c
blob6dcdb7b991431b0a83f155b3b939b0a826aaa81f
1 #include "git-compat-util.h"
2 #include "git-curl-compat.h"
3 #include "config.h"
4 #include "environment.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "remote.h"
8 #include "connect.h"
9 #include "strbuf.h"
10 #include "walker.h"
11 #include "http.h"
12 #include "run-command.h"
13 #include "pkt-line.h"
14 #include "string-list.h"
15 #include "strvec.h"
16 #include "credential.h"
17 #include "oid-array.h"
18 #include "send-pack.h"
19 #include "setup.h"
20 #include "protocol.h"
21 #include "quote.h"
22 #include "trace2.h"
23 #include "transport.h"
24 #include "url.h"
25 #include "write-or-die.h"
27 static struct remote *remote;
28 /* always ends with a trailing slash */
29 static struct strbuf url = STRBUF_INIT;
31 struct options {
32 int verbosity;
33 unsigned long depth;
34 char *deepen_since;
35 struct string_list deepen_not;
36 struct string_list push_options;
37 char *filter;
38 unsigned progress : 1,
39 check_self_contained_and_connected : 1,
40 cloning : 1,
41 update_shallow : 1,
42 followtags : 1,
43 dry_run : 1,
44 thin : 1,
45 /* One of the SEND_PACK_PUSH_CERT_* constants. */
46 push_cert : 2,
47 deepen_relative : 1,
49 /* see documentation of corresponding flag in fetch-pack.h */
50 from_promisor : 1,
52 refetch : 1,
53 atomic : 1,
54 object_format : 1,
55 force_if_includes : 1;
56 const struct git_hash_algo *hash_algo;
58 static struct options options;
59 static struct string_list cas_options = STRING_LIST_INIT_DUP;
61 static int set_option(const char *name, const char *value)
63 if (!strcmp(name, "verbosity")) {
64 char *end;
65 int v = strtol(value, &end, 10);
66 if (value == end || *end)
67 return -1;
68 options.verbosity = v;
69 return 0;
71 else if (!strcmp(name, "progress")) {
72 if (!strcmp(value, "true"))
73 options.progress = 1;
74 else if (!strcmp(value, "false"))
75 options.progress = 0;
76 else
77 return -1;
78 return 0;
80 else if (!strcmp(name, "depth")) {
81 char *end;
82 unsigned long v = strtoul(value, &end, 10);
83 if (value == end || *end)
84 return -1;
85 options.depth = v;
86 return 0;
88 else if (!strcmp(name, "deepen-since")) {
89 options.deepen_since = xstrdup(value);
90 return 0;
92 else if (!strcmp(name, "deepen-not")) {
93 string_list_append(&options.deepen_not, value);
94 return 0;
96 else if (!strcmp(name, "deepen-relative")) {
97 if (!strcmp(value, "true"))
98 options.deepen_relative = 1;
99 else if (!strcmp(value, "false"))
100 options.deepen_relative = 0;
101 else
102 return -1;
103 return 0;
105 else if (!strcmp(name, "followtags")) {
106 if (!strcmp(value, "true"))
107 options.followtags = 1;
108 else if (!strcmp(value, "false"))
109 options.followtags = 0;
110 else
111 return -1;
112 return 0;
114 else if (!strcmp(name, "dry-run")) {
115 if (!strcmp(value, "true"))
116 options.dry_run = 1;
117 else if (!strcmp(value, "false"))
118 options.dry_run = 0;
119 else
120 return -1;
121 return 0;
123 else if (!strcmp(name, "check-connectivity")) {
124 if (!strcmp(value, "true"))
125 options.check_self_contained_and_connected = 1;
126 else if (!strcmp(value, "false"))
127 options.check_self_contained_and_connected = 0;
128 else
129 return -1;
130 return 0;
132 else if (!strcmp(name, "cas")) {
133 struct strbuf val = STRBUF_INIT;
134 strbuf_addstr(&val, "--force-with-lease=");
135 if (*value != '"')
136 strbuf_addstr(&val, value);
137 else if (unquote_c_style(&val, value, NULL))
138 return -1;
139 string_list_append(&cas_options, val.buf);
140 strbuf_release(&val);
141 return 0;
142 } else if (!strcmp(name, TRANS_OPT_FORCE_IF_INCLUDES)) {
143 if (!strcmp(value, "true"))
144 options.force_if_includes = 1;
145 else if (!strcmp(value, "false"))
146 options.force_if_includes = 0;
147 else
148 return -1;
149 return 0;
150 } else if (!strcmp(name, "cloning")) {
151 if (!strcmp(value, "true"))
152 options.cloning = 1;
153 else if (!strcmp(value, "false"))
154 options.cloning = 0;
155 else
156 return -1;
157 return 0;
158 } else if (!strcmp(name, "update-shallow")) {
159 if (!strcmp(value, "true"))
160 options.update_shallow = 1;
161 else if (!strcmp(value, "false"))
162 options.update_shallow = 0;
163 else
164 return -1;
165 return 0;
166 } else if (!strcmp(name, "pushcert")) {
167 if (!strcmp(value, "true"))
168 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
169 else if (!strcmp(value, "false"))
170 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
171 else if (!strcmp(value, "if-asked"))
172 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
173 else
174 return -1;
175 return 0;
176 } else if (!strcmp(name, "atomic")) {
177 if (!strcmp(value, "true"))
178 options.atomic = 1;
179 else if (!strcmp(value, "false"))
180 options.atomic = 0;
181 else
182 return -1;
183 return 0;
184 } else if (!strcmp(name, "push-option")) {
185 if (*value != '"')
186 string_list_append(&options.push_options, value);
187 else {
188 struct strbuf unquoted = STRBUF_INIT;
189 if (unquote_c_style(&unquoted, value, NULL) < 0)
190 die(_("invalid quoting in push-option value: '%s'"), value);
191 string_list_append_nodup(&options.push_options,
192 strbuf_detach(&unquoted, NULL));
194 return 0;
195 } else if (!strcmp(name, "family")) {
196 if (!strcmp(value, "ipv4"))
197 git_curl_ipresolve = CURL_IPRESOLVE_V4;
198 else if (!strcmp(value, "ipv6"))
199 git_curl_ipresolve = CURL_IPRESOLVE_V6;
200 else if (!strcmp(value, "all"))
201 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
202 else
203 return -1;
204 return 0;
205 } else if (!strcmp(name, "from-promisor")) {
206 options.from_promisor = 1;
207 return 0;
208 } else if (!strcmp(name, "refetch")) {
209 options.refetch = 1;
210 return 0;
211 } else if (!strcmp(name, "filter")) {
212 options.filter = xstrdup(value);
213 return 0;
214 } else if (!strcmp(name, "object-format")) {
215 int algo;
216 options.object_format = 1;
217 if (strcmp(value, "true")) {
218 algo = hash_algo_by_name(value);
219 if (algo == GIT_HASH_UNKNOWN)
220 die("unknown object format '%s'", value);
221 options.hash_algo = &hash_algos[algo];
223 return 0;
224 } else {
225 return 1 /* unsupported */;
229 struct discovery {
230 char *service;
231 char *buf_alloc;
232 char *buf;
233 size_t len;
234 struct ref *refs;
235 struct oid_array shallow;
236 enum protocol_version version;
237 unsigned proto_git : 1;
239 static struct discovery *last_discovery;
241 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
243 struct ref *list = NULL;
244 struct packet_reader reader;
246 packet_reader_init(&reader, -1, heads->buf, heads->len,
247 PACKET_READ_CHOMP_NEWLINE |
248 PACKET_READ_GENTLE_ON_EOF |
249 PACKET_READ_DIE_ON_ERR_PACKET);
251 heads->version = discover_version(&reader);
252 switch (heads->version) {
253 case protocol_v2:
255 * Do nothing. This isn't a list of refs but rather a
256 * capability advertisement. Client would have run
257 * 'stateless-connect' so we'll dump this capability listing
258 * and let them request the refs themselves.
260 break;
261 case protocol_v1:
262 case protocol_v0:
263 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
264 NULL, &heads->shallow);
265 options.hash_algo = reader.hash_algo;
266 break;
267 case protocol_unknown_version:
268 BUG("unknown protocol version");
271 return list;
274 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
276 const char *p = memchr(heads->buf, '\t', heads->len);
277 int algo;
278 if (!p)
279 return the_hash_algo;
281 algo = hash_algo_by_length((p - heads->buf) / 2);
282 if (algo == GIT_HASH_UNKNOWN)
283 return NULL;
284 return &hash_algos[algo];
287 static struct ref *parse_info_refs(struct discovery *heads)
289 char *data, *start, *mid;
290 char *ref_name;
291 int i = 0;
293 struct ref *refs = NULL;
294 struct ref *ref = NULL;
295 struct ref *last_ref = NULL;
297 options.hash_algo = detect_hash_algo(heads);
298 if (!options.hash_algo)
299 die("%sinfo/refs not valid: could not determine hash algorithm; "
300 "is this a git repository?",
301 transport_anonymize_url(url.buf));
303 data = heads->buf;
304 start = NULL;
305 mid = data;
306 while (i < heads->len) {
307 if (!start) {
308 start = &data[i];
310 if (data[i] == '\t')
311 mid = &data[i];
312 if (data[i] == '\n') {
313 if (mid - start != options.hash_algo->hexsz)
314 die(_("%sinfo/refs not valid: is this a git repository?"),
315 transport_anonymize_url(url.buf));
316 data[i] = 0;
317 ref_name = mid + 1;
318 ref = alloc_ref(ref_name);
319 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
320 if (!refs)
321 refs = ref;
322 if (last_ref)
323 last_ref->next = ref;
324 last_ref = ref;
325 start = NULL;
327 i++;
330 ref = alloc_ref("HEAD");
331 if (!http_fetch_ref(url.buf, ref) &&
332 !resolve_remote_symref(ref, refs)) {
333 ref->next = refs;
334 refs = ref;
335 } else {
336 free(ref);
339 return refs;
342 static void free_discovery(struct discovery *d)
344 if (d) {
345 if (d == last_discovery)
346 last_discovery = NULL;
347 free(d->shallow.oid);
348 free(d->buf_alloc);
349 free_refs(d->refs);
350 free(d->service);
351 free(d);
355 static int show_http_message(struct strbuf *type, struct strbuf *charset,
356 struct strbuf *msg)
358 const char *p, *eol;
361 * We only show text/plain parts, as other types are likely
362 * to be ugly to look at on the user's terminal.
364 if (strcmp(type->buf, "text/plain"))
365 return -1;
366 if (charset->len)
367 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
369 strbuf_trim(msg);
370 if (!msg->len)
371 return -1;
373 p = msg->buf;
374 do {
375 eol = strchrnul(p, '\n');
376 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
377 p = eol + 1;
378 } while(*eol);
379 return 0;
382 static int get_protocol_http_header(enum protocol_version version,
383 struct strbuf *header)
385 if (version > 0) {
386 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
387 version);
389 return 1;
392 return 0;
395 static void check_smart_http(struct discovery *d, const char *service,
396 struct strbuf *type)
398 const char *p;
399 struct packet_reader reader;
402 * If we don't see x-$service-advertisement, then it's not smart-http.
403 * But once we do, we commit to it and assume any other protocol
404 * violations are hard errors.
406 if (!skip_prefix(type->buf, "application/x-", &p) ||
407 !skip_prefix(p, service, &p) ||
408 strcmp(p, "-advertisement"))
409 return;
411 packet_reader_init(&reader, -1, d->buf, d->len,
412 PACKET_READ_CHOMP_NEWLINE |
413 PACKET_READ_DIE_ON_ERR_PACKET);
414 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
415 die(_("invalid server response; expected service, got flush packet"));
417 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
419 * The header can include additional metadata lines, up
420 * until a packet flush marker. Ignore these now, but
421 * in the future we might start to scan them.
423 for (;;) {
424 packet_reader_read(&reader);
425 if (reader.pktlen <= 0) {
426 break;
431 * v0 smart http; callers expect us to soak up the
432 * service and header packets
434 d->buf = reader.src_buffer;
435 d->len = reader.src_len;
436 d->proto_git = 1;
438 } else if (!strcmp(reader.line, "version 2")) {
440 * v2 smart http; do not consume version packet, which will
441 * be handled elsewhere.
443 d->proto_git = 1;
445 } else {
446 die(_("invalid server response; got '%s'"), reader.line);
450 static struct discovery *discover_refs(const char *service, int for_push)
452 struct strbuf type = STRBUF_INIT;
453 struct strbuf charset = STRBUF_INIT;
454 struct strbuf buffer = STRBUF_INIT;
455 struct strbuf refs_url = STRBUF_INIT;
456 struct strbuf effective_url = STRBUF_INIT;
457 struct strbuf protocol_header = STRBUF_INIT;
458 struct string_list extra_headers = STRING_LIST_INIT_DUP;
459 struct discovery *last = last_discovery;
460 int http_ret, maybe_smart = 0;
461 struct http_get_options http_options;
462 enum protocol_version version = get_protocol_version_config();
464 if (last && !strcmp(service, last->service))
465 return last;
466 free_discovery(last);
468 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
469 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
470 git_env_bool("GIT_SMART_HTTP", 1)) {
471 maybe_smart = 1;
472 if (!strchr(url.buf, '?'))
473 strbuf_addch(&refs_url, '?');
474 else
475 strbuf_addch(&refs_url, '&');
476 strbuf_addf(&refs_url, "service=%s", service);
480 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
481 * to perform any operation that doesn't involve upload-pack (i.e., a
482 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
483 * to do anything else (like push or remote archive) via v2.
485 if (version == protocol_v2 && strcmp("git-upload-pack", service))
486 version = protocol_v0;
488 /* Add the extra Git-Protocol header */
489 if (get_protocol_http_header(version, &protocol_header))
490 string_list_append(&extra_headers, protocol_header.buf);
492 memset(&http_options, 0, sizeof(http_options));
493 http_options.content_type = &type;
494 http_options.charset = &charset;
495 http_options.effective_url = &effective_url;
496 http_options.base_url = &url;
497 http_options.extra_headers = &extra_headers;
498 http_options.initial_request = 1;
499 http_options.no_cache = 1;
501 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
502 switch (http_ret) {
503 case HTTP_OK:
504 break;
505 case HTTP_MISSING_TARGET:
506 show_http_message(&type, &charset, &buffer);
507 die(_("repository '%s' not found"),
508 transport_anonymize_url(url.buf));
509 case HTTP_NOAUTH:
510 show_http_message(&type, &charset, &buffer);
511 die(_("Authentication failed for '%s'"),
512 transport_anonymize_url(url.buf));
513 case HTTP_NOMATCHPUBLICKEY:
514 show_http_message(&type, &charset, &buffer);
515 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
516 transport_anonymize_url(url.buf), curl_errorstr);
517 default:
518 show_http_message(&type, &charset, &buffer);
519 die(_("unable to access '%s': %s"),
520 transport_anonymize_url(url.buf), curl_errorstr);
523 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
524 char *u = transport_anonymize_url(url.buf);
525 warning(_("redirecting to %s"), u);
526 free(u);
529 last= xcalloc(1, sizeof(*last_discovery));
530 last->service = xstrdup(service);
531 last->buf_alloc = strbuf_detach(&buffer, &last->len);
532 last->buf = last->buf_alloc;
534 if (maybe_smart)
535 check_smart_http(last, service, &type);
537 if (last->proto_git)
538 last->refs = parse_git_refs(last, for_push);
539 else
540 last->refs = parse_info_refs(last);
542 strbuf_release(&refs_url);
543 strbuf_release(&type);
544 strbuf_release(&charset);
545 strbuf_release(&effective_url);
546 strbuf_release(&buffer);
547 strbuf_release(&protocol_header);
548 string_list_clear(&extra_headers, 0);
549 last_discovery = last;
550 return last;
553 static struct ref *get_refs(int for_push)
555 struct discovery *heads;
557 if (for_push)
558 heads = discover_refs("git-receive-pack", for_push);
559 else
560 heads = discover_refs("git-upload-pack", for_push);
562 return heads->refs;
565 static void output_refs(struct ref *refs)
567 struct ref *posn;
568 if (options.object_format && options.hash_algo) {
569 printf(":object-format %s\n", options.hash_algo->name);
570 repo_set_hash_algo(the_repository,
571 hash_algo_by_ptr(options.hash_algo));
573 for (posn = refs; posn; posn = posn->next) {
574 if (posn->symref)
575 printf("@%s %s\n", posn->symref, posn->name);
576 else
577 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
578 options.hash_algo),
579 posn->name);
581 printf("\n");
582 fflush(stdout);
585 struct rpc_state {
586 const char *service_name;
587 char *service_url;
588 char *hdr_content_type;
589 char *hdr_accept;
590 char *hdr_accept_language;
591 char *protocol_header;
592 char *buf;
593 size_t alloc;
594 size_t len;
595 size_t pos;
596 int in;
597 int out;
598 int any_written;
599 unsigned gzip_request : 1;
600 unsigned initial_buffer : 1;
603 * Whenever a pkt-line is read into buf, append the 4 characters
604 * denoting its length before appending the payload.
606 unsigned write_line_lengths : 1;
609 * Used by rpc_out; initialize to 0. This is true if a flush has been
610 * read, but the corresponding line length (if write_line_lengths is
611 * true) and EOF have not been sent to libcurl. Since each flush marks
612 * the end of a request, each flush must be completely sent before any
613 * further reading occurs.
615 unsigned flush_read_but_not_sent : 1;
618 #define RPC_STATE_INIT { 0 }
621 * Appends the result of reading from rpc->out to the string represented by
622 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
623 * enough space, 0 otherwise.
625 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
626 * hexadecimal string before appending the result described above.
628 * Writes the total number of bytes appended into appended.
630 static int rpc_read_from_out(struct rpc_state *rpc, int options,
631 size_t *appended,
632 enum packet_read_status *status) {
633 size_t left;
634 char *buf;
635 int pktlen_raw;
637 if (rpc->write_line_lengths) {
638 left = rpc->alloc - rpc->len - 4;
639 buf = rpc->buf + rpc->len + 4;
640 } else {
641 left = rpc->alloc - rpc->len;
642 buf = rpc->buf + rpc->len;
645 if (left < LARGE_PACKET_MAX)
646 return 0;
648 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
649 left, &pktlen_raw, options);
650 if (*status != PACKET_READ_EOF) {
651 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
652 rpc->len += *appended;
655 if (rpc->write_line_lengths) {
656 switch (*status) {
657 case PACKET_READ_EOF:
658 if (!(options & PACKET_READ_GENTLE_ON_EOF))
659 die(_("shouldn't have EOF when not gentle on EOF"));
660 break;
661 case PACKET_READ_NORMAL:
662 set_packet_header(buf - 4, *appended);
663 break;
664 case PACKET_READ_DELIM:
665 memcpy(buf - 4, "0001", 4);
666 break;
667 case PACKET_READ_FLUSH:
668 memcpy(buf - 4, "0000", 4);
669 break;
670 case PACKET_READ_RESPONSE_END:
671 die(_("remote server sent unexpected response end packet"));
675 return 1;
678 static size_t rpc_out(void *ptr, size_t eltsize,
679 size_t nmemb, void *buffer_)
681 size_t max = eltsize * nmemb;
682 struct rpc_state *rpc = buffer_;
683 size_t avail = rpc->len - rpc->pos;
684 enum packet_read_status status;
686 if (!avail) {
687 rpc->initial_buffer = 0;
688 rpc->len = 0;
689 rpc->pos = 0;
690 if (!rpc->flush_read_but_not_sent) {
691 if (!rpc_read_from_out(rpc, 0, &avail, &status))
692 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
693 if (status == PACKET_READ_FLUSH)
694 rpc->flush_read_but_not_sent = 1;
697 * If flush_read_but_not_sent is true, we have already read one
698 * full request but have not fully sent it + EOF, which is why
699 * we need to refrain from reading.
702 if (rpc->flush_read_but_not_sent) {
703 if (!avail) {
705 * The line length either does not need to be sent at
706 * all or has already been completely sent. Now we can
707 * return 0, indicating EOF, meaning that the flush has
708 * been fully sent.
710 rpc->flush_read_but_not_sent = 0;
711 return 0;
714 * If avail is non-zero, the line length for the flush still
715 * hasn't been fully sent. Proceed with sending the line
716 * length.
720 if (max < avail)
721 avail = max;
722 memcpy(ptr, rpc->buf + rpc->pos, avail);
723 rpc->pos += avail;
724 return avail;
727 static int rpc_seek(void *clientp, curl_off_t offset, int origin)
729 struct rpc_state *rpc = clientp;
731 if (origin != SEEK_SET)
732 BUG("rpc_seek only handles SEEK_SET, not %d", origin);
734 if (rpc->initial_buffer) {
735 if (offset < 0 || offset > rpc->len) {
736 error("curl seek would be outside of rpc buffer");
737 return CURL_SEEKFUNC_FAIL;
739 rpc->pos = offset;
740 return CURL_SEEKFUNC_OK;
742 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
743 return CURL_SEEKFUNC_FAIL;
746 struct check_pktline_state {
747 char len_buf[4];
748 int len_filled;
749 int remaining;
752 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
754 while (size) {
755 if (!state->remaining) {
756 int digits_remaining = 4 - state->len_filled;
757 if (digits_remaining > size)
758 digits_remaining = size;
759 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
760 state->len_filled += digits_remaining;
761 ptr += digits_remaining;
762 size -= digits_remaining;
764 if (state->len_filled == 4) {
765 state->remaining = packet_length(state->len_buf,
766 sizeof(state->len_buf));
767 if (state->remaining < 0) {
768 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
769 } else if (state->remaining == 2) {
770 die(_("remote-curl: unexpected response end packet"));
771 } else if (state->remaining < 4) {
772 state->remaining = 0;
773 } else {
774 state->remaining -= 4;
776 state->len_filled = 0;
780 if (state->remaining) {
781 int remaining = state->remaining;
782 if (remaining > size)
783 remaining = size;
784 ptr += remaining;
785 size -= remaining;
786 state->remaining -= remaining;
791 struct rpc_in_data {
792 struct rpc_state *rpc;
793 struct active_request_slot *slot;
794 int check_pktline;
795 struct check_pktline_state pktline_state;
799 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
800 * from ptr.
802 static size_t rpc_in(char *ptr, size_t eltsize,
803 size_t nmemb, void *buffer_)
805 size_t size = eltsize * nmemb;
806 struct rpc_in_data *data = buffer_;
807 long response_code;
809 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
810 &response_code) != CURLE_OK)
811 return size;
812 if (response_code >= 300)
813 return size;
814 if (size)
815 data->rpc->any_written = 1;
816 if (data->check_pktline)
817 check_pktline(&data->pktline_state, ptr, size);
818 write_or_die(data->rpc->in, ptr, size);
819 return size;
822 static int run_slot(struct active_request_slot *slot,
823 struct slot_results *results)
825 int err;
826 struct slot_results results_buf;
828 if (!results)
829 results = &results_buf;
831 err = run_one_slot(slot, results);
833 if (err != HTTP_OK && err != HTTP_REAUTH) {
834 struct strbuf msg = STRBUF_INIT;
835 if (results->http_code && results->http_code != 200)
836 strbuf_addf(&msg, "HTTP %ld", results->http_code);
837 if (results->curl_result != CURLE_OK) {
838 if (msg.len)
839 strbuf_addch(&msg, ' ');
840 strbuf_addf(&msg, "curl %d", results->curl_result);
841 if (curl_errorstr[0]) {
842 strbuf_addch(&msg, ' ');
843 strbuf_addstr(&msg, curl_errorstr);
846 error(_("RPC failed; %s"), msg.buf);
847 strbuf_release(&msg);
850 return err;
853 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
855 struct active_request_slot *slot;
856 struct curl_slist *headers = http_copy_default_headers();
857 struct strbuf buf = STRBUF_INIT;
858 int err;
860 slot = get_active_slot();
862 headers = curl_slist_append(headers, rpc->hdr_content_type);
863 headers = curl_slist_append(headers, rpc->hdr_accept);
865 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
866 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
867 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
868 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
869 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
870 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
871 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
872 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
873 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf);
875 err = run_slot(slot, results);
877 curl_slist_free_all(headers);
878 strbuf_release(&buf);
879 return err;
882 static curl_off_t xcurl_off_t(size_t len)
884 uintmax_t size = len;
885 if (size > maximum_signed_value_of_type(curl_off_t))
886 die(_("cannot handle pushes this big"));
887 return (curl_off_t)size;
891 * If flush_received is true, do not attempt to read any more; just use what's
892 * in rpc->buf.
894 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
896 struct active_request_slot *slot;
897 struct curl_slist *headers = http_copy_default_headers();
898 int use_gzip = rpc->gzip_request;
899 char *gzip_body = NULL;
900 size_t gzip_size = 0;
901 int err, large_request = 0;
902 int needs_100_continue = 0;
903 struct rpc_in_data rpc_in_data;
905 /* Try to load the entire request, if we can fit it into the
906 * allocated buffer space we can use HTTP/1.0 and avoid the
907 * chunked encoding mess.
909 if (!flush_received) {
910 while (1) {
911 size_t n;
912 enum packet_read_status status;
914 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
915 large_request = 1;
916 use_gzip = 0;
917 break;
919 if (status == PACKET_READ_FLUSH)
920 break;
924 if (large_request) {
925 struct slot_results results;
927 do {
928 err = probe_rpc(rpc, &results);
929 if (err == HTTP_REAUTH)
930 credential_fill(&http_auth);
931 } while (err == HTTP_REAUTH);
932 if (err != HTTP_OK)
933 return -1;
935 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
936 needs_100_continue = 1;
939 headers = curl_slist_append(headers, rpc->hdr_content_type);
940 headers = curl_slist_append(headers, rpc->hdr_accept);
941 headers = curl_slist_append(headers, needs_100_continue ?
942 "Expect: 100-continue" : "Expect:");
944 /* Add Accept-Language header */
945 if (rpc->hdr_accept_language)
946 headers = curl_slist_append(headers, rpc->hdr_accept_language);
948 /* Add the extra Git-Protocol header */
949 if (rpc->protocol_header)
950 headers = curl_slist_append(headers, rpc->protocol_header);
952 retry:
953 slot = get_active_slot();
955 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
956 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
957 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
958 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
960 if (large_request) {
961 /* The request body is large and the size cannot be predicted.
962 * We must use chunked encoding to send it.
964 #ifdef GIT_CURL_NEED_TRANSFER_ENCODING_HEADER
965 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
966 #endif
967 rpc->initial_buffer = 1;
968 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
969 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
970 curl_easy_setopt(slot->curl, CURLOPT_SEEKFUNCTION, rpc_seek);
971 curl_easy_setopt(slot->curl, CURLOPT_SEEKDATA, rpc);
972 if (options.verbosity > 1) {
973 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
974 fflush(stderr);
977 } else if (gzip_body) {
979 * If we are looping to retry authentication, then the previous
980 * run will have set up the headers and gzip buffer already,
981 * and we just need to send it.
983 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
984 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
986 } else if (use_gzip && 1024 < rpc->len) {
987 /* The client backend isn't giving us compressed data so
988 * we can try to deflate it ourselves, this may save on
989 * the transfer time.
991 git_zstream stream;
992 int ret;
994 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
995 gzip_size = git_deflate_bound(&stream, rpc->len);
996 gzip_body = xmalloc(gzip_size);
998 stream.next_in = (unsigned char *)rpc->buf;
999 stream.avail_in = rpc->len;
1000 stream.next_out = (unsigned char *)gzip_body;
1001 stream.avail_out = gzip_size;
1003 ret = git_deflate(&stream, Z_FINISH);
1004 if (ret != Z_STREAM_END)
1005 die(_("cannot deflate request; zlib deflate error %d"), ret);
1007 ret = git_deflate_end_gently(&stream);
1008 if (ret != Z_OK)
1009 die(_("cannot deflate request; zlib end error %d"), ret);
1011 gzip_size = stream.total_out;
1013 headers = curl_slist_append(headers, "Content-Encoding: gzip");
1014 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
1015 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
1017 if (options.verbosity > 1) {
1018 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
1019 rpc->service_name,
1020 (unsigned long)rpc->len, (unsigned long)gzip_size);
1021 fflush(stderr);
1023 } else {
1024 /* We know the complete request size in advance, use the
1025 * more normal Content-Length approach.
1027 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1028 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1029 if (options.verbosity > 1) {
1030 fprintf(stderr, "POST %s (%lu bytes)\n",
1031 rpc->service_name, (unsigned long)rpc->len);
1032 fflush(stderr);
1036 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1037 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
1038 rpc_in_data.rpc = rpc;
1039 rpc_in_data.slot = slot;
1040 rpc_in_data.check_pktline = stateless_connect;
1041 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
1042 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &rpc_in_data);
1043 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1046 rpc->any_written = 0;
1047 err = run_slot(slot, NULL);
1048 if (err == HTTP_REAUTH && !large_request) {
1049 credential_fill(&http_auth);
1050 goto retry;
1052 if (err != HTTP_OK)
1053 err = -1;
1055 if (!rpc->any_written)
1056 err = -1;
1058 if (rpc_in_data.pktline_state.len_filled)
1059 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1060 if (rpc_in_data.pktline_state.remaining)
1061 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1063 if (stateless_connect)
1064 packet_response_end(rpc->in);
1066 curl_slist_free_all(headers);
1067 free(gzip_body);
1068 return err;
1071 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
1072 const char **client_argv, const struct strbuf *preamble,
1073 struct strbuf *rpc_result)
1075 const char *svc = rpc->service_name;
1076 struct strbuf buf = STRBUF_INIT;
1077 struct child_process client = CHILD_PROCESS_INIT;
1078 int err = 0;
1080 client.in = -1;
1081 client.out = -1;
1082 client.git_cmd = 1;
1083 strvec_pushv(&client.args, client_argv);
1084 if (start_command(&client))
1085 exit(1);
1086 write_or_die(client.in, preamble->buf, preamble->len);
1087 if (heads)
1088 write_or_die(client.in, heads->buf, heads->len);
1090 rpc->alloc = http_post_buffer;
1091 rpc->buf = xmalloc(rpc->alloc);
1092 rpc->in = client.in;
1093 rpc->out = client.out;
1095 strbuf_addf(&buf, "%s%s", url.buf, svc);
1096 rpc->service_url = strbuf_detach(&buf, NULL);
1098 rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
1100 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1101 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1103 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
1104 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1106 if (get_protocol_http_header(heads->version, &buf))
1107 rpc->protocol_header = strbuf_detach(&buf, NULL);
1108 else
1109 rpc->protocol_header = NULL;
1111 while (!err) {
1112 int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0);
1113 if (!n)
1114 break;
1115 rpc->pos = 0;
1116 rpc->len = n;
1117 err |= post_rpc(rpc, 0, 0);
1120 close(client.in);
1121 client.in = -1;
1122 if (!err) {
1123 strbuf_read(rpc_result, client.out, 0);
1124 } else {
1125 char buf[4096];
1126 for (;;)
1127 if (xread(client.out, buf, sizeof(buf)) <= 0)
1128 break;
1131 close(client.out);
1132 client.out = -1;
1134 err |= finish_command(&client);
1135 free(rpc->service_url);
1136 free(rpc->hdr_content_type);
1137 free(rpc->hdr_accept);
1138 free(rpc->hdr_accept_language);
1139 free(rpc->protocol_header);
1140 free(rpc->buf);
1141 strbuf_release(&buf);
1142 return err;
1145 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1147 struct walker *walker;
1148 char **targets;
1149 int ret, i;
1151 ALLOC_ARRAY(targets, nr_heads);
1152 if (options.depth || options.deepen_since)
1153 die(_("dumb http transport does not support shallow capabilities"));
1154 for (i = 0; i < nr_heads; i++)
1155 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1157 walker = get_http_walker(url.buf);
1158 walker->get_verbosely = options.verbosity >= 3;
1159 walker->get_progress = options.progress;
1160 walker->get_recover = 0;
1161 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1162 walker_free(walker);
1164 for (i = 0; i < nr_heads; i++)
1165 free(targets[i]);
1166 free(targets);
1168 return ret ? error(_("fetch failed.")) : 0;
1171 static int fetch_git(struct discovery *heads,
1172 int nr_heads, struct ref **to_fetch)
1174 struct rpc_state rpc = RPC_STATE_INIT;
1175 struct strbuf preamble = STRBUF_INIT;
1176 int i, err;
1177 struct strvec args = STRVEC_INIT;
1178 struct strbuf rpc_result = STRBUF_INIT;
1180 strvec_pushl(&args, "fetch-pack", "--stateless-rpc",
1181 "--stdin", "--lock-pack", NULL);
1182 if (options.followtags)
1183 strvec_push(&args, "--include-tag");
1184 if (options.thin)
1185 strvec_push(&args, "--thin");
1186 if (options.verbosity >= 3)
1187 strvec_pushl(&args, "-v", "-v", NULL);
1188 if (options.check_self_contained_and_connected)
1189 strvec_push(&args, "--check-self-contained-and-connected");
1190 if (options.cloning)
1191 strvec_push(&args, "--cloning");
1192 if (options.update_shallow)
1193 strvec_push(&args, "--update-shallow");
1194 if (!options.progress)
1195 strvec_push(&args, "--no-progress");
1196 if (options.depth)
1197 strvec_pushf(&args, "--depth=%lu", options.depth);
1198 if (options.deepen_since)
1199 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since);
1200 for (i = 0; i < options.deepen_not.nr; i++)
1201 strvec_pushf(&args, "--shallow-exclude=%s",
1202 options.deepen_not.items[i].string);
1203 if (options.deepen_relative && options.depth)
1204 strvec_push(&args, "--deepen-relative");
1205 if (options.from_promisor)
1206 strvec_push(&args, "--from-promisor");
1207 if (options.refetch)
1208 strvec_push(&args, "--refetch");
1209 if (options.filter)
1210 strvec_pushf(&args, "--filter=%s", options.filter);
1211 strvec_push(&args, url.buf);
1213 for (i = 0; i < nr_heads; i++) {
1214 struct ref *ref = to_fetch[i];
1215 if (!*ref->name)
1216 die(_("cannot fetch by sha1 over smart http"));
1217 packet_buf_write(&preamble, "%s %s\n",
1218 oid_to_hex(&ref->old_oid), ref->name);
1220 packet_buf_flush(&preamble);
1222 memset(&rpc, 0, sizeof(rpc));
1223 rpc.service_name = "git-upload-pack",
1224 rpc.gzip_request = 1;
1226 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1227 if (rpc_result.len)
1228 write_or_die(1, rpc_result.buf, rpc_result.len);
1229 strbuf_release(&rpc_result);
1230 strbuf_release(&preamble);
1231 strvec_clear(&args);
1232 return err;
1235 static int fetch(int nr_heads, struct ref **to_fetch)
1237 struct discovery *d = discover_refs("git-upload-pack", 0);
1238 if (d->proto_git)
1239 return fetch_git(d, nr_heads, to_fetch);
1240 else
1241 return fetch_dumb(nr_heads, to_fetch);
1244 static void parse_fetch(struct strbuf *buf)
1246 struct ref **to_fetch = NULL;
1247 struct ref *list_head = NULL;
1248 struct ref **list = &list_head;
1249 int alloc_heads = 0, nr_heads = 0;
1251 do {
1252 const char *p;
1253 if (skip_prefix(buf->buf, "fetch ", &p)) {
1254 const char *name;
1255 struct ref *ref;
1256 struct object_id old_oid;
1257 const char *q;
1259 if (parse_oid_hex(p, &old_oid, &q))
1260 die(_("protocol error: expected sha/ref, got '%s'"), p);
1261 if (*q == ' ')
1262 name = q + 1;
1263 else if (!*q)
1264 name = "";
1265 else
1266 die(_("protocol error: expected sha/ref, got '%s'"), p);
1268 ref = alloc_ref(name);
1269 oidcpy(&ref->old_oid, &old_oid);
1271 *list = ref;
1272 list = &ref->next;
1274 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1275 to_fetch[nr_heads++] = ref;
1277 else
1278 die(_("http transport does not support %s"), buf->buf);
1280 strbuf_reset(buf);
1281 if (strbuf_getline_lf(buf, stdin) == EOF)
1282 return;
1283 if (!*buf->buf)
1284 break;
1285 } while (1);
1287 if (fetch(nr_heads, to_fetch))
1288 exit(128); /* error already reported */
1289 free_refs(list_head);
1290 free(to_fetch);
1292 printf("\n");
1293 fflush(stdout);
1294 strbuf_reset(buf);
1297 static void parse_get(const char *arg)
1299 struct strbuf url = STRBUF_INIT;
1300 struct strbuf path = STRBUF_INIT;
1301 const char *space;
1303 space = strchr(arg, ' ');
1305 if (!space)
1306 die(_("protocol error: expected '<url> <path>', missing space"));
1308 strbuf_add(&url, arg, space - arg);
1309 strbuf_addstr(&path, space + 1);
1311 if (http_get_file(url.buf, path.buf, NULL))
1312 die(_("failed to download file at URL '%s'"), url.buf);
1314 strbuf_release(&url);
1315 strbuf_release(&path);
1316 printf("\n");
1317 fflush(stdout);
1320 static int push_dav(int nr_spec, const char **specs)
1322 struct child_process child = CHILD_PROCESS_INIT;
1323 size_t i;
1325 child.git_cmd = 1;
1326 strvec_push(&child.args, "http-push");
1327 strvec_push(&child.args, "--helper-status");
1328 if (options.dry_run)
1329 strvec_push(&child.args, "--dry-run");
1330 if (options.verbosity > 1)
1331 strvec_push(&child.args, "--verbose");
1332 strvec_push(&child.args, url.buf);
1333 for (i = 0; i < nr_spec; i++)
1334 strvec_push(&child.args, specs[i]);
1336 if (run_command(&child))
1337 die(_("git-http-push failed"));
1338 return 0;
1341 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1343 struct rpc_state rpc = RPC_STATE_INIT;
1344 int i, err;
1345 struct strvec args;
1346 struct string_list_item *cas_option;
1347 struct strbuf preamble = STRBUF_INIT;
1348 struct strbuf rpc_result = STRBUF_INIT;
1350 strvec_init(&args);
1351 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1352 NULL);
1354 if (options.thin)
1355 strvec_push(&args, "--thin");
1356 if (options.dry_run)
1357 strvec_push(&args, "--dry-run");
1358 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1359 strvec_push(&args, "--signed=yes");
1360 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1361 strvec_push(&args, "--signed=if-asked");
1362 if (options.atomic)
1363 strvec_push(&args, "--atomic");
1364 if (options.verbosity == 0)
1365 strvec_push(&args, "--quiet");
1366 else if (options.verbosity > 1)
1367 strvec_push(&args, "--verbose");
1368 for (i = 0; i < options.push_options.nr; i++)
1369 strvec_pushf(&args, "--push-option=%s",
1370 options.push_options.items[i].string);
1371 strvec_push(&args, options.progress ? "--progress" : "--no-progress");
1372 for_each_string_list_item(cas_option, &cas_options)
1373 strvec_push(&args, cas_option->string);
1374 strvec_push(&args, url.buf);
1376 if (options.force_if_includes)
1377 strvec_push(&args, "--force-if-includes");
1379 strvec_push(&args, "--stdin");
1380 for (i = 0; i < nr_spec; i++)
1381 packet_buf_write(&preamble, "%s\n", specs[i]);
1382 packet_buf_flush(&preamble);
1384 memset(&rpc, 0, sizeof(rpc));
1385 rpc.service_name = "git-receive-pack",
1387 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1388 if (rpc_result.len)
1389 write_or_die(1, rpc_result.buf, rpc_result.len);
1390 strbuf_release(&rpc_result);
1391 strbuf_release(&preamble);
1392 strvec_clear(&args);
1393 return err;
1396 static int push(int nr_spec, const char **specs)
1398 struct discovery *heads = discover_refs("git-receive-pack", 1);
1399 int ret;
1401 if (heads->proto_git)
1402 ret = push_git(heads, nr_spec, specs);
1403 else
1404 ret = push_dav(nr_spec, specs);
1405 free_discovery(heads);
1406 return ret;
1409 static void parse_push(struct strbuf *buf)
1411 struct strvec specs = STRVEC_INIT;
1412 int ret;
1414 do {
1415 const char *arg;
1416 if (skip_prefix(buf->buf, "push ", &arg))
1417 strvec_push(&specs, arg);
1418 else
1419 die(_("http transport does not support %s"), buf->buf);
1421 strbuf_reset(buf);
1422 if (strbuf_getline_lf(buf, stdin) == EOF)
1423 goto free_specs;
1424 if (!*buf->buf)
1425 break;
1426 } while (1);
1428 ret = push(specs.nr, specs.v);
1429 printf("\n");
1430 fflush(stdout);
1432 if (ret)
1433 exit(128); /* error already reported */
1435 free_specs:
1436 strvec_clear(&specs);
1439 static int stateless_connect(const char *service_name)
1441 struct discovery *discover;
1442 struct rpc_state rpc = RPC_STATE_INIT;
1443 struct strbuf buf = STRBUF_INIT;
1444 const char *accept_language;
1447 * Run the info/refs request and see if the server supports protocol
1448 * v2. If and only if the server supports v2 can we successfully
1449 * establish a stateless connection, otherwise we need to tell the
1450 * client to fallback to using other transport helper functions to
1451 * complete their request.
1453 discover = discover_refs(service_name, 0);
1454 if (discover->version != protocol_v2) {
1455 printf("fallback\n");
1456 fflush(stdout);
1457 return -1;
1458 } else {
1459 /* Stateless Connection established */
1460 printf("\n");
1461 fflush(stdout);
1463 accept_language = http_get_accept_language_header();
1464 if (accept_language)
1465 rpc.hdr_accept_language = xstrfmt("%s", accept_language);
1467 rpc.service_name = service_name;
1468 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1469 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1470 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1471 if (get_protocol_http_header(discover->version, &buf)) {
1472 rpc.protocol_header = strbuf_detach(&buf, NULL);
1473 } else {
1474 rpc.protocol_header = NULL;
1475 strbuf_release(&buf);
1477 rpc.buf = xmalloc(http_post_buffer);
1478 rpc.alloc = http_post_buffer;
1479 rpc.len = 0;
1480 rpc.pos = 0;
1481 rpc.in = 1;
1482 rpc.out = 0;
1483 rpc.any_written = 0;
1484 rpc.gzip_request = 1;
1485 rpc.initial_buffer = 0;
1486 rpc.write_line_lengths = 1;
1487 rpc.flush_read_but_not_sent = 0;
1490 * Dump the capability listing that we got from the server earlier
1491 * during the info/refs request.
1493 write_or_die(rpc.in, discover->buf, discover->len);
1495 /* Until we see EOF keep sending POSTs */
1496 while (1) {
1497 size_t avail;
1498 enum packet_read_status status;
1500 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1501 &status))
1502 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1503 if (status == PACKET_READ_EOF)
1504 break;
1505 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
1506 /* We would have an err here */
1507 break;
1508 /* Reset the buffer for next request */
1509 rpc.len = 0;
1512 free(rpc.service_url);
1513 free(rpc.hdr_content_type);
1514 free(rpc.hdr_accept);
1515 free(rpc.hdr_accept_language);
1516 free(rpc.protocol_header);
1517 free(rpc.buf);
1518 strbuf_release(&buf);
1520 return 0;
1523 int cmd_main(int argc, const char **argv)
1525 struct strbuf buf = STRBUF_INIT;
1526 int nongit;
1527 int ret = 1;
1529 setup_git_directory_gently(&nongit);
1530 if (argc < 2) {
1531 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1532 goto cleanup;
1535 options.verbosity = 1;
1536 options.progress = !!isatty(2);
1537 options.thin = 1;
1538 string_list_init_dup(&options.deepen_not);
1539 string_list_init_dup(&options.push_options);
1542 * Just report "remote-curl" here (folding all the various aliases
1543 * ("git-remote-http", "git-remote-https", and etc.) here since they
1544 * are all just copies of the same actual executable.
1546 trace2_cmd_name("remote-curl");
1548 remote = remote_get(argv[1]);
1550 if (argc > 2) {
1551 end_url_with_slash(&url, argv[2]);
1552 } else {
1553 end_url_with_slash(&url, remote->url[0]);
1556 http_init(remote, url.buf, 0);
1558 do {
1559 const char *arg;
1561 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1562 if (ferror(stdin))
1563 error(_("remote-curl: error reading command stream from git"));
1564 goto cleanup;
1566 if (buf.len == 0)
1567 break;
1568 if (starts_with(buf.buf, "fetch ")) {
1569 if (nongit)
1570 die(_("remote-curl: fetch attempted without a local repo"));
1571 parse_fetch(&buf);
1573 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1574 int for_push = !!strstr(buf.buf + 4, "for-push");
1575 output_refs(get_refs(for_push));
1577 } else if (starts_with(buf.buf, "push ")) {
1578 parse_push(&buf);
1580 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1581 char *value = strchr(arg, ' ');
1582 int result;
1584 if (value)
1585 *value++ = '\0';
1586 else
1587 value = "true";
1589 result = set_option(arg, value);
1590 if (!result)
1591 printf("ok\n");
1592 else if (result < 0)
1593 printf("error invalid value\n");
1594 else
1595 printf("unsupported\n");
1596 fflush(stdout);
1598 } else if (skip_prefix(buf.buf, "get ", &arg)) {
1599 parse_get(arg);
1600 fflush(stdout);
1602 } else if (!strcmp(buf.buf, "capabilities")) {
1603 printf("stateless-connect\n");
1604 printf("fetch\n");
1605 printf("get\n");
1606 printf("option\n");
1607 printf("push\n");
1608 printf("check-connectivity\n");
1609 printf("object-format\n");
1610 printf("\n");
1611 fflush(stdout);
1612 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1613 if (!stateless_connect(arg))
1614 break;
1615 } else {
1616 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1617 goto cleanup;
1619 strbuf_reset(&buf);
1620 } while (1);
1622 http_cleanup();
1623 ret = 0;
1624 cleanup:
1625 strbuf_release(&buf);
1627 return ret;