Merge branch 'ds/ahead-behind'
[git.git] / remote-curl.c
blobbf5a0b186fe658abab4be1be610fef8b6fd0d2d4
1 #include "git-compat-util.h"
2 #include "alloc.h"
3 #include "config.h"
4 #include "hex.h"
5 #include "remote.h"
6 #include "connect.h"
7 #include "strbuf.h"
8 #include "walker.h"
9 #include "http.h"
10 #include "exec-cmd.h"
11 #include "run-command.h"
12 #include "pkt-line.h"
13 #include "string-list.h"
14 #include "sideband.h"
15 #include "strvec.h"
16 #include "credential.h"
17 #include "oid-array.h"
18 #include "send-pack.h"
19 #include "protocol.h"
20 #include "quote.h"
21 #include "transport.h"
23 static struct remote *remote;
24 /* always ends with a trailing slash */
25 static struct strbuf url = STRBUF_INIT;
27 struct options {
28 int verbosity;
29 unsigned long depth;
30 char *deepen_since;
31 struct string_list deepen_not;
32 struct string_list push_options;
33 char *filter;
34 unsigned progress : 1,
35 check_self_contained_and_connected : 1,
36 cloning : 1,
37 update_shallow : 1,
38 followtags : 1,
39 dry_run : 1,
40 thin : 1,
41 /* One of the SEND_PACK_PUSH_CERT_* constants. */
42 push_cert : 2,
43 deepen_relative : 1,
45 /* see documentation of corresponding flag in fetch-pack.h */
46 from_promisor : 1,
48 refetch : 1,
49 atomic : 1,
50 object_format : 1,
51 force_if_includes : 1;
52 const struct git_hash_algo *hash_algo;
54 static struct options options;
55 static struct string_list cas_options = STRING_LIST_INIT_DUP;
57 static int set_option(const char *name, const char *value)
59 if (!strcmp(name, "verbosity")) {
60 char *end;
61 int v = strtol(value, &end, 10);
62 if (value == end || *end)
63 return -1;
64 options.verbosity = v;
65 return 0;
67 else if (!strcmp(name, "progress")) {
68 if (!strcmp(value, "true"))
69 options.progress = 1;
70 else if (!strcmp(value, "false"))
71 options.progress = 0;
72 else
73 return -1;
74 return 0;
76 else if (!strcmp(name, "depth")) {
77 char *end;
78 unsigned long v = strtoul(value, &end, 10);
79 if (value == end || *end)
80 return -1;
81 options.depth = v;
82 return 0;
84 else if (!strcmp(name, "deepen-since")) {
85 options.deepen_since = xstrdup(value);
86 return 0;
88 else if (!strcmp(name, "deepen-not")) {
89 string_list_append(&options.deepen_not, value);
90 return 0;
92 else if (!strcmp(name, "deepen-relative")) {
93 if (!strcmp(value, "true"))
94 options.deepen_relative = 1;
95 else if (!strcmp(value, "false"))
96 options.deepen_relative = 0;
97 else
98 return -1;
99 return 0;
101 else if (!strcmp(name, "followtags")) {
102 if (!strcmp(value, "true"))
103 options.followtags = 1;
104 else if (!strcmp(value, "false"))
105 options.followtags = 0;
106 else
107 return -1;
108 return 0;
110 else if (!strcmp(name, "dry-run")) {
111 if (!strcmp(value, "true"))
112 options.dry_run = 1;
113 else if (!strcmp(value, "false"))
114 options.dry_run = 0;
115 else
116 return -1;
117 return 0;
119 else if (!strcmp(name, "check-connectivity")) {
120 if (!strcmp(value, "true"))
121 options.check_self_contained_and_connected = 1;
122 else if (!strcmp(value, "false"))
123 options.check_self_contained_and_connected = 0;
124 else
125 return -1;
126 return 0;
128 else if (!strcmp(name, "cas")) {
129 struct strbuf val = STRBUF_INIT;
130 strbuf_addstr(&val, "--force-with-lease=");
131 if (*value != '"')
132 strbuf_addstr(&val, value);
133 else if (unquote_c_style(&val, value, NULL))
134 return -1;
135 string_list_append(&cas_options, val.buf);
136 strbuf_release(&val);
137 return 0;
138 } else if (!strcmp(name, TRANS_OPT_FORCE_IF_INCLUDES)) {
139 if (!strcmp(value, "true"))
140 options.force_if_includes = 1;
141 else if (!strcmp(value, "false"))
142 options.force_if_includes = 0;
143 else
144 return -1;
145 return 0;
146 } else if (!strcmp(name, "cloning")) {
147 if (!strcmp(value, "true"))
148 options.cloning = 1;
149 else if (!strcmp(value, "false"))
150 options.cloning = 0;
151 else
152 return -1;
153 return 0;
154 } else if (!strcmp(name, "update-shallow")) {
155 if (!strcmp(value, "true"))
156 options.update_shallow = 1;
157 else if (!strcmp(value, "false"))
158 options.update_shallow = 0;
159 else
160 return -1;
161 return 0;
162 } else if (!strcmp(name, "pushcert")) {
163 if (!strcmp(value, "true"))
164 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
165 else if (!strcmp(value, "false"))
166 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
167 else if (!strcmp(value, "if-asked"))
168 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
169 else
170 return -1;
171 return 0;
172 } else if (!strcmp(name, "atomic")) {
173 if (!strcmp(value, "true"))
174 options.atomic = 1;
175 else if (!strcmp(value, "false"))
176 options.atomic = 0;
177 else
178 return -1;
179 return 0;
180 } else if (!strcmp(name, "push-option")) {
181 if (*value != '"')
182 string_list_append(&options.push_options, value);
183 else {
184 struct strbuf unquoted = STRBUF_INIT;
185 if (unquote_c_style(&unquoted, value, NULL) < 0)
186 die(_("invalid quoting in push-option value: '%s'"), value);
187 string_list_append_nodup(&options.push_options,
188 strbuf_detach(&unquoted, NULL));
190 return 0;
191 } else if (!strcmp(name, "family")) {
192 if (!strcmp(value, "ipv4"))
193 git_curl_ipresolve = CURL_IPRESOLVE_V4;
194 else if (!strcmp(value, "ipv6"))
195 git_curl_ipresolve = CURL_IPRESOLVE_V6;
196 else if (!strcmp(value, "all"))
197 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
198 else
199 return -1;
200 return 0;
201 } else if (!strcmp(name, "from-promisor")) {
202 options.from_promisor = 1;
203 return 0;
204 } else if (!strcmp(name, "refetch")) {
205 options.refetch = 1;
206 return 0;
207 } else if (!strcmp(name, "filter")) {
208 options.filter = xstrdup(value);
209 return 0;
210 } else if (!strcmp(name, "object-format")) {
211 int algo;
212 options.object_format = 1;
213 if (strcmp(value, "true")) {
214 algo = hash_algo_by_name(value);
215 if (algo == GIT_HASH_UNKNOWN)
216 die("unknown object format '%s'", value);
217 options.hash_algo = &hash_algos[algo];
219 return 0;
220 } else {
221 return 1 /* unsupported */;
225 struct discovery {
226 char *service;
227 char *buf_alloc;
228 char *buf;
229 size_t len;
230 struct ref *refs;
231 struct oid_array shallow;
232 enum protocol_version version;
233 unsigned proto_git : 1;
235 static struct discovery *last_discovery;
237 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
239 struct ref *list = NULL;
240 struct packet_reader reader;
242 packet_reader_init(&reader, -1, heads->buf, heads->len,
243 PACKET_READ_CHOMP_NEWLINE |
244 PACKET_READ_GENTLE_ON_EOF |
245 PACKET_READ_DIE_ON_ERR_PACKET);
247 heads->version = discover_version(&reader);
248 switch (heads->version) {
249 case protocol_v2:
251 * Do nothing. This isn't a list of refs but rather a
252 * capability advertisement. Client would have run
253 * 'stateless-connect' so we'll dump this capability listing
254 * and let them request the refs themselves.
256 break;
257 case protocol_v1:
258 case protocol_v0:
259 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
260 NULL, &heads->shallow);
261 options.hash_algo = reader.hash_algo;
262 break;
263 case protocol_unknown_version:
264 BUG("unknown protocol version");
267 return list;
270 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
272 const char *p = memchr(heads->buf, '\t', heads->len);
273 int algo;
274 if (!p)
275 return the_hash_algo;
277 algo = hash_algo_by_length((p - heads->buf) / 2);
278 if (algo == GIT_HASH_UNKNOWN)
279 return NULL;
280 return &hash_algos[algo];
283 static struct ref *parse_info_refs(struct discovery *heads)
285 char *data, *start, *mid;
286 char *ref_name;
287 int i = 0;
289 struct ref *refs = NULL;
290 struct ref *ref = NULL;
291 struct ref *last_ref = NULL;
293 options.hash_algo = detect_hash_algo(heads);
294 if (!options.hash_algo)
295 die("%sinfo/refs not valid: could not determine hash algorithm; "
296 "is this a git repository?",
297 transport_anonymize_url(url.buf));
299 data = heads->buf;
300 start = NULL;
301 mid = data;
302 while (i < heads->len) {
303 if (!start) {
304 start = &data[i];
306 if (data[i] == '\t')
307 mid = &data[i];
308 if (data[i] == '\n') {
309 if (mid - start != options.hash_algo->hexsz)
310 die(_("%sinfo/refs not valid: is this a git repository?"),
311 transport_anonymize_url(url.buf));
312 data[i] = 0;
313 ref_name = mid + 1;
314 ref = alloc_ref(ref_name);
315 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
316 if (!refs)
317 refs = ref;
318 if (last_ref)
319 last_ref->next = ref;
320 last_ref = ref;
321 start = NULL;
323 i++;
326 ref = alloc_ref("HEAD");
327 if (!http_fetch_ref(url.buf, ref) &&
328 !resolve_remote_symref(ref, refs)) {
329 ref->next = refs;
330 refs = ref;
331 } else {
332 free(ref);
335 return refs;
338 static void free_discovery(struct discovery *d)
340 if (d) {
341 if (d == last_discovery)
342 last_discovery = NULL;
343 free(d->shallow.oid);
344 free(d->buf_alloc);
345 free_refs(d->refs);
346 free(d->service);
347 free(d);
351 static int show_http_message(struct strbuf *type, struct strbuf *charset,
352 struct strbuf *msg)
354 const char *p, *eol;
357 * We only show text/plain parts, as other types are likely
358 * to be ugly to look at on the user's terminal.
360 if (strcmp(type->buf, "text/plain"))
361 return -1;
362 if (charset->len)
363 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
365 strbuf_trim(msg);
366 if (!msg->len)
367 return -1;
369 p = msg->buf;
370 do {
371 eol = strchrnul(p, '\n');
372 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
373 p = eol + 1;
374 } while(*eol);
375 return 0;
378 static int get_protocol_http_header(enum protocol_version version,
379 struct strbuf *header)
381 if (version > 0) {
382 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
383 version);
385 return 1;
388 return 0;
391 static void check_smart_http(struct discovery *d, const char *service,
392 struct strbuf *type)
394 const char *p;
395 struct packet_reader reader;
398 * If we don't see x-$service-advertisement, then it's not smart-http.
399 * But once we do, we commit to it and assume any other protocol
400 * violations are hard errors.
402 if (!skip_prefix(type->buf, "application/x-", &p) ||
403 !skip_prefix(p, service, &p) ||
404 strcmp(p, "-advertisement"))
405 return;
407 packet_reader_init(&reader, -1, d->buf, d->len,
408 PACKET_READ_CHOMP_NEWLINE |
409 PACKET_READ_DIE_ON_ERR_PACKET);
410 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
411 die(_("invalid server response; expected service, got flush packet"));
413 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
415 * The header can include additional metadata lines, up
416 * until a packet flush marker. Ignore these now, but
417 * in the future we might start to scan them.
419 for (;;) {
420 packet_reader_read(&reader);
421 if (reader.pktlen <= 0) {
422 break;
427 * v0 smart http; callers expect us to soak up the
428 * service and header packets
430 d->buf = reader.src_buffer;
431 d->len = reader.src_len;
432 d->proto_git = 1;
434 } else if (!strcmp(reader.line, "version 2")) {
436 * v2 smart http; do not consume version packet, which will
437 * be handled elsewhere.
439 d->proto_git = 1;
441 } else {
442 die(_("invalid server response; got '%s'"), reader.line);
446 static struct discovery *discover_refs(const char *service, int for_push)
448 struct strbuf type = STRBUF_INIT;
449 struct strbuf charset = STRBUF_INIT;
450 struct strbuf buffer = STRBUF_INIT;
451 struct strbuf refs_url = STRBUF_INIT;
452 struct strbuf effective_url = STRBUF_INIT;
453 struct strbuf protocol_header = STRBUF_INIT;
454 struct string_list extra_headers = STRING_LIST_INIT_DUP;
455 struct discovery *last = last_discovery;
456 int http_ret, maybe_smart = 0;
457 struct http_get_options http_options;
458 enum protocol_version version = get_protocol_version_config();
460 if (last && !strcmp(service, last->service))
461 return last;
462 free_discovery(last);
464 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
465 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
466 git_env_bool("GIT_SMART_HTTP", 1)) {
467 maybe_smart = 1;
468 if (!strchr(url.buf, '?'))
469 strbuf_addch(&refs_url, '?');
470 else
471 strbuf_addch(&refs_url, '&');
472 strbuf_addf(&refs_url, "service=%s", service);
476 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
477 * to perform any operation that doesn't involve upload-pack (i.e., a
478 * fetch, ls-remote, etc), then fallback to v0 since we don't know how
479 * to do anything else (like push or remote archive) via v2.
481 if (version == protocol_v2 && strcmp("git-upload-pack", service))
482 version = protocol_v0;
484 /* Add the extra Git-Protocol header */
485 if (get_protocol_http_header(version, &protocol_header))
486 string_list_append(&extra_headers, protocol_header.buf);
488 memset(&http_options, 0, sizeof(http_options));
489 http_options.content_type = &type;
490 http_options.charset = &charset;
491 http_options.effective_url = &effective_url;
492 http_options.base_url = &url;
493 http_options.extra_headers = &extra_headers;
494 http_options.initial_request = 1;
495 http_options.no_cache = 1;
497 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
498 switch (http_ret) {
499 case HTTP_OK:
500 break;
501 case HTTP_MISSING_TARGET:
502 show_http_message(&type, &charset, &buffer);
503 die(_("repository '%s' not found"),
504 transport_anonymize_url(url.buf));
505 case HTTP_NOAUTH:
506 show_http_message(&type, &charset, &buffer);
507 die(_("Authentication failed for '%s'"),
508 transport_anonymize_url(url.buf));
509 case HTTP_NOMATCHPUBLICKEY:
510 show_http_message(&type, &charset, &buffer);
511 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
512 transport_anonymize_url(url.buf), curl_errorstr);
513 default:
514 show_http_message(&type, &charset, &buffer);
515 die(_("unable to access '%s': %s"),
516 transport_anonymize_url(url.buf), curl_errorstr);
519 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
520 char *u = transport_anonymize_url(url.buf);
521 warning(_("redirecting to %s"), u);
522 free(u);
525 last= xcalloc(1, sizeof(*last_discovery));
526 last->service = xstrdup(service);
527 last->buf_alloc = strbuf_detach(&buffer, &last->len);
528 last->buf = last->buf_alloc;
530 if (maybe_smart)
531 check_smart_http(last, service, &type);
533 if (last->proto_git)
534 last->refs = parse_git_refs(last, for_push);
535 else
536 last->refs = parse_info_refs(last);
538 strbuf_release(&refs_url);
539 strbuf_release(&type);
540 strbuf_release(&charset);
541 strbuf_release(&effective_url);
542 strbuf_release(&buffer);
543 strbuf_release(&protocol_header);
544 string_list_clear(&extra_headers, 0);
545 last_discovery = last;
546 return last;
549 static struct ref *get_refs(int for_push)
551 struct discovery *heads;
553 if (for_push)
554 heads = discover_refs("git-receive-pack", for_push);
555 else
556 heads = discover_refs("git-upload-pack", for_push);
558 return heads->refs;
561 static void output_refs(struct ref *refs)
563 struct ref *posn;
564 if (options.object_format && options.hash_algo) {
565 printf(":object-format %s\n", options.hash_algo->name);
566 repo_set_hash_algo(the_repository,
567 hash_algo_by_ptr(options.hash_algo));
569 for (posn = refs; posn; posn = posn->next) {
570 if (posn->symref)
571 printf("@%s %s\n", posn->symref, posn->name);
572 else
573 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
574 options.hash_algo),
575 posn->name);
577 printf("\n");
578 fflush(stdout);
581 struct rpc_state {
582 const char *service_name;
583 char *service_url;
584 char *hdr_content_type;
585 char *hdr_accept;
586 char *hdr_accept_language;
587 char *protocol_header;
588 char *buf;
589 size_t alloc;
590 size_t len;
591 size_t pos;
592 int in;
593 int out;
594 int any_written;
595 unsigned gzip_request : 1;
596 unsigned initial_buffer : 1;
599 * Whenever a pkt-line is read into buf, append the 4 characters
600 * denoting its length before appending the payload.
602 unsigned write_line_lengths : 1;
605 * Used by rpc_out; initialize to 0. This is true if a flush has been
606 * read, but the corresponding line length (if write_line_lengths is
607 * true) and EOF have not been sent to libcurl. Since each flush marks
608 * the end of a request, each flush must be completely sent before any
609 * further reading occurs.
611 unsigned flush_read_but_not_sent : 1;
614 #define RPC_STATE_INIT { 0 }
617 * Appends the result of reading from rpc->out to the string represented by
618 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
619 * enough space, 0 otherwise.
621 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
622 * hexadecimal string before appending the result described above.
624 * Writes the total number of bytes appended into appended.
626 static int rpc_read_from_out(struct rpc_state *rpc, int options,
627 size_t *appended,
628 enum packet_read_status *status) {
629 size_t left;
630 char *buf;
631 int pktlen_raw;
633 if (rpc->write_line_lengths) {
634 left = rpc->alloc - rpc->len - 4;
635 buf = rpc->buf + rpc->len + 4;
636 } else {
637 left = rpc->alloc - rpc->len;
638 buf = rpc->buf + rpc->len;
641 if (left < LARGE_PACKET_MAX)
642 return 0;
644 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
645 left, &pktlen_raw, options);
646 if (*status != PACKET_READ_EOF) {
647 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
648 rpc->len += *appended;
651 if (rpc->write_line_lengths) {
652 switch (*status) {
653 case PACKET_READ_EOF:
654 if (!(options & PACKET_READ_GENTLE_ON_EOF))
655 die(_("shouldn't have EOF when not gentle on EOF"));
656 break;
657 case PACKET_READ_NORMAL:
658 set_packet_header(buf - 4, *appended);
659 break;
660 case PACKET_READ_DELIM:
661 memcpy(buf - 4, "0001", 4);
662 break;
663 case PACKET_READ_FLUSH:
664 memcpy(buf - 4, "0000", 4);
665 break;
666 case PACKET_READ_RESPONSE_END:
667 die(_("remote server sent unexpected response end packet"));
671 return 1;
674 static size_t rpc_out(void *ptr, size_t eltsize,
675 size_t nmemb, void *buffer_)
677 size_t max = eltsize * nmemb;
678 struct rpc_state *rpc = buffer_;
679 size_t avail = rpc->len - rpc->pos;
680 enum packet_read_status status;
682 if (!avail) {
683 rpc->initial_buffer = 0;
684 rpc->len = 0;
685 rpc->pos = 0;
686 if (!rpc->flush_read_but_not_sent) {
687 if (!rpc_read_from_out(rpc, 0, &avail, &status))
688 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
689 if (status == PACKET_READ_FLUSH)
690 rpc->flush_read_but_not_sent = 1;
693 * If flush_read_but_not_sent is true, we have already read one
694 * full request but have not fully sent it + EOF, which is why
695 * we need to refrain from reading.
698 if (rpc->flush_read_but_not_sent) {
699 if (!avail) {
701 * The line length either does not need to be sent at
702 * all or has already been completely sent. Now we can
703 * return 0, indicating EOF, meaning that the flush has
704 * been fully sent.
706 rpc->flush_read_but_not_sent = 0;
707 return 0;
710 * If avail is non-zero, the line length for the flush still
711 * hasn't been fully sent. Proceed with sending the line
712 * length.
716 if (max < avail)
717 avail = max;
718 memcpy(ptr, rpc->buf + rpc->pos, avail);
719 rpc->pos += avail;
720 return avail;
723 static int rpc_seek(void *clientp, curl_off_t offset, int origin)
725 struct rpc_state *rpc = clientp;
727 if (origin != SEEK_SET)
728 BUG("rpc_seek only handles SEEK_SET, not %d", origin);
730 if (rpc->initial_buffer) {
731 if (offset < 0 || offset > rpc->len) {
732 error("curl seek would be outside of rpc buffer");
733 return CURL_SEEKFUNC_FAIL;
735 rpc->pos = offset;
736 return CURL_SEEKFUNC_OK;
738 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
739 return CURL_SEEKFUNC_FAIL;
742 struct check_pktline_state {
743 char len_buf[4];
744 int len_filled;
745 int remaining;
748 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
750 while (size) {
751 if (!state->remaining) {
752 int digits_remaining = 4 - state->len_filled;
753 if (digits_remaining > size)
754 digits_remaining = size;
755 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
756 state->len_filled += digits_remaining;
757 ptr += digits_remaining;
758 size -= digits_remaining;
760 if (state->len_filled == 4) {
761 state->remaining = packet_length(state->len_buf);
762 if (state->remaining < 0) {
763 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
764 } else if (state->remaining == 2) {
765 die(_("remote-curl: unexpected response end packet"));
766 } else if (state->remaining < 4) {
767 state->remaining = 0;
768 } else {
769 state->remaining -= 4;
771 state->len_filled = 0;
775 if (state->remaining) {
776 int remaining = state->remaining;
777 if (remaining > size)
778 remaining = size;
779 ptr += remaining;
780 size -= remaining;
781 state->remaining -= remaining;
786 struct rpc_in_data {
787 struct rpc_state *rpc;
788 struct active_request_slot *slot;
789 int check_pktline;
790 struct check_pktline_state pktline_state;
794 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
795 * from ptr.
797 static size_t rpc_in(char *ptr, size_t eltsize,
798 size_t nmemb, void *buffer_)
800 size_t size = eltsize * nmemb;
801 struct rpc_in_data *data = buffer_;
802 long response_code;
804 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
805 &response_code) != CURLE_OK)
806 return size;
807 if (response_code >= 300)
808 return size;
809 if (size)
810 data->rpc->any_written = 1;
811 if (data->check_pktline)
812 check_pktline(&data->pktline_state, ptr, size);
813 write_or_die(data->rpc->in, ptr, size);
814 return size;
817 static int run_slot(struct active_request_slot *slot,
818 struct slot_results *results)
820 int err;
821 struct slot_results results_buf;
823 if (!results)
824 results = &results_buf;
826 err = run_one_slot(slot, results);
828 if (err != HTTP_OK && err != HTTP_REAUTH) {
829 struct strbuf msg = STRBUF_INIT;
830 if (results->http_code && results->http_code != 200)
831 strbuf_addf(&msg, "HTTP %ld", results->http_code);
832 if (results->curl_result != CURLE_OK) {
833 if (msg.len)
834 strbuf_addch(&msg, ' ');
835 strbuf_addf(&msg, "curl %d", results->curl_result);
836 if (curl_errorstr[0]) {
837 strbuf_addch(&msg, ' ');
838 strbuf_addstr(&msg, curl_errorstr);
841 error(_("RPC failed; %s"), msg.buf);
842 strbuf_release(&msg);
845 return err;
848 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
850 struct active_request_slot *slot;
851 struct curl_slist *headers = http_copy_default_headers();
852 struct strbuf buf = STRBUF_INIT;
853 int err;
855 slot = get_active_slot();
857 headers = curl_slist_append(headers, rpc->hdr_content_type);
858 headers = curl_slist_append(headers, rpc->hdr_accept);
860 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
861 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
862 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
863 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
864 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
865 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
866 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
867 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
868 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf);
870 err = run_slot(slot, results);
872 curl_slist_free_all(headers);
873 strbuf_release(&buf);
874 return err;
877 static curl_off_t xcurl_off_t(size_t len)
879 uintmax_t size = len;
880 if (size > maximum_signed_value_of_type(curl_off_t))
881 die(_("cannot handle pushes this big"));
882 return (curl_off_t)size;
886 * If flush_received is true, do not attempt to read any more; just use what's
887 * in rpc->buf.
889 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
891 struct active_request_slot *slot;
892 struct curl_slist *headers = http_copy_default_headers();
893 int use_gzip = rpc->gzip_request;
894 char *gzip_body = NULL;
895 size_t gzip_size = 0;
896 int err, large_request = 0;
897 int needs_100_continue = 0;
898 struct rpc_in_data rpc_in_data;
900 /* Try to load the entire request, if we can fit it into the
901 * allocated buffer space we can use HTTP/1.0 and avoid the
902 * chunked encoding mess.
904 if (!flush_received) {
905 while (1) {
906 size_t n;
907 enum packet_read_status status;
909 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
910 large_request = 1;
911 use_gzip = 0;
912 break;
914 if (status == PACKET_READ_FLUSH)
915 break;
919 if (large_request) {
920 struct slot_results results;
922 do {
923 err = probe_rpc(rpc, &results);
924 if (err == HTTP_REAUTH)
925 credential_fill(&http_auth);
926 } while (err == HTTP_REAUTH);
927 if (err != HTTP_OK)
928 return -1;
930 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
931 needs_100_continue = 1;
934 headers = curl_slist_append(headers, rpc->hdr_content_type);
935 headers = curl_slist_append(headers, rpc->hdr_accept);
936 headers = curl_slist_append(headers, needs_100_continue ?
937 "Expect: 100-continue" : "Expect:");
939 /* Add Accept-Language header */
940 if (rpc->hdr_accept_language)
941 headers = curl_slist_append(headers, rpc->hdr_accept_language);
943 /* Add the extra Git-Protocol header */
944 if (rpc->protocol_header)
945 headers = curl_slist_append(headers, rpc->protocol_header);
947 retry:
948 slot = get_active_slot();
950 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
951 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
952 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
953 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
955 if (large_request) {
956 /* The request body is large and the size cannot be predicted.
957 * We must use chunked encoding to send it.
959 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
960 rpc->initial_buffer = 1;
961 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
962 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
963 curl_easy_setopt(slot->curl, CURLOPT_SEEKFUNCTION, rpc_seek);
964 curl_easy_setopt(slot->curl, CURLOPT_SEEKDATA, rpc);
965 if (options.verbosity > 1) {
966 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
967 fflush(stderr);
970 } else if (gzip_body) {
972 * If we are looping to retry authentication, then the previous
973 * run will have set up the headers and gzip buffer already,
974 * and we just need to send it.
976 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
977 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
979 } else if (use_gzip && 1024 < rpc->len) {
980 /* The client backend isn't giving us compressed data so
981 * we can try to deflate it ourselves, this may save on
982 * the transfer time.
984 git_zstream stream;
985 int ret;
987 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
988 gzip_size = git_deflate_bound(&stream, rpc->len);
989 gzip_body = xmalloc(gzip_size);
991 stream.next_in = (unsigned char *)rpc->buf;
992 stream.avail_in = rpc->len;
993 stream.next_out = (unsigned char *)gzip_body;
994 stream.avail_out = gzip_size;
996 ret = git_deflate(&stream, Z_FINISH);
997 if (ret != Z_STREAM_END)
998 die(_("cannot deflate request; zlib deflate error %d"), ret);
1000 ret = git_deflate_end_gently(&stream);
1001 if (ret != Z_OK)
1002 die(_("cannot deflate request; zlib end error %d"), ret);
1004 gzip_size = stream.total_out;
1006 headers = curl_slist_append(headers, "Content-Encoding: gzip");
1007 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
1008 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
1010 if (options.verbosity > 1) {
1011 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
1012 rpc->service_name,
1013 (unsigned long)rpc->len, (unsigned long)gzip_size);
1014 fflush(stderr);
1016 } else {
1017 /* We know the complete request size in advance, use the
1018 * more normal Content-Length approach.
1020 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
1021 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
1022 if (options.verbosity > 1) {
1023 fprintf(stderr, "POST %s (%lu bytes)\n",
1024 rpc->service_name, (unsigned long)rpc->len);
1025 fflush(stderr);
1029 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1030 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
1031 rpc_in_data.rpc = rpc;
1032 rpc_in_data.slot = slot;
1033 rpc_in_data.check_pktline = stateless_connect;
1034 memset(&rpc_in_data.pktline_state, 0, sizeof(rpc_in_data.pktline_state));
1035 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &rpc_in_data);
1036 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1039 rpc->any_written = 0;
1040 err = run_slot(slot, NULL);
1041 if (err == HTTP_REAUTH && !large_request) {
1042 credential_fill(&http_auth);
1043 goto retry;
1045 if (err != HTTP_OK)
1046 err = -1;
1048 if (!rpc->any_written)
1049 err = -1;
1051 if (rpc_in_data.pktline_state.len_filled)
1052 err = error(_("%d bytes of length header were received"), rpc_in_data.pktline_state.len_filled);
1053 if (rpc_in_data.pktline_state.remaining)
1054 err = error(_("%d bytes of body are still expected"), rpc_in_data.pktline_state.remaining);
1056 if (stateless_connect)
1057 packet_response_end(rpc->in);
1059 curl_slist_free_all(headers);
1060 free(gzip_body);
1061 return err;
1064 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
1065 const char **client_argv, const struct strbuf *preamble,
1066 struct strbuf *rpc_result)
1068 const char *svc = rpc->service_name;
1069 struct strbuf buf = STRBUF_INIT;
1070 struct child_process client = CHILD_PROCESS_INIT;
1071 int err = 0;
1073 client.in = -1;
1074 client.out = -1;
1075 client.git_cmd = 1;
1076 strvec_pushv(&client.args, client_argv);
1077 if (start_command(&client))
1078 exit(1);
1079 write_or_die(client.in, preamble->buf, preamble->len);
1080 if (heads)
1081 write_or_die(client.in, heads->buf, heads->len);
1083 rpc->alloc = http_post_buffer;
1084 rpc->buf = xmalloc(rpc->alloc);
1085 rpc->in = client.in;
1086 rpc->out = client.out;
1088 strbuf_addf(&buf, "%s%s", url.buf, svc);
1089 rpc->service_url = strbuf_detach(&buf, NULL);
1091 rpc->hdr_accept_language = xstrdup_or_null(http_get_accept_language_header());
1093 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
1094 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
1096 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
1097 rpc->hdr_accept = strbuf_detach(&buf, NULL);
1099 if (get_protocol_http_header(heads->version, &buf))
1100 rpc->protocol_header = strbuf_detach(&buf, NULL);
1101 else
1102 rpc->protocol_header = NULL;
1104 while (!err) {
1105 int n = packet_read(rpc->out, rpc->buf, rpc->alloc, 0);
1106 if (!n)
1107 break;
1108 rpc->pos = 0;
1109 rpc->len = n;
1110 err |= post_rpc(rpc, 0, 0);
1113 close(client.in);
1114 client.in = -1;
1115 if (!err) {
1116 strbuf_read(rpc_result, client.out, 0);
1117 } else {
1118 char buf[4096];
1119 for (;;)
1120 if (xread(client.out, buf, sizeof(buf)) <= 0)
1121 break;
1124 close(client.out);
1125 client.out = -1;
1127 err |= finish_command(&client);
1128 free(rpc->service_url);
1129 free(rpc->hdr_content_type);
1130 free(rpc->hdr_accept);
1131 free(rpc->hdr_accept_language);
1132 free(rpc->protocol_header);
1133 free(rpc->buf);
1134 strbuf_release(&buf);
1135 return err;
1138 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1140 struct walker *walker;
1141 char **targets;
1142 int ret, i;
1144 ALLOC_ARRAY(targets, nr_heads);
1145 if (options.depth || options.deepen_since)
1146 die(_("dumb http transport does not support shallow capabilities"));
1147 for (i = 0; i < nr_heads; i++)
1148 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1150 walker = get_http_walker(url.buf);
1151 walker->get_verbosely = options.verbosity >= 3;
1152 walker->get_progress = options.progress;
1153 walker->get_recover = 0;
1154 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1155 walker_free(walker);
1157 for (i = 0; i < nr_heads; i++)
1158 free(targets[i]);
1159 free(targets);
1161 return ret ? error(_("fetch failed.")) : 0;
1164 static int fetch_git(struct discovery *heads,
1165 int nr_heads, struct ref **to_fetch)
1167 struct rpc_state rpc = RPC_STATE_INIT;
1168 struct strbuf preamble = STRBUF_INIT;
1169 int i, err;
1170 struct strvec args = STRVEC_INIT;
1171 struct strbuf rpc_result = STRBUF_INIT;
1173 strvec_pushl(&args, "fetch-pack", "--stateless-rpc",
1174 "--stdin", "--lock-pack", NULL);
1175 if (options.followtags)
1176 strvec_push(&args, "--include-tag");
1177 if (options.thin)
1178 strvec_push(&args, "--thin");
1179 if (options.verbosity >= 3)
1180 strvec_pushl(&args, "-v", "-v", NULL);
1181 if (options.check_self_contained_and_connected)
1182 strvec_push(&args, "--check-self-contained-and-connected");
1183 if (options.cloning)
1184 strvec_push(&args, "--cloning");
1185 if (options.update_shallow)
1186 strvec_push(&args, "--update-shallow");
1187 if (!options.progress)
1188 strvec_push(&args, "--no-progress");
1189 if (options.depth)
1190 strvec_pushf(&args, "--depth=%lu", options.depth);
1191 if (options.deepen_since)
1192 strvec_pushf(&args, "--shallow-since=%s", options.deepen_since);
1193 for (i = 0; i < options.deepen_not.nr; i++)
1194 strvec_pushf(&args, "--shallow-exclude=%s",
1195 options.deepen_not.items[i].string);
1196 if (options.deepen_relative && options.depth)
1197 strvec_push(&args, "--deepen-relative");
1198 if (options.from_promisor)
1199 strvec_push(&args, "--from-promisor");
1200 if (options.refetch)
1201 strvec_push(&args, "--refetch");
1202 if (options.filter)
1203 strvec_pushf(&args, "--filter=%s", options.filter);
1204 strvec_push(&args, url.buf);
1206 for (i = 0; i < nr_heads; i++) {
1207 struct ref *ref = to_fetch[i];
1208 if (!*ref->name)
1209 die(_("cannot fetch by sha1 over smart http"));
1210 packet_buf_write(&preamble, "%s %s\n",
1211 oid_to_hex(&ref->old_oid), ref->name);
1213 packet_buf_flush(&preamble);
1215 memset(&rpc, 0, sizeof(rpc));
1216 rpc.service_name = "git-upload-pack",
1217 rpc.gzip_request = 1;
1219 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1220 if (rpc_result.len)
1221 write_or_die(1, rpc_result.buf, rpc_result.len);
1222 strbuf_release(&rpc_result);
1223 strbuf_release(&preamble);
1224 strvec_clear(&args);
1225 return err;
1228 static int fetch(int nr_heads, struct ref **to_fetch)
1230 struct discovery *d = discover_refs("git-upload-pack", 0);
1231 if (d->proto_git)
1232 return fetch_git(d, nr_heads, to_fetch);
1233 else
1234 return fetch_dumb(nr_heads, to_fetch);
1237 static void parse_fetch(struct strbuf *buf)
1239 struct ref **to_fetch = NULL;
1240 struct ref *list_head = NULL;
1241 struct ref **list = &list_head;
1242 int alloc_heads = 0, nr_heads = 0;
1244 do {
1245 const char *p;
1246 if (skip_prefix(buf->buf, "fetch ", &p)) {
1247 const char *name;
1248 struct ref *ref;
1249 struct object_id old_oid;
1250 const char *q;
1252 if (parse_oid_hex(p, &old_oid, &q))
1253 die(_("protocol error: expected sha/ref, got '%s'"), p);
1254 if (*q == ' ')
1255 name = q + 1;
1256 else if (!*q)
1257 name = "";
1258 else
1259 die(_("protocol error: expected sha/ref, got '%s'"), p);
1261 ref = alloc_ref(name);
1262 oidcpy(&ref->old_oid, &old_oid);
1264 *list = ref;
1265 list = &ref->next;
1267 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1268 to_fetch[nr_heads++] = ref;
1270 else
1271 die(_("http transport does not support %s"), buf->buf);
1273 strbuf_reset(buf);
1274 if (strbuf_getline_lf(buf, stdin) == EOF)
1275 return;
1276 if (!*buf->buf)
1277 break;
1278 } while (1);
1280 if (fetch(nr_heads, to_fetch))
1281 exit(128); /* error already reported */
1282 free_refs(list_head);
1283 free(to_fetch);
1285 printf("\n");
1286 fflush(stdout);
1287 strbuf_reset(buf);
1290 static void parse_get(const char *arg)
1292 struct strbuf url = STRBUF_INIT;
1293 struct strbuf path = STRBUF_INIT;
1294 const char *space;
1296 space = strchr(arg, ' ');
1298 if (!space)
1299 die(_("protocol error: expected '<url> <path>', missing space"));
1301 strbuf_add(&url, arg, space - arg);
1302 strbuf_addstr(&path, space + 1);
1304 if (http_get_file(url.buf, path.buf, NULL))
1305 die(_("failed to download file at URL '%s'"), url.buf);
1307 strbuf_release(&url);
1308 strbuf_release(&path);
1309 printf("\n");
1310 fflush(stdout);
1313 static int push_dav(int nr_spec, const char **specs)
1315 struct child_process child = CHILD_PROCESS_INIT;
1316 size_t i;
1318 child.git_cmd = 1;
1319 strvec_push(&child.args, "http-push");
1320 strvec_push(&child.args, "--helper-status");
1321 if (options.dry_run)
1322 strvec_push(&child.args, "--dry-run");
1323 if (options.verbosity > 1)
1324 strvec_push(&child.args, "--verbose");
1325 strvec_push(&child.args, url.buf);
1326 for (i = 0; i < nr_spec; i++)
1327 strvec_push(&child.args, specs[i]);
1329 if (run_command(&child))
1330 die(_("git-http-push failed"));
1331 return 0;
1334 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1336 struct rpc_state rpc = RPC_STATE_INIT;
1337 int i, err;
1338 struct strvec args;
1339 struct string_list_item *cas_option;
1340 struct strbuf preamble = STRBUF_INIT;
1341 struct strbuf rpc_result = STRBUF_INIT;
1343 strvec_init(&args);
1344 strvec_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1345 NULL);
1347 if (options.thin)
1348 strvec_push(&args, "--thin");
1349 if (options.dry_run)
1350 strvec_push(&args, "--dry-run");
1351 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1352 strvec_push(&args, "--signed=yes");
1353 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1354 strvec_push(&args, "--signed=if-asked");
1355 if (options.atomic)
1356 strvec_push(&args, "--atomic");
1357 if (options.verbosity == 0)
1358 strvec_push(&args, "--quiet");
1359 else if (options.verbosity > 1)
1360 strvec_push(&args, "--verbose");
1361 for (i = 0; i < options.push_options.nr; i++)
1362 strvec_pushf(&args, "--push-option=%s",
1363 options.push_options.items[i].string);
1364 strvec_push(&args, options.progress ? "--progress" : "--no-progress");
1365 for_each_string_list_item(cas_option, &cas_options)
1366 strvec_push(&args, cas_option->string);
1367 strvec_push(&args, url.buf);
1369 if (options.force_if_includes)
1370 strvec_push(&args, "--force-if-includes");
1372 strvec_push(&args, "--stdin");
1373 for (i = 0; i < nr_spec; i++)
1374 packet_buf_write(&preamble, "%s\n", specs[i]);
1375 packet_buf_flush(&preamble);
1377 memset(&rpc, 0, sizeof(rpc));
1378 rpc.service_name = "git-receive-pack",
1380 err = rpc_service(&rpc, heads, args.v, &preamble, &rpc_result);
1381 if (rpc_result.len)
1382 write_or_die(1, rpc_result.buf, rpc_result.len);
1383 strbuf_release(&rpc_result);
1384 strbuf_release(&preamble);
1385 strvec_clear(&args);
1386 return err;
1389 static int push(int nr_spec, const char **specs)
1391 struct discovery *heads = discover_refs("git-receive-pack", 1);
1392 int ret;
1394 if (heads->proto_git)
1395 ret = push_git(heads, nr_spec, specs);
1396 else
1397 ret = push_dav(nr_spec, specs);
1398 free_discovery(heads);
1399 return ret;
1402 static void parse_push(struct strbuf *buf)
1404 struct strvec specs = STRVEC_INIT;
1405 int ret;
1407 do {
1408 const char *arg;
1409 if (skip_prefix(buf->buf, "push ", &arg))
1410 strvec_push(&specs, arg);
1411 else
1412 die(_("http transport does not support %s"), buf->buf);
1414 strbuf_reset(buf);
1415 if (strbuf_getline_lf(buf, stdin) == EOF)
1416 goto free_specs;
1417 if (!*buf->buf)
1418 break;
1419 } while (1);
1421 ret = push(specs.nr, specs.v);
1422 printf("\n");
1423 fflush(stdout);
1425 if (ret)
1426 exit(128); /* error already reported */
1428 free_specs:
1429 strvec_clear(&specs);
1432 static int stateless_connect(const char *service_name)
1434 struct discovery *discover;
1435 struct rpc_state rpc = RPC_STATE_INIT;
1436 struct strbuf buf = STRBUF_INIT;
1437 const char *accept_language;
1440 * Run the info/refs request and see if the server supports protocol
1441 * v2. If and only if the server supports v2 can we successfully
1442 * establish a stateless connection, otherwise we need to tell the
1443 * client to fallback to using other transport helper functions to
1444 * complete their request.
1446 discover = discover_refs(service_name, 0);
1447 if (discover->version != protocol_v2) {
1448 printf("fallback\n");
1449 fflush(stdout);
1450 return -1;
1451 } else {
1452 /* Stateless Connection established */
1453 printf("\n");
1454 fflush(stdout);
1456 accept_language = http_get_accept_language_header();
1457 if (accept_language)
1458 rpc.hdr_accept_language = xstrfmt("%s", accept_language);
1460 rpc.service_name = service_name;
1461 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1462 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1463 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1464 if (get_protocol_http_header(discover->version, &buf)) {
1465 rpc.protocol_header = strbuf_detach(&buf, NULL);
1466 } else {
1467 rpc.protocol_header = NULL;
1468 strbuf_release(&buf);
1470 rpc.buf = xmalloc(http_post_buffer);
1471 rpc.alloc = http_post_buffer;
1472 rpc.len = 0;
1473 rpc.pos = 0;
1474 rpc.in = 1;
1475 rpc.out = 0;
1476 rpc.any_written = 0;
1477 rpc.gzip_request = 1;
1478 rpc.initial_buffer = 0;
1479 rpc.write_line_lengths = 1;
1480 rpc.flush_read_but_not_sent = 0;
1483 * Dump the capability listing that we got from the server earlier
1484 * during the info/refs request.
1486 write_or_die(rpc.in, discover->buf, discover->len);
1488 /* Until we see EOF keep sending POSTs */
1489 while (1) {
1490 size_t avail;
1491 enum packet_read_status status;
1493 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1494 &status))
1495 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1496 if (status == PACKET_READ_EOF)
1497 break;
1498 if (post_rpc(&rpc, 1, status == PACKET_READ_FLUSH))
1499 /* We would have an err here */
1500 break;
1501 /* Reset the buffer for next request */
1502 rpc.len = 0;
1505 free(rpc.service_url);
1506 free(rpc.hdr_content_type);
1507 free(rpc.hdr_accept);
1508 free(rpc.hdr_accept_language);
1509 free(rpc.protocol_header);
1510 free(rpc.buf);
1511 strbuf_release(&buf);
1513 return 0;
1516 int cmd_main(int argc, const char **argv)
1518 struct strbuf buf = STRBUF_INIT;
1519 int nongit;
1520 int ret = 1;
1522 setup_git_directory_gently(&nongit);
1523 if (argc < 2) {
1524 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1525 goto cleanup;
1528 options.verbosity = 1;
1529 options.progress = !!isatty(2);
1530 options.thin = 1;
1531 string_list_init_dup(&options.deepen_not);
1532 string_list_init_dup(&options.push_options);
1535 * Just report "remote-curl" here (folding all the various aliases
1536 * ("git-remote-http", "git-remote-https", and etc.) here since they
1537 * are all just copies of the same actual executable.
1539 trace2_cmd_name("remote-curl");
1541 remote = remote_get(argv[1]);
1543 if (argc > 2) {
1544 end_url_with_slash(&url, argv[2]);
1545 } else {
1546 end_url_with_slash(&url, remote->url[0]);
1549 http_init(remote, url.buf, 0);
1551 do {
1552 const char *arg;
1554 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1555 if (ferror(stdin))
1556 error(_("remote-curl: error reading command stream from git"));
1557 goto cleanup;
1559 if (buf.len == 0)
1560 break;
1561 if (starts_with(buf.buf, "fetch ")) {
1562 if (nongit)
1563 die(_("remote-curl: fetch attempted without a local repo"));
1564 parse_fetch(&buf);
1566 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1567 int for_push = !!strstr(buf.buf + 4, "for-push");
1568 output_refs(get_refs(for_push));
1570 } else if (starts_with(buf.buf, "push ")) {
1571 parse_push(&buf);
1573 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1574 char *value = strchr(arg, ' ');
1575 int result;
1577 if (value)
1578 *value++ = '\0';
1579 else
1580 value = "true";
1582 result = set_option(arg, value);
1583 if (!result)
1584 printf("ok\n");
1585 else if (result < 0)
1586 printf("error invalid value\n");
1587 else
1588 printf("unsupported\n");
1589 fflush(stdout);
1591 } else if (skip_prefix(buf.buf, "get ", &arg)) {
1592 parse_get(arg);
1593 fflush(stdout);
1595 } else if (!strcmp(buf.buf, "capabilities")) {
1596 printf("stateless-connect\n");
1597 printf("fetch\n");
1598 printf("get\n");
1599 printf("option\n");
1600 printf("push\n");
1601 printf("check-connectivity\n");
1602 printf("object-format\n");
1603 printf("\n");
1604 fflush(stdout);
1605 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1606 if (!stateless_connect(arg))
1607 break;
1608 } else {
1609 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1610 goto cleanup;
1612 strbuf_reset(&buf);
1613 } while (1);
1615 http_cleanup();
1616 ret = 0;
1617 cleanup:
1618 strbuf_release(&buf);
1620 return ret;