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