Documentation/gitprotocol-v2.txt: fix a slight inconsistency in format
[git/gitster.git] / remote-curl.c
blob322cd0f9f4a32b3adc09c7fee718dbf1f36f0c5c
1 #include "cache.h"
2 #include "git-curl-compat.h"
3 #include "config.h"
4 #include "remote.h"
5 #include "connect.h"
6 #include "strbuf.h"
7 #include "walker.h"
8 #include "http.h"
9 #include "exec-cmd.h"
10 #include "run-command.h"
11 #include "pkt-line.h"
12 #include "string-list.h"
13 #include "sideband.h"
14 #include "strvec.h"
15 #include "credential.h"
16 #include "oid-array.h"
17 #include "send-pack.h"
18 #include "protocol.h"
19 #include "quote.h"
20 #include "transport.h"
22 static struct remote *remote;
23 /* always ends with a trailing slash */
24 static struct strbuf url = STRBUF_INIT;
26 struct options {
27 int verbosity;
28 unsigned long depth;
29 char *deepen_since;
30 struct string_list deepen_not;
31 struct string_list push_options;
32 char *filter;
33 unsigned progress : 1,
34 check_self_contained_and_connected : 1,
35 cloning : 1,
36 update_shallow : 1,
37 followtags : 1,
38 dry_run : 1,
39 thin : 1,
40 /* One of the SEND_PACK_PUSH_CERT_* constants. */
41 push_cert : 2,
42 deepen_relative : 1,
44 /* see documentation of corresponding flag in fetch-pack.h */
45 from_promisor : 1,
47 refetch : 1,
48 atomic : 1,
49 object_format : 1,
50 force_if_includes : 1;
51 const struct git_hash_algo *hash_algo;
53 static struct options options;
54 static struct string_list cas_options = STRING_LIST_INIT_DUP;
56 static int set_option(const char *name, const char *value)
58 if (!strcmp(name, "verbosity")) {
59 char *end;
60 int v = strtol(value, &end, 10);
61 if (value == end || *end)
62 return -1;
63 options.verbosity = v;
64 return 0;
66 else if (!strcmp(name, "progress")) {
67 if (!strcmp(value, "true"))
68 options.progress = 1;
69 else if (!strcmp(value, "false"))
70 options.progress = 0;
71 else
72 return -1;
73 return 0;
75 else if (!strcmp(name, "depth")) {
76 char *end;
77 unsigned long v = strtoul(value, &end, 10);
78 if (value == end || *end)
79 return -1;
80 options.depth = v;
81 return 0;
83 else if (!strcmp(name, "deepen-since")) {
84 options.deepen_since = xstrdup(value);
85 return 0;
87 else if (!strcmp(name, "deepen-not")) {
88 string_list_append(&options.deepen_not, value);
89 return 0;
91 else if (!strcmp(name, "deepen-relative")) {
92 if (!strcmp(value, "true"))
93 options.deepen_relative = 1;
94 else if (!strcmp(value, "false"))
95 options.deepen_relative = 0;
96 else
97 return -1;
98 return 0;
100 else if (!strcmp(name, "followtags")) {
101 if (!strcmp(value, "true"))
102 options.followtags = 1;
103 else if (!strcmp(value, "false"))
104 options.followtags = 0;
105 else
106 return -1;
107 return 0;
109 else if (!strcmp(name, "dry-run")) {
110 if (!strcmp(value, "true"))
111 options.dry_run = 1;
112 else if (!strcmp(value, "false"))
113 options.dry_run = 0;
114 else
115 return -1;
116 return 0;
118 else if (!strcmp(name, "check-connectivity")) {
119 if (!strcmp(value, "true"))
120 options.check_self_contained_and_connected = 1;
121 else if (!strcmp(value, "false"))
122 options.check_self_contained_and_connected = 0;
123 else
124 return -1;
125 return 0;
127 else if (!strcmp(name, "cas")) {
128 struct strbuf val = STRBUF_INIT;
129 strbuf_addstr(&val, "--force-with-lease=");
130 if (*value != '"')
131 strbuf_addstr(&val, value);
132 else if (unquote_c_style(&val, value, NULL))
133 return -1;
134 string_list_append(&cas_options, val.buf);
135 strbuf_release(&val);
136 return 0;
137 } else if (!strcmp(name, TRANS_OPT_FORCE_IF_INCLUDES)) {
138 if (!strcmp(value, "true"))
139 options.force_if_includes = 1;
140 else if (!strcmp(value, "false"))
141 options.force_if_includes = 0;
142 else
143 return -1;
144 return 0;
145 } else if (!strcmp(name, "cloning")) {
146 if (!strcmp(value, "true"))
147 options.cloning = 1;
148 else if (!strcmp(value, "false"))
149 options.cloning = 0;
150 else
151 return -1;
152 return 0;
153 } else if (!strcmp(name, "update-shallow")) {
154 if (!strcmp(value, "true"))
155 options.update_shallow = 1;
156 else if (!strcmp(value, "false"))
157 options.update_shallow = 0;
158 else
159 return -1;
160 return 0;
161 } else if (!strcmp(name, "pushcert")) {
162 if (!strcmp(value, "true"))
163 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
164 else if (!strcmp(value, "false"))
165 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
166 else if (!strcmp(value, "if-asked"))
167 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
168 else
169 return -1;
170 return 0;
171 } else if (!strcmp(name, "atomic")) {
172 if (!strcmp(value, "true"))
173 options.atomic = 1;
174 else if (!strcmp(value, "false"))
175 options.atomic = 0;
176 else
177 return -1;
178 return 0;
179 } else if (!strcmp(name, "push-option")) {
180 if (*value != '"')
181 string_list_append(&options.push_options, value);
182 else {
183 struct strbuf unquoted = STRBUF_INIT;
184 if (unquote_c_style(&unquoted, value, NULL) < 0)
185 die(_("invalid quoting in push-option value: '%s'"), value);
186 string_list_append_nodup(&options.push_options,
187 strbuf_detach(&unquoted, NULL));
189 return 0;
190 } else if (!strcmp(name, "family")) {
191 if (!strcmp(value, "ipv4"))
192 git_curl_ipresolve = CURL_IPRESOLVE_V4;
193 else if (!strcmp(value, "ipv6"))
194 git_curl_ipresolve = CURL_IPRESOLVE_V6;
195 else if (!strcmp(value, "all"))
196 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
197 else
198 return -1;
199 return 0;
200 } else if (!strcmp(name, "from-promisor")) {
201 options.from_promisor = 1;
202 return 0;
203 } else if (!strcmp(name, "refetch")) {
204 options.refetch = 1;
205 return 0;
206 } else if (!strcmp(name, "filter")) {
207 options.filter = xstrdup(value);
208 return 0;
209 } else if (!strcmp(name, "object-format")) {
210 int algo;
211 options.object_format = 1;
212 if (strcmp(value, "true")) {
213 algo = hash_algo_by_name(value);
214 if (algo == GIT_HASH_UNKNOWN)
215 die("unknown object format '%s'", value);
216 options.hash_algo = &hash_algos[algo];
218 return 0;
219 } else {
220 return 1 /* unsupported */;
224 struct discovery {
225 char *service;
226 char *buf_alloc;
227 char *buf;
228 size_t len;
229 struct ref *refs;
230 struct oid_array shallow;
231 enum protocol_version version;
232 unsigned proto_git : 1;
234 static struct discovery *last_discovery;
236 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
238 struct ref *list = NULL;
239 struct packet_reader reader;
241 packet_reader_init(&reader, -1, heads->buf, heads->len,
242 PACKET_READ_CHOMP_NEWLINE |
243 PACKET_READ_GENTLE_ON_EOF |
244 PACKET_READ_DIE_ON_ERR_PACKET);
246 heads->version = discover_version(&reader);
247 switch (heads->version) {
248 case protocol_v2:
250 * Do nothing. This isn't a list of refs but rather a
251 * capability advertisement. Client would have run
252 * 'stateless-connect' so we'll dump this capability listing
253 * and let them request the refs themselves.
255 break;
256 case protocol_v1:
257 case protocol_v0:
258 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
259 NULL, &heads->shallow);
260 options.hash_algo = reader.hash_algo;
261 break;
262 case protocol_unknown_version:
263 BUG("unknown protocol version");
266 return list;
269 static const struct git_hash_algo *detect_hash_algo(struct discovery *heads)
271 const char *p = memchr(heads->buf, '\t', heads->len);
272 int algo;
273 if (!p)
274 return the_hash_algo;
276 algo = hash_algo_by_length((p - heads->buf) / 2);
277 if (algo == GIT_HASH_UNKNOWN)
278 return NULL;
279 return &hash_algos[algo];
282 static struct ref *parse_info_refs(struct discovery *heads)
284 char *data, *start, *mid;
285 char *ref_name;
286 int i = 0;
288 struct ref *refs = NULL;
289 struct ref *ref = NULL;
290 struct ref *last_ref = NULL;
292 options.hash_algo = detect_hash_algo(heads);
293 if (!options.hash_algo)
294 die("%sinfo/refs not valid: could not determine hash algorithm; "
295 "is this a git repository?",
296 transport_anonymize_url(url.buf));
298 data = heads->buf;
299 start = NULL;
300 mid = data;
301 while (i < heads->len) {
302 if (!start) {
303 start = &data[i];
305 if (data[i] == '\t')
306 mid = &data[i];
307 if (data[i] == '\n') {
308 if (mid - start != options.hash_algo->hexsz)
309 die(_("%sinfo/refs not valid: is this a git repository?"),
310 transport_anonymize_url(url.buf));
311 data[i] = 0;
312 ref_name = mid + 1;
313 ref = alloc_ref(ref_name);
314 get_oid_hex_algop(start, &ref->old_oid, options.hash_algo);
315 if (!refs)
316 refs = ref;
317 if (last_ref)
318 last_ref->next = ref;
319 last_ref = ref;
320 start = NULL;
322 i++;
325 ref = alloc_ref("HEAD");
326 if (!http_fetch_ref(url.buf, ref) &&
327 !resolve_remote_symref(ref, refs)) {
328 ref->next = refs;
329 refs = ref;
330 } else {
331 free(ref);
334 return refs;
337 static void free_discovery(struct discovery *d)
339 if (d) {
340 if (d == last_discovery)
341 last_discovery = NULL;
342 free(d->shallow.oid);
343 free(d->buf_alloc);
344 free_refs(d->refs);
345 free(d->service);
346 free(d);
350 static int show_http_message(struct strbuf *type, struct strbuf *charset,
351 struct strbuf *msg)
353 const char *p, *eol;
356 * We only show text/plain parts, as other types are likely
357 * to be ugly to look at on the user's terminal.
359 if (strcmp(type->buf, "text/plain"))
360 return -1;
361 if (charset->len)
362 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
364 strbuf_trim(msg);
365 if (!msg->len)
366 return -1;
368 p = msg->buf;
369 do {
370 eol = strchrnul(p, '\n');
371 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
372 p = eol + 1;
373 } while(*eol);
374 return 0;
377 static int get_protocol_http_header(enum protocol_version version,
378 struct strbuf *header)
380 if (version > 0) {
381 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
382 version);
384 return 1;
387 return 0;
390 static void check_smart_http(struct discovery *d, const char *service,
391 struct strbuf *type)
393 const char *p;
394 struct packet_reader reader;
397 * If we don't see x-$service-advertisement, then it's not smart-http.
398 * But once we do, we commit to it and assume any other protocol
399 * violations are hard errors.
401 if (!skip_prefix(type->buf, "application/x-", &p) ||
402 !skip_prefix(p, service, &p) ||
403 strcmp(p, "-advertisement"))
404 return;
406 packet_reader_init(&reader, -1, d->buf, d->len,
407 PACKET_READ_CHOMP_NEWLINE |
408 PACKET_READ_DIE_ON_ERR_PACKET);
409 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
410 die(_("invalid server response; expected service, got flush packet"));
412 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
414 * The header can include additional metadata lines, up
415 * until a packet flush marker. Ignore these now, but
416 * in the future we might start to scan them.
418 for (;;) {
419 packet_reader_read(&reader);
420 if (reader.pktlen <= 0) {
421 break;
426 * v0 smart http; callers expect us to soak up the
427 * service and header packets
429 d->buf = reader.src_buffer;
430 d->len = reader.src_len;
431 d->proto_git = 1;
433 } else if (!strcmp(reader.line, "version 2")) {
435 * v2 smart http; do not consume version packet, which will
436 * be handled elsewhere.
438 d->proto_git = 1;
440 } else {
441 die(_("invalid server response; got '%s'"), reader.line);
445 static struct discovery *discover_refs(const char *service, int for_push)
447 struct strbuf type = STRBUF_INIT;
448 struct strbuf charset = STRBUF_INIT;
449 struct strbuf buffer = STRBUF_INIT;
450 struct strbuf refs_url = STRBUF_INIT;
451 struct strbuf effective_url = STRBUF_INIT;
452 struct strbuf protocol_header = STRBUF_INIT;
453 struct string_list extra_headers = STRING_LIST_INIT_DUP;
454 struct discovery *last = last_discovery;
455 int http_ret, maybe_smart = 0;
456 struct http_get_options http_options;
457 enum protocol_version version = get_protocol_version_config();
459 if (last && !strcmp(service, last->service))
460 return last;
461 free_discovery(last);
463 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
464 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
465 git_env_bool("GIT_SMART_HTTP", 1)) {
466 maybe_smart = 1;
467 if (!strchr(url.buf, '?'))
468 strbuf_addch(&refs_url, '?');
469 else
470 strbuf_addch(&refs_url, '&');
471 strbuf_addf(&refs_url, "service=%s", service);
475 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
476 * to perform a push, then fallback to v0 since the client doesn't know
477 * how to push yet using v2.
479 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
480 version = protocol_v0;
482 /* Add the extra Git-Protocol header */
483 if (get_protocol_http_header(version, &protocol_header))
484 string_list_append(&extra_headers, protocol_header.buf);
486 memset(&http_options, 0, sizeof(http_options));
487 http_options.content_type = &type;
488 http_options.charset = &charset;
489 http_options.effective_url = &effective_url;
490 http_options.base_url = &url;
491 http_options.extra_headers = &extra_headers;
492 http_options.initial_request = 1;
493 http_options.no_cache = 1;
495 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
496 switch (http_ret) {
497 case HTTP_OK:
498 break;
499 case HTTP_MISSING_TARGET:
500 show_http_message(&type, &charset, &buffer);
501 die(_("repository '%s' not found"),
502 transport_anonymize_url(url.buf));
503 case HTTP_NOAUTH:
504 show_http_message(&type, &charset, &buffer);
505 die(_("Authentication failed for '%s'"),
506 transport_anonymize_url(url.buf));
507 case HTTP_NOMATCHPUBLICKEY:
508 show_http_message(&type, &charset, &buffer);
509 die(_("unable to access '%s' with http.pinnedPubkey configuration: %s"),
510 transport_anonymize_url(url.buf), curl_errorstr);
511 default:
512 show_http_message(&type, &charset, &buffer);
513 die(_("unable to access '%s': %s"),
514 transport_anonymize_url(url.buf), curl_errorstr);
517 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
518 char *u = transport_anonymize_url(url.buf);
519 warning(_("redirecting to %s"), u);
520 free(u);
523 last= xcalloc(1, sizeof(*last_discovery));
524 last->service = xstrdup(service);
525 last->buf_alloc = strbuf_detach(&buffer, &last->len);
526 last->buf = last->buf_alloc;
528 if (maybe_smart)
529 check_smart_http(last, service, &type);
531 if (last->proto_git)
532 last->refs = parse_git_refs(last, for_push);
533 else
534 last->refs = parse_info_refs(last);
536 strbuf_release(&refs_url);
537 strbuf_release(&type);
538 strbuf_release(&charset);
539 strbuf_release(&effective_url);
540 strbuf_release(&buffer);
541 strbuf_release(&protocol_header);
542 string_list_clear(&extra_headers, 0);
543 last_discovery = last;
544 return last;
547 static struct ref *get_refs(int for_push)
549 struct discovery *heads;
551 if (for_push)
552 heads = discover_refs("git-receive-pack", for_push);
553 else
554 heads = discover_refs("git-upload-pack", for_push);
556 return heads->refs;
559 static void output_refs(struct ref *refs)
561 struct ref *posn;
562 if (options.object_format && options.hash_algo) {
563 printf(":object-format %s\n", options.hash_algo->name);
564 repo_set_hash_algo(the_repository,
565 hash_algo_by_ptr(options.hash_algo));
567 for (posn = refs; posn; posn = posn->next) {
568 if (posn->symref)
569 printf("@%s %s\n", posn->symref, posn->name);
570 else
571 printf("%s %s\n", hash_to_hex_algop(posn->old_oid.hash,
572 options.hash_algo),
573 posn->name);
575 printf("\n");
576 fflush(stdout);
579 struct rpc_state {
580 const char *service_name;
581 char *service_url;
582 char *hdr_content_type;
583 char *hdr_accept;
584 char *hdr_accept_language;
585 char *protocol_header;
586 char *buf;
587 size_t alloc;
588 size_t len;
589 size_t pos;
590 int in;
591 int out;
592 int any_written;
593 unsigned gzip_request : 1;
594 unsigned initial_buffer : 1;
597 * Whenever a pkt-line is read into buf, append the 4 characters
598 * denoting its length before appending the payload.
600 unsigned write_line_lengths : 1;
603 * Used by rpc_out; initialize to 0. This is true if a flush has been
604 * read, but the corresponding line length (if write_line_lengths is
605 * true) and EOF have not been sent to libcurl. Since each flush marks
606 * the end of a request, each flush must be completely sent before any
607 * further reading occurs.
609 unsigned flush_read_but_not_sent : 1;
612 #define RPC_STATE_INIT { 0 }
615 * Appends the result of reading from rpc->out to the string represented by
616 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
617 * enough space, 0 otherwise.
619 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
620 * hexadecimal string before appending the result described above.
622 * Writes the total number of bytes appended into appended.
624 static int rpc_read_from_out(struct rpc_state *rpc, int options,
625 size_t *appended,
626 enum packet_read_status *status) {
627 size_t left;
628 char *buf;
629 int pktlen_raw;
631 if (rpc->write_line_lengths) {
632 left = rpc->alloc - rpc->len - 4;
633 buf = rpc->buf + rpc->len + 4;
634 } else {
635 left = rpc->alloc - rpc->len;
636 buf = rpc->buf + rpc->len;
639 if (left < LARGE_PACKET_MAX)
640 return 0;
642 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
643 left, &pktlen_raw, options);
644 if (*status != PACKET_READ_EOF) {
645 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
646 rpc->len += *appended;
649 if (rpc->write_line_lengths) {
650 switch (*status) {
651 case PACKET_READ_EOF:
652 if (!(options & PACKET_READ_GENTLE_ON_EOF))
653 die(_("shouldn't have EOF when not gentle on EOF"));
654 break;
655 case PACKET_READ_NORMAL:
656 set_packet_header(buf - 4, *appended);
657 break;
658 case PACKET_READ_DELIM:
659 memcpy(buf - 4, "0001", 4);
660 break;
661 case PACKET_READ_FLUSH:
662 memcpy(buf - 4, "0000", 4);
663 break;
664 case PACKET_READ_RESPONSE_END:
665 die(_("remote server sent unexpected response end packet"));
669 return 1;
672 static size_t rpc_out(void *ptr, size_t eltsize,
673 size_t nmemb, void *buffer_)
675 size_t max = eltsize * nmemb;
676 struct rpc_state *rpc = buffer_;
677 size_t avail = rpc->len - rpc->pos;
678 enum packet_read_status status;
680 if (!avail) {
681 rpc->initial_buffer = 0;
682 rpc->len = 0;
683 rpc->pos = 0;
684 if (!rpc->flush_read_but_not_sent) {
685 if (!rpc_read_from_out(rpc, 0, &avail, &status))
686 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
687 if (status == PACKET_READ_FLUSH)
688 rpc->flush_read_but_not_sent = 1;
691 * If flush_read_but_not_sent is true, we have already read one
692 * full request but have not fully sent it + EOF, which is why
693 * we need to refrain from reading.
696 if (rpc->flush_read_but_not_sent) {
697 if (!avail) {
699 * The line length either does not need to be sent at
700 * all or has already been completely sent. Now we can
701 * return 0, indicating EOF, meaning that the flush has
702 * been fully sent.
704 rpc->flush_read_but_not_sent = 0;
705 return 0;
708 * If avail is non-zero, the line length for the flush still
709 * hasn't been fully sent. Proceed with sending the line
710 * length.
714 if (max < avail)
715 avail = max;
716 memcpy(ptr, rpc->buf + rpc->pos, avail);
717 rpc->pos += avail;
718 return avail;
721 static int rpc_seek(void *clientp, curl_off_t offset, int origin)
723 struct rpc_state *rpc = clientp;
725 if (origin != SEEK_SET)
726 BUG("rpc_seek only handles SEEK_SET, not %d", origin);
728 if (rpc->initial_buffer) {
729 if (offset < 0 || offset > rpc->len) {
730 error("curl seek would be outside of rpc buffer");
731 return CURL_SEEKFUNC_FAIL;
733 rpc->pos = offset;
734 return CURL_SEEKFUNC_OK;
736 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
737 return CURL_SEEKFUNC_FAIL;
740 struct check_pktline_state {
741 char len_buf[4];
742 int len_filled;
743 int remaining;
746 static void check_pktline(struct check_pktline_state *state, const char *ptr, size_t size)
748 while (size) {
749 if (!state->remaining) {
750 int digits_remaining = 4 - state->len_filled;
751 if (digits_remaining > size)
752 digits_remaining = size;
753 memcpy(&state->len_buf[state->len_filled], ptr, digits_remaining);
754 state->len_filled += digits_remaining;
755 ptr += digits_remaining;
756 size -= digits_remaining;
758 if (state->len_filled == 4) {
759 state->remaining = packet_length(state->len_buf);
760 if (state->remaining < 0) {
761 die(_("remote-curl: bad line length character: %.4s"), state->len_buf);
762 } else if (state->remaining == 2) {
763 die(_("remote-curl: unexpected response end packet"));
764 } else if (state->remaining < 4) {
765 state->remaining = 0;
766 } else {
767 state->remaining -= 4;
769 state->len_filled = 0;
773 if (state->remaining) {
774 int remaining = state->remaining;
775 if (remaining > size)
776 remaining = size;
777 ptr += remaining;
778 size -= remaining;
779 state->remaining -= remaining;
784 struct rpc_in_data {
785 struct rpc_state *rpc;
786 struct active_request_slot *slot;
787 int check_pktline;
788 struct check_pktline_state pktline_state;
792 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
793 * from ptr.
795 static size_t rpc_in(char *ptr, size_t eltsize,
796 size_t nmemb, void *buffer_)
798 size_t size = eltsize * nmemb;
799 struct rpc_in_data *data = buffer_;
800 long response_code;
802 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
803 &response_code) != CURLE_OK)
804 return size;
805 if (response_code >= 300)
806 return size;
807 if (size)
808 data->rpc->any_written = 1;
809 if (data->check_pktline)
810 check_pktline(&data->pktline_state, ptr, size);
811 write_or_die(data->rpc->in, ptr, size);
812 return size;
815 static int run_slot(struct active_request_slot *slot,
816 struct slot_results *results)
818 int err;
819 struct slot_results results_buf;
821 if (!results)
822 results = &results_buf;
824 err = run_one_slot(slot, results);
826 if (err != HTTP_OK && err != HTTP_REAUTH) {
827 struct strbuf msg = STRBUF_INIT;
828 if (results->http_code && results->http_code != 200)
829 strbuf_addf(&msg, "HTTP %ld", results->http_code);
830 if (results->curl_result != CURLE_OK) {
831 if (msg.len)
832 strbuf_addch(&msg, ' ');
833 strbuf_addf(&msg, "curl %d", results->curl_result);
834 if (curl_errorstr[0]) {
835 strbuf_addch(&msg, ' ');
836 strbuf_addstr(&msg, curl_errorstr);
839 error(_("RPC failed; %s"), msg.buf);
840 strbuf_release(&msg);
843 return err;
846 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
848 struct active_request_slot *slot;
849 struct curl_slist *headers = http_copy_default_headers();
850 struct strbuf buf = STRBUF_INIT;
851 int err;
853 slot = get_active_slot();
855 headers = curl_slist_append(headers, rpc->hdr_content_type);
856 headers = curl_slist_append(headers, rpc->hdr_accept);
858 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
859 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
860 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
861 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
862 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
863 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
864 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
865 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
866 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, &buf);
868 err = run_slot(slot, results);
870 curl_slist_free_all(headers);
871 strbuf_release(&buf);
872 return err;
875 static curl_off_t xcurl_off_t(size_t len)
877 uintmax_t size = len;
878 if (size > maximum_signed_value_of_type(curl_off_t))
879 die(_("cannot handle pushes this big"));
880 return (curl_off_t)size;
884 * If flush_received is true, do not attempt to read any more; just use what's
885 * in rpc->buf.
887 static int post_rpc(struct rpc_state *rpc, int stateless_connect, int flush_received)
889 struct active_request_slot *slot;
890 struct curl_slist *headers = http_copy_default_headers();
891 int use_gzip = rpc->gzip_request;
892 char *gzip_body = NULL;
893 size_t gzip_size = 0;
894 int err, large_request = 0;
895 int needs_100_continue = 0;
896 struct rpc_in_data rpc_in_data;
898 /* Try to load the entire request, if we can fit it into the
899 * allocated buffer space we can use HTTP/1.0 and avoid the
900 * chunked encoding mess.
902 if (!flush_received) {
903 while (1) {
904 size_t n;
905 enum packet_read_status status;
907 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
908 large_request = 1;
909 use_gzip = 0;
910 break;
912 if (status == PACKET_READ_FLUSH)
913 break;
917 if (large_request) {
918 struct slot_results results;
920 do {
921 err = probe_rpc(rpc, &results);
922 if (err == HTTP_REAUTH)
923 credential_fill(&http_auth);
924 } while (err == HTTP_REAUTH);
925 if (err != HTTP_OK)
926 return -1;
928 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
929 needs_100_continue = 1;
932 headers = curl_slist_append(headers, rpc->hdr_content_type);
933 headers = curl_slist_append(headers, rpc->hdr_accept);
934 headers = curl_slist_append(headers, needs_100_continue ?
935 "Expect: 100-continue" : "Expect:");
937 /* Add Accept-Language header */
938 if (rpc->hdr_accept_language)
939 headers = curl_slist_append(headers, rpc->hdr_accept_language);
941 /* Add the extra Git-Protocol header */
942 if (rpc->protocol_header)
943 headers = curl_slist_append(headers, rpc->protocol_header);
945 retry:
946 slot = get_active_slot();
948 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
949 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
950 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
951 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
953 if (large_request) {
954 /* The request body is large and the size cannot be predicted.
955 * We must use chunked encoding to send it.
957 #ifdef GIT_CURL_NEED_TRANSFER_ENCODING_HEADER
958 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
959 #endif
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;