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