setup.h: move declarations for setup.c functions from cache.h
[git/debian.git] / remote-curl.c
blob260ea200bb091f71f1b5d25ee28b17f8042719e7
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 "transport.h"
26 static struct remote *remote;
27 /* always ends with a trailing slash */
28 static struct strbuf url = STRBUF_INIT;
30 struct options {
31 int verbosity;
32 unsigned long depth;
33 char *deepen_since;
34 struct string_list deepen_not;
35 struct string_list push_options;
36 char *filter;
37 unsigned progress : 1,
38 check_self_contained_and_connected : 1,
39 cloning : 1,
40 update_shallow : 1,
41 followtags : 1,
42 dry_run : 1,
43 thin : 1,
44 /* One of the SEND_PACK_PUSH_CERT_* constants. */
45 push_cert : 2,
46 deepen_relative : 1,
48 /* see documentation of corresponding flag in fetch-pack.h */
49 from_promisor : 1,
51 refetch : 1,
52 atomic : 1,
53 object_format : 1,
54 force_if_includes : 1;
55 const struct git_hash_algo *hash_algo;
57 static struct options options;
58 static struct string_list cas_options = STRING_LIST_INIT_DUP;
60 static int set_option(const char *name, const char *value)
62 if (!strcmp(name, "verbosity")) {
63 char *end;
64 int v = strtol(value, &end, 10);
65 if (value == end || *end)
66 return -1;
67 options.verbosity = v;
68 return 0;
70 else if (!strcmp(name, "progress")) {
71 if (!strcmp(value, "true"))
72 options.progress = 1;
73 else if (!strcmp(value, "false"))
74 options.progress = 0;
75 else
76 return -1;
77 return 0;
79 else if (!strcmp(name, "depth")) {
80 char *end;
81 unsigned long v = strtoul(value, &end, 10);
82 if (value == end || *end)
83 return -1;
84 options.depth = v;
85 return 0;
87 else if (!strcmp(name, "deepen-since")) {
88 options.deepen_since = xstrdup(value);
89 return 0;
91 else if (!strcmp(name, "deepen-not")) {
92 string_list_append(&options.deepen_not, value);
93 return 0;
95 else if (!strcmp(name, "deepen-relative")) {
96 if (!strcmp(value, "true"))
97 options.deepen_relative = 1;
98 else if (!strcmp(value, "false"))
99 options.deepen_relative = 0;
100 else
101 return -1;
102 return 0;
104 else if (!strcmp(name, "followtags")) {
105 if (!strcmp(value, "true"))
106 options.followtags = 1;
107 else if (!strcmp(value, "false"))
108 options.followtags = 0;
109 else
110 return -1;
111 return 0;
113 else if (!strcmp(name, "dry-run")) {
114 if (!strcmp(value, "true"))
115 options.dry_run = 1;
116 else if (!strcmp(value, "false"))
117 options.dry_run = 0;
118 else
119 return -1;
120 return 0;
122 else if (!strcmp(name, "check-connectivity")) {
123 if (!strcmp(value, "true"))
124 options.check_self_contained_and_connected = 1;
125 else if (!strcmp(value, "false"))
126 options.check_self_contained_and_connected = 0;
127 else
128 return -1;
129 return 0;
131 else if (!strcmp(name, "cas")) {
132 struct strbuf val = STRBUF_INIT;
133 strbuf_addstr(&val, "--force-with-lease=");
134 if (*value != '"')
135 strbuf_addstr(&val, value);
136 else if (unquote_c_style(&val, value, NULL))
137 return -1;
138 string_list_append(&cas_options, val.buf);
139 strbuf_release(&val);
140 return 0;
141 } else if (!strcmp(name, TRANS_OPT_FORCE_IF_INCLUDES)) {
142 if (!strcmp(value, "true"))
143 options.force_if_includes = 1;
144 else if (!strcmp(value, "false"))
145 options.force_if_includes = 0;
146 else
147 return -1;
148 return 0;
149 } else if (!strcmp(name, "cloning")) {
150 if (!strcmp(value, "true"))
151 options.cloning = 1;
152 else if (!strcmp(value, "false"))
153 options.cloning = 0;
154 else
155 return -1;
156 return 0;
157 } else if (!strcmp(name, "update-shallow")) {
158 if (!strcmp(value, "true"))
159 options.update_shallow = 1;
160 else if (!strcmp(value, "false"))
161 options.update_shallow = 0;
162 else
163 return -1;
164 return 0;
165 } else if (!strcmp(name, "pushcert")) {
166 if (!strcmp(value, "true"))
167 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
168 else if (!strcmp(value, "false"))
169 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
170 else if (!strcmp(value, "if-asked"))
171 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
172 else
173 return -1;
174 return 0;
175 } else if (!strcmp(name, "atomic")) {
176 if (!strcmp(value, "true"))
177 options.atomic = 1;
178 else if (!strcmp(value, "false"))
179 options.atomic = 0;
180 else
181 return -1;
182 return 0;
183 } else if (!strcmp(name, "push-option")) {
184 if (*value != '"')
185 string_list_append(&options.push_options, value);
186 else {
187 struct strbuf unquoted = STRBUF_INIT;
188 if (unquote_c_style(&unquoted, value, NULL) < 0)
189 die(_("invalid quoting in push-option value: '%s'"), value);
190 string_list_append_nodup(&options.push_options,
191 strbuf_detach(&unquoted, NULL));
193 return 0;
194 } else if (!strcmp(name, "family")) {
195 if (!strcmp(value, "ipv4"))
196 git_curl_ipresolve = CURL_IPRESOLVE_V4;
197 else if (!strcmp(value, "ipv6"))
198 git_curl_ipresolve = CURL_IPRESOLVE_V6;
199 else if (!strcmp(value, "all"))
200 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
201 else
202 return -1;
203 return 0;
204 } else if (!strcmp(name, "from-promisor")) {
205 options.from_promisor = 1;
206 return 0;
207 } else if (!strcmp(name, "refetch")) {
208 options.refetch = 1;
209 return 0;
210 } else if (!strcmp(name, "filter")) {
211 options.filter = xstrdup(value);
212 return 0;
213 } else if (!strcmp(name, "object-format")) {
214 int algo;
215 options.object_format = 1;
216 if (strcmp(value, "true")) {
217 algo = hash_algo_by_name(value);
218 if (algo == GIT_HASH_UNKNOWN)
219 die("unknown object format '%s'", value);
220 options.hash_algo = &hash_algos[algo];
222 return 0;
223 } else {
224 return 1 /* unsupported */;
228 struct discovery {
229 char *service;
230 char *buf_alloc;
231 char *buf;
232 size_t len;
233 struct ref *refs;
234 struct oid_array shallow;
235 enum protocol_version version;
236 unsigned proto_git : 1;
238 static struct discovery *last_discovery;
240 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
242 struct ref *list = NULL;
243 struct packet_reader reader;
245 packet_reader_init(&reader, -1, heads->buf, heads->len,
246 PACKET_READ_CHOMP_NEWLINE |
247 PACKET_READ_GENTLE_ON_EOF |
248 PACKET_READ_DIE_ON_ERR_PACKET);
250 heads->version = discover_version(&reader);
251 switch (heads->version) {
252 case protocol_v2:
254 * Do nothing. This isn't a list of refs but rather a
255 * capability advertisement. Client would have run
256 * 'stateless-connect' so we'll dump this capability listing
257 * and let them request the refs themselves.
259 break;
260 case protocol_v1:
261 case protocol_v0:
262 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
263 NULL, &heads->shallow);
264 options.hash_algo = reader.hash_algo;
265 break;
266 case protocol_unknown_version:
267 BUG("unknown protocol version");
270 return list;
273 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
275 const char *p = memchr(heads->buf, '\t', heads->len);
276 int algo;
277 if (!p)
278 return the_hash_algo;
280 algo = hash_algo_by_length((p - heads->buf) / 2);
281 if (algo == GIT_HASH_UNKNOWN)
282 return NULL;
283 return &hash_algos[algo];
286 static struct ref *parse_info_refs(struct discovery *heads)
288 char *data, *start, *mid;
289 char *ref_name;
290 int i = 0;
292 struct ref *refs = NULL;
293 struct ref *ref = NULL;
294 struct ref *last_ref = NULL;
296 options.hash_algo = detect_hash_algo(heads);
297 if (!options.hash_algo)
298 die("%sinfo/refs not valid: could not determine hash algorithm; "
299 "is this a git repository?",
300 transport_anonymize_url(url.buf));
302 data = heads->buf;
303 start = NULL;
304 mid = data;
305 while (i < heads->len) {
306 if (!start) {
307 start = &data[i];
309 if (data[i] == '\t')
310 mid = &data[i];
311 if (data[i] == '\n') {
312 if (mid - start != options.hash_algo->hexsz)
313 die(_("%sinfo/refs not valid: is this a git repository?"),
314 transport_anonymize_url(url.buf));
315 data[i] = 0;
316 ref_name = mid + 1;
317 ref = alloc_ref(ref_name);
318 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
319 if (!refs)
320 refs = ref;
321 if (last_ref)
322 last_ref->next = ref;
323 last_ref = ref;
324 start = NULL;
326 i++;
329 ref = alloc_ref("HEAD");
330 if (!http_fetch_ref(url.buf, ref) &&
331 !resolve_remote_symref(ref, refs)) {
332 ref->next = refs;
333 refs = ref;
334 } else {
335 free(ref);
338 return refs;
341 static void free_discovery(struct discovery *d)
343 if (d) {
344 if (d == last_discovery)
345 last_discovery = NULL;
346 free(d->shallow.oid);
347 free(d->buf_alloc);
348 free_refs(d->refs);
349 free(d->service);
350 free(d);
354 static int show_http_message(struct strbuf *type, struct strbuf *charset,
355 struct strbuf *msg)
357 const char *p, *eol;
360 * We only show text/plain parts, as other types are likely
361 * to be ugly to look at on the user's terminal.
363 if (strcmp(type->buf, "text/plain"))
364 return -1;
365 if (charset->len)
366 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
368 strbuf_trim(msg);
369 if (!msg->len)
370 return -1;
372 p = msg->buf;
373 do {
374 eol = strchrnul(p, '\n');
375 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
376 p = eol + 1;
377 } while(*eol);
378 return 0;
381 static int get_protocol_http_header(enum protocol_version version,
382 struct strbuf *header)
384 if (version > 0) {
385 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
386 version);
388 return 1;
391 return 0;
394 static void check_smart_http(struct discovery *d, const char *service,
395 struct strbuf *type)
397 const char *p;
398 struct packet_reader reader;
401 * If we don't see x-$service-advertisement, then it's not smart-http.
402 * But once we do, we commit to it and assume any other protocol
403 * violations are hard errors.
405 if (!skip_prefix(type->buf, "application/x-", &p) ||
406 !skip_prefix(p, service, &p) ||
407 strcmp(p, "-advertisement"))
408 return;
410 packet_reader_init(&reader, -1, d->buf, d->len,
411 PACKET_READ_CHOMP_NEWLINE |
412 PACKET_READ_DIE_ON_ERR_PACKET);
413 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
414 die(_("invalid server response; expected service, got flush packet"));
416 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
418 * The header can include additional metadata lines, up
419 * until a packet flush marker. Ignore these now, but
420 * in the future we might start to scan them.
422 for (;;) {
423 packet_reader_read(&reader);
424 if (reader.pktlen <= 0) {
425 break;
430 * v0 smart http; callers expect us to soak up the
431 * service and header packets
433 d->buf = reader.src_buffer;
434 d->len = reader.src_len;
435 d->proto_git = 1;
437 } else if (!strcmp(reader.line, "version 2")) {
439 * v2 smart http; do not consume version packet, which will
440 * be handled elsewhere.
442 d->proto_git = 1;
444 } else {
445 die(_("invalid server response; got '%s'"), reader.line);
449 static struct discovery *discover_refs(const char *service, int for_push)
451 struct strbuf type = STRBUF_INIT;
452 struct strbuf charset = STRBUF_INIT;
453 struct strbuf buffer = STRBUF_INIT;
454 struct strbuf refs_url = STRBUF_INIT;
455 struct strbuf effective_url = STRBUF_INIT;
456 struct strbuf protocol_header = STRBUF_INIT;
457 struct string_list extra_headers = STRING_LIST_INIT_DUP;
458 struct discovery *last = last_discovery;
459 int http_ret, maybe_smart = 0;
460 struct http_get_options http_options;
461 enum protocol_version version = get_protocol_version_config();
463 if (last && !strcmp(service, last->service))
464 return last;
465 free_discovery(last);
467 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
468 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
469 git_env_bool("GIT_SMART_HTTP", 1)) {
470 maybe_smart = 1;
471 if (!strchr(url.buf, '?'))
472 strbuf_addch(&refs_url, '?');
473 else
474 strbuf_addch(&refs_url, '&');
475 strbuf_addf(&refs_url, "service=%s", service);
479 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
480 * to perform a push, then fallback to v0 since the client doesn't know
481 * how to push yet using v2.
483 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
484 version = protocol_v0;
486 /* Add the extra Git-Protocol header */
487 if (get_protocol_http_header(version, &protocol_header))
488 string_list_append(&extra_headers, protocol_header.buf);
490 memset(&http_options, 0, sizeof(http_options));
491 http_options.content_type = &type;
492 http_options.charset = &charset;
493 http_options.effective_url = &effective_url;
494 http_options.base_url = &url;
495 http_options.extra_headers = &extra_headers;
496 http_options.initial_request = 1;
497 http_options.no_cache = 1;
499 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
500 switch (http_ret) {
501 case HTTP_OK:
502 break;
503 case HTTP_MISSING_TARGET:
504 show_http_message(&type, &charset, &buffer);
505 die(_("repository '%s' not found"),
506 transport_anonymize_url(url.buf));
507 case HTTP_NOAUTH:
508 show_http_message(&type, &charset, &buffer);
509 die(_("Authentication failed for '%s'"),
510 transport_anonymize_url(url.buf));
511 case HTTP_NOMATCHPUBLICKEY:
512 show_http_message(&type, &charset, &buffer);
513 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
514 transport_anonymize_url(url.buf), curl_errorstr);
515 default:
516 show_http_message(&type, &charset, &buffer);
517 die(_("unable to access '%s': %s"),
518 transport_anonymize_url(url.buf), curl_errorstr);
521 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
522 char *u = transport_anonymize_url(url.buf);
523 warning(_("redirecting to %s"), u);
524 free(u);
527 last= xcalloc(1, sizeof(*last_discovery));
528 last->service = xstrdup(service);
529 last->buf_alloc = strbuf_detach(&buffer, &last->len);
530 last->buf = last->buf_alloc;
532 if (maybe_smart)
533 check_smart_http(last, service, &type);
535 if (last->proto_git)
536 last->refs = parse_git_refs(last, for_push);
537 else
538 last->refs = parse_info_refs(last);
540 strbuf_release(&refs_url);
541 strbuf_release(&type);
542 strbuf_release(&charset);
543 strbuf_release(&effective_url);
544 strbuf_release(&buffer);
545 strbuf_release(&protocol_header);
546 string_list_clear(&extra_headers, 0);
547 last_discovery = last;
548 return last;
551 static struct ref *get_refs(int for_push)
553 struct discovery *heads;
555 if (for_push)
556 heads = discover_refs("git-receive-pack", for_push);
557 else
558 heads = discover_refs("git-upload-pack", for_push);
560 return heads->refs;
563 static void output_refs(struct ref *refs)
565 struct ref *posn;
566 if (options.object_format && options.hash_algo) {
567 printf(":object-format %s\n", options.hash_algo->name);
568 repo_set_hash_algo(the_repository,
569 hash_algo_by_ptr(options.hash_algo));
571 for (posn = refs; posn; posn = posn->next) {
572 if (posn->symref)
573 printf("@%s %s\n", posn->symref, posn->name);
574 else
575 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
576 options.hash_algo),
577 posn->name);
579 printf("\n");
580 fflush(stdout);
583 struct rpc_state {
584 const char *service_name;
585 char *service_url;
586 char *hdr_content_type;
587 char *hdr_accept;
588 char *hdr_accept_language;
589 char *protocol_header;
590 char *buf;
591 size_t alloc;
592 size_t len;
593 size_t pos;
594 int in;
595 int out;
596 int any_written;
597 unsigned gzip_request : 1;
598 unsigned initial_buffer : 1;
601 * Whenever a pkt-line is read into buf, append the 4 characters
602 * denoting its length before appending the payload.
604 unsigned write_line_lengths : 1;
607 * Used by rpc_out; initialize to 0. This is true if a flush has been
608 * read, but the corresponding line length (if write_line_lengths is
609 * true) and EOF have not been sent to libcurl. Since each flush marks
610 * the end of a request, each flush must be completely sent before any
611 * further reading occurs.
613 unsigned flush_read_but_not_sent : 1;
616 #define RPC_STATE_INIT { 0 }
619 * Appends the result of reading from rpc->out to the string represented by
620 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
621 * enough space, 0 otherwise.
623 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
624 * hexadecimal string before appending the result described above.
626 * Writes the total number of bytes appended into appended.
628 static int rpc_read_from_out(struct rpc_state *rpc, int options,
629 size_t *appended,
630 enum packet_read_status *status) {
631 size_t left;
632 char *buf;
633 int pktlen_raw;
635 if (rpc->write_line_lengths) {
636 left = rpc->alloc - rpc->len - 4;
637 buf = rpc->buf + rpc->len + 4;
638 } else {
639 left = rpc->alloc - rpc->len;
640 buf = rpc->buf + rpc->len;
643 if (left < LARGE_PACKET_MAX)
644 return 0;
646 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
647 left, &pktlen_raw, options);
648 if (*status != PACKET_READ_EOF) {
649 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
650 rpc->len += *appended;
653 if (rpc->write_line_lengths) {
654 switch (*status) {
655 case PACKET_READ_EOF:
656 if (!(options & PACKET_READ_GENTLE_ON_EOF))
657 die(_("shouldn't have EOF when not gentle on EOF"));
658 break;
659 case PACKET_READ_NORMAL:
660 set_packet_header(buf - 4, *appended);
661 break;
662 case PACKET_READ_DELIM:
663 memcpy(buf - 4, "0001", 4);
664 break;
665 case PACKET_READ_FLUSH:
666 memcpy(buf - 4, "0000", 4);
667 break;
668 case PACKET_READ_RESPONSE_END:
669 die(_("remote server sent unexpected response end packet"));
673 return 1;
676 static size_t rpc_out(void *ptr, size_t eltsize,
677 size_t nmemb, void *buffer_)
679 size_t max = eltsize * nmemb;
680 struct rpc_state *rpc = buffer_;
681 size_t avail = rpc->len - rpc->pos;
682 enum packet_read_status status;
684 if (!avail) {
685 rpc->initial_buffer = 0;
686 rpc->len = 0;
687 rpc->pos = 0;
688 if (!rpc->flush_read_but_not_sent) {
689 if (!rpc_read_from_out(rpc, 0, &avail, &status))
690 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
691 if (status == PACKET_READ_FLUSH)
692 rpc->flush_read_but_not_sent = 1;
695 * If flush_read_but_not_sent is true, we have already read one
696 * full request but have not fully sent it + EOF, which is why
697 * we need to refrain from reading.
700 if (rpc->flush_read_but_not_sent) {
701 if (!avail) {
703 * The line length either does not need to be sent at
704 * all or has already been completely sent. Now we can
705 * return 0, indicating EOF, meaning that the flush has
706 * been fully sent.
708 rpc->flush_read_but_not_sent = 0;
709 return 0;
712 * If avail is non-zero, the line length for the flush still
713 * hasn't been fully sent. Proceed with sending the line
714 * length.
718 if (max < avail)
719 avail = max;
720 memcpy(ptr, rpc->buf + rpc->pos, avail);
721 rpc->pos += avail;
722 return avail;
725 static int rpc_seek(void *clientp, curl_off_t offset, int origin)
727 struct rpc_state *rpc = clientp;
729 if (origin != SEEK_SET)
730 BUG("rpc_seek only handles SEEK_SET, not %d", origin);
732 if (rpc->initial_buffer) {
733 if (offset < 0 || offset > rpc->len) {
734 error("curl seek would be outside of rpc buffer");
735 return CURL_SEEKFUNC_FAIL;
737 rpc->pos = offset;
738 return CURL_SEEKFUNC_OK;
740 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
741 return CURL_SEEKFUNC_FAIL;
744 struct check_pktline_state {
745 char len_buf[4];
746 int len_filled;
747 int remaining;
750 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
752 while (size) {
753 if (!state->remaining) {
754 int digits_remaining = 4 - state->len_filled;
755 if (digits_remaining > size)
756 digits_remaining = size;
757 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
758 state->len_filled += digits_remaining;
759 ptr += digits_remaining;
760 size -= digits_remaining;
762 if (state->len_filled == 4) {
763 state->remaining = packet_length(state->len_buf);
764 if (state->remaining < 0) {
765 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
766 } else if (state->remaining == 2) {
767 die(_("remote-curl: unexpected response end packet"));
768 } else if (state->remaining < 4) {
769 state->remaining = 0;
770 } else {
771 state->remaining -= 4;
773 state->len_filled = 0;
777 if (state->remaining) {
778 int remaining = state->remaining;
779 if (remaining > size)
780 remaining = size;
781 ptr += remaining;
782 size -= remaining;
783 state->remaining -= remaining;
788 struct rpc_in_data {
789 struct rpc_state *rpc;
790 struct active_request_slot *slot;
791 int check_pktline;
792 struct check_pktline_state pktline_state;
796 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
797 * from ptr.
799 static size_t rpc_in(char *ptr, size_t eltsize,
800 size_t nmemb, void *buffer_)
802 size_t size = eltsize * nmemb;
803 struct rpc_in_data *data = buffer_;
804 long response_code;
806 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
807 &response_code) != CURLE_OK)
808 return size;
809 if (response_code >= 300)
810 return size;
811 if (size)
812 data->rpc->any_written = 1;
813 if (data->check_pktline)
814 check_pktline(&data->pktline_state, ptr, size);
815 write_or_die(data->rpc->in, ptr, size);
816 return size;
819 static int run_slot(struct active_request_slot *slot,
820 struct slot_results *results)
822 int err;
823 struct slot_results results_buf;
825 if (!results)
826 results = &results_buf;
828 err = run_one_slot(slot, results);
830 if (err != HTTP_OK && err != HTTP_REAUTH) {
831 struct strbuf msg = STRBUF_INIT;
832 if (results->http_code && results->http_code != 200)
833 strbuf_addf(&msg, "HTTP %ld", results->http_code);
834 if (results->curl_result != CURLE_OK) {
835 if (msg.len)
836 strbuf_addch(&msg, ' ');
837 strbuf_addf(&msg, "curl %d", results->curl_result);
838 if (curl_errorstr[0]) {
839 strbuf_addch(&msg, ' ');
840 strbuf_addstr(&msg, curl_errorstr);
843 error(_("RPC failed; %s"), msg.buf);
844 strbuf_release(&msg);
847 return err;
850 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
852 struct active_request_slot *slot;
853 struct curl_slist *headers = http_copy_default_headers();
854 struct strbuf buf = STRBUF_INIT;
855 int err;
857 slot = get_active_slot();
859 headers = curl_slist_append(headers, rpc->hdr_content_type);
860 headers = curl_slist_append(headers, rpc->hdr_accept);
862 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
863 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
864 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
865 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
866 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
867 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
868 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
869 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
870 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf);
872 err = run_slot(slot, results);
874 curl_slist_free_all(headers);
875 strbuf_release(&buf);
876 return err;
879 static curl_off_t xcurl_off_t(size_t len)
881 uintmax_t size = len;
882 if (size > maximum_signed_value_of_type(curl_off_t))
883 die(_("cannot handle pushes this big"));
884 return (curl_off_t)size;
888 * If flush_received is true, do not attempt to read any more; just use what's
889 * in rpc->buf.
891 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
893 struct active_request_slot *slot;
894 struct curl_slist *headers = http_copy_default_headers();
895 int use_gzip = rpc->gzip_request;
896 char *gzip_body = NULL;
897 size_t gzip_size = 0;
898 int err, large_request = 0;
899 int needs_100_continue = 0;
900 struct rpc_in_data rpc_in_data;
902 /* Try to load the entire request, if we can fit it into the
903 * allocated buffer space we can use HTTP/1.0 and avoid the
904 * chunked encoding mess.
906 if (!flush_received) {
907 while (1) {
908 size_t n;
909 enum packet_read_status status;
911 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
912 large_request = 1;
913 use_gzip = 0;
914 break;
916 if (status == PACKET_READ_FLUSH)
917 break;
921 if (large_request) {
922 struct slot_results results;
924 do {
925 err = probe_rpc(rpc, &results);
926 if (err == HTTP_REAUTH)
927 credential_fill(&http_auth);
928 } while (err == HTTP_REAUTH);
929 if (err != HTTP_OK)
930 return -1;
932 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
933 needs_100_continue = 1;
936 headers = curl_slist_append(headers, rpc->hdr_content_type);
937 headers = curl_slist_append(headers, rpc->hdr_accept);
938 headers = curl_slist_append(headers, needs_100_continue ?
939 "Expect: 100-continue" : "Expect:");
941 /* Add Accept-Language header */
942 if (rpc->hdr_accept_language)
943 headers = curl_slist_append(headers, rpc->hdr_accept_language);
945 /* Add the extra Git-Protocol header */
946 if (rpc->protocol_header)
947 headers = curl_slist_append(headers, rpc->protocol_header);
949 retry:
950 slot = get_active_slot();
952 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
953 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
954 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
955 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
957 if (large_request) {
958 /* The request body is large and the size cannot be predicted.
959 * We must use chunked encoding to send it.
961 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
962 rpc->initial_buffer = 1;
963 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
964 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
965 curl_easy_setopt(slot->curl, CURLOPT_SEEKFUNCTION, rpc_seek);
966 curl_easy_setopt(slot->curl, CURLOPT_SEEKDATA, rpc);
967 if (options.verbosity > 1) {
968 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
969 fflush(stderr);
972 } else if (gzip_body) {
974 * If we are looping to retry authentication, then the previous
975 * run will have set up the headers and gzip buffer already,
976 * and we just need to send it.
978 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
979 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
981 } else if (use_gzip && 1024 < rpc->len) {
982 /* The client backend isn't giving us compressed data so
983 * we can try to deflate it ourselves, this may save on
984 * the transfer time.
986 git_zstream stream;
987 int ret;
989 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
990 gzip_size = git_deflate_bound(&stream, rpc->len);
991 gzip_body = xmalloc(gzip_size);
993 stream.next_in = (unsigned char *)rpc->buf;
994 stream.avail_in = rpc->len;
995 stream.next_out = (unsigned char *)gzip_body;
996 stream.avail_out = gzip_size;
998 ret = git_deflate(&stream, Z_FINISH);
999 if (ret != Z_STREAM_END)
1000 die(_("cannot deflate request; zlib deflate error %d"), ret);
1002 ret = git_deflate_end_gently(&stream);
1003 if (ret != Z_OK)
1004 die(_("cannot deflate request; zlib end error %d"), ret);
1006 gzip_size = stream.total_out;
1008 headers = curl_slist_append(headers, "Content-Encoding: gzip");
1009 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
1010 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
1012 if (options.verbosity > 1) {
1013 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
1014 rpc->service_name,
1015 (unsigned long)rpc->len, (unsigned long)gzip_size);
1016 fflush(stderr);
1018 } else {
1019 /* We know the complete request size in advance, use the
1020 * more normal Content-Length approach.
1022 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1023 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1024 if (options.verbosity > 1) {
1025 fprintf(stderr, "POST %s (%lu bytes)\n",
1026 rpc->service_name, (unsigned long)rpc->len);
1027 fflush(stderr);
1031 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1032 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
1033 rpc_in_data.rpc = rpc;
1034 rpc_in_data.slot = slot;
1035 rpc_in_data.check_pktline = stateless_connect;
1036 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
1037 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &rpc_in_data);
1038 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1041 rpc->any_written = 0;
1042 err = run_slot(slot, NULL);
1043 if (err == HTTP_REAUTH && !large_request) {
1044 credential_fill(&http_auth);
1045 goto retry;
1047 if (err != HTTP_OK)
1048 err = -1;
1050 if (!rpc->any_written)
1051 err = -1;
1053 if (rpc_in_data.pktline_state.len_filled)
1054 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1055 if (rpc_in_data.pktline_state.remaining)
1056 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1058 if (stateless_connect)
1059 packet_response_end(rpc->in);
1061 curl_slist_free_all(headers);
1062 free(gzip_body);
1063 return err;
1066 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
1067 const char **client_argv, const struct strbuf *preamble,
1068 struct strbuf *rpc_result)
1070 const char *svc = rpc->service_name;
1071 struct strbuf buf = STRBUF_INIT;
1072 struct child_process client = CHILD_PROCESS_INIT;
1073 int err = 0;
1075 client.in = -1;
1076 client.out = -1;
1077 client.git_cmd = 1;
1078 strvec_pushv(&client.args, client_argv);
1079 if (start_command(&client))
1080 exit(1);
1081 write_or_die(client.in, preamble->buf, preamble->len);
1082 if (heads)
1083 write_or_die(client.in, heads->buf, heads->len);
1085 rpc->alloc = http_post_buffer;
1086 rpc->buf = xmalloc(rpc->alloc);
1087 rpc->in = client.in;
1088 rpc->out = client.out;
1090 strbuf_addf(&buf, "%s%s", url.buf, svc);
1091 rpc->service_url = strbuf_detach(&buf, NULL);
1093 rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
1095 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1096 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1098 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
1099 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1101 if (get_protocol_http_header(heads->version, &buf))
1102 rpc->protocol_header = strbuf_detach(&buf, NULL);
1103 else
1104 rpc->protocol_header = NULL;
1106 while (!err) {
1107 int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0);
1108 if (!n)
1109 break;
1110 rpc->pos = 0;
1111 rpc->len = n;
1112 err |= post_rpc(rpc, 0, 0);
1115 close(client.in);
1116 client.in = -1;
1117 if (!err) {
1118 strbuf_read(rpc_result, client.out, 0);
1119 } else {
1120 char buf[4096];
1121 for (;;)
1122 if (xread(client.out, buf, sizeof(buf)) <= 0)
1123 break;
1126 close(client.out);
1127 client.out = -1;
1129 err |= finish_command(&client);
1130 free(rpc->service_url);
1131 free(rpc->hdr_content_type);
1132 free(rpc->hdr_accept);
1133 free(rpc->hdr_accept_language);
1134 free(rpc->protocol_header);
1135 free(rpc->buf);
1136 strbuf_release(&buf);
1137 return err;
1140 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1142 struct walker *walker;
1143 char **targets;
1144 int ret, i;
1146 ALLOC_ARRAY(targets, nr_heads);
1147 if (options.depth || options.deepen_since)
1148 die(_("dumb http transport does not support shallow capabilities"));
1149 for (i = 0; i < nr_heads; i++)
1150 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1152 walker = get_http_walker(url.buf);
1153 walker->get_verbosely = options.verbosity >= 3;
1154 walker->get_progress = options.progress;
1155 walker->get_recover = 0;
1156 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1157 walker_free(walker);
1159 for (i = 0; i < nr_heads; i++)
1160 free(targets[i]);
1161 free(targets);
1163 return ret ? error(_("fetch failed.")) : 0;
1166 static int fetch_git(struct discovery *heads,
1167 int nr_heads, struct ref **to_fetch)
1169 struct rpc_state rpc = RPC_STATE_INIT;
1170 struct strbuf preamble = STRBUF_INIT;
1171 int i, err;
1172 struct strvec args = STRVEC_INIT;
1173 struct strbuf rpc_result = STRBUF_INIT;
1175 strvec_pushl(&args, "fetch-pack", "--stateless-rpc",
1176 "--stdin", "--lock-pack", NULL);
1177 if (options.followtags)
1178 strvec_push(&args, "--include-tag");
1179 if (options.thin)
1180 strvec_push(&args, "--thin");
1181 if (options.verbosity >= 3)
1182 strvec_pushl(&args, "-v", "-v", NULL);
1183 if (options.check_self_contained_and_connected)
1184 strvec_push(&args, "--check-self-contained-and-connected");
1185 if (options.cloning)
1186 strvec_push(&args, "--cloning");
1187 if (options.update_shallow)
1188 strvec_push(&args, "--update-shallow");
1189 if (!options.progress)
1190 strvec_push(&args, "--no-progress");
1191 if (options.depth)
1192 strvec_pushf(&args, "--depth=%lu", options.depth);
1193 if (options.deepen_since)
1194 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since);
1195 for (i = 0; i < options.deepen_not.nr; i++)
1196 strvec_pushf(&args, "--shallow-exclude=%s",
1197 options.deepen_not.items[i].string);
1198 if (options.deepen_relative && options.depth)
1199 strvec_push(&args, "--deepen-relative");
1200 if (options.from_promisor)
1201 strvec_push(&args, "--from-promisor");
1202 if (options.refetch)
1203 strvec_push(&args, "--refetch");
1204 if (options.filter)
1205 strvec_pushf(&args, "--filter=%s", options.filter);
1206 strvec_push(&args, url.buf);
1208 for (i = 0; i < nr_heads; i++) {
1209 struct ref *ref = to_fetch[i];
1210 if (!*ref->name)
1211 die(_("cannot fetch by sha1 over smart http"));
1212 packet_buf_write(&preamble, "%s %s\n",
1213 oid_to_hex(&ref->old_oid), ref->name);
1215 packet_buf_flush(&preamble);
1217 memset(&rpc, 0, sizeof(rpc));
1218 rpc.service_name = "git-upload-pack",
1219 rpc.gzip_request = 1;
1221 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1222 if (rpc_result.len)
1223 write_or_die(1, rpc_result.buf, rpc_result.len);
1224 strbuf_release(&rpc_result);
1225 strbuf_release(&preamble);
1226 strvec_clear(&args);
1227 return err;
1230 static int fetch(int nr_heads, struct ref **to_fetch)
1232 struct discovery *d = discover_refs("git-upload-pack", 0);
1233 if (d->proto_git)
1234 return fetch_git(d, nr_heads, to_fetch);
1235 else
1236 return fetch_dumb(nr_heads, to_fetch);
1239 static void parse_fetch(struct strbuf *buf)
1241 struct ref **to_fetch = NULL;
1242 struct ref *list_head = NULL;
1243 struct ref **list = &list_head;
1244 int alloc_heads = 0, nr_heads = 0;
1246 do {
1247 const char *p;
1248 if (skip_prefix(buf->buf, "fetch ", &p)) {
1249 const char *name;
1250 struct ref *ref;
1251 struct object_id old_oid;
1252 const char *q;
1254 if (parse_oid_hex(p, &old_oid, &q))
1255 die(_("protocol error: expected sha/ref, got '%s'"), p);
1256 if (*q == ' ')
1257 name = q + 1;
1258 else if (!*q)
1259 name = "";
1260 else
1261 die(_("protocol error: expected sha/ref, got '%s'"), p);
1263 ref = alloc_ref(name);
1264 oidcpy(&ref->old_oid, &old_oid);
1266 *list = ref;
1267 list = &ref->next;
1269 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1270 to_fetch[nr_heads++] = ref;
1272 else
1273 die(_("http transport does not support %s"), buf->buf);
1275 strbuf_reset(buf);
1276 if (strbuf_getline_lf(buf, stdin) == EOF)
1277 return;
1278 if (!*buf->buf)
1279 break;
1280 } while (1);
1282 if (fetch(nr_heads, to_fetch))
1283 exit(128); /* error already reported */
1284 free_refs(list_head);
1285 free(to_fetch);
1287 printf("\n");
1288 fflush(stdout);
1289 strbuf_reset(buf);
1292 static void parse_get(const char *arg)
1294 struct strbuf url = STRBUF_INIT;
1295 struct strbuf path = STRBUF_INIT;
1296 const char *space;
1298 space = strchr(arg, ' ');
1300 if (!space)
1301 die(_("protocol error: expected '<url> <path>', missing space"));
1303 strbuf_add(&url, arg, space - arg);
1304 strbuf_addstr(&path, space + 1);
1306 if (http_get_file(url.buf, path.buf, NULL))
1307 die(_("failed to download file at URL '%s'"), url.buf);
1309 strbuf_release(&url);
1310 strbuf_release(&path);
1311 printf("\n");
1312 fflush(stdout);
1315 static int push_dav(int nr_spec, const char **specs)
1317 struct child_process child = CHILD_PROCESS_INIT;
1318 size_t i;
1320 child.git_cmd = 1;
1321 strvec_push(&child.args, "http-push");
1322 strvec_push(&child.args, "--helper-status");
1323 if (options.dry_run)
1324 strvec_push(&child.args, "--dry-run");
1325 if (options.verbosity > 1)
1326 strvec_push(&child.args, "--verbose");
1327 strvec_push(&child.args, url.buf);
1328 for (i = 0; i < nr_spec; i++)
1329 strvec_push(&child.args, specs[i]);
1331 if (run_command(&child))
1332 die(_("git-http-push failed"));
1333 return 0;
1336 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1338 struct rpc_state rpc = RPC_STATE_INIT;
1339 int i, err;
1340 struct strvec args;
1341 struct string_list_item *cas_option;
1342 struct strbuf preamble = STRBUF_INIT;
1343 struct strbuf rpc_result = STRBUF_INIT;
1345 strvec_init(&args);
1346 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1347 NULL);
1349 if (options.thin)
1350 strvec_push(&args, "--thin");
1351 if (options.dry_run)
1352 strvec_push(&args, "--dry-run");
1353 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1354 strvec_push(&args, "--signed=yes");
1355 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1356 strvec_push(&args, "--signed=if-asked");
1357 if (options.atomic)
1358 strvec_push(&args, "--atomic");
1359 if (options.verbosity == 0)
1360 strvec_push(&args, "--quiet");
1361 else if (options.verbosity > 1)
1362 strvec_push(&args, "--verbose");
1363 for (i = 0; i < options.push_options.nr; i++)
1364 strvec_pushf(&args, "--push-option=%s",
1365 options.push_options.items[i].string);
1366 strvec_push(&args, options.progress ? "--progress" : "--no-progress");
1367 for_each_string_list_item(cas_option, &cas_options)
1368 strvec_push(&args, cas_option->string);
1369 strvec_push(&args, url.buf);
1371 if (options.force_if_includes)
1372 strvec_push(&args, "--force-if-includes");
1374 strvec_push(&args, "--stdin");
1375 for (i = 0; i < nr_spec; i++)
1376 packet_buf_write(&preamble, "%s\n", specs[i]);
1377 packet_buf_flush(&preamble);
1379 memset(&rpc, 0, sizeof(rpc));
1380 rpc.service_name = "git-receive-pack",
1382 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1383 if (rpc_result.len)
1384 write_or_die(1, rpc_result.buf, rpc_result.len);
1385 strbuf_release(&rpc_result);
1386 strbuf_release(&preamble);
1387 strvec_clear(&args);
1388 return err;
1391 static int push(int nr_spec, const char **specs)
1393 struct discovery *heads = discover_refs("git-receive-pack", 1);
1394 int ret;
1396 if (heads->proto_git)
1397 ret = push_git(heads, nr_spec, specs);
1398 else
1399 ret = push_dav(nr_spec, specs);
1400 free_discovery(heads);
1401 return ret;
1404 static void parse_push(struct strbuf *buf)
1406 struct strvec specs = STRVEC_INIT;
1407 int ret;
1409 do {
1410 const char *arg;
1411 if (skip_prefix(buf->buf, "push ", &arg))
1412 strvec_push(&specs, arg);
1413 else
1414 die(_("http transport does not support %s"), buf->buf);
1416 strbuf_reset(buf);
1417 if (strbuf_getline_lf(buf, stdin) == EOF)
1418 goto free_specs;
1419 if (!*buf->buf)
1420 break;
1421 } while (1);
1423 ret = push(specs.nr, specs.v);
1424 printf("\n");
1425 fflush(stdout);
1427 if (ret)
1428 exit(128); /* error already reported */
1430 free_specs:
1431 strvec_clear(&specs);
1434 static int stateless_connect(const char *service_name)
1436 struct discovery *discover;
1437 struct rpc_state rpc = RPC_STATE_INIT;
1438 struct strbuf buf = STRBUF_INIT;
1439 const char *accept_language;
1442 * Run the info/refs request and see if the server supports protocol
1443 * v2. If and only if the server supports v2 can we successfully
1444 * establish a stateless connection, otherwise we need to tell the
1445 * client to fallback to using other transport helper functions to
1446 * complete their request.
1448 discover = discover_refs(service_name, 0);
1449 if (discover->version != protocol_v2) {
1450 printf("fallback\n");
1451 fflush(stdout);
1452 return -1;
1453 } else {
1454 /* Stateless Connection established */
1455 printf("\n");
1456 fflush(stdout);
1458 accept_language = http_get_accept_language_header();
1459 if (accept_language)
1460 rpc.hdr_accept_language = xstrfmt("%s", accept_language);
1462 rpc.service_name = service_name;
1463 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1464 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1465 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1466 if (get_protocol_http_header(discover->version, &buf)) {
1467 rpc.protocol_header = strbuf_detach(&buf, NULL);
1468 } else {
1469 rpc.protocol_header = NULL;
1470 strbuf_release(&buf);
1472 rpc.buf = xmalloc(http_post_buffer);
1473 rpc.alloc = http_post_buffer;
1474 rpc.len = 0;
1475 rpc.pos = 0;
1476 rpc.in = 1;
1477 rpc.out = 0;
1478 rpc.any_written = 0;
1479 rpc.gzip_request = 1;
1480 rpc.initial_buffer = 0;
1481 rpc.write_line_lengths = 1;
1482 rpc.flush_read_but_not_sent = 0;
1485 * Dump the capability listing that we got from the server earlier
1486 * during the info/refs request.
1488 write_or_die(rpc.in, discover->buf, discover->len);
1490 /* Until we see EOF keep sending POSTs */
1491 while (1) {
1492 size_t avail;
1493 enum packet_read_status status;
1495 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1496 &status))
1497 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1498 if (status == PACKET_READ_EOF)
1499 break;
1500 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
1501 /* We would have an err here */
1502 break;
1503 /* Reset the buffer for next request */
1504 rpc.len = 0;
1507 free(rpc.service_url);
1508 free(rpc.hdr_content_type);
1509 free(rpc.hdr_accept);
1510 free(rpc.hdr_accept_language);
1511 free(rpc.protocol_header);
1512 free(rpc.buf);
1513 strbuf_release(&buf);
1515 return 0;
1518 int cmd_main(int argc, const char **argv)
1520 struct strbuf buf = STRBUF_INIT;
1521 int nongit;
1522 int ret = 1;
1524 setup_git_directory_gently(&nongit);
1525 if (argc < 2) {
1526 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1527 goto cleanup;
1530 options.verbosity = 1;
1531 options.progress = !!isatty(2);
1532 options.thin = 1;
1533 string_list_init_dup(&options.deepen_not);
1534 string_list_init_dup(&options.push_options);
1537 * Just report "remote-curl" here (folding all the various aliases
1538 * ("git-remote-http", "git-remote-https", and etc.) here since they
1539 * are all just copies of the same actual executable.
1541 trace2_cmd_name("remote-curl");
1543 remote = remote_get(argv[1]);
1545 if (argc > 2) {
1546 end_url_with_slash(&url, argv[2]);
1547 } else {
1548 end_url_with_slash(&url, remote->url[0]);
1551 http_init(remote, url.buf, 0);
1553 do {
1554 const char *arg;
1556 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1557 if (ferror(stdin))
1558 error(_("remote-curl: error reading command stream from git"));
1559 goto cleanup;
1561 if (buf.len == 0)
1562 break;
1563 if (starts_with(buf.buf, "fetch ")) {
1564 if (nongit)
1565 die(_("remote-curl: fetch attempted without a local repo"));
1566 parse_fetch(&buf);
1568 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1569 int for_push = !!strstr(buf.buf + 4, "for-push");
1570 output_refs(get_refs(for_push));
1572 } else if (starts_with(buf.buf, "push ")) {
1573 parse_push(&buf);
1575 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1576 char *value = strchr(arg, ' ');
1577 int result;
1579 if (value)
1580 *value++ = '\0';
1581 else
1582 value = "true";
1584 result = set_option(arg, value);
1585 if (!result)
1586 printf("ok\n");
1587 else if (result < 0)
1588 printf("error invalid value\n");
1589 else
1590 printf("unsupported\n");
1591 fflush(stdout);
1593 } else if (skip_prefix(buf.buf, "get ", &arg)) {
1594 parse_get(arg);
1595 fflush(stdout);
1597 } else if (!strcmp(buf.buf, "capabilities")) {
1598 printf("stateless-connect\n");
1599 printf("fetch\n");
1600 printf("get\n");
1601 printf("option\n");
1602 printf("push\n");
1603 printf("check-connectivity\n");
1604 printf("object-format\n");
1605 printf("\n");
1606 fflush(stdout);
1607 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1608 if (!stateless_connect(arg))
1609 break;
1610 } else {
1611 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1612 goto cleanup;
1614 strbuf_reset(&buf);
1615 } while (1);
1617 http_cleanup();
1618 ret = 0;
1619 cleanup:
1620 strbuf_release(&buf);
1622 return ret;