unicode: update the width tables to Unicode 15
[git/gitster.git] / remote-curl.c
bloba76b6405eb2fc443ff22b93b1598892d02508e20
1 #include "cache.h"
2 #include "config.h"
3 #include "remote.h"
4 #include "connect.h"
5 #include "strbuf.h"
6 #include "walker.h"
7 #include "http.h"
8 #include "exec-cmd.h"
9 #include "run-command.h"
10 #include "pkt-line.h"
11 #include "string-list.h"
12 #include "sideband.h"
13 #include "strvec.h"
14 #include "credential.h"
15 #include "oid-array.h"
16 #include "send-pack.h"
17 #include "protocol.h"
18 #include "quote.h"
19 #include "transport.h"
21 static struct remote *remote;
22 /* always ends with a trailing slash */
23 static struct strbuf url = STRBUF_INIT;
25 struct options {
26 int verbosity;
27 unsigned long depth;
28 char *deepen_since;
29 struct string_list deepen_not;
30 struct string_list push_options;
31 char *filter;
32 unsigned progress : 1,
33 check_self_contained_and_connected : 1,
34 cloning : 1,
35 update_shallow : 1,
36 followtags : 1,
37 dry_run : 1,
38 thin : 1,
39 /* One of the SEND_PACK_PUSH_CERT_* constants. */
40 push_cert : 2,
41 deepen_relative : 1,
43 /* see documentation of corresponding flag in fetch-pack.h */
44 from_promisor : 1,
46 refetch : 1,
47 atomic : 1,
48 object_format : 1,
49 force_if_includes : 1;
50 const struct git_hash_algo *hash_algo;
52 static struct options options;
53 static struct string_list cas_options = STRING_LIST_INIT_DUP;
55 static int set_option(const char *name, const char *value)
57 if (!strcmp(name, "verbosity")) {
58 char *end;
59 int v = strtol(value, &end, 10);
60 if (value == end || *end)
61 return -1;
62 options.verbosity = v;
63 return 0;
65 else if (!strcmp(name, "progress")) {
66 if (!strcmp(value, "true"))
67 options.progress = 1;
68 else if (!strcmp(value, "false"))
69 options.progress = 0;
70 else
71 return -1;
72 return 0;
74 else if (!strcmp(name, "depth")) {
75 char *end;
76 unsigned long v = strtoul(value, &end, 10);
77 if (value == end || *end)
78 return -1;
79 options.depth = v;
80 return 0;
82 else if (!strcmp(name, "deepen-since")) {
83 options.deepen_since = xstrdup(value);
84 return 0;
86 else if (!strcmp(name, "deepen-not")) {
87 string_list_append(&options.deepen_not, value);
88 return 0;
90 else if (!strcmp(name, "deepen-relative")) {
91 if (!strcmp(value, "true"))
92 options.deepen_relative = 1;
93 else if (!strcmp(value, "false"))
94 options.deepen_relative = 0;
95 else
96 return -1;
97 return 0;
99 else if (!strcmp(name, "followtags")) {
100 if (!strcmp(value, "true"))
101 options.followtags = 1;
102 else if (!strcmp(value, "false"))
103 options.followtags = 0;
104 else
105 return -1;
106 return 0;
108 else if (!strcmp(name, "dry-run")) {
109 if (!strcmp(value, "true"))
110 options.dry_run = 1;
111 else if (!strcmp(value, "false"))
112 options.dry_run = 0;
113 else
114 return -1;
115 return 0;
117 else if (!strcmp(name, "check-connectivity")) {
118 if (!strcmp(value, "true"))
119 options.check_self_contained_and_connected = 1;
120 else if (!strcmp(value, "false"))
121 options.check_self_contained_and_connected = 0;
122 else
123 return -1;
124 return 0;
126 else if (!strcmp(name, "cas")) {
127 struct strbuf val = STRBUF_INIT;
128 strbuf_addstr(&val, "--force-with-lease=");
129 if (*value != '"')
130 strbuf_addstr(&val, value);
131 else if (unquote_c_style(&val, value, NULL))
132 return -1;
133 string_list_append(&cas_options, val.buf);
134 strbuf_release(&val);
135 return 0;
136 } else if (!strcmp(name, TRANS_OPT_FORCE_IF_INCLUDES)) {
137 if (!strcmp(value, "true"))
138 options.force_if_includes = 1;
139 else if (!strcmp(value, "false"))
140 options.force_if_includes = 0;
141 else
142 return -1;
143 return 0;
144 } else if (!strcmp(name, "cloning")) {
145 if (!strcmp(value, "true"))
146 options.cloning = 1;
147 else if (!strcmp(value, "false"))
148 options.cloning = 0;
149 else
150 return -1;
151 return 0;
152 } else if (!strcmp(name, "update-shallow")) {
153 if (!strcmp(value, "true"))
154 options.update_shallow = 1;
155 else if (!strcmp(value, "false"))
156 options.update_shallow = 0;
157 else
158 return -1;
159 return 0;
160 } else if (!strcmp(name, "pushcert")) {
161 if (!strcmp(value, "true"))
162 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
163 else if (!strcmp(value, "false"))
164 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
165 else if (!strcmp(value, "if-asked"))
166 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
167 else
168 return -1;
169 return 0;
170 } else if (!strcmp(name, "atomic")) {
171 if (!strcmp(value, "true"))
172 options.atomic = 1;
173 else if (!strcmp(value, "false"))
174 options.atomic = 0;
175 else
176 return -1;
177 return 0;
178 } else if (!strcmp(name, "push-option")) {
179 if (*value != '"')
180 string_list_append(&options.push_options, value);
181 else {
182 struct strbuf unquoted = STRBUF_INIT;
183 if (unquote_c_style(&unquoted, value, NULL) < 0)
184 die(_("invalid quoting in push-option value: '%s'"), value);
185 string_list_append_nodup(&options.push_options,
186 strbuf_detach(&unquoted, NULL));
188 return 0;
189 } else if (!strcmp(name, "family")) {
190 if (!strcmp(value, "ipv4"))
191 git_curl_ipresolve = CURL_IPRESOLVE_V4;
192 else if (!strcmp(value, "ipv6"))
193 git_curl_ipresolve = CURL_IPRESOLVE_V6;
194 else if (!strcmp(value, "all"))
195 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
196 else
197 return -1;
198 return 0;
199 } else if (!strcmp(name, "from-promisor")) {
200 options.from_promisor = 1;
201 return 0;
202 } else if (!strcmp(name, "refetch")) {
203 options.refetch = 1;
204 return 0;
205 } else if (!strcmp(name, "filter")) {
206 options.filter = xstrdup(value);
207 return 0;
208 } else if (!strcmp(name, "object-format")) {
209 int algo;
210 options.object_format = 1;
211 if (strcmp(value, "true")) {
212 algo = hash_algo_by_name(value);
213 if (algo == GIT_HASH_UNKNOWN)
214 die("unknown object format '%s'", value);
215 options.hash_algo = &hash_algos[algo];
217 return 0;
218 } else {
219 return 1 /* unsupported */;
223 struct discovery {
224 char *service;
225 char *buf_alloc;
226 char *buf;
227 size_t len;
228 struct ref *refs;
229 struct oid_array shallow;
230 enum protocol_version version;
231 unsigned proto_git : 1;
233 static struct discovery *last_discovery;
235 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
237 struct ref *list = NULL;
238 struct packet_reader reader;
240 packet_reader_init(&reader, -1, heads->buf, heads->len,
241 PACKET_READ_CHOMP_NEWLINE |
242 PACKET_READ_GENTLE_ON_EOF |
243 PACKET_READ_DIE_ON_ERR_PACKET);
245 heads->version = discover_version(&reader);
246 switch (heads->version) {
247 case protocol_v2:
249 * Do nothing. This isn't a list of refs but rather a
250 * capability advertisement. Client would have run
251 * 'stateless-connect' so we'll dump this capability listing
252 * and let them request the refs themselves.
254 break;
255 case protocol_v1:
256 case protocol_v0:
257 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
258 NULL, &heads->shallow);
259 options.hash_algo = reader.hash_algo;
260 break;
261 case protocol_unknown_version:
262 BUG("unknown protocol version");
265 return list;
268 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
270 const char *p = memchr(heads->buf, '\t', heads->len);
271 int algo;
272 if (!p)
273 return the_hash_algo;
275 algo = hash_algo_by_length((p - heads->buf) / 2);
276 if (algo == GIT_HASH_UNKNOWN)
277 return NULL;
278 return &hash_algos[algo];
281 static struct ref *parse_info_refs(struct discovery *heads)
283 char *data, *start, *mid;
284 char *ref_name;
285 int i = 0;
287 struct ref *refs = NULL;
288 struct ref *ref = NULL;
289 struct ref *last_ref = NULL;
291 options.hash_algo = detect_hash_algo(heads);
292 if (!options.hash_algo)
293 die("%sinfo/refs not valid: could not determine hash algorithm; "
294 "is this a git repository?",
295 transport_anonymize_url(url.buf));
297 data = heads->buf;
298 start = NULL;
299 mid = data;
300 while (i < heads->len) {
301 if (!start) {
302 start = &data[i];
304 if (data[i] == '\t')
305 mid = &data[i];
306 if (data[i] == '\n') {
307 if (mid - start != options.hash_algo->hexsz)
308 die(_("%sinfo/refs not valid: is this a git repository?"),
309 transport_anonymize_url(url.buf));
310 data[i] = 0;
311 ref_name = mid + 1;
312 ref = alloc_ref(ref_name);
313 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
314 if (!refs)
315 refs = ref;
316 if (last_ref)
317 last_ref->next = ref;
318 last_ref = ref;
319 start = NULL;
321 i++;
324 ref = alloc_ref("HEAD");
325 if (!http_fetch_ref(url.buf, ref) &&
326 !resolve_remote_symref(ref, refs)) {
327 ref->next = refs;
328 refs = ref;
329 } else {
330 free(ref);
333 return refs;
336 static void free_discovery(struct discovery *d)
338 if (d) {
339 if (d == last_discovery)
340 last_discovery = NULL;
341 free(d->shallow.oid);
342 free(d->buf_alloc);
343 free_refs(d->refs);
344 free(d->service);
345 free(d);
349 static int show_http_message(struct strbuf *type, struct strbuf *charset,
350 struct strbuf *msg)
352 const char *p, *eol;
355 * We only show text/plain parts, as other types are likely
356 * to be ugly to look at on the user's terminal.
358 if (strcmp(type->buf, "text/plain"))
359 return -1;
360 if (charset->len)
361 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
363 strbuf_trim(msg);
364 if (!msg->len)
365 return -1;
367 p = msg->buf;
368 do {
369 eol = strchrnul(p, '\n');
370 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
371 p = eol + 1;
372 } while(*eol);
373 return 0;
376 static int get_protocol_http_header(enum protocol_version version,
377 struct strbuf *header)
379 if (version > 0) {
380 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
381 version);
383 return 1;
386 return 0;
389 static void check_smart_http(struct discovery *d, const char *service,
390 struct strbuf *type)
392 const char *p;
393 struct packet_reader reader;
396 * If we don't see x-$service-advertisement, then it's not smart-http.
397 * But once we do, we commit to it and assume any other protocol
398 * violations are hard errors.
400 if (!skip_prefix(type->buf, "application/x-", &p) ||
401 !skip_prefix(p, service, &p) ||
402 strcmp(p, "-advertisement"))
403 return;
405 packet_reader_init(&reader, -1, d->buf, d->len,
406 PACKET_READ_CHOMP_NEWLINE |
407 PACKET_READ_DIE_ON_ERR_PACKET);
408 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
409 die(_("invalid server response; expected service, got flush packet"));
411 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
413 * The header can include additional metadata lines, up
414 * until a packet flush marker. Ignore these now, but
415 * in the future we might start to scan them.
417 for (;;) {
418 packet_reader_read(&reader);
419 if (reader.pktlen <= 0) {
420 break;
425 * v0 smart http; callers expect us to soak up the
426 * service and header packets
428 d->buf = reader.src_buffer;
429 d->len = reader.src_len;
430 d->proto_git = 1;
432 } else if (!strcmp(reader.line, "version 2")) {
434 * v2 smart http; do not consume version packet, which will
435 * be handled elsewhere.
437 d->proto_git = 1;
439 } else {
440 die(_("invalid server response; got '%s'"), reader.line);
444 static struct discovery *discover_refs(const char *service, int for_push)
446 struct strbuf type = STRBUF_INIT;
447 struct strbuf charset = STRBUF_INIT;
448 struct strbuf buffer = STRBUF_INIT;
449 struct strbuf refs_url = STRBUF_INIT;
450 struct strbuf effective_url = STRBUF_INIT;
451 struct strbuf protocol_header = STRBUF_INIT;
452 struct string_list extra_headers = STRING_LIST_INIT_DUP;
453 struct discovery *last = last_discovery;
454 int http_ret, maybe_smart = 0;
455 struct http_get_options http_options;
456 enum protocol_version version = get_protocol_version_config();
458 if (last && !strcmp(service, last->service))
459 return last;
460 free_discovery(last);
462 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
463 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
464 git_env_bool("GIT_SMART_HTTP", 1)) {
465 maybe_smart = 1;
466 if (!strchr(url.buf, '?'))
467 strbuf_addch(&refs_url, '?');
468 else
469 strbuf_addch(&refs_url, '&');
470 strbuf_addf(&refs_url, "service=%s", service);
474 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
475 * to perform a push, then fallback to v0 since the client doesn't know
476 * how to push yet using v2.
478 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
479 version = protocol_v0;
481 /* Add the extra Git-Protocol header */
482 if (get_protocol_http_header(version, &protocol_header))
483 string_list_append(&extra_headers, protocol_header.buf);
485 memset(&http_options, 0, sizeof(http_options));
486 http_options.content_type = &type;
487 http_options.charset = &charset;
488 http_options.effective_url = &effective_url;
489 http_options.base_url = &url;
490 http_options.extra_headers = &extra_headers;
491 http_options.initial_request = 1;
492 http_options.no_cache = 1;
494 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
495 switch (http_ret) {
496 case HTTP_OK:
497 break;
498 case HTTP_MISSING_TARGET:
499 show_http_message(&type, &charset, &buffer);
500 die(_("repository '%s' not found"),
501 transport_anonymize_url(url.buf));
502 case HTTP_NOAUTH:
503 show_http_message(&type, &charset, &buffer);
504 die(_("Authentication failed for '%s'"),
505 transport_anonymize_url(url.buf));
506 case HTTP_NOMATCHPUBLICKEY:
507 show_http_message(&type, &charset, &buffer);
508 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
509 transport_anonymize_url(url.buf), curl_errorstr);
510 default:
511 show_http_message(&type, &charset, &buffer);
512 die(_("unable to access '%s': %s"),
513 transport_anonymize_url(url.buf), curl_errorstr);
516 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
517 char *u = transport_anonymize_url(url.buf);
518 warning(_("redirecting to %s"), u);
519 free(u);
522 last= xcalloc(1, sizeof(*last_discovery));
523 last->service = xstrdup(service);
524 last->buf_alloc = strbuf_detach(&buffer, &last->len);
525 last->buf = last->buf_alloc;
527 if (maybe_smart)
528 check_smart_http(last, service, &type);
530 if (last->proto_git)
531 last->refs = parse_git_refs(last, for_push);
532 else
533 last->refs = parse_info_refs(last);
535 strbuf_release(&refs_url);
536 strbuf_release(&type);
537 strbuf_release(&charset);
538 strbuf_release(&effective_url);
539 strbuf_release(&buffer);
540 strbuf_release(&protocol_header);
541 string_list_clear(&extra_headers, 0);
542 last_discovery = last;
543 return last;
546 static struct ref *get_refs(int for_push)
548 struct discovery *heads;
550 if (for_push)
551 heads = discover_refs("git-receive-pack", for_push);
552 else
553 heads = discover_refs("git-upload-pack", for_push);
555 return heads->refs;
558 static void output_refs(struct ref *refs)
560 struct ref *posn;
561 if (options.object_format && options.hash_algo) {
562 printf(":object-format %s\n", options.hash_algo->name);
563 repo_set_hash_algo(the_repository,
564 hash_algo_by_ptr(options.hash_algo));
566 for (posn = refs; posn; posn = posn->next) {
567 if (posn->symref)
568 printf("@%s %s\n", posn->symref, posn->name);
569 else
570 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
571 options.hash_algo),
572 posn->name);
574 printf("\n");
575 fflush(stdout);
578 struct rpc_state {
579 const char *service_name;
580 char *service_url;
581 char *hdr_content_type;
582 char *hdr_accept;
583 char *hdr_accept_language;
584 char *protocol_header;
585 char *buf;
586 size_t alloc;
587 size_t len;
588 size_t pos;
589 int in;
590 int out;
591 int any_written;
592 unsigned gzip_request : 1;
593 unsigned initial_buffer : 1;
596 * Whenever a pkt-line is read into buf, append the 4 characters
597 * denoting its length before appending the payload.
599 unsigned write_line_lengths : 1;
602 * Used by rpc_out; initialize to 0. This is true if a flush has been
603 * read, but the corresponding line length (if write_line_lengths is
604 * true) and EOF have not been sent to libcurl. Since each flush marks
605 * the end of a request, each flush must be completely sent before any
606 * further reading occurs.
608 unsigned flush_read_but_not_sent : 1;
611 #define RPC_STATE_INIT { 0 }
614 * Appends the result of reading from rpc->out to the string represented by
615 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
616 * enough space, 0 otherwise.
618 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
619 * hexadecimal string before appending the result described above.
621 * Writes the total number of bytes appended into appended.
623 static int rpc_read_from_out(struct rpc_state *rpc, int options,
624 size_t *appended,
625 enum packet_read_status *status) {
626 size_t left;
627 char *buf;
628 int pktlen_raw;
630 if (rpc->write_line_lengths) {
631 left = rpc->alloc - rpc->len - 4;
632 buf = rpc->buf + rpc->len + 4;
633 } else {
634 left = rpc->alloc - rpc->len;
635 buf = rpc->buf + rpc->len;
638 if (left < LARGE_PACKET_MAX)
639 return 0;
641 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
642 left, &pktlen_raw, options);
643 if (*status != PACKET_READ_EOF) {
644 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
645 rpc->len += *appended;
648 if (rpc->write_line_lengths) {
649 switch (*status) {
650 case PACKET_READ_EOF:
651 if (!(options & PACKET_READ_GENTLE_ON_EOF))
652 die(_("shouldn't have EOF when not gentle on EOF"));
653 break;
654 case PACKET_READ_NORMAL:
655 set_packet_header(buf - 4, *appended);
656 break;
657 case PACKET_READ_DELIM:
658 memcpy(buf - 4, "0001", 4);
659 break;
660 case PACKET_READ_FLUSH:
661 memcpy(buf - 4, "0000", 4);
662 break;
663 case PACKET_READ_RESPONSE_END:
664 die(_("remote server sent unexpected response end packet"));
668 return 1;
671 static size_t rpc_out(void *ptr, size_t eltsize,
672 size_t nmemb, void *buffer_)
674 size_t max = eltsize * nmemb;
675 struct rpc_state *rpc = buffer_;
676 size_t avail = rpc->len - rpc->pos;
677 enum packet_read_status status;
679 if (!avail) {
680 rpc->initial_buffer = 0;
681 rpc->len = 0;
682 rpc->pos = 0;
683 if (!rpc->flush_read_but_not_sent) {
684 if (!rpc_read_from_out(rpc, 0, &avail, &status))
685 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
686 if (status == PACKET_READ_FLUSH)
687 rpc->flush_read_but_not_sent = 1;
690 * If flush_read_but_not_sent is true, we have already read one
691 * full request but have not fully sent it + EOF, which is why
692 * we need to refrain from reading.
695 if (rpc->flush_read_but_not_sent) {
696 if (!avail) {
698 * The line length either does not need to be sent at
699 * all or has already been completely sent. Now we can
700 * return 0, indicating EOF, meaning that the flush has
701 * been fully sent.
703 rpc->flush_read_but_not_sent = 0;
704 return 0;
707 * If avail is non-zero, the line length for the flush still
708 * hasn't been fully sent. Proceed with sending the line
709 * length.
713 if (max < avail)
714 avail = max;
715 memcpy(ptr, rpc->buf + rpc->pos, avail);
716 rpc->pos += avail;
717 return avail;
720 static int rpc_seek(void *clientp, curl_off_t offset, int origin)
722 struct rpc_state *rpc = clientp;
724 if (origin != SEEK_SET)
725 BUG("rpc_seek only handles SEEK_SET, not %d", origin);
727 if (rpc->initial_buffer) {
728 if (offset < 0 || offset > rpc->len) {
729 error("curl seek would be outside of rpc buffer");
730 return CURL_SEEKFUNC_FAIL;
732 rpc->pos = offset;
733 return CURL_SEEKFUNC_OK;
735 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
736 return CURL_SEEKFUNC_FAIL;
739 struct check_pktline_state {
740 char len_buf[4];
741 int len_filled;
742 int remaining;
745 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
747 while (size) {
748 if (!state->remaining) {
749 int digits_remaining = 4 - state->len_filled;
750 if (digits_remaining > size)
751 digits_remaining = size;
752 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
753 state->len_filled += digits_remaining;
754 ptr += digits_remaining;
755 size -= digits_remaining;
757 if (state->len_filled == 4) {
758 state->remaining = packet_length(state->len_buf);
759 if (state->remaining < 0) {
760 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
761 } else if (state->remaining == 2) {
762 die(_("remote-curl: unexpected response end packet"));
763 } else if (state->remaining < 4) {
764 state->remaining = 0;
765 } else {
766 state->remaining -= 4;
768 state->len_filled = 0;
772 if (state->remaining) {
773 int remaining = state->remaining;
774 if (remaining > size)
775 remaining = size;
776 ptr += remaining;
777 size -= remaining;
778 state->remaining -= remaining;
783 struct rpc_in_data {
784 struct rpc_state *rpc;
785 struct active_request_slot *slot;
786 int check_pktline;
787 struct check_pktline_state pktline_state;
791 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
792 * from ptr.
794 static size_t rpc_in(char *ptr, size_t eltsize,
795 size_t nmemb, void *buffer_)
797 size_t size = eltsize * nmemb;
798 struct rpc_in_data *data = buffer_;
799 long response_code;
801 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
802 &response_code) != CURLE_OK)
803 return size;
804 if (response_code >= 300)
805 return size;
806 if (size)
807 data->rpc->any_written = 1;
808 if (data->check_pktline)
809 check_pktline(&data->pktline_state, ptr, size);
810 write_or_die(data->rpc->in, ptr, size);
811 return size;
814 static int run_slot(struct active_request_slot *slot,
815 struct slot_results *results)
817 int err;
818 struct slot_results results_buf;
820 if (!results)
821 results = &results_buf;
823 err = run_one_slot(slot, results);
825 if (err != HTTP_OK && err != HTTP_REAUTH) {
826 struct strbuf msg = STRBUF_INIT;
827 if (results->http_code && results->http_code != 200)
828 strbuf_addf(&msg, "HTTP %ld", results->http_code);
829 if (results->curl_result != CURLE_OK) {
830 if (msg.len)
831 strbuf_addch(&msg, ' ');
832 strbuf_addf(&msg, "curl %d", results->curl_result);
833 if (curl_errorstr[0]) {
834 strbuf_addch(&msg, ' ');
835 strbuf_addstr(&msg, curl_errorstr);
838 error(_("RPC failed; %s"), msg.buf);
839 strbuf_release(&msg);
842 return err;
845 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
847 struct active_request_slot *slot;
848 struct curl_slist *headers = http_copy_default_headers();
849 struct strbuf buf = STRBUF_INIT;
850 int err;
852 slot = get_active_slot();
854 headers = curl_slist_append(headers, rpc->hdr_content_type);
855 headers = curl_slist_append(headers, rpc->hdr_accept);
857 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
858 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
859 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
860 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
861 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
862 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
863 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
864 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
865 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf);
867 err = run_slot(slot, results);
869 curl_slist_free_all(headers);
870 strbuf_release(&buf);
871 return err;
874 static curl_off_t xcurl_off_t(size_t len)
876 uintmax_t size = len;
877 if (size > maximum_signed_value_of_type(curl_off_t))
878 die(_("cannot handle pushes this big"));
879 return (curl_off_t)size;
883 * If flush_received is true, do not attempt to read any more; just use what's
884 * in rpc->buf.
886 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
888 struct active_request_slot *slot;
889 struct curl_slist *headers = http_copy_default_headers();
890 int use_gzip = rpc->gzip_request;
891 char *gzip_body = NULL;
892 size_t gzip_size = 0;
893 int err, large_request = 0;
894 int needs_100_continue = 0;
895 struct rpc_in_data rpc_in_data;
897 /* Try to load the entire request, if we can fit it into the
898 * allocated buffer space we can use HTTP/1.0 and avoid the
899 * chunked encoding mess.
901 if (!flush_received) {
902 while (1) {
903 size_t n;
904 enum packet_read_status status;
906 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
907 large_request = 1;
908 use_gzip = 0;
909 break;
911 if (status == PACKET_READ_FLUSH)
912 break;
916 if (large_request) {
917 struct slot_results results;
919 do {
920 err = probe_rpc(rpc, &results);
921 if (err == HTTP_REAUTH)
922 credential_fill(&http_auth);
923 } while (err == HTTP_REAUTH);
924 if (err != HTTP_OK)
925 return -1;
927 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
928 needs_100_continue = 1;
931 headers = curl_slist_append(headers, rpc->hdr_content_type);
932 headers = curl_slist_append(headers, rpc->hdr_accept);
933 headers = curl_slist_append(headers, needs_100_continue ?
934 "Expect: 100-continue" : "Expect:");
936 /* Add Accept-Language header */
937 if (rpc->hdr_accept_language)
938 headers = curl_slist_append(headers, rpc->hdr_accept_language);
940 /* Add the extra Git-Protocol header */
941 if (rpc->protocol_header)
942 headers = curl_slist_append(headers, rpc->protocol_header);
944 retry:
945 slot = get_active_slot();
947 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
948 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
949 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
950 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
952 if (large_request) {
953 /* The request body is large and the size cannot be predicted.
954 * We must use chunked encoding to send it.
956 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
957 rpc->initial_buffer = 1;
958 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
959 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
960 curl_easy_setopt(slot->curl, CURLOPT_SEEKFUNCTION, rpc_seek);
961 curl_easy_setopt(slot->curl, CURLOPT_SEEKDATA, rpc);
962 if (options.verbosity > 1) {
963 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
964 fflush(stderr);
967 } else if (gzip_body) {
969 * If we are looping to retry authentication, then the previous
970 * run will have set up the headers and gzip buffer already,
971 * and we just need to send it.
973 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
974 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
976 } else if (use_gzip && 1024 < rpc->len) {
977 /* The client backend isn't giving us compressed data so
978 * we can try to deflate it ourselves, this may save on
979 * the transfer time.
981 git_zstream stream;
982 int ret;
984 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
985 gzip_size = git_deflate_bound(&stream, rpc->len);
986 gzip_body = xmalloc(gzip_size);
988 stream.next_in = (unsigned char *)rpc->buf;
989 stream.avail_in = rpc->len;
990 stream.next_out = (unsigned char *)gzip_body;
991 stream.avail_out = gzip_size;
993 ret = git_deflate(&stream, Z_FINISH);
994 if (ret != Z_STREAM_END)
995 die(_("cannot deflate request; zlib deflate error %d"), ret);
997 ret = git_deflate_end_gently(&stream);
998 if (ret != Z_OK)
999 die(_("cannot deflate request; zlib end error %d"), ret);
1001 gzip_size = stream.total_out;
1003 headers = curl_slist_append(headers, "Content-Encoding: gzip");
1004 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
1005 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
1007 if (options.verbosity > 1) {
1008 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
1009 rpc->service_name,
1010 (unsigned long)rpc->len, (unsigned long)gzip_size);
1011 fflush(stderr);
1013 } else {
1014 /* We know the complete request size in advance, use the
1015 * more normal Content-Length approach.
1017 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1018 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1019 if (options.verbosity > 1) {
1020 fprintf(stderr, "POST %s (%lu bytes)\n",
1021 rpc->service_name, (unsigned long)rpc->len);
1022 fflush(stderr);
1026 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1027 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
1028 rpc_in_data.rpc = rpc;
1029 rpc_in_data.slot = slot;
1030 rpc_in_data.check_pktline = stateless_connect;
1031 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
1032 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &rpc_in_data);
1033 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1036 rpc->any_written = 0;
1037 err = run_slot(slot, NULL);
1038 if (err == HTTP_REAUTH && !large_request) {
1039 credential_fill(&http_auth);
1040 goto retry;
1042 if (err != HTTP_OK)
1043 err = -1;
1045 if (!rpc->any_written)
1046 err = -1;
1048 if (rpc_in_data.pktline_state.len_filled)
1049 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1050 if (rpc_in_data.pktline_state.remaining)
1051 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1053 if (stateless_connect)
1054 packet_response_end(rpc->in);
1056 curl_slist_free_all(headers);
1057 free(gzip_body);
1058 return err;
1061 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
1062 const char **client_argv, const struct strbuf *preamble,
1063 struct strbuf *rpc_result)
1065 const char *svc = rpc->service_name;
1066 struct strbuf buf = STRBUF_INIT;
1067 struct child_process client = CHILD_PROCESS_INIT;
1068 int err = 0;
1070 client.in = -1;
1071 client.out = -1;
1072 client.git_cmd = 1;
1073 strvec_pushv(&client.args, client_argv);
1074 if (start_command(&client))
1075 exit(1);
1076 write_or_die(client.in, preamble->buf, preamble->len);
1077 if (heads)
1078 write_or_die(client.in, heads->buf, heads->len);
1080 rpc->alloc = http_post_buffer;
1081 rpc->buf = xmalloc(rpc->alloc);
1082 rpc->in = client.in;
1083 rpc->out = client.out;
1085 strbuf_addf(&buf, "%s%s", url.buf, svc);
1086 rpc->service_url = strbuf_detach(&buf, NULL);
1088 rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
1090 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1091 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1093 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
1094 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1096 if (get_protocol_http_header(heads->version, &buf))
1097 rpc->protocol_header = strbuf_detach(&buf, NULL);
1098 else
1099 rpc->protocol_header = NULL;
1101 while (!err) {
1102 int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0);
1103 if (!n)
1104 break;
1105 rpc->pos = 0;
1106 rpc->len = n;
1107 err |= post_rpc(rpc, 0, 0);
1110 close(client.in);
1111 client.in = -1;
1112 if (!err) {
1113 strbuf_read(rpc_result, client.out, 0);
1114 } else {
1115 char buf[4096];
1116 for (;;)
1117 if (xread(client.out, buf, sizeof(buf)) <= 0)
1118 break;
1121 close(client.out);
1122 client.out = -1;
1124 err |= finish_command(&client);
1125 free(rpc->service_url);
1126 free(rpc->hdr_content_type);
1127 free(rpc->hdr_accept);
1128 free(rpc->hdr_accept_language);
1129 free(rpc->protocol_header);
1130 free(rpc->buf);
1131 strbuf_release(&buf);
1132 return err;
1135 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1137 struct walker *walker;
1138 char **targets;
1139 int ret, i;
1141 ALLOC_ARRAY(targets, nr_heads);
1142 if (options.depth || options.deepen_since)
1143 die(_("dumb http transport does not support shallow capabilities"));
1144 for (i = 0; i < nr_heads; i++)
1145 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1147 walker = get_http_walker(url.buf);
1148 walker->get_verbosely = options.verbosity >= 3;
1149 walker->get_progress = options.progress;
1150 walker->get_recover = 0;
1151 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1152 walker_free(walker);
1154 for (i = 0; i < nr_heads; i++)
1155 free(targets[i]);
1156 free(targets);
1158 return ret ? error(_("fetch failed.")) : 0;
1161 static int fetch_git(struct discovery *heads,
1162 int nr_heads, struct ref **to_fetch)
1164 struct rpc_state rpc = RPC_STATE_INIT;
1165 struct strbuf preamble = STRBUF_INIT;
1166 int i, err;
1167 struct strvec args = STRVEC_INIT;
1168 struct strbuf rpc_result = STRBUF_INIT;
1170 strvec_pushl(&args, "fetch-pack", "--stateless-rpc",
1171 "--stdin", "--lock-pack", NULL);
1172 if (options.followtags)
1173 strvec_push(&args, "--include-tag");
1174 if (options.thin)
1175 strvec_push(&args, "--thin");
1176 if (options.verbosity >= 3)
1177 strvec_pushl(&args, "-v", "-v", NULL);
1178 if (options.check_self_contained_and_connected)
1179 strvec_push(&args, "--check-self-contained-and-connected");
1180 if (options.cloning)
1181 strvec_push(&args, "--cloning");
1182 if (options.update_shallow)
1183 strvec_push(&args, "--update-shallow");
1184 if (!options.progress)
1185 strvec_push(&args, "--no-progress");
1186 if (options.depth)
1187 strvec_pushf(&args, "--depth=%lu", options.depth);
1188 if (options.deepen_since)
1189 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since);
1190 for (i = 0; i < options.deepen_not.nr; i++)
1191 strvec_pushf(&args, "--shallow-exclude=%s",
1192 options.deepen_not.items[i].string);
1193 if (options.deepen_relative && options.depth)
1194 strvec_push(&args, "--deepen-relative");
1195 if (options.from_promisor)
1196 strvec_push(&args, "--from-promisor");
1197 if (options.refetch)
1198 strvec_push(&args, "--refetch");
1199 if (options.filter)
1200 strvec_pushf(&args, "--filter=%s", options.filter);
1201 strvec_push(&args, url.buf);
1203 for (i = 0; i < nr_heads; i++) {
1204 struct ref *ref = to_fetch[i];
1205 if (!*ref->name)
1206 die(_("cannot fetch by sha1 over smart http"));
1207 packet_buf_write(&preamble, "%s %s\n",
1208 oid_to_hex(&ref->old_oid), ref->name);
1210 packet_buf_flush(&preamble);
1212 memset(&rpc, 0, sizeof(rpc));
1213 rpc.service_name = "git-upload-pack",
1214 rpc.gzip_request = 1;
1216 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1217 if (rpc_result.len)
1218 write_or_die(1, rpc_result.buf, rpc_result.len);
1219 strbuf_release(&rpc_result);
1220 strbuf_release(&preamble);
1221 strvec_clear(&args);
1222 return err;
1225 static int fetch(int nr_heads, struct ref **to_fetch)
1227 struct discovery *d = discover_refs("git-upload-pack", 0);
1228 if (d->proto_git)
1229 return fetch_git(d, nr_heads, to_fetch);
1230 else
1231 return fetch_dumb(nr_heads, to_fetch);
1234 static void parse_fetch(struct strbuf *buf)
1236 struct ref **to_fetch = NULL;
1237 struct ref *list_head = NULL;
1238 struct ref **list = &list_head;
1239 int alloc_heads = 0, nr_heads = 0;
1241 do {
1242 const char *p;
1243 if (skip_prefix(buf->buf, "fetch ", &p)) {
1244 const char *name;
1245 struct ref *ref;
1246 struct object_id old_oid;
1247 const char *q;
1249 if (parse_oid_hex(p, &old_oid, &q))
1250 die(_("protocol error: expected sha/ref, got '%s'"), p);
1251 if (*q == ' ')
1252 name = q + 1;
1253 else if (!*q)
1254 name = "";
1255 else
1256 die(_("protocol error: expected sha/ref, got '%s'"), p);
1258 ref = alloc_ref(name);
1259 oidcpy(&ref->old_oid, &old_oid);
1261 *list = ref;
1262 list = &ref->next;
1264 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1265 to_fetch[nr_heads++] = ref;
1267 else
1268 die(_("http transport does not support %s"), buf->buf);
1270 strbuf_reset(buf);
1271 if (strbuf_getline_lf(buf, stdin) == EOF)
1272 return;
1273 if (!*buf->buf)
1274 break;
1275 } while (1);
1277 if (fetch(nr_heads, to_fetch))
1278 exit(128); /* error already reported */
1279 free_refs(list_head);
1280 free(to_fetch);
1282 printf("\n");
1283 fflush(stdout);
1284 strbuf_reset(buf);
1287 static void parse_get(const char *arg)
1289 struct strbuf url = STRBUF_INIT;
1290 struct strbuf path = STRBUF_INIT;
1291 const char *space;
1293 space = strchr(arg, ' ');
1295 if (!space)
1296 die(_("protocol error: expected '<url> <path>', missing space"));
1298 strbuf_add(&url, arg, space - arg);
1299 strbuf_addstr(&path, space + 1);
1301 if (http_get_file(url.buf, path.buf, NULL))
1302 die(_("failed to download file at URL '%s'"), url.buf);
1304 strbuf_release(&url);
1305 strbuf_release(&path);
1306 printf("\n");
1307 fflush(stdout);
1310 static int push_dav(int nr_spec, const char **specs)
1312 struct child_process child = CHILD_PROCESS_INIT;
1313 size_t i;
1315 child.git_cmd = 1;
1316 strvec_push(&child.args, "http-push");
1317 strvec_push(&child.args, "--helper-status");
1318 if (options.dry_run)
1319 strvec_push(&child.args, "--dry-run");
1320 if (options.verbosity > 1)
1321 strvec_push(&child.args, "--verbose");
1322 strvec_push(&child.args, url.buf);
1323 for (i = 0; i < nr_spec; i++)
1324 strvec_push(&child.args, specs[i]);
1326 if (run_command(&child))
1327 die(_("git-http-push failed"));
1328 return 0;
1331 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1333 struct rpc_state rpc = RPC_STATE_INIT;
1334 int i, err;
1335 struct strvec args;
1336 struct string_list_item *cas_option;
1337 struct strbuf preamble = STRBUF_INIT;
1338 struct strbuf rpc_result = STRBUF_INIT;
1340 strvec_init(&args);
1341 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1342 NULL);
1344 if (options.thin)
1345 strvec_push(&args, "--thin");
1346 if (options.dry_run)
1347 strvec_push(&args, "--dry-run");
1348 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1349 strvec_push(&args, "--signed=yes");
1350 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1351 strvec_push(&args, "--signed=if-asked");
1352 if (options.atomic)
1353 strvec_push(&args, "--atomic");
1354 if (options.verbosity == 0)
1355 strvec_push(&args, "--quiet");
1356 else if (options.verbosity > 1)
1357 strvec_push(&args, "--verbose");
1358 for (i = 0; i < options.push_options.nr; i++)
1359 strvec_pushf(&args, "--push-option=%s",
1360 options.push_options.items[i].string);
1361 strvec_push(&args, options.progress ? "--progress" : "--no-progress");
1362 for_each_string_list_item(cas_option, &cas_options)
1363 strvec_push(&args, cas_option->string);
1364 strvec_push(&args, url.buf);
1366 if (options.force_if_includes)
1367 strvec_push(&args, "--force-if-includes");
1369 strvec_push(&args, "--stdin");
1370 for (i = 0; i < nr_spec; i++)
1371 packet_buf_write(&preamble, "%s\n", specs[i]);
1372 packet_buf_flush(&preamble);
1374 memset(&rpc, 0, sizeof(rpc));
1375 rpc.service_name = "git-receive-pack",
1377 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1378 if (rpc_result.len)
1379 write_or_die(1, rpc_result.buf, rpc_result.len);
1380 strbuf_release(&rpc_result);
1381 strbuf_release(&preamble);
1382 strvec_clear(&args);
1383 return err;
1386 static int push(int nr_spec, const char **specs)
1388 struct discovery *heads = discover_refs("git-receive-pack", 1);
1389 int ret;
1391 if (heads->proto_git)
1392 ret = push_git(heads, nr_spec, specs);
1393 else
1394 ret = push_dav(nr_spec, specs);
1395 free_discovery(heads);
1396 return ret;
1399 static void parse_push(struct strbuf *buf)
1401 struct strvec specs = STRVEC_INIT;
1402 int ret;
1404 do {
1405 const char *arg;
1406 if (skip_prefix(buf->buf, "push ", &arg))
1407 strvec_push(&specs, arg);
1408 else
1409 die(_("http transport does not support %s"), buf->buf);
1411 strbuf_reset(buf);
1412 if (strbuf_getline_lf(buf, stdin) == EOF)
1413 goto free_specs;
1414 if (!*buf->buf)
1415 break;
1416 } while (1);
1418 ret = push(specs.nr, specs.v);
1419 printf("\n");
1420 fflush(stdout);
1422 if (ret)
1423 exit(128); /* error already reported */
1425 free_specs:
1426 strvec_clear(&specs);
1429 static int stateless_connect(const char *service_name)
1431 struct discovery *discover;
1432 struct rpc_state rpc = RPC_STATE_INIT;
1433 struct strbuf buf = STRBUF_INIT;
1434 const char *accept_language;
1437 * Run the info/refs request and see if the server supports protocol
1438 * v2. If and only if the server supports v2 can we successfully
1439 * establish a stateless connection, otherwise we need to tell the
1440 * client to fallback to using other transport helper functions to
1441 * complete their request.
1443 discover = discover_refs(service_name, 0);
1444 if (discover->version != protocol_v2) {
1445 printf("fallback\n");
1446 fflush(stdout);
1447 return -1;
1448 } else {
1449 /* Stateless Connection established */
1450 printf("\n");
1451 fflush(stdout);
1453 accept_language = http_get_accept_language_header();
1454 if (accept_language)
1455 rpc.hdr_accept_language = xstrfmt("%s", accept_language);
1457 rpc.service_name = service_name;
1458 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1459 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1460 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1461 if (get_protocol_http_header(discover->version, &buf)) {
1462 rpc.protocol_header = strbuf_detach(&buf, NULL);
1463 } else {
1464 rpc.protocol_header = NULL;
1465 strbuf_release(&buf);
1467 rpc.buf = xmalloc(http_post_buffer);
1468 rpc.alloc = http_post_buffer;
1469 rpc.len = 0;
1470 rpc.pos = 0;
1471 rpc.in = 1;
1472 rpc.out = 0;
1473 rpc.any_written = 0;
1474 rpc.gzip_request = 1;
1475 rpc.initial_buffer = 0;
1476 rpc.write_line_lengths = 1;
1477 rpc.flush_read_but_not_sent = 0;
1480 * Dump the capability listing that we got from the server earlier
1481 * during the info/refs request.
1483 write_or_die(rpc.in, discover->buf, discover->len);
1485 /* Until we see EOF keep sending POSTs */
1486 while (1) {
1487 size_t avail;
1488 enum packet_read_status status;
1490 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1491 &status))
1492 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1493 if (status == PACKET_READ_EOF)
1494 break;
1495 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
1496 /* We would have an err here */
1497 break;
1498 /* Reset the buffer for next request */
1499 rpc.len = 0;
1502 free(rpc.service_url);
1503 free(rpc.hdr_content_type);
1504 free(rpc.hdr_accept);
1505 free(rpc.hdr_accept_language);
1506 free(rpc.protocol_header);
1507 free(rpc.buf);
1508 strbuf_release(&buf);
1510 return 0;
1513 int cmd_main(int argc, const char **argv)
1515 struct strbuf buf = STRBUF_INIT;
1516 int nongit;
1517 int ret = 1;
1519 setup_git_directory_gently(&nongit);
1520 if (argc < 2) {
1521 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1522 goto cleanup;
1525 options.verbosity = 1;
1526 options.progress = !!isatty(2);
1527 options.thin = 1;
1528 string_list_init_dup(&options.deepen_not);
1529 string_list_init_dup(&options.push_options);
1532 * Just report "remote-curl" here (folding all the various aliases
1533 * ("git-remote-http", "git-remote-https", and etc.) here since they
1534 * are all just copies of the same actual executable.
1536 trace2_cmd_name("remote-curl");
1538 remote = remote_get(argv[1]);
1540 if (argc > 2) {
1541 end_url_with_slash(&url, argv[2]);
1542 } else {
1543 end_url_with_slash(&url, remote->url[0]);
1546 http_init(remote, url.buf, 0);
1548 do {
1549 const char *arg;
1551 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1552 if (ferror(stdin))
1553 error(_("remote-curl: error reading command stream from git"));
1554 goto cleanup;
1556 if (buf.len == 0)
1557 break;
1558 if (starts_with(buf.buf, "fetch ")) {
1559 if (nongit)
1560 die(_("remote-curl: fetch attempted without a local repo"));
1561 parse_fetch(&buf);
1563 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1564 int for_push = !!strstr(buf.buf + 4, "for-push");
1565 output_refs(get_refs(for_push));
1567 } else if (starts_with(buf.buf, "push ")) {
1568 parse_push(&buf);
1570 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1571 char *value = strchr(arg, ' ');
1572 int result;
1574 if (value)
1575 *value++ = '\0';
1576 else
1577 value = "true";
1579 result = set_option(arg, value);
1580 if (!result)
1581 printf("ok\n");
1582 else if (result < 0)
1583 printf("error invalid value\n");
1584 else
1585 printf("unsupported\n");
1586 fflush(stdout);
1588 } else if (skip_prefix(buf.buf, "get ", &arg)) {
1589 parse_get(arg);
1590 fflush(stdout);
1592 } else if (!strcmp(buf.buf, "capabilities")) {
1593 printf("stateless-connect\n");
1594 printf("fetch\n");
1595 printf("get\n");
1596 printf("option\n");
1597 printf("push\n");
1598 printf("check-connectivity\n");
1599 printf("object-format\n");
1600 printf("\n");
1601 fflush(stdout);
1602 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1603 if (!stateless_connect(arg))
1604 break;
1605 } else {
1606 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1607 goto cleanup;
1609 strbuf_reset(&buf);
1610 } while (1);
1612 http_cleanup();
1613 ret = 0;
1614 cleanup:
1615 strbuf_release(&buf);
1617 return ret;