Merge branch 'rs/ls-tree-prefix-simplify'
[alt-git.git] / remote-curl.c
blob143318658e610e5277ccae7d8afd1eecc54ea15b
1 #include "git-compat-util.h"
2 #include "alloc.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 "exec-cmd.h"
13 #include "run-command.h"
14 #include "pkt-line.h"
15 #include "string-list.h"
16 #include "sideband.h"
17 #include "strvec.h"
18 #include "credential.h"
19 #include "oid-array.h"
20 #include "send-pack.h"
21 #include "setup.h"
22 #include "protocol.h"
23 #include "quote.h"
24 #include "trace2.h"
25 #include "transport.h"
26 #include "write-or-die.h"
28 static struct remote *remote;
29 /* always ends with a trailing slash */
30 static struct strbuf url = STRBUF_INIT;
32 struct options {
33 int verbosity;
34 unsigned long depth;
35 char *deepen_since;
36 struct string_list deepen_not;
37 struct string_list push_options;
38 char *filter;
39 unsigned progress : 1,
40 check_self_contained_and_connected : 1,
41 cloning : 1,
42 update_shallow : 1,
43 followtags : 1,
44 dry_run : 1,
45 thin : 1,
46 /* One of the SEND_PACK_PUSH_CERT_* constants. */
47 push_cert : 2,
48 deepen_relative : 1,
50 /* see documentation of corresponding flag in fetch-pack.h */
51 from_promisor : 1,
53 refetch : 1,
54 atomic : 1,
55 object_format : 1,
56 force_if_includes : 1;
57 const struct git_hash_algo *hash_algo;
59 static struct options options;
60 static struct string_list cas_options = STRING_LIST_INIT_DUP;
62 static int set_option(const char *name, const char *value)
64 if (!strcmp(name, "verbosity")) {
65 char *end;
66 int v = strtol(value, &end, 10);
67 if (value == end || *end)
68 return -1;
69 options.verbosity = v;
70 return 0;
72 else if (!strcmp(name, "progress")) {
73 if (!strcmp(value, "true"))
74 options.progress = 1;
75 else if (!strcmp(value, "false"))
76 options.progress = 0;
77 else
78 return -1;
79 return 0;
81 else if (!strcmp(name, "depth")) {
82 char *end;
83 unsigned long v = strtoul(value, &end, 10);
84 if (value == end || *end)
85 return -1;
86 options.depth = v;
87 return 0;
89 else if (!strcmp(name, "deepen-since")) {
90 options.deepen_since = xstrdup(value);
91 return 0;
93 else if (!strcmp(name, "deepen-not")) {
94 string_list_append(&options.deepen_not, value);
95 return 0;
97 else if (!strcmp(name, "deepen-relative")) {
98 if (!strcmp(value, "true"))
99 options.deepen_relative = 1;
100 else if (!strcmp(value, "false"))
101 options.deepen_relative = 0;
102 else
103 return -1;
104 return 0;
106 else if (!strcmp(name, "followtags")) {
107 if (!strcmp(value, "true"))
108 options.followtags = 1;
109 else if (!strcmp(value, "false"))
110 options.followtags = 0;
111 else
112 return -1;
113 return 0;
115 else if (!strcmp(name, "dry-run")) {
116 if (!strcmp(value, "true"))
117 options.dry_run = 1;
118 else if (!strcmp(value, "false"))
119 options.dry_run = 0;
120 else
121 return -1;
122 return 0;
124 else if (!strcmp(name, "check-connectivity")) {
125 if (!strcmp(value, "true"))
126 options.check_self_contained_and_connected = 1;
127 else if (!strcmp(value, "false"))
128 options.check_self_contained_and_connected = 0;
129 else
130 return -1;
131 return 0;
133 else if (!strcmp(name, "cas")) {
134 struct strbuf val = STRBUF_INIT;
135 strbuf_addstr(&val, "--force-with-lease=");
136 if (*value != '"')
137 strbuf_addstr(&val, value);
138 else if (unquote_c_style(&val, value, NULL))
139 return -1;
140 string_list_append(&cas_options, val.buf);
141 strbuf_release(&val);
142 return 0;
143 } else if (!strcmp(name, TRANS_OPT_FORCE_IF_INCLUDES)) {
144 if (!strcmp(value, "true"))
145 options.force_if_includes = 1;
146 else if (!strcmp(value, "false"))
147 options.force_if_includes = 0;
148 else
149 return -1;
150 return 0;
151 } else if (!strcmp(name, "cloning")) {
152 if (!strcmp(value, "true"))
153 options.cloning = 1;
154 else if (!strcmp(value, "false"))
155 options.cloning = 0;
156 else
157 return -1;
158 return 0;
159 } else if (!strcmp(name, "update-shallow")) {
160 if (!strcmp(value, "true"))
161 options.update_shallow = 1;
162 else if (!strcmp(value, "false"))
163 options.update_shallow = 0;
164 else
165 return -1;
166 return 0;
167 } else if (!strcmp(name, "pushcert")) {
168 if (!strcmp(value, "true"))
169 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
170 else if (!strcmp(value, "false"))
171 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
172 else if (!strcmp(value, "if-asked"))
173 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
174 else
175 return -1;
176 return 0;
177 } else if (!strcmp(name, "atomic")) {
178 if (!strcmp(value, "true"))
179 options.atomic = 1;
180 else if (!strcmp(value, "false"))
181 options.atomic = 0;
182 else
183 return -1;
184 return 0;
185 } else if (!strcmp(name, "push-option")) {
186 if (*value != '"')
187 string_list_append(&options.push_options, value);
188 else {
189 struct strbuf unquoted = STRBUF_INIT;
190 if (unquote_c_style(&unquoted, value, NULL) < 0)
191 die(_("invalid quoting in push-option value: '%s'"), value);
192 string_list_append_nodup(&options.push_options,
193 strbuf_detach(&unquoted, NULL));
195 return 0;
196 } else if (!strcmp(name, "family")) {
197 if (!strcmp(value, "ipv4"))
198 git_curl_ipresolve = CURL_IPRESOLVE_V4;
199 else if (!strcmp(value, "ipv6"))
200 git_curl_ipresolve = CURL_IPRESOLVE_V6;
201 else if (!strcmp(value, "all"))
202 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
203 else
204 return -1;
205 return 0;
206 } else if (!strcmp(name, "from-promisor")) {
207 options.from_promisor = 1;
208 return 0;
209 } else if (!strcmp(name, "refetch")) {
210 options.refetch = 1;
211 return 0;
212 } else if (!strcmp(name, "filter")) {
213 options.filter = xstrdup(value);
214 return 0;
215 } else if (!strcmp(name, "object-format")) {
216 int algo;
217 options.object_format = 1;
218 if (strcmp(value, "true")) {
219 algo = hash_algo_by_name(value);
220 if (algo == GIT_HASH_UNKNOWN)
221 die("unknown object format '%s'", value);
222 options.hash_algo = &hash_algos[algo];
224 return 0;
225 } else {
226 return 1 /* unsupported */;
230 struct discovery {
231 char *service;
232 char *buf_alloc;
233 char *buf;
234 size_t len;
235 struct ref *refs;
236 struct oid_array shallow;
237 enum protocol_version version;
238 unsigned proto_git : 1;
240 static struct discovery *last_discovery;
242 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
244 struct ref *list = NULL;
245 struct packet_reader reader;
247 packet_reader_init(&reader, -1, heads->buf, heads->len,
248 PACKET_READ_CHOMP_NEWLINE |
249 PACKET_READ_GENTLE_ON_EOF |
250 PACKET_READ_DIE_ON_ERR_PACKET);
252 heads->version = discover_version(&reader);
253 switch (heads->version) {
254 case protocol_v2:
256 * Do nothing. This isn't a list of refs but rather a
257 * capability advertisement. Client would have run
258 * 'stateless-connect' so we'll dump this capability listing
259 * and let them request the refs themselves.
261 break;
262 case protocol_v1:
263 case protocol_v0:
264 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
265 NULL, &heads->shallow);
266 options.hash_algo = reader.hash_algo;
267 break;
268 case protocol_unknown_version:
269 BUG("unknown protocol version");
272 return list;
275 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
277 const char *p = memchr(heads->buf, '\t', heads->len);
278 int algo;
279 if (!p)
280 return the_hash_algo;
282 algo = hash_algo_by_length((p - heads->buf) / 2);
283 if (algo == GIT_HASH_UNKNOWN)
284 return NULL;
285 return &hash_algos[algo];
288 static struct ref *parse_info_refs(struct discovery *heads)
290 char *data, *start, *mid;
291 char *ref_name;
292 int i = 0;
294 struct ref *refs = NULL;
295 struct ref *ref = NULL;
296 struct ref *last_ref = NULL;
298 options.hash_algo = detect_hash_algo(heads);
299 if (!options.hash_algo)
300 die("%sinfo/refs not valid: could not determine hash algorithm; "
301 "is this a git repository?",
302 transport_anonymize_url(url.buf));
304 data = heads->buf;
305 start = NULL;
306 mid = data;
307 while (i < heads->len) {
308 if (!start) {
309 start = &data[i];
311 if (data[i] == '\t')
312 mid = &data[i];
313 if (data[i] == '\n') {
314 if (mid - start != options.hash_algo->hexsz)
315 die(_("%sinfo/refs not valid: is this a git repository?"),
316 transport_anonymize_url(url.buf));
317 data[i] = 0;
318 ref_name = mid + 1;
319 ref = alloc_ref(ref_name);
320 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
321 if (!refs)
322 refs = ref;
323 if (last_ref)
324 last_ref->next = ref;
325 last_ref = ref;
326 start = NULL;
328 i++;
331 ref = alloc_ref("HEAD");
332 if (!http_fetch_ref(url.buf, ref) &&
333 !resolve_remote_symref(ref, refs)) {
334 ref->next = refs;
335 refs = ref;
336 } else {
337 free(ref);
340 return refs;
343 static void free_discovery(struct discovery *d)
345 if (d) {
346 if (d == last_discovery)
347 last_discovery = NULL;
348 free(d->shallow.oid);
349 free(d->buf_alloc);
350 free_refs(d->refs);
351 free(d->service);
352 free(d);
356 static int show_http_message(struct strbuf *type, struct strbuf *charset,
357 struct strbuf *msg)
359 const char *p, *eol;
362 * We only show text/plain parts, as other types are likely
363 * to be ugly to look at on the user's terminal.
365 if (strcmp(type->buf, "text/plain"))
366 return -1;
367 if (charset->len)
368 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
370 strbuf_trim(msg);
371 if (!msg->len)
372 return -1;
374 p = msg->buf;
375 do {
376 eol = strchrnul(p, '\n');
377 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
378 p = eol + 1;
379 } while(*eol);
380 return 0;
383 static int get_protocol_http_header(enum protocol_version version,
384 struct strbuf *header)
386 if (version > 0) {
387 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
388 version);
390 return 1;
393 return 0;
396 static void check_smart_http(struct discovery *d, const char *service,
397 struct strbuf *type)
399 const char *p;
400 struct packet_reader reader;
403 * If we don't see x-$service-advertisement, then it's not smart-http.
404 * But once we do, we commit to it and assume any other protocol
405 * violations are hard errors.
407 if (!skip_prefix(type->buf, "application/x-", &p) ||
408 !skip_prefix(p, service, &p) ||
409 strcmp(p, "-advertisement"))
410 return;
412 packet_reader_init(&reader, -1, d->buf, d->len,
413 PACKET_READ_CHOMP_NEWLINE |
414 PACKET_READ_DIE_ON_ERR_PACKET);
415 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
416 die(_("invalid server response; expected service, got flush packet"));
418 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
420 * The header can include additional metadata lines, up
421 * until a packet flush marker. Ignore these now, but
422 * in the future we might start to scan them.
424 for (;;) {
425 packet_reader_read(&reader);
426 if (reader.pktlen <= 0) {
427 break;
432 * v0 smart http; callers expect us to soak up the
433 * service and header packets
435 d->buf = reader.src_buffer;
436 d->len = reader.src_len;
437 d->proto_git = 1;
439 } else if (!strcmp(reader.line, "version 2")) {
441 * v2 smart http; do not consume version packet, which will
442 * be handled elsewhere.
444 d->proto_git = 1;
446 } else {
447 die(_("invalid server response; got '%s'"), reader.line);
451 static struct discovery *discover_refs(const char *service, int for_push)
453 struct strbuf type = STRBUF_INIT;
454 struct strbuf charset = STRBUF_INIT;
455 struct strbuf buffer = STRBUF_INIT;
456 struct strbuf refs_url = STRBUF_INIT;
457 struct strbuf effective_url = STRBUF_INIT;
458 struct strbuf protocol_header = STRBUF_INIT;
459 struct string_list extra_headers = STRING_LIST_INIT_DUP;
460 struct discovery *last = last_discovery;
461 int http_ret, maybe_smart = 0;
462 struct http_get_options http_options;
463 enum protocol_version version = get_protocol_version_config();
465 if (last && !strcmp(service, last->service))
466 return last;
467 free_discovery(last);
469 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
470 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
471 git_env_bool("GIT_SMART_HTTP", 1)) {
472 maybe_smart = 1;
473 if (!strchr(url.buf, '?'))
474 strbuf_addch(&refs_url, '?');
475 else
476 strbuf_addch(&refs_url, '&');
477 strbuf_addf(&refs_url, "service=%s", service);
481 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
482 * to perform any operation that doesn't involve upload-pack (i.e., a
483 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
484 * to do anything else (like push or remote archive) via v2.
486 if (version == protocol_v2 && strcmp("git-upload-pack", service))
487 version = protocol_v0;
489 /* Add the extra Git-Protocol header */
490 if (get_protocol_http_header(version, &protocol_header))
491 string_list_append(&extra_headers, protocol_header.buf);
493 memset(&http_options, 0, sizeof(http_options));
494 http_options.content_type = &type;
495 http_options.charset = &charset;
496 http_options.effective_url = &effective_url;
497 http_options.base_url = &url;
498 http_options.extra_headers = &extra_headers;
499 http_options.initial_request = 1;
500 http_options.no_cache = 1;
502 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
503 switch (http_ret) {
504 case HTTP_OK:
505 break;
506 case HTTP_MISSING_TARGET:
507 show_http_message(&type, &charset, &buffer);
508 die(_("repository '%s' not found"),
509 transport_anonymize_url(url.buf));
510 case HTTP_NOAUTH:
511 show_http_message(&type, &charset, &buffer);
512 die(_("Authentication failed for '%s'"),
513 transport_anonymize_url(url.buf));
514 case HTTP_NOMATCHPUBLICKEY:
515 show_http_message(&type, &charset, &buffer);
516 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
517 transport_anonymize_url(url.buf), curl_errorstr);
518 default:
519 show_http_message(&type, &charset, &buffer);
520 die(_("unable to access '%s': %s"),
521 transport_anonymize_url(url.buf), curl_errorstr);
524 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
525 char *u = transport_anonymize_url(url.buf);
526 warning(_("redirecting to %s"), u);
527 free(u);
530 last= xcalloc(1, sizeof(*last_discovery));
531 last->service = xstrdup(service);
532 last->buf_alloc = strbuf_detach(&buffer, &last->len);
533 last->buf = last->buf_alloc;
535 if (maybe_smart)
536 check_smart_http(last, service, &type);
538 if (last->proto_git)
539 last->refs = parse_git_refs(last, for_push);
540 else
541 last->refs = parse_info_refs(last);
543 strbuf_release(&refs_url);
544 strbuf_release(&type);
545 strbuf_release(&charset);
546 strbuf_release(&effective_url);
547 strbuf_release(&buffer);
548 strbuf_release(&protocol_header);
549 string_list_clear(&extra_headers, 0);
550 last_discovery = last;
551 return last;
554 static struct ref *get_refs(int for_push)
556 struct discovery *heads;
558 if (for_push)
559 heads = discover_refs("git-receive-pack", for_push);
560 else
561 heads = discover_refs("git-upload-pack", for_push);
563 return heads->refs;
566 static void output_refs(struct ref *refs)
568 struct ref *posn;
569 if (options.object_format && options.hash_algo) {
570 printf(":object-format %s\n", options.hash_algo->name);
571 repo_set_hash_algo(the_repository,
572 hash_algo_by_ptr(options.hash_algo));
574 for (posn = refs; posn; posn = posn->next) {
575 if (posn->symref)
576 printf("@%s %s\n", posn->symref, posn->name);
577 else
578 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
579 options.hash_algo),
580 posn->name);
582 printf("\n");
583 fflush(stdout);
586 struct rpc_state {
587 const char *service_name;
588 char *service_url;
589 char *hdr_content_type;
590 char *hdr_accept;
591 char *hdr_accept_language;
592 char *protocol_header;
593 char *buf;
594 size_t alloc;
595 size_t len;
596 size_t pos;
597 int in;
598 int out;
599 int any_written;
600 unsigned gzip_request : 1;
601 unsigned initial_buffer : 1;
604 * Whenever a pkt-line is read into buf, append the 4 characters
605 * denoting its length before appending the payload.
607 unsigned write_line_lengths : 1;
610 * Used by rpc_out; initialize to 0. This is true if a flush has been
611 * read, but the corresponding line length (if write_line_lengths is
612 * true) and EOF have not been sent to libcurl. Since each flush marks
613 * the end of a request, each flush must be completely sent before any
614 * further reading occurs.
616 unsigned flush_read_but_not_sent : 1;
619 #define RPC_STATE_INIT { 0 }
622 * Appends the result of reading from rpc->out to the string represented by
623 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
624 * enough space, 0 otherwise.
626 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
627 * hexadecimal string before appending the result described above.
629 * Writes the total number of bytes appended into appended.
631 static int rpc_read_from_out(struct rpc_state *rpc, int options,
632 size_t *appended,
633 enum packet_read_status *status) {
634 size_t left;
635 char *buf;
636 int pktlen_raw;
638 if (rpc->write_line_lengths) {
639 left = rpc->alloc - rpc->len - 4;
640 buf = rpc->buf + rpc->len + 4;
641 } else {
642 left = rpc->alloc - rpc->len;
643 buf = rpc->buf + rpc->len;
646 if (left < LARGE_PACKET_MAX)
647 return 0;
649 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
650 left, &pktlen_raw, options);
651 if (*status != PACKET_READ_EOF) {
652 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
653 rpc->len += *appended;
656 if (rpc->write_line_lengths) {
657 switch (*status) {
658 case PACKET_READ_EOF:
659 if (!(options & PACKET_READ_GENTLE_ON_EOF))
660 die(_("shouldn't have EOF when not gentle on EOF"));
661 break;
662 case PACKET_READ_NORMAL:
663 set_packet_header(buf - 4, *appended);
664 break;
665 case PACKET_READ_DELIM:
666 memcpy(buf - 4, "0001", 4);
667 break;
668 case PACKET_READ_FLUSH:
669 memcpy(buf - 4, "0000", 4);
670 break;
671 case PACKET_READ_RESPONSE_END:
672 die(_("remote server sent unexpected response end packet"));
676 return 1;
679 static size_t rpc_out(void *ptr, size_t eltsize,
680 size_t nmemb, void *buffer_)
682 size_t max = eltsize * nmemb;
683 struct rpc_state *rpc = buffer_;
684 size_t avail = rpc->len - rpc->pos;
685 enum packet_read_status status;
687 if (!avail) {
688 rpc->initial_buffer = 0;
689 rpc->len = 0;
690 rpc->pos = 0;
691 if (!rpc->flush_read_but_not_sent) {
692 if (!rpc_read_from_out(rpc, 0, &avail, &status))
693 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
694 if (status == PACKET_READ_FLUSH)
695 rpc->flush_read_but_not_sent = 1;
698 * If flush_read_but_not_sent is true, we have already read one
699 * full request but have not fully sent it + EOF, which is why
700 * we need to refrain from reading.
703 if (rpc->flush_read_but_not_sent) {
704 if (!avail) {
706 * The line length either does not need to be sent at
707 * all or has already been completely sent. Now we can
708 * return 0, indicating EOF, meaning that the flush has
709 * been fully sent.
711 rpc->flush_read_but_not_sent = 0;
712 return 0;
715 * If avail is non-zero, the line length for the flush still
716 * hasn't been fully sent. Proceed with sending the line
717 * length.
721 if (max < avail)
722 avail = max;
723 memcpy(ptr, rpc->buf + rpc->pos, avail);
724 rpc->pos += avail;
725 return avail;
728 static int rpc_seek(void *clientp, curl_off_t offset, int origin)
730 struct rpc_state *rpc = clientp;
732 if (origin != SEEK_SET)
733 BUG("rpc_seek only handles SEEK_SET, not %d", origin);
735 if (rpc->initial_buffer) {
736 if (offset < 0 || offset > rpc->len) {
737 error("curl seek would be outside of rpc buffer");
738 return CURL_SEEKFUNC_FAIL;
740 rpc->pos = offset;
741 return CURL_SEEKFUNC_OK;
743 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
744 return CURL_SEEKFUNC_FAIL;
747 struct check_pktline_state {
748 char len_buf[4];
749 int len_filled;
750 int remaining;
753 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
755 while (size) {
756 if (!state->remaining) {
757 int digits_remaining = 4 - state->len_filled;
758 if (digits_remaining > size)
759 digits_remaining = size;
760 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
761 state->len_filled += digits_remaining;
762 ptr += digits_remaining;
763 size -= digits_remaining;
765 if (state->len_filled == 4) {
766 state->remaining = packet_length(state->len_buf,
767 sizeof(state->len_buf));
768 if (state->remaining < 0) {
769 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
770 } else if (state->remaining == 2) {
771 die(_("remote-curl: unexpected response end packet"));
772 } else if (state->remaining < 4) {
773 state->remaining = 0;
774 } else {
775 state->remaining -= 4;
777 state->len_filled = 0;
781 if (state->remaining) {
782 int remaining = state->remaining;
783 if (remaining > size)
784 remaining = size;
785 ptr += remaining;
786 size -= remaining;
787 state->remaining -= remaining;
792 struct rpc_in_data {
793 struct rpc_state *rpc;
794 struct active_request_slot *slot;
795 int check_pktline;
796 struct check_pktline_state pktline_state;
800 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
801 * from ptr.
803 static size_t rpc_in(char *ptr, size_t eltsize,
804 size_t nmemb, void *buffer_)
806 size_t size = eltsize * nmemb;
807 struct rpc_in_data *data = buffer_;
808 long response_code;
810 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
811 &response_code) != CURLE_OK)
812 return size;
813 if (response_code >= 300)
814 return size;
815 if (size)
816 data->rpc->any_written = 1;
817 if (data->check_pktline)
818 check_pktline(&data->pktline_state, ptr, size);
819 write_or_die(data->rpc->in, ptr, size);
820 return size;
823 static int run_slot(struct active_request_slot *slot,
824 struct slot_results *results)
826 int err;
827 struct slot_results results_buf;
829 if (!results)
830 results = &results_buf;
832 err = run_one_slot(slot, results);
834 if (err != HTTP_OK && err != HTTP_REAUTH) {
835 struct strbuf msg = STRBUF_INIT;
836 if (results->http_code && results->http_code != 200)
837 strbuf_addf(&msg, "HTTP %ld", results->http_code);
838 if (results->curl_result != CURLE_OK) {
839 if (msg.len)
840 strbuf_addch(&msg, ' ');
841 strbuf_addf(&msg, "curl %d", results->curl_result);
842 if (curl_errorstr[0]) {
843 strbuf_addch(&msg, ' ');
844 strbuf_addstr(&msg, curl_errorstr);
847 error(_("RPC failed; %s"), msg.buf);
848 strbuf_release(&msg);
851 return err;
854 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
856 struct active_request_slot *slot;
857 struct curl_slist *headers = http_copy_default_headers();
858 struct strbuf buf = STRBUF_INIT;
859 int err;
861 slot = get_active_slot();
863 headers = curl_slist_append(headers, rpc->hdr_content_type);
864 headers = curl_slist_append(headers, rpc->hdr_accept);
866 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
867 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
868 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
869 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
870 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
871 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
872 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
873 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
874 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf);
876 err = run_slot(slot, results);
878 curl_slist_free_all(headers);
879 strbuf_release(&buf);
880 return err;
883 static curl_off_t xcurl_off_t(size_t len)
885 uintmax_t size = len;
886 if (size > maximum_signed_value_of_type(curl_off_t))
887 die(_("cannot handle pushes this big"));
888 return (curl_off_t)size;
892 * If flush_received is true, do not attempt to read any more; just use what's
893 * in rpc->buf.
895 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
897 struct active_request_slot *slot;
898 struct curl_slist *headers = http_copy_default_headers();
899 int use_gzip = rpc->gzip_request;
900 char *gzip_body = NULL;
901 size_t gzip_size = 0;
902 int err, large_request = 0;
903 int needs_100_continue = 0;
904 struct rpc_in_data rpc_in_data;
906 /* Try to load the entire request, if we can fit it into the
907 * allocated buffer space we can use HTTP/1.0 and avoid the
908 * chunked encoding mess.
910 if (!flush_received) {
911 while (1) {
912 size_t n;
913 enum packet_read_status status;
915 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
916 large_request = 1;
917 use_gzip = 0;
918 break;
920 if (status == PACKET_READ_FLUSH)
921 break;
925 if (large_request) {
926 struct slot_results results;
928 do {
929 err = probe_rpc(rpc, &results);
930 if (err == HTTP_REAUTH)
931 credential_fill(&http_auth);
932 } while (err == HTTP_REAUTH);
933 if (err != HTTP_OK)
934 return -1;
936 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
937 needs_100_continue = 1;
940 headers = curl_slist_append(headers, rpc->hdr_content_type);
941 headers = curl_slist_append(headers, rpc->hdr_accept);
942 headers = curl_slist_append(headers, needs_100_continue ?
943 "Expect: 100-continue" : "Expect:");
945 /* Add Accept-Language header */
946 if (rpc->hdr_accept_language)
947 headers = curl_slist_append(headers, rpc->hdr_accept_language);
949 /* Add the extra Git-Protocol header */
950 if (rpc->protocol_header)
951 headers = curl_slist_append(headers, rpc->protocol_header);
953 retry:
954 slot = get_active_slot();
956 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
957 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
958 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
959 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
961 if (large_request) {
962 /* The request body is large and the size cannot be predicted.
963 * We must use chunked encoding to send it.
965 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
966 rpc->initial_buffer = 1;
967 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
968 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
969 curl_easy_setopt(slot->curl, CURLOPT_SEEKFUNCTION, rpc_seek);
970 curl_easy_setopt(slot->curl, CURLOPT_SEEKDATA, rpc);
971 if (options.verbosity > 1) {
972 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
973 fflush(stderr);
976 } else if (gzip_body) {
978 * If we are looping to retry authentication, then the previous
979 * run will have set up the headers and gzip buffer already,
980 * and we just need to send it.
982 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
983 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
985 } else if (use_gzip && 1024 < rpc->len) {
986 /* The client backend isn't giving us compressed data so
987 * we can try to deflate it ourselves, this may save on
988 * the transfer time.
990 git_zstream stream;
991 int ret;
993 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
994 gzip_size = git_deflate_bound(&stream, rpc->len);
995 gzip_body = xmalloc(gzip_size);
997 stream.next_in = (unsigned char *)rpc->buf;
998 stream.avail_in = rpc->len;
999 stream.next_out = (unsigned char *)gzip_body;
1000 stream.avail_out = gzip_size;
1002 ret = git_deflate(&stream, Z_FINISH);
1003 if (ret != Z_STREAM_END)
1004 die(_("cannot deflate request; zlib deflate error %d"), ret);
1006 ret = git_deflate_end_gently(&stream);
1007 if (ret != Z_OK)
1008 die(_("cannot deflate request; zlib end error %d"), ret);
1010 gzip_size = stream.total_out;
1012 headers = curl_slist_append(headers, "Content-Encoding: gzip");
1013 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
1014 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
1016 if (options.verbosity > 1) {
1017 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
1018 rpc->service_name,
1019 (unsigned long)rpc->len, (unsigned long)gzip_size);
1020 fflush(stderr);
1022 } else {
1023 /* We know the complete request size in advance, use the
1024 * more normal Content-Length approach.
1026 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1027 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1028 if (options.verbosity > 1) {
1029 fprintf(stderr, "POST %s (%lu bytes)\n",
1030 rpc->service_name, (unsigned long)rpc->len);
1031 fflush(stderr);
1035 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1036 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
1037 rpc_in_data.rpc = rpc;
1038 rpc_in_data.slot = slot;
1039 rpc_in_data.check_pktline = stateless_connect;
1040 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
1041 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &rpc_in_data);
1042 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1045 rpc->any_written = 0;
1046 err = run_slot(slot, NULL);
1047 if (err == HTTP_REAUTH && !large_request) {
1048 credential_fill(&http_auth);
1049 goto retry;
1051 if (err != HTTP_OK)
1052 err = -1;
1054 if (!rpc->any_written)
1055 err = -1;
1057 if (rpc_in_data.pktline_state.len_filled)
1058 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1059 if (rpc_in_data.pktline_state.remaining)
1060 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1062 if (stateless_connect)
1063 packet_response_end(rpc->in);
1065 curl_slist_free_all(headers);
1066 free(gzip_body);
1067 return err;
1070 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
1071 const char **client_argv, const struct strbuf *preamble,
1072 struct strbuf *rpc_result)
1074 const char *svc = rpc->service_name;
1075 struct strbuf buf = STRBUF_INIT;
1076 struct child_process client = CHILD_PROCESS_INIT;
1077 int err = 0;
1079 client.in = -1;
1080 client.out = -1;
1081 client.git_cmd = 1;
1082 strvec_pushv(&client.args, client_argv);
1083 if (start_command(&client))
1084 exit(1);
1085 write_or_die(client.in, preamble->buf, preamble->len);
1086 if (heads)
1087 write_or_die(client.in, heads->buf, heads->len);
1089 rpc->alloc = http_post_buffer;
1090 rpc->buf = xmalloc(rpc->alloc);
1091 rpc->in = client.in;
1092 rpc->out = client.out;
1094 strbuf_addf(&buf, "%s%s", url.buf, svc);
1095 rpc->service_url = strbuf_detach(&buf, NULL);
1097 rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
1099 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1100 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1102 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
1103 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1105 if (get_protocol_http_header(heads->version, &buf))
1106 rpc->protocol_header = strbuf_detach(&buf, NULL);
1107 else
1108 rpc->protocol_header = NULL;
1110 while (!err) {
1111 int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0);
1112 if (!n)
1113 break;
1114 rpc->pos = 0;
1115 rpc->len = n;
1116 err |= post_rpc(rpc, 0, 0);
1119 close(client.in);
1120 client.in = -1;
1121 if (!err) {
1122 strbuf_read(rpc_result, client.out, 0);
1123 } else {
1124 char buf[4096];
1125 for (;;)
1126 if (xread(client.out, buf, sizeof(buf)) <= 0)
1127 break;
1130 close(client.out);
1131 client.out = -1;
1133 err |= finish_command(&client);
1134 free(rpc->service_url);
1135 free(rpc->hdr_content_type);
1136 free(rpc->hdr_accept);
1137 free(rpc->hdr_accept_language);
1138 free(rpc->protocol_header);
1139 free(rpc->buf);
1140 strbuf_release(&buf);
1141 return err;
1144 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1146 struct walker *walker;
1147 char **targets;
1148 int ret, i;
1150 ALLOC_ARRAY(targets, nr_heads);
1151 if (options.depth || options.deepen_since)
1152 die(_("dumb http transport does not support shallow capabilities"));
1153 for (i = 0; i < nr_heads; i++)
1154 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1156 walker = get_http_walker(url.buf);
1157 walker->get_verbosely = options.verbosity >= 3;
1158 walker->get_progress = options.progress;
1159 walker->get_recover = 0;
1160 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1161 walker_free(walker);
1163 for (i = 0; i < nr_heads; i++)
1164 free(targets[i]);
1165 free(targets);
1167 return ret ? error(_("fetch failed.")) : 0;
1170 static int fetch_git(struct discovery *heads,
1171 int nr_heads, struct ref **to_fetch)
1173 struct rpc_state rpc = RPC_STATE_INIT;
1174 struct strbuf preamble = STRBUF_INIT;
1175 int i, err;
1176 struct strvec args = STRVEC_INIT;
1177 struct strbuf rpc_result = STRBUF_INIT;
1179 strvec_pushl(&args, "fetch-pack", "--stateless-rpc",
1180 "--stdin", "--lock-pack", NULL);
1181 if (options.followtags)
1182 strvec_push(&args, "--include-tag");
1183 if (options.thin)
1184 strvec_push(&args, "--thin");
1185 if (options.verbosity >= 3)
1186 strvec_pushl(&args, "-v", "-v", NULL);
1187 if (options.check_self_contained_and_connected)
1188 strvec_push(&args, "--check-self-contained-and-connected");
1189 if (options.cloning)
1190 strvec_push(&args, "--cloning");
1191 if (options.update_shallow)
1192 strvec_push(&args, "--update-shallow");
1193 if (!options.progress)
1194 strvec_push(&args, "--no-progress");
1195 if (options.depth)
1196 strvec_pushf(&args, "--depth=%lu", options.depth);
1197 if (options.deepen_since)
1198 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since);
1199 for (i = 0; i < options.deepen_not.nr; i++)
1200 strvec_pushf(&args, "--shallow-exclude=%s",
1201 options.deepen_not.items[i].string);
1202 if (options.deepen_relative && options.depth)
1203 strvec_push(&args, "--deepen-relative");
1204 if (options.from_promisor)
1205 strvec_push(&args, "--from-promisor");
1206 if (options.refetch)
1207 strvec_push(&args, "--refetch");
1208 if (options.filter)
1209 strvec_pushf(&args, "--filter=%s", options.filter);
1210 strvec_push(&args, url.buf);
1212 for (i = 0; i < nr_heads; i++) {
1213 struct ref *ref = to_fetch[i];
1214 if (!*ref->name)
1215 die(_("cannot fetch by sha1 over smart http"));
1216 packet_buf_write(&preamble, "%s %s\n",
1217 oid_to_hex(&ref->old_oid), ref->name);
1219 packet_buf_flush(&preamble);
1221 memset(&rpc, 0, sizeof(rpc));
1222 rpc.service_name = "git-upload-pack",
1223 rpc.gzip_request = 1;
1225 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1226 if (rpc_result.len)
1227 write_or_die(1, rpc_result.buf, rpc_result.len);
1228 strbuf_release(&rpc_result);
1229 strbuf_release(&preamble);
1230 strvec_clear(&args);
1231 return err;
1234 static int fetch(int nr_heads, struct ref **to_fetch)
1236 struct discovery *d = discover_refs("git-upload-pack", 0);
1237 if (d->proto_git)
1238 return fetch_git(d, nr_heads, to_fetch);
1239 else
1240 return fetch_dumb(nr_heads, to_fetch);
1243 static void parse_fetch(struct strbuf *buf)
1245 struct ref **to_fetch = NULL;
1246 struct ref *list_head = NULL;
1247 struct ref **list = &list_head;
1248 int alloc_heads = 0, nr_heads = 0;
1250 do {
1251 const char *p;
1252 if (skip_prefix(buf->buf, "fetch ", &p)) {
1253 const char *name;
1254 struct ref *ref;
1255 struct object_id old_oid;
1256 const char *q;
1258 if (parse_oid_hex(p, &old_oid, &q))
1259 die(_("protocol error: expected sha/ref, got '%s'"), p);
1260 if (*q == ' ')
1261 name = q + 1;
1262 else if (!*q)
1263 name = "";
1264 else
1265 die(_("protocol error: expected sha/ref, got '%s'"), p);
1267 ref = alloc_ref(name);
1268 oidcpy(&ref->old_oid, &old_oid);
1270 *list = ref;
1271 list = &ref->next;
1273 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1274 to_fetch[nr_heads++] = ref;
1276 else
1277 die(_("http transport does not support %s"), buf->buf);
1279 strbuf_reset(buf);
1280 if (strbuf_getline_lf(buf, stdin) == EOF)
1281 return;
1282 if (!*buf->buf)
1283 break;
1284 } while (1);
1286 if (fetch(nr_heads, to_fetch))
1287 exit(128); /* error already reported */
1288 free_refs(list_head);
1289 free(to_fetch);
1291 printf("\n");
1292 fflush(stdout);
1293 strbuf_reset(buf);
1296 static void parse_get(const char *arg)
1298 struct strbuf url = STRBUF_INIT;
1299 struct strbuf path = STRBUF_INIT;
1300 const char *space;
1302 space = strchr(arg, ' ');
1304 if (!space)
1305 die(_("protocol error: expected '<url> <path>', missing space"));
1307 strbuf_add(&url, arg, space - arg);
1308 strbuf_addstr(&path, space + 1);
1310 if (http_get_file(url.buf, path.buf, NULL))
1311 die(_("failed to download file at URL '%s'"), url.buf);
1313 strbuf_release(&url);
1314 strbuf_release(&path);
1315 printf("\n");
1316 fflush(stdout);
1319 static int push_dav(int nr_spec, const char **specs)
1321 struct child_process child = CHILD_PROCESS_INIT;
1322 size_t i;
1324 child.git_cmd = 1;
1325 strvec_push(&child.args, "http-push");
1326 strvec_push(&child.args, "--helper-status");
1327 if (options.dry_run)
1328 strvec_push(&child.args, "--dry-run");
1329 if (options.verbosity > 1)
1330 strvec_push(&child.args, "--verbose");
1331 strvec_push(&child.args, url.buf);
1332 for (i = 0; i < nr_spec; i++)
1333 strvec_push(&child.args, specs[i]);
1335 if (run_command(&child))
1336 die(_("git-http-push failed"));
1337 return 0;
1340 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1342 struct rpc_state rpc = RPC_STATE_INIT;
1343 int i, err;
1344 struct strvec args;
1345 struct string_list_item *cas_option;
1346 struct strbuf preamble = STRBUF_INIT;
1347 struct strbuf rpc_result = STRBUF_INIT;
1349 strvec_init(&args);
1350 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1351 NULL);
1353 if (options.thin)
1354 strvec_push(&args, "--thin");
1355 if (options.dry_run)
1356 strvec_push(&args, "--dry-run");
1357 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1358 strvec_push(&args, "--signed=yes");
1359 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1360 strvec_push(&args, "--signed=if-asked");
1361 if (options.atomic)
1362 strvec_push(&args, "--atomic");
1363 if (options.verbosity == 0)
1364 strvec_push(&args, "--quiet");
1365 else if (options.verbosity > 1)
1366 strvec_push(&args, "--verbose");
1367 for (i = 0; i < options.push_options.nr; i++)
1368 strvec_pushf(&args, "--push-option=%s",
1369 options.push_options.items[i].string);
1370 strvec_push(&args, options.progress ? "--progress" : "--no-progress");
1371 for_each_string_list_item(cas_option, &cas_options)
1372 strvec_push(&args, cas_option->string);
1373 strvec_push(&args, url.buf);
1375 if (options.force_if_includes)
1376 strvec_push(&args, "--force-if-includes");
1378 strvec_push(&args, "--stdin");
1379 for (i = 0; i < nr_spec; i++)
1380 packet_buf_write(&preamble, "%s\n", specs[i]);
1381 packet_buf_flush(&preamble);
1383 memset(&rpc, 0, sizeof(rpc));
1384 rpc.service_name = "git-receive-pack",
1386 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1387 if (rpc_result.len)
1388 write_or_die(1, rpc_result.buf, rpc_result.len);
1389 strbuf_release(&rpc_result);
1390 strbuf_release(&preamble);
1391 strvec_clear(&args);
1392 return err;
1395 static int push(int nr_spec, const char **specs)
1397 struct discovery *heads = discover_refs("git-receive-pack", 1);
1398 int ret;
1400 if (heads->proto_git)
1401 ret = push_git(heads, nr_spec, specs);
1402 else
1403 ret = push_dav(nr_spec, specs);
1404 free_discovery(heads);
1405 return ret;
1408 static void parse_push(struct strbuf *buf)
1410 struct strvec specs = STRVEC_INIT;
1411 int ret;
1413 do {
1414 const char *arg;
1415 if (skip_prefix(buf->buf, "push ", &arg))
1416 strvec_push(&specs, arg);
1417 else
1418 die(_("http transport does not support %s"), buf->buf);
1420 strbuf_reset(buf);
1421 if (strbuf_getline_lf(buf, stdin) == EOF)
1422 goto free_specs;
1423 if (!*buf->buf)
1424 break;
1425 } while (1);
1427 ret = push(specs.nr, specs.v);
1428 printf("\n");
1429 fflush(stdout);
1431 if (ret)
1432 exit(128); /* error already reported */
1434 free_specs:
1435 strvec_clear(&specs);
1438 static int stateless_connect(const char *service_name)
1440 struct discovery *discover;
1441 struct rpc_state rpc = RPC_STATE_INIT;
1442 struct strbuf buf = STRBUF_INIT;
1443 const char *accept_language;
1446 * Run the info/refs request and see if the server supports protocol
1447 * v2. If and only if the server supports v2 can we successfully
1448 * establish a stateless connection, otherwise we need to tell the
1449 * client to fallback to using other transport helper functions to
1450 * complete their request.
1452 discover = discover_refs(service_name, 0);
1453 if (discover->version != protocol_v2) {
1454 printf("fallback\n");
1455 fflush(stdout);
1456 return -1;
1457 } else {
1458 /* Stateless Connection established */
1459 printf("\n");
1460 fflush(stdout);
1462 accept_language = http_get_accept_language_header();
1463 if (accept_language)
1464 rpc.hdr_accept_language = xstrfmt("%s", accept_language);
1466 rpc.service_name = service_name;
1467 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1468 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1469 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1470 if (get_protocol_http_header(discover->version, &buf)) {
1471 rpc.protocol_header = strbuf_detach(&buf, NULL);
1472 } else {
1473 rpc.protocol_header = NULL;
1474 strbuf_release(&buf);
1476 rpc.buf = xmalloc(http_post_buffer);
1477 rpc.alloc = http_post_buffer;
1478 rpc.len = 0;
1479 rpc.pos = 0;
1480 rpc.in = 1;
1481 rpc.out = 0;
1482 rpc.any_written = 0;
1483 rpc.gzip_request = 1;
1484 rpc.initial_buffer = 0;
1485 rpc.write_line_lengths = 1;
1486 rpc.flush_read_but_not_sent = 0;
1489 * Dump the capability listing that we got from the server earlier
1490 * during the info/refs request.
1492 write_or_die(rpc.in, discover->buf, discover->len);
1494 /* Until we see EOF keep sending POSTs */
1495 while (1) {
1496 size_t avail;
1497 enum packet_read_status status;
1499 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1500 &status))
1501 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1502 if (status == PACKET_READ_EOF)
1503 break;
1504 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
1505 /* We would have an err here */
1506 break;
1507 /* Reset the buffer for next request */
1508 rpc.len = 0;
1511 free(rpc.service_url);
1512 free(rpc.hdr_content_type);
1513 free(rpc.hdr_accept);
1514 free(rpc.hdr_accept_language);
1515 free(rpc.protocol_header);
1516 free(rpc.buf);
1517 strbuf_release(&buf);
1519 return 0;
1522 int cmd_main(int argc, const char **argv)
1524 struct strbuf buf = STRBUF_INIT;
1525 int nongit;
1526 int ret = 1;
1528 setup_git_directory_gently(&nongit);
1529 if (argc < 2) {
1530 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1531 goto cleanup;
1534 options.verbosity = 1;
1535 options.progress = !!isatty(2);
1536 options.thin = 1;
1537 string_list_init_dup(&options.deepen_not);
1538 string_list_init_dup(&options.push_options);
1541 * Just report "remote-curl" here (folding all the various aliases
1542 * ("git-remote-http", "git-remote-https", and etc.) here since they
1543 * are all just copies of the same actual executable.
1545 trace2_cmd_name("remote-curl");
1547 remote = remote_get(argv[1]);
1549 if (argc > 2) {
1550 end_url_with_slash(&url, argv[2]);
1551 } else {
1552 end_url_with_slash(&url, remote->url[0]);
1555 http_init(remote, url.buf, 0);
1557 do {
1558 const char *arg;
1560 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1561 if (ferror(stdin))
1562 error(_("remote-curl: error reading command stream from git"));
1563 goto cleanup;
1565 if (buf.len == 0)
1566 break;
1567 if (starts_with(buf.buf, "fetch ")) {
1568 if (nongit)
1569 die(_("remote-curl: fetch attempted without a local repo"));
1570 parse_fetch(&buf);
1572 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1573 int for_push = !!strstr(buf.buf + 4, "for-push");
1574 output_refs(get_refs(for_push));
1576 } else if (starts_with(buf.buf, "push ")) {
1577 parse_push(&buf);
1579 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1580 char *value = strchr(arg, ' ');
1581 int result;
1583 if (value)
1584 *value++ = '\0';
1585 else
1586 value = "true";
1588 result = set_option(arg, value);
1589 if (!result)
1590 printf("ok\n");
1591 else if (result < 0)
1592 printf("error invalid value\n");
1593 else
1594 printf("unsupported\n");
1595 fflush(stdout);
1597 } else if (skip_prefix(buf.buf, "get ", &arg)) {
1598 parse_get(arg);
1599 fflush(stdout);
1601 } else if (!strcmp(buf.buf, "capabilities")) {
1602 printf("stateless-connect\n");
1603 printf("fetch\n");
1604 printf("get\n");
1605 printf("option\n");
1606 printf("push\n");
1607 printf("check-connectivity\n");
1608 printf("object-format\n");
1609 printf("\n");
1610 fflush(stdout);
1611 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1612 if (!stateless_connect(arg))
1613 break;
1614 } else {
1615 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1616 goto cleanup;
1618 strbuf_reset(&buf);
1619 } while (1);
1621 http_cleanup();
1622 ret = 0;
1623 cleanup:
1624 strbuf_release(&buf);
1626 return ret;