]> git.ipfire.org Git - thirdparty/git.git/blob - remote-curl.c
remote-curl: implement object-format extensions
[thirdparty/git.git] / remote-curl.c
1 #include "cache.h"
2 #include "config.h"
3 #include "remote.h"
4 #include "connect.h"
5 #include "strbuf.h"
6 #include "walker.h"
7 #include "http.h"
8 #include "exec-cmd.h"
9 #include "run-command.h"
10 #include "pkt-line.h"
11 #include "string-list.h"
12 #include "sideband.h"
13 #include "argv-array.h"
14 #include "credential.h"
15 #include "oid-array.h"
16 #include "send-pack.h"
17 #include "protocol.h"
18 #include "quote.h"
19 #include "transport.h"
20
21 static struct remote *remote;
22 /* always ends with a trailing slash */
23 static struct strbuf url = STRBUF_INIT;
24
25 struct options {
26 int verbosity;
27 unsigned long depth;
28 char *deepen_since;
29 struct string_list deepen_not;
30 struct string_list push_options;
31 char *filter;
32 unsigned progress : 1,
33 check_self_contained_and_connected : 1,
34 cloning : 1,
35 update_shallow : 1,
36 followtags : 1,
37 dry_run : 1,
38 thin : 1,
39 /* One of the SEND_PACK_PUSH_CERT_* constants. */
40 push_cert : 2,
41 deepen_relative : 1,
42 from_promisor : 1,
43 no_dependents : 1,
44 atomic : 1,
45 object_format : 1;
46 const struct git_hash_algo *hash_algo;
47 };
48 static struct options options;
49 static struct string_list cas_options = STRING_LIST_INIT_DUP;
50
51 static int set_option(const char *name, const char *value)
52 {
53 if (!strcmp(name, "verbosity")) {
54 char *end;
55 int v = strtol(value, &end, 10);
56 if (value == end || *end)
57 return -1;
58 options.verbosity = v;
59 return 0;
60 }
61 else if (!strcmp(name, "progress")) {
62 if (!strcmp(value, "true"))
63 options.progress = 1;
64 else if (!strcmp(value, "false"))
65 options.progress = 0;
66 else
67 return -1;
68 return 0;
69 }
70 else if (!strcmp(name, "depth")) {
71 char *end;
72 unsigned long v = strtoul(value, &end, 10);
73 if (value == end || *end)
74 return -1;
75 options.depth = v;
76 return 0;
77 }
78 else if (!strcmp(name, "deepen-since")) {
79 options.deepen_since = xstrdup(value);
80 return 0;
81 }
82 else if (!strcmp(name, "deepen-not")) {
83 string_list_append(&options.deepen_not, value);
84 return 0;
85 }
86 else if (!strcmp(name, "deepen-relative")) {
87 if (!strcmp(value, "true"))
88 options.deepen_relative = 1;
89 else if (!strcmp(value, "false"))
90 options.deepen_relative = 0;
91 else
92 return -1;
93 return 0;
94 }
95 else if (!strcmp(name, "followtags")) {
96 if (!strcmp(value, "true"))
97 options.followtags = 1;
98 else if (!strcmp(value, "false"))
99 options.followtags = 0;
100 else
101 return -1;
102 return 0;
103 }
104 else if (!strcmp(name, "dry-run")) {
105 if (!strcmp(value, "true"))
106 options.dry_run = 1;
107 else if (!strcmp(value, "false"))
108 options.dry_run = 0;
109 else
110 return -1;
111 return 0;
112 }
113 else if (!strcmp(name, "check-connectivity")) {
114 if (!strcmp(value, "true"))
115 options.check_self_contained_and_connected = 1;
116 else if (!strcmp(value, "false"))
117 options.check_self_contained_and_connected = 0;
118 else
119 return -1;
120 return 0;
121 }
122 else if (!strcmp(name, "cas")) {
123 struct strbuf val = STRBUF_INIT;
124 strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
125 string_list_append(&cas_options, val.buf);
126 strbuf_release(&val);
127 return 0;
128 } else if (!strcmp(name, "cloning")) {
129 if (!strcmp(value, "true"))
130 options.cloning = 1;
131 else if (!strcmp(value, "false"))
132 options.cloning = 0;
133 else
134 return -1;
135 return 0;
136 } else if (!strcmp(name, "update-shallow")) {
137 if (!strcmp(value, "true"))
138 options.update_shallow = 1;
139 else if (!strcmp(value, "false"))
140 options.update_shallow = 0;
141 else
142 return -1;
143 return 0;
144 } else if (!strcmp(name, "pushcert")) {
145 if (!strcmp(value, "true"))
146 options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
147 else if (!strcmp(value, "false"))
148 options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
149 else if (!strcmp(value, "if-asked"))
150 options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
151 else
152 return -1;
153 return 0;
154 } else if (!strcmp(name, "atomic")) {
155 if (!strcmp(value, "true"))
156 options.atomic = 1;
157 else if (!strcmp(value, "false"))
158 options.atomic = 0;
159 else
160 return -1;
161 return 0;
162 } else if (!strcmp(name, "push-option")) {
163 if (*value != '"')
164 string_list_append(&options.push_options, value);
165 else {
166 struct strbuf unquoted = STRBUF_INIT;
167 if (unquote_c_style(&unquoted, value, NULL) < 0)
168 die(_("invalid quoting in push-option value: '%s'"), value);
169 string_list_append_nodup(&options.push_options,
170 strbuf_detach(&unquoted, NULL));
171 }
172 return 0;
173
174 #if LIBCURL_VERSION_NUM >= 0x070a08
175 } else if (!strcmp(name, "family")) {
176 if (!strcmp(value, "ipv4"))
177 git_curl_ipresolve = CURL_IPRESOLVE_V4;
178 else if (!strcmp(value, "ipv6"))
179 git_curl_ipresolve = CURL_IPRESOLVE_V6;
180 else if (!strcmp(value, "all"))
181 git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
182 else
183 return -1;
184 return 0;
185 #endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
186 } else if (!strcmp(name, "from-promisor")) {
187 options.from_promisor = 1;
188 return 0;
189 } else if (!strcmp(name, "no-dependents")) {
190 options.no_dependents = 1;
191 return 0;
192 } else if (!strcmp(name, "filter")) {
193 options.filter = xstrdup(value);
194 return 0;
195 } else if (!strcmp(name, "object-format")) {
196 int algo;
197 options.object_format = 1;
198 if (strcmp(value, "true")) {
199 algo = hash_algo_by_name(value);
200 if (algo == GIT_HASH_UNKNOWN)
201 die("unknown object format '%s'", value);
202 options.hash_algo = &hash_algos[algo];
203 }
204 return 0;
205 } else {
206 return 1 /* unsupported */;
207 }
208 }
209
210 struct discovery {
211 char *service;
212 char *buf_alloc;
213 char *buf;
214 size_t len;
215 struct ref *refs;
216 struct oid_array shallow;
217 enum protocol_version version;
218 unsigned proto_git : 1;
219 };
220 static struct discovery *last_discovery;
221
222 static struct ref *parse_git_refs(struct discovery *heads, int for_push)
223 {
224 struct ref *list = NULL;
225 struct packet_reader reader;
226
227 packet_reader_init(&reader, -1, heads->buf, heads->len,
228 PACKET_READ_CHOMP_NEWLINE |
229 PACKET_READ_GENTLE_ON_EOF |
230 PACKET_READ_DIE_ON_ERR_PACKET);
231
232 heads->version = discover_version(&reader);
233 switch (heads->version) {
234 case protocol_v2:
235 /*
236 * Do nothing. This isn't a list of refs but rather a
237 * capability advertisement. Client would have run
238 * 'stateless-connect' so we'll dump this capability listing
239 * and let them request the refs themselves.
240 */
241 break;
242 case protocol_v1:
243 case protocol_v0:
244 get_remote_heads(&reader, &list, for_push ? REF_NORMAL : 0,
245 NULL, &heads->shallow);
246 options.hash_algo = reader.hash_algo;
247 break;
248 case protocol_unknown_version:
249 BUG("unknown protocol version");
250 }
251
252 return list;
253 }
254
255 static struct ref *parse_info_refs(struct discovery *heads)
256 {
257 char *data, *start, *mid;
258 char *ref_name;
259 int i = 0;
260
261 struct ref *refs = NULL;
262 struct ref *ref = NULL;
263 struct ref *last_ref = NULL;
264
265 data = heads->buf;
266 start = NULL;
267 mid = data;
268 while (i < heads->len) {
269 if (!start) {
270 start = &data[i];
271 }
272 if (data[i] == '\t')
273 mid = &data[i];
274 if (data[i] == '\n') {
275 if (mid - start != the_hash_algo->hexsz)
276 die(_("%sinfo/refs not valid: is this a git repository?"),
277 transport_anonymize_url(url.buf));
278 data[i] = 0;
279 ref_name = mid + 1;
280 ref = alloc_ref(ref_name);
281 get_oid_hex(start, &ref->old_oid);
282 if (!refs)
283 refs = ref;
284 if (last_ref)
285 last_ref->next = ref;
286 last_ref = ref;
287 start = NULL;
288 }
289 i++;
290 }
291
292 ref = alloc_ref("HEAD");
293 if (!http_fetch_ref(url.buf, ref) &&
294 !resolve_remote_symref(ref, refs)) {
295 ref->next = refs;
296 refs = ref;
297 } else {
298 free(ref);
299 }
300
301 return refs;
302 }
303
304 static void free_discovery(struct discovery *d)
305 {
306 if (d) {
307 if (d == last_discovery)
308 last_discovery = NULL;
309 free(d->shallow.oid);
310 free(d->buf_alloc);
311 free_refs(d->refs);
312 free(d->service);
313 free(d);
314 }
315 }
316
317 static int show_http_message(struct strbuf *type, struct strbuf *charset,
318 struct strbuf *msg)
319 {
320 const char *p, *eol;
321
322 /*
323 * We only show text/plain parts, as other types are likely
324 * to be ugly to look at on the user's terminal.
325 */
326 if (strcmp(type->buf, "text/plain"))
327 return -1;
328 if (charset->len)
329 strbuf_reencode(msg, charset->buf, get_log_output_encoding());
330
331 strbuf_trim(msg);
332 if (!msg->len)
333 return -1;
334
335 p = msg->buf;
336 do {
337 eol = strchrnul(p, '\n');
338 fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
339 p = eol + 1;
340 } while(*eol);
341 return 0;
342 }
343
344 static int get_protocol_http_header(enum protocol_version version,
345 struct strbuf *header)
346 {
347 if (version > 0) {
348 strbuf_addf(header, GIT_PROTOCOL_HEADER ": version=%d",
349 version);
350
351 return 1;
352 }
353
354 return 0;
355 }
356
357 static void check_smart_http(struct discovery *d, const char *service,
358 struct strbuf *type)
359 {
360 const char *p;
361 struct packet_reader reader;
362
363 /*
364 * If we don't see x-$service-advertisement, then it's not smart-http.
365 * But once we do, we commit to it and assume any other protocol
366 * violations are hard errors.
367 */
368 if (!skip_prefix(type->buf, "application/x-", &p) ||
369 !skip_prefix(p, service, &p) ||
370 strcmp(p, "-advertisement"))
371 return;
372
373 packet_reader_init(&reader, -1, d->buf, d->len,
374 PACKET_READ_CHOMP_NEWLINE |
375 PACKET_READ_DIE_ON_ERR_PACKET);
376 if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
377 die(_("invalid server response; expected service, got flush packet"));
378
379 if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
380 /*
381 * The header can include additional metadata lines, up
382 * until a packet flush marker. Ignore these now, but
383 * in the future we might start to scan them.
384 */
385 for (;;) {
386 packet_reader_read(&reader);
387 if (reader.pktlen <= 0) {
388 break;
389 }
390 }
391
392 /*
393 * v0 smart http; callers expect us to soak up the
394 * service and header packets
395 */
396 d->buf = reader.src_buffer;
397 d->len = reader.src_len;
398 d->proto_git = 1;
399
400 } else if (!strcmp(reader.line, "version 2")) {
401 /*
402 * v2 smart http; do not consume version packet, which will
403 * be handled elsewhere.
404 */
405 d->proto_git = 1;
406
407 } else {
408 die(_("invalid server response; got '%s'"), reader.line);
409 }
410 }
411
412 static struct discovery *discover_refs(const char *service, int for_push)
413 {
414 struct strbuf type = STRBUF_INIT;
415 struct strbuf charset = STRBUF_INIT;
416 struct strbuf buffer = STRBUF_INIT;
417 struct strbuf refs_url = STRBUF_INIT;
418 struct strbuf effective_url = STRBUF_INIT;
419 struct strbuf protocol_header = STRBUF_INIT;
420 struct string_list extra_headers = STRING_LIST_INIT_DUP;
421 struct discovery *last = last_discovery;
422 int http_ret, maybe_smart = 0;
423 struct http_get_options http_options;
424 enum protocol_version version = get_protocol_version_config();
425
426 if (last && !strcmp(service, last->service))
427 return last;
428 free_discovery(last);
429
430 strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
431 if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
432 git_env_bool("GIT_SMART_HTTP", 1)) {
433 maybe_smart = 1;
434 if (!strchr(url.buf, '?'))
435 strbuf_addch(&refs_url, '?');
436 else
437 strbuf_addch(&refs_url, '&');
438 strbuf_addf(&refs_url, "service=%s", service);
439 }
440
441 /*
442 * NEEDSWORK: If we are trying to use protocol v2 and we are planning
443 * to perform a push, then fallback to v0 since the client doesn't know
444 * how to push yet using v2.
445 */
446 if (version == protocol_v2 && !strcmp("git-receive-pack", service))
447 version = protocol_v0;
448
449 /* Add the extra Git-Protocol header */
450 if (get_protocol_http_header(version, &protocol_header))
451 string_list_append(&extra_headers, protocol_header.buf);
452
453 memset(&http_options, 0, sizeof(http_options));
454 http_options.content_type = &type;
455 http_options.charset = &charset;
456 http_options.effective_url = &effective_url;
457 http_options.base_url = &url;
458 http_options.extra_headers = &extra_headers;
459 http_options.initial_request = 1;
460 http_options.no_cache = 1;
461
462 http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
463 switch (http_ret) {
464 case HTTP_OK:
465 break;
466 case HTTP_MISSING_TARGET:
467 show_http_message(&type, &charset, &buffer);
468 die(_("repository '%s' not found"),
469 transport_anonymize_url(url.buf));
470 case HTTP_NOAUTH:
471 show_http_message(&type, &charset, &buffer);
472 die(_("Authentication failed for '%s'"),
473 transport_anonymize_url(url.buf));
474 default:
475 show_http_message(&type, &charset, &buffer);
476 die(_("unable to access '%s': %s"),
477 transport_anonymize_url(url.buf), curl_errorstr);
478 }
479
480 if (options.verbosity && !starts_with(refs_url.buf, url.buf)) {
481 char *u = transport_anonymize_url(url.buf);
482 warning(_("redirecting to %s"), u);
483 free(u);
484 }
485
486 last= xcalloc(1, sizeof(*last_discovery));
487 last->service = xstrdup(service);
488 last->buf_alloc = strbuf_detach(&buffer, &last->len);
489 last->buf = last->buf_alloc;
490
491 if (maybe_smart)
492 check_smart_http(last, service, &type);
493
494 if (last->proto_git)
495 last->refs = parse_git_refs(last, for_push);
496 else
497 last->refs = parse_info_refs(last);
498
499 strbuf_release(&refs_url);
500 strbuf_release(&type);
501 strbuf_release(&charset);
502 strbuf_release(&effective_url);
503 strbuf_release(&buffer);
504 strbuf_release(&protocol_header);
505 string_list_clear(&extra_headers, 0);
506 last_discovery = last;
507 return last;
508 }
509
510 static struct ref *get_refs(int for_push)
511 {
512 struct discovery *heads;
513
514 if (for_push)
515 heads = discover_refs("git-receive-pack", for_push);
516 else
517 heads = discover_refs("git-upload-pack", for_push);
518
519 return heads->refs;
520 }
521
522 static void output_refs(struct ref *refs)
523 {
524 struct ref *posn;
525 if (options.object_format && options.hash_algo) {
526 printf(":object-format %s\n", options.hash_algo->name);
527 }
528 for (posn = refs; posn; posn = posn->next) {
529 if (posn->symref)
530 printf("@%s %s\n", posn->symref, posn->name);
531 else
532 printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
533 }
534 printf("\n");
535 fflush(stdout);
536 }
537
538 struct rpc_state {
539 const char *service_name;
540 char *service_url;
541 char *hdr_content_type;
542 char *hdr_accept;
543 char *protocol_header;
544 char *buf;
545 size_t alloc;
546 size_t len;
547 size_t pos;
548 int in;
549 int out;
550 int any_written;
551 unsigned gzip_request : 1;
552 unsigned initial_buffer : 1;
553
554 /*
555 * Whenever a pkt-line is read into buf, append the 4 characters
556 * denoting its length before appending the payload.
557 */
558 unsigned write_line_lengths : 1;
559
560 /*
561 * Used by rpc_out; initialize to 0. This is true if a flush has been
562 * read, but the corresponding line length (if write_line_lengths is
563 * true) and EOF have not been sent to libcurl. Since each flush marks
564 * the end of a request, each flush must be completely sent before any
565 * further reading occurs.
566 */
567 unsigned flush_read_but_not_sent : 1;
568 };
569
570 /*
571 * Appends the result of reading from rpc->out to the string represented by
572 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
573 * enough space, 0 otherwise.
574 *
575 * If rpc->write_line_lengths is true, appends the line length as a 4-byte
576 * hexadecimal string before appending the result described above.
577 *
578 * Writes the total number of bytes appended into appended.
579 */
580 static int rpc_read_from_out(struct rpc_state *rpc, int options,
581 size_t *appended,
582 enum packet_read_status *status) {
583 size_t left;
584 char *buf;
585 int pktlen_raw;
586
587 if (rpc->write_line_lengths) {
588 left = rpc->alloc - rpc->len - 4;
589 buf = rpc->buf + rpc->len + 4;
590 } else {
591 left = rpc->alloc - rpc->len;
592 buf = rpc->buf + rpc->len;
593 }
594
595 if (left < LARGE_PACKET_MAX)
596 return 0;
597
598 *status = packet_read_with_status(rpc->out, NULL, NULL, buf,
599 left, &pktlen_raw, options);
600 if (*status != PACKET_READ_EOF) {
601 *appended = pktlen_raw + (rpc->write_line_lengths ? 4 : 0);
602 rpc->len += *appended;
603 }
604
605 if (rpc->write_line_lengths) {
606 switch (*status) {
607 case PACKET_READ_EOF:
608 if (!(options & PACKET_READ_GENTLE_ON_EOF))
609 die(_("shouldn't have EOF when not gentle on EOF"));
610 break;
611 case PACKET_READ_NORMAL:
612 set_packet_header(buf - 4, *appended);
613 break;
614 case PACKET_READ_DELIM:
615 memcpy(buf - 4, "0001", 4);
616 break;
617 case PACKET_READ_FLUSH:
618 memcpy(buf - 4, "0000", 4);
619 break;
620 }
621 }
622
623 return 1;
624 }
625
626 static size_t rpc_out(void *ptr, size_t eltsize,
627 size_t nmemb, void *buffer_)
628 {
629 size_t max = eltsize * nmemb;
630 struct rpc_state *rpc = buffer_;
631 size_t avail = rpc->len - rpc->pos;
632 enum packet_read_status status;
633
634 if (!avail) {
635 rpc->initial_buffer = 0;
636 rpc->len = 0;
637 rpc->pos = 0;
638 if (!rpc->flush_read_but_not_sent) {
639 if (!rpc_read_from_out(rpc, 0, &avail, &status))
640 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
641 if (status == PACKET_READ_FLUSH)
642 rpc->flush_read_but_not_sent = 1;
643 }
644 /*
645 * If flush_read_but_not_sent is true, we have already read one
646 * full request but have not fully sent it + EOF, which is why
647 * we need to refrain from reading.
648 */
649 }
650 if (rpc->flush_read_but_not_sent) {
651 if (!avail) {
652 /*
653 * The line length either does not need to be sent at
654 * all or has already been completely sent. Now we can
655 * return 0, indicating EOF, meaning that the flush has
656 * been fully sent.
657 */
658 rpc->flush_read_but_not_sent = 0;
659 return 0;
660 }
661 /*
662 * If avail is non-zerp, the line length for the flush still
663 * hasn't been fully sent. Proceed with sending the line
664 * length.
665 */
666 }
667
668 if (max < avail)
669 avail = max;
670 memcpy(ptr, rpc->buf + rpc->pos, avail);
671 rpc->pos += avail;
672 return avail;
673 }
674
675 #ifndef NO_CURL_IOCTL
676 static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
677 {
678 struct rpc_state *rpc = clientp;
679
680 switch (cmd) {
681 case CURLIOCMD_NOP:
682 return CURLIOE_OK;
683
684 case CURLIOCMD_RESTARTREAD:
685 if (rpc->initial_buffer) {
686 rpc->pos = 0;
687 return CURLIOE_OK;
688 }
689 error(_("unable to rewind rpc post data - try increasing http.postBuffer"));
690 return CURLIOE_FAILRESTART;
691
692 default:
693 return CURLIOE_UNKNOWNCMD;
694 }
695 }
696 #endif
697
698 struct rpc_in_data {
699 struct rpc_state *rpc;
700 struct active_request_slot *slot;
701 };
702
703 /*
704 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
705 * from ptr.
706 */
707 static size_t rpc_in(char *ptr, size_t eltsize,
708 size_t nmemb, void *buffer_)
709 {
710 size_t size = eltsize * nmemb;
711 struct rpc_in_data *data = buffer_;
712 long response_code;
713
714 if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
715 &response_code) != CURLE_OK)
716 return size;
717 if (response_code >= 300)
718 return size;
719 if (size)
720 data->rpc->any_written = 1;
721 write_or_die(data->rpc->in, ptr, size);
722 return size;
723 }
724
725 static int run_slot(struct active_request_slot *slot,
726 struct slot_results *results)
727 {
728 int err;
729 struct slot_results results_buf;
730
731 if (!results)
732 results = &results_buf;
733
734 err = run_one_slot(slot, results);
735
736 if (err != HTTP_OK && err != HTTP_REAUTH) {
737 struct strbuf msg = STRBUF_INIT;
738 if (results->http_code && results->http_code != 200)
739 strbuf_addf(&msg, "HTTP %ld", results->http_code);
740 if (results->curl_result != CURLE_OK) {
741 if (msg.len)
742 strbuf_addch(&msg, ' ');
743 strbuf_addf(&msg, "curl %d", results->curl_result);
744 if (curl_errorstr[0]) {
745 strbuf_addch(&msg, ' ');
746 strbuf_addstr(&msg, curl_errorstr);
747 }
748 }
749 error(_("RPC failed; %s"), msg.buf);
750 strbuf_release(&msg);
751 }
752
753 return err;
754 }
755
756 static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
757 {
758 struct active_request_slot *slot;
759 struct curl_slist *headers = http_copy_default_headers();
760 struct strbuf buf = STRBUF_INIT;
761 int err;
762
763 slot = get_active_slot();
764
765 headers = curl_slist_append(headers, rpc->hdr_content_type);
766 headers = curl_slist_append(headers, rpc->hdr_accept);
767
768 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
769 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
770 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
771 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
772 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
773 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
774 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
775 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
776 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
777
778 err = run_slot(slot, results);
779
780 curl_slist_free_all(headers);
781 strbuf_release(&buf);
782 return err;
783 }
784
785 static curl_off_t xcurl_off_t(size_t len)
786 {
787 uintmax_t size = len;
788 if (size > maximum_signed_value_of_type(curl_off_t))
789 die(_("cannot handle pushes this big"));
790 return (curl_off_t)size;
791 }
792
793 /*
794 * If flush_received is true, do not attempt to read any more; just use what's
795 * in rpc->buf.
796 */
797 static int post_rpc(struct rpc_state *rpc, int flush_received)
798 {
799 struct active_request_slot *slot;
800 struct curl_slist *headers = http_copy_default_headers();
801 int use_gzip = rpc->gzip_request;
802 char *gzip_body = NULL;
803 size_t gzip_size = 0;
804 int err, large_request = 0;
805 int needs_100_continue = 0;
806 struct rpc_in_data rpc_in_data;
807
808 /* Try to load the entire request, if we can fit it into the
809 * allocated buffer space we can use HTTP/1.0 and avoid the
810 * chunked encoding mess.
811 */
812 if (!flush_received) {
813 while (1) {
814 size_t n;
815 enum packet_read_status status;
816
817 if (!rpc_read_from_out(rpc, 0, &n, &status)) {
818 large_request = 1;
819 use_gzip = 0;
820 break;
821 }
822 if (status == PACKET_READ_FLUSH)
823 break;
824 }
825 }
826
827 if (large_request) {
828 struct slot_results results;
829
830 do {
831 err = probe_rpc(rpc, &results);
832 if (err == HTTP_REAUTH)
833 credential_fill(&http_auth);
834 } while (err == HTTP_REAUTH);
835 if (err != HTTP_OK)
836 return -1;
837
838 if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
839 needs_100_continue = 1;
840 }
841
842 headers = curl_slist_append(headers, rpc->hdr_content_type);
843 headers = curl_slist_append(headers, rpc->hdr_accept);
844 headers = curl_slist_append(headers, needs_100_continue ?
845 "Expect: 100-continue" : "Expect:");
846
847 /* Add the extra Git-Protocol header */
848 if (rpc->protocol_header)
849 headers = curl_slist_append(headers, rpc->protocol_header);
850
851 retry:
852 slot = get_active_slot();
853
854 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
855 curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
856 curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
857 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
858
859 if (large_request) {
860 /* The request body is large and the size cannot be predicted.
861 * We must use chunked encoding to send it.
862 */
863 headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
864 rpc->initial_buffer = 1;
865 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
866 curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
867 #ifndef NO_CURL_IOCTL
868 curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
869 curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
870 #endif
871 if (options.verbosity > 1) {
872 fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
873 fflush(stderr);
874 }
875
876 } else if (gzip_body) {
877 /*
878 * If we are looping to retry authentication, then the previous
879 * run will have set up the headers and gzip buffer already,
880 * and we just need to send it.
881 */
882 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
883 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
884
885 } else if (use_gzip && 1024 < rpc->len) {
886 /* The client backend isn't giving us compressed data so
887 * we can try to deflate it ourselves, this may save on
888 * the transfer time.
889 */
890 git_zstream stream;
891 int ret;
892
893 git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
894 gzip_size = git_deflate_bound(&stream, rpc->len);
895 gzip_body = xmalloc(gzip_size);
896
897 stream.next_in = (unsigned char *)rpc->buf;
898 stream.avail_in = rpc->len;
899 stream.next_out = (unsigned char *)gzip_body;
900 stream.avail_out = gzip_size;
901
902 ret = git_deflate(&stream, Z_FINISH);
903 if (ret != Z_STREAM_END)
904 die(_("cannot deflate request; zlib deflate error %d"), ret);
905
906 ret = git_deflate_end_gently(&stream);
907 if (ret != Z_OK)
908 die(_("cannot deflate request; zlib end error %d"), ret);
909
910 gzip_size = stream.total_out;
911
912 headers = curl_slist_append(headers, "Content-Encoding: gzip");
913 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
914 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
915
916 if (options.verbosity > 1) {
917 fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
918 rpc->service_name,
919 (unsigned long)rpc->len, (unsigned long)gzip_size);
920 fflush(stderr);
921 }
922 } else {
923 /* We know the complete request size in advance, use the
924 * more normal Content-Length approach.
925 */
926 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
927 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
928 if (options.verbosity > 1) {
929 fprintf(stderr, "POST %s (%lu bytes)\n",
930 rpc->service_name, (unsigned long)rpc->len);
931 fflush(stderr);
932 }
933 }
934
935 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
936 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
937 rpc_in_data.rpc = rpc;
938 rpc_in_data.slot = slot;
939 curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
940 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
941
942
943 rpc->any_written = 0;
944 err = run_slot(slot, NULL);
945 if (err == HTTP_REAUTH && !large_request) {
946 credential_fill(&http_auth);
947 goto retry;
948 }
949 if (err != HTTP_OK)
950 err = -1;
951
952 if (!rpc->any_written)
953 err = -1;
954
955 curl_slist_free_all(headers);
956 free(gzip_body);
957 return err;
958 }
959
960 static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
961 const char **client_argv, const struct strbuf *preamble,
962 struct strbuf *rpc_result)
963 {
964 const char *svc = rpc->service_name;
965 struct strbuf buf = STRBUF_INIT;
966 struct child_process client = CHILD_PROCESS_INIT;
967 int err = 0;
968
969 client.in = -1;
970 client.out = -1;
971 client.git_cmd = 1;
972 client.argv = client_argv;
973 if (start_command(&client))
974 exit(1);
975 write_or_die(client.in, preamble->buf, preamble->len);
976 if (heads)
977 write_or_die(client.in, heads->buf, heads->len);
978
979 rpc->alloc = http_post_buffer;
980 rpc->buf = xmalloc(rpc->alloc);
981 rpc->in = client.in;
982 rpc->out = client.out;
983
984 strbuf_addf(&buf, "%s%s", url.buf, svc);
985 rpc->service_url = strbuf_detach(&buf, NULL);
986
987 strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
988 rpc->hdr_content_type = strbuf_detach(&buf, NULL);
989
990 strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
991 rpc->hdr_accept = strbuf_detach(&buf, NULL);
992
993 if (get_protocol_http_header(heads->version, &buf))
994 rpc->protocol_header = strbuf_detach(&buf, NULL);
995 else
996 rpc->protocol_header = NULL;
997
998 while (!err) {
999 int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
1000 if (!n)
1001 break;
1002 rpc->pos = 0;
1003 rpc->len = n;
1004 err |= post_rpc(rpc, 0);
1005 }
1006
1007 close(client.in);
1008 client.in = -1;
1009 if (!err) {
1010 strbuf_read(rpc_result, client.out, 0);
1011 } else {
1012 char buf[4096];
1013 for (;;)
1014 if (xread(client.out, buf, sizeof(buf)) <= 0)
1015 break;
1016 }
1017
1018 close(client.out);
1019 client.out = -1;
1020
1021 err |= finish_command(&client);
1022 free(rpc->service_url);
1023 free(rpc->hdr_content_type);
1024 free(rpc->hdr_accept);
1025 free(rpc->protocol_header);
1026 free(rpc->buf);
1027 strbuf_release(&buf);
1028 return err;
1029 }
1030
1031 static int fetch_dumb(int nr_heads, struct ref **to_fetch)
1032 {
1033 struct walker *walker;
1034 char **targets;
1035 int ret, i;
1036
1037 ALLOC_ARRAY(targets, nr_heads);
1038 if (options.depth || options.deepen_since)
1039 die(_("dumb http transport does not support shallow capabilities"));
1040 for (i = 0; i < nr_heads; i++)
1041 targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
1042
1043 walker = get_http_walker(url.buf);
1044 walker->get_verbosely = options.verbosity >= 3;
1045 walker->get_progress = options.progress;
1046 walker->get_recover = 0;
1047 ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
1048 walker_free(walker);
1049
1050 for (i = 0; i < nr_heads; i++)
1051 free(targets[i]);
1052 free(targets);
1053
1054 return ret ? error(_("fetch failed.")) : 0;
1055 }
1056
1057 static int fetch_git(struct discovery *heads,
1058 int nr_heads, struct ref **to_fetch)
1059 {
1060 struct rpc_state rpc;
1061 struct strbuf preamble = STRBUF_INIT;
1062 int i, err;
1063 struct argv_array args = ARGV_ARRAY_INIT;
1064 struct strbuf rpc_result = STRBUF_INIT;
1065
1066 argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
1067 "--stdin", "--lock-pack", NULL);
1068 if (options.followtags)
1069 argv_array_push(&args, "--include-tag");
1070 if (options.thin)
1071 argv_array_push(&args, "--thin");
1072 if (options.verbosity >= 3)
1073 argv_array_pushl(&args, "-v", "-v", NULL);
1074 if (options.check_self_contained_and_connected)
1075 argv_array_push(&args, "--check-self-contained-and-connected");
1076 if (options.cloning)
1077 argv_array_push(&args, "--cloning");
1078 if (options.update_shallow)
1079 argv_array_push(&args, "--update-shallow");
1080 if (!options.progress)
1081 argv_array_push(&args, "--no-progress");
1082 if (options.depth)
1083 argv_array_pushf(&args, "--depth=%lu", options.depth);
1084 if (options.deepen_since)
1085 argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
1086 for (i = 0; i < options.deepen_not.nr; i++)
1087 argv_array_pushf(&args, "--shallow-exclude=%s",
1088 options.deepen_not.items[i].string);
1089 if (options.deepen_relative && options.depth)
1090 argv_array_push(&args, "--deepen-relative");
1091 if (options.from_promisor)
1092 argv_array_push(&args, "--from-promisor");
1093 if (options.no_dependents)
1094 argv_array_push(&args, "--no-dependents");
1095 if (options.filter)
1096 argv_array_pushf(&args, "--filter=%s", options.filter);
1097 argv_array_push(&args, url.buf);
1098
1099 for (i = 0; i < nr_heads; i++) {
1100 struct ref *ref = to_fetch[i];
1101 if (!*ref->name)
1102 die(_("cannot fetch by sha1 over smart http"));
1103 packet_buf_write(&preamble, "%s %s\n",
1104 oid_to_hex(&ref->old_oid), ref->name);
1105 }
1106 packet_buf_flush(&preamble);
1107
1108 memset(&rpc, 0, sizeof(rpc));
1109 rpc.service_name = "git-upload-pack",
1110 rpc.gzip_request = 1;
1111
1112 err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1113 if (rpc_result.len)
1114 write_or_die(1, rpc_result.buf, rpc_result.len);
1115 strbuf_release(&rpc_result);
1116 strbuf_release(&preamble);
1117 argv_array_clear(&args);
1118 return err;
1119 }
1120
1121 static int fetch(int nr_heads, struct ref **to_fetch)
1122 {
1123 struct discovery *d = discover_refs("git-upload-pack", 0);
1124 if (d->proto_git)
1125 return fetch_git(d, nr_heads, to_fetch);
1126 else
1127 return fetch_dumb(nr_heads, to_fetch);
1128 }
1129
1130 static void parse_fetch(struct strbuf *buf)
1131 {
1132 struct ref **to_fetch = NULL;
1133 struct ref *list_head = NULL;
1134 struct ref **list = &list_head;
1135 int alloc_heads = 0, nr_heads = 0;
1136
1137 do {
1138 const char *p;
1139 if (skip_prefix(buf->buf, "fetch ", &p)) {
1140 const char *name;
1141 struct ref *ref;
1142 struct object_id old_oid;
1143 const char *q;
1144
1145 if (parse_oid_hex(p, &old_oid, &q))
1146 die(_("protocol error: expected sha/ref, got '%s'"), p);
1147 if (*q == ' ')
1148 name = q + 1;
1149 else if (!*q)
1150 name = "";
1151 else
1152 die(_("protocol error: expected sha/ref, got '%s'"), p);
1153
1154 ref = alloc_ref(name);
1155 oidcpy(&ref->old_oid, &old_oid);
1156
1157 *list = ref;
1158 list = &ref->next;
1159
1160 ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1161 to_fetch[nr_heads++] = ref;
1162 }
1163 else
1164 die(_("http transport does not support %s"), buf->buf);
1165
1166 strbuf_reset(buf);
1167 if (strbuf_getline_lf(buf, stdin) == EOF)
1168 return;
1169 if (!*buf->buf)
1170 break;
1171 } while (1);
1172
1173 if (fetch(nr_heads, to_fetch))
1174 exit(128); /* error already reported */
1175 free_refs(list_head);
1176 free(to_fetch);
1177
1178 printf("\n");
1179 fflush(stdout);
1180 strbuf_reset(buf);
1181 }
1182
1183 static int push_dav(int nr_spec, const char **specs)
1184 {
1185 struct child_process child = CHILD_PROCESS_INIT;
1186 size_t i;
1187
1188 child.git_cmd = 1;
1189 argv_array_push(&child.args, "http-push");
1190 argv_array_push(&child.args, "--helper-status");
1191 if (options.dry_run)
1192 argv_array_push(&child.args, "--dry-run");
1193 if (options.verbosity > 1)
1194 argv_array_push(&child.args, "--verbose");
1195 argv_array_push(&child.args, url.buf);
1196 for (i = 0; i < nr_spec; i++)
1197 argv_array_push(&child.args, specs[i]);
1198
1199 if (run_command(&child))
1200 die(_("git-http-push failed"));
1201 return 0;
1202 }
1203
1204 static int push_git(struct discovery *heads, int nr_spec, const char **specs)
1205 {
1206 struct rpc_state rpc;
1207 int i, err;
1208 struct argv_array args;
1209 struct string_list_item *cas_option;
1210 struct strbuf preamble = STRBUF_INIT;
1211 struct strbuf rpc_result = STRBUF_INIT;
1212
1213 argv_array_init(&args);
1214 argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1215 NULL);
1216
1217 if (options.thin)
1218 argv_array_push(&args, "--thin");
1219 if (options.dry_run)
1220 argv_array_push(&args, "--dry-run");
1221 if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1222 argv_array_push(&args, "--signed=yes");
1223 else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1224 argv_array_push(&args, "--signed=if-asked");
1225 if (options.atomic)
1226 argv_array_push(&args, "--atomic");
1227 if (options.verbosity == 0)
1228 argv_array_push(&args, "--quiet");
1229 else if (options.verbosity > 1)
1230 argv_array_push(&args, "--verbose");
1231 for (i = 0; i < options.push_options.nr; i++)
1232 argv_array_pushf(&args, "--push-option=%s",
1233 options.push_options.items[i].string);
1234 argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1235 for_each_string_list_item(cas_option, &cas_options)
1236 argv_array_push(&args, cas_option->string);
1237 argv_array_push(&args, url.buf);
1238
1239 argv_array_push(&args, "--stdin");
1240 for (i = 0; i < nr_spec; i++)
1241 packet_buf_write(&preamble, "%s\n", specs[i]);
1242 packet_buf_flush(&preamble);
1243
1244 memset(&rpc, 0, sizeof(rpc));
1245 rpc.service_name = "git-receive-pack",
1246
1247 err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1248 if (rpc_result.len)
1249 write_or_die(1, rpc_result.buf, rpc_result.len);
1250 strbuf_release(&rpc_result);
1251 strbuf_release(&preamble);
1252 argv_array_clear(&args);
1253 return err;
1254 }
1255
1256 static int push(int nr_spec, const char **specs)
1257 {
1258 struct discovery *heads = discover_refs("git-receive-pack", 1);
1259 int ret;
1260
1261 if (heads->proto_git)
1262 ret = push_git(heads, nr_spec, specs);
1263 else
1264 ret = push_dav(nr_spec, specs);
1265 free_discovery(heads);
1266 return ret;
1267 }
1268
1269 static void parse_push(struct strbuf *buf)
1270 {
1271 struct argv_array specs = ARGV_ARRAY_INIT;
1272 int ret;
1273
1274 do {
1275 const char *arg;
1276 if (skip_prefix(buf->buf, "push ", &arg))
1277 argv_array_push(&specs, arg);
1278 else
1279 die(_("http transport does not support %s"), buf->buf);
1280
1281 strbuf_reset(buf);
1282 if (strbuf_getline_lf(buf, stdin) == EOF)
1283 goto free_specs;
1284 if (!*buf->buf)
1285 break;
1286 } while (1);
1287
1288 ret = push(specs.argc, specs.argv);
1289 printf("\n");
1290 fflush(stdout);
1291
1292 if (ret)
1293 exit(128); /* error already reported */
1294
1295 free_specs:
1296 argv_array_clear(&specs);
1297 }
1298
1299 static int stateless_connect(const char *service_name)
1300 {
1301 struct discovery *discover;
1302 struct rpc_state rpc;
1303 struct strbuf buf = STRBUF_INIT;
1304
1305 /*
1306 * Run the info/refs request and see if the server supports protocol
1307 * v2. If and only if the server supports v2 can we successfully
1308 * establish a stateless connection, otherwise we need to tell the
1309 * client to fallback to using other transport helper functions to
1310 * complete their request.
1311 */
1312 discover = discover_refs(service_name, 0);
1313 if (discover->version != protocol_v2) {
1314 printf("fallback\n");
1315 fflush(stdout);
1316 return -1;
1317 } else {
1318 /* Stateless Connection established */
1319 printf("\n");
1320 fflush(stdout);
1321 }
1322
1323 rpc.service_name = service_name;
1324 rpc.service_url = xstrfmt("%s%s", url.buf, rpc.service_name);
1325 rpc.hdr_content_type = xstrfmt("Content-Type: application/x-%s-request", rpc.service_name);
1326 rpc.hdr_accept = xstrfmt("Accept: application/x-%s-result", rpc.service_name);
1327 if (get_protocol_http_header(discover->version, &buf)) {
1328 rpc.protocol_header = strbuf_detach(&buf, NULL);
1329 } else {
1330 rpc.protocol_header = NULL;
1331 strbuf_release(&buf);
1332 }
1333 rpc.buf = xmalloc(http_post_buffer);
1334 rpc.alloc = http_post_buffer;
1335 rpc.len = 0;
1336 rpc.pos = 0;
1337 rpc.in = 1;
1338 rpc.out = 0;
1339 rpc.any_written = 0;
1340 rpc.gzip_request = 1;
1341 rpc.initial_buffer = 0;
1342 rpc.write_line_lengths = 1;
1343 rpc.flush_read_but_not_sent = 0;
1344
1345 /*
1346 * Dump the capability listing that we got from the server earlier
1347 * during the info/refs request.
1348 */
1349 write_or_die(rpc.in, discover->buf, discover->len);
1350
1351 /* Until we see EOF keep sending POSTs */
1352 while (1) {
1353 size_t avail;
1354 enum packet_read_status status;
1355
1356 if (!rpc_read_from_out(&rpc, PACKET_READ_GENTLE_ON_EOF, &avail,
1357 &status))
1358 BUG("The entire rpc->buf should be larger than LARGE_PACKET_MAX");
1359 if (status == PACKET_READ_EOF)
1360 break;
1361 if (post_rpc(&rpc, status == PACKET_READ_FLUSH))
1362 /* We would have an err here */
1363 break;
1364 /* Reset the buffer for next request */
1365 rpc.len = 0;
1366 }
1367
1368 free(rpc.service_url);
1369 free(rpc.hdr_content_type);
1370 free(rpc.hdr_accept);
1371 free(rpc.protocol_header);
1372 free(rpc.buf);
1373 strbuf_release(&buf);
1374
1375 return 0;
1376 }
1377
1378 int cmd_main(int argc, const char **argv)
1379 {
1380 struct strbuf buf = STRBUF_INIT;
1381 int nongit;
1382
1383 setup_git_directory_gently(&nongit);
1384 if (argc < 2) {
1385 error(_("remote-curl: usage: git remote-curl <remote> [<url>]"));
1386 return 1;
1387 }
1388
1389 options.verbosity = 1;
1390 options.progress = !!isatty(2);
1391 options.thin = 1;
1392 string_list_init(&options.deepen_not, 1);
1393 string_list_init(&options.push_options, 1);
1394
1395 /*
1396 * Just report "remote-curl" here (folding all the various aliases
1397 * ("git-remote-http", "git-remote-https", and etc.) here since they
1398 * are all just copies of the same actual executable.
1399 */
1400 trace2_cmd_name("remote-curl");
1401
1402 remote = remote_get(argv[1]);
1403
1404 if (argc > 2) {
1405 end_url_with_slash(&url, argv[2]);
1406 } else {
1407 end_url_with_slash(&url, remote->url[0]);
1408 }
1409
1410 http_init(remote, url.buf, 0);
1411
1412 do {
1413 const char *arg;
1414
1415 if (strbuf_getline_lf(&buf, stdin) == EOF) {
1416 if (ferror(stdin))
1417 error(_("remote-curl: error reading command stream from git"));
1418 return 1;
1419 }
1420 if (buf.len == 0)
1421 break;
1422 if (starts_with(buf.buf, "fetch ")) {
1423 if (nongit)
1424 die(_("remote-curl: fetch attempted without a local repo"));
1425 parse_fetch(&buf);
1426
1427 } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1428 int for_push = !!strstr(buf.buf + 4, "for-push");
1429 output_refs(get_refs(for_push));
1430
1431 } else if (starts_with(buf.buf, "push ")) {
1432 parse_push(&buf);
1433
1434 } else if (skip_prefix(buf.buf, "option ", &arg)) {
1435 char *value = strchr(arg, ' ');
1436 int result;
1437
1438 if (value)
1439 *value++ = '\0';
1440 else
1441 value = "true";
1442
1443 result = set_option(arg, value);
1444 if (!result)
1445 printf("ok\n");
1446 else if (result < 0)
1447 printf("error invalid value\n");
1448 else
1449 printf("unsupported\n");
1450 fflush(stdout);
1451
1452 } else if (!strcmp(buf.buf, "capabilities")) {
1453 printf("stateless-connect\n");
1454 printf("fetch\n");
1455 printf("option\n");
1456 printf("push\n");
1457 printf("check-connectivity\n");
1458 printf("object-format\n");
1459 printf("\n");
1460 fflush(stdout);
1461 } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1462 if (!stateless_connect(arg))
1463 break;
1464 } else {
1465 error(_("remote-curl: unknown command '%s' from git"), buf.buf);
1466 return 1;
1467 }
1468 strbuf_reset(&buf);
1469 } while (1);
1470
1471 http_cleanup();
1472
1473 return 0;
1474 }