remote-curl.con commit remote-curl: refactor reading into rpc_state's buf (78ad917)
   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                           PACKET_READ_DIE_ON_ERR_PACKET);
 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
 232static 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                                    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
 281static 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
 294static 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
 321static 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
 334static void check_smart_http(struct discovery *d, const char *service,
 335                             struct strbuf *type)
 336{
 337        const char *p;
 338        struct packet_reader reader;
 339
 340        /*
 341         * If we don't see x-$service-advertisement, then it's not smart-http.
 342         * But once we do, we commit to it and assume any other protocol
 343         * violations are hard errors.
 344         */
 345        if (!skip_prefix(type->buf, "application/x-", &p) ||
 346            !skip_prefix(p, service, &p) ||
 347            strcmp(p, "-advertisement"))
 348                return;
 349
 350        packet_reader_init(&reader, -1, d->buf, d->len,
 351                           PACKET_READ_CHOMP_NEWLINE |
 352                           PACKET_READ_DIE_ON_ERR_PACKET);
 353        if (packet_reader_read(&reader) != PACKET_READ_NORMAL)
 354                die("invalid server response; expected service, got flush packet");
 355
 356        if (skip_prefix(reader.line, "# service=", &p) && !strcmp(p, service)) {
 357                /*
 358                 * The header can include additional metadata lines, up
 359                 * until a packet flush marker.  Ignore these now, but
 360                 * in the future we might start to scan them.
 361                 */
 362                for (;;) {
 363                        packet_reader_read(&reader);
 364                        if (reader.pktlen <= 0) {
 365                                break;
 366                        }
 367                }
 368
 369                /*
 370                 * v0 smart http; callers expect us to soak up the
 371                 * service and header packets
 372                 */
 373                d->buf = reader.src_buffer;
 374                d->len = reader.src_len;
 375                d->proto_git = 1;
 376
 377        } else if (!strcmp(reader.line, "version 2")) {
 378                /*
 379                 * v2 smart http; do not consume version packet, which will
 380                 * be handled elsewhere.
 381                 */
 382                d->proto_git = 1;
 383
 384        } else {
 385                die("invalid server response; got '%s'", reader.line);
 386        }
 387}
 388
 389static struct discovery *discover_refs(const char *service, int for_push)
 390{
 391        struct strbuf type = STRBUF_INIT;
 392        struct strbuf charset = STRBUF_INIT;
 393        struct strbuf buffer = STRBUF_INIT;
 394        struct strbuf refs_url = STRBUF_INIT;
 395        struct strbuf effective_url = STRBUF_INIT;
 396        struct strbuf protocol_header = STRBUF_INIT;
 397        struct string_list extra_headers = STRING_LIST_INIT_DUP;
 398        struct discovery *last = last_discovery;
 399        int http_ret, maybe_smart = 0;
 400        struct http_get_options http_options;
 401        enum protocol_version version = get_protocol_version_config();
 402
 403        if (last && !strcmp(service, last->service))
 404                return last;
 405        free_discovery(last);
 406
 407        strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
 408        if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
 409             git_env_bool("GIT_SMART_HTTP", 1)) {
 410                maybe_smart = 1;
 411                if (!strchr(url.buf, '?'))
 412                        strbuf_addch(&refs_url, '?');
 413                else
 414                        strbuf_addch(&refs_url, '&');
 415                strbuf_addf(&refs_url, "service=%s", service);
 416        }
 417
 418        /*
 419         * NEEDSWORK: If we are trying to use protocol v2 and we are planning
 420         * to perform a push, then fallback to v0 since the client doesn't know
 421         * how to push yet using v2.
 422         */
 423        if (version == protocol_v2 && !strcmp("git-receive-pack", service))
 424                version = protocol_v0;
 425
 426        /* Add the extra Git-Protocol header */
 427        if (get_protocol_http_header(version, &protocol_header))
 428                string_list_append(&extra_headers, protocol_header.buf);
 429
 430        memset(&http_options, 0, sizeof(http_options));
 431        http_options.content_type = &type;
 432        http_options.charset = &charset;
 433        http_options.effective_url = &effective_url;
 434        http_options.base_url = &url;
 435        http_options.extra_headers = &extra_headers;
 436        http_options.initial_request = 1;
 437        http_options.no_cache = 1;
 438
 439        http_ret = http_get_strbuf(refs_url.buf, &buffer, &http_options);
 440        switch (http_ret) {
 441        case HTTP_OK:
 442                break;
 443        case HTTP_MISSING_TARGET:
 444                show_http_message(&type, &charset, &buffer);
 445                die("repository '%s' not found", url.buf);
 446        case HTTP_NOAUTH:
 447                show_http_message(&type, &charset, &buffer);
 448                die("Authentication failed for '%s'", url.buf);
 449        default:
 450                show_http_message(&type, &charset, &buffer);
 451                die("unable to access '%s': %s", url.buf, curl_errorstr);
 452        }
 453
 454        if (options.verbosity && !starts_with(refs_url.buf, url.buf))
 455                warning(_("redirecting to %s"), url.buf);
 456
 457        last= xcalloc(1, sizeof(*last_discovery));
 458        last->service = xstrdup(service);
 459        last->buf_alloc = strbuf_detach(&buffer, &last->len);
 460        last->buf = last->buf_alloc;
 461
 462        if (maybe_smart)
 463                check_smart_http(last, service, &type);
 464
 465        if (last->proto_git)
 466                last->refs = parse_git_refs(last, for_push);
 467        else
 468                last->refs = parse_info_refs(last);
 469
 470        strbuf_release(&refs_url);
 471        strbuf_release(&type);
 472        strbuf_release(&charset);
 473        strbuf_release(&effective_url);
 474        strbuf_release(&buffer);
 475        strbuf_release(&protocol_header);
 476        string_list_clear(&extra_headers, 0);
 477        last_discovery = last;
 478        return last;
 479}
 480
 481static struct ref *get_refs(int for_push)
 482{
 483        struct discovery *heads;
 484
 485        if (for_push)
 486                heads = discover_refs("git-receive-pack", for_push);
 487        else
 488                heads = discover_refs("git-upload-pack", for_push);
 489
 490        return heads->refs;
 491}
 492
 493static void output_refs(struct ref *refs)
 494{
 495        struct ref *posn;
 496        for (posn = refs; posn; posn = posn->next) {
 497                if (posn->symref)
 498                        printf("@%s %s\n", posn->symref, posn->name);
 499                else
 500                        printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
 501        }
 502        printf("\n");
 503        fflush(stdout);
 504}
 505
 506struct rpc_state {
 507        const char *service_name;
 508        char *service_url;
 509        char *hdr_content_type;
 510        char *hdr_accept;
 511        char *protocol_header;
 512        char *buf;
 513        size_t alloc;
 514        size_t len;
 515        size_t pos;
 516        int in;
 517        int out;
 518        int any_written;
 519        unsigned gzip_request : 1;
 520        unsigned initial_buffer : 1;
 521};
 522
 523/*
 524 * Appends the result of reading from rpc->out to the string represented by
 525 * rpc->buf and rpc->len if there is enough space. Returns 1 if there was
 526 * enough space, 0 otherwise.
 527 *
 528 * Writes the number of bytes appended into appended.
 529 */
 530static int rpc_read_from_out(struct rpc_state *rpc, size_t *appended) {
 531        size_t left = rpc->alloc - rpc->len;
 532        char *buf = rpc->buf + rpc->len;
 533
 534        if (left < LARGE_PACKET_MAX)
 535                return 0;
 536
 537        *appended = packet_read(rpc->out, NULL, NULL, buf, left, 0);
 538        rpc->len += *appended;
 539        return 1;
 540}
 541
 542static size_t rpc_out(void *ptr, size_t eltsize,
 543                size_t nmemb, void *buffer_)
 544{
 545        size_t max = eltsize * nmemb;
 546        struct rpc_state *rpc = buffer_;
 547        size_t avail = rpc->len - rpc->pos;
 548
 549        if (!avail) {
 550                rpc->initial_buffer = 0;
 551                rpc->len = 0;
 552                if (!rpc_read_from_out(rpc, &avail))
 553                        BUG("The entire rpc->buf should be larger than LARGE_PACKET_DATA_MAX");
 554                if (!avail)
 555                        return 0;
 556                rpc->pos = 0;
 557        }
 558
 559        if (max < avail)
 560                avail = max;
 561        memcpy(ptr, rpc->buf + rpc->pos, avail);
 562        rpc->pos += avail;
 563        return avail;
 564}
 565
 566#ifndef NO_CURL_IOCTL
 567static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
 568{
 569        struct rpc_state *rpc = clientp;
 570
 571        switch (cmd) {
 572        case CURLIOCMD_NOP:
 573                return CURLIOE_OK;
 574
 575        case CURLIOCMD_RESTARTREAD:
 576                if (rpc->initial_buffer) {
 577                        rpc->pos = 0;
 578                        return CURLIOE_OK;
 579                }
 580                error("unable to rewind rpc post data - try increasing http.postBuffer");
 581                return CURLIOE_FAILRESTART;
 582
 583        default:
 584                return CURLIOE_UNKNOWNCMD;
 585        }
 586}
 587#endif
 588
 589struct rpc_in_data {
 590        struct rpc_state *rpc;
 591        struct active_request_slot *slot;
 592};
 593
 594/*
 595 * A callback for CURLOPT_WRITEFUNCTION. The return value is the bytes consumed
 596 * from ptr.
 597 */
 598static size_t rpc_in(char *ptr, size_t eltsize,
 599                size_t nmemb, void *buffer_)
 600{
 601        size_t size = eltsize * nmemb;
 602        struct rpc_in_data *data = buffer_;
 603        long response_code;
 604
 605        if (curl_easy_getinfo(data->slot->curl, CURLINFO_RESPONSE_CODE,
 606                              &response_code) != CURLE_OK)
 607                return size;
 608        if (response_code >= 300)
 609                return size;
 610        if (size)
 611                data->rpc->any_written = 1;
 612        write_or_die(data->rpc->in, ptr, size);
 613        return size;
 614}
 615
 616static int run_slot(struct active_request_slot *slot,
 617                    struct slot_results *results)
 618{
 619        int err;
 620        struct slot_results results_buf;
 621
 622        if (!results)
 623                results = &results_buf;
 624
 625        err = run_one_slot(slot, results);
 626
 627        if (err != HTTP_OK && err != HTTP_REAUTH) {
 628                struct strbuf msg = STRBUF_INIT;
 629                if (results->http_code && results->http_code != 200)
 630                        strbuf_addf(&msg, "HTTP %ld", results->http_code);
 631                if (results->curl_result != CURLE_OK) {
 632                        if (msg.len)
 633                                strbuf_addch(&msg, ' ');
 634                        strbuf_addf(&msg, "curl %d", results->curl_result);
 635                        if (curl_errorstr[0]) {
 636                                strbuf_addch(&msg, ' ');
 637                                strbuf_addstr(&msg, curl_errorstr);
 638                        }
 639                }
 640                error("RPC failed; %s", msg.buf);
 641                strbuf_release(&msg);
 642        }
 643
 644        return err;
 645}
 646
 647static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
 648{
 649        struct active_request_slot *slot;
 650        struct curl_slist *headers = http_copy_default_headers();
 651        struct strbuf buf = STRBUF_INIT;
 652        int err;
 653
 654        slot = get_active_slot();
 655
 656        headers = curl_slist_append(headers, rpc->hdr_content_type);
 657        headers = curl_slist_append(headers, rpc->hdr_accept);
 658
 659        curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
 660        curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
 661        curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
 662        curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
 663        curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
 664        curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
 665        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
 666        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 667        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
 668
 669        err = run_slot(slot, results);
 670
 671        curl_slist_free_all(headers);
 672        strbuf_release(&buf);
 673        return err;
 674}
 675
 676static curl_off_t xcurl_off_t(size_t len)
 677{
 678        uintmax_t size = len;
 679        if (size > maximum_signed_value_of_type(curl_off_t))
 680                die("cannot handle pushes this big");
 681        return (curl_off_t)size;
 682}
 683
 684static int post_rpc(struct rpc_state *rpc)
 685{
 686        struct active_request_slot *slot;
 687        struct curl_slist *headers = http_copy_default_headers();
 688        int use_gzip = rpc->gzip_request;
 689        char *gzip_body = NULL;
 690        size_t gzip_size = 0;
 691        int err, large_request = 0;
 692        int needs_100_continue = 0;
 693        struct rpc_in_data rpc_in_data;
 694
 695        /* Try to load the entire request, if we can fit it into the
 696         * allocated buffer space we can use HTTP/1.0 and avoid the
 697         * chunked encoding mess.
 698         */
 699        while (1) {
 700                size_t n;
 701
 702                if (!rpc_read_from_out(rpc, &n)) {
 703                        large_request = 1;
 704                        use_gzip = 0;
 705                        break;
 706                }
 707                if (!n)
 708                        break;
 709        }
 710
 711        if (large_request) {
 712                struct slot_results results;
 713
 714                do {
 715                        err = probe_rpc(rpc, &results);
 716                        if (err == HTTP_REAUTH)
 717                                credential_fill(&http_auth);
 718                } while (err == HTTP_REAUTH);
 719                if (err != HTTP_OK)
 720                        return -1;
 721
 722                if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
 723                        needs_100_continue = 1;
 724        }
 725
 726        headers = curl_slist_append(headers, rpc->hdr_content_type);
 727        headers = curl_slist_append(headers, rpc->hdr_accept);
 728        headers = curl_slist_append(headers, needs_100_continue ?
 729                "Expect: 100-continue" : "Expect:");
 730
 731        /* Add the extra Git-Protocol header */
 732        if (rpc->protocol_header)
 733                headers = curl_slist_append(headers, rpc->protocol_header);
 734
 735retry:
 736        slot = get_active_slot();
 737
 738        curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
 739        curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
 740        curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
 741        curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
 742
 743        if (large_request) {
 744                /* The request body is large and the size cannot be predicted.
 745                 * We must use chunked encoding to send it.
 746                 */
 747                headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
 748                rpc->initial_buffer = 1;
 749                curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
 750                curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
 751#ifndef NO_CURL_IOCTL
 752                curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
 753                curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
 754#endif
 755                if (options.verbosity > 1) {
 756                        fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
 757                        fflush(stderr);
 758                }
 759
 760        } else if (gzip_body) {
 761                /*
 762                 * If we are looping to retry authentication, then the previous
 763                 * run will have set up the headers and gzip buffer already,
 764                 * and we just need to send it.
 765                 */
 766                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
 767                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
 768
 769        } else if (use_gzip && 1024 < rpc->len) {
 770                /* The client backend isn't giving us compressed data so
 771                 * we can try to deflate it ourselves, this may save on
 772                 * the transfer time.
 773                 */
 774                git_zstream stream;
 775                int ret;
 776
 777                git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
 778                gzip_size = git_deflate_bound(&stream, rpc->len);
 779                gzip_body = xmalloc(gzip_size);
 780
 781                stream.next_in = (unsigned char *)rpc->buf;
 782                stream.avail_in = rpc->len;
 783                stream.next_out = (unsigned char *)gzip_body;
 784                stream.avail_out = gzip_size;
 785
 786                ret = git_deflate(&stream, Z_FINISH);
 787                if (ret != Z_STREAM_END)
 788                        die("cannot deflate request; zlib deflate error %d", ret);
 789
 790                ret = git_deflate_end_gently(&stream);
 791                if (ret != Z_OK)
 792                        die("cannot deflate request; zlib end error %d", ret);
 793
 794                gzip_size = stream.total_out;
 795
 796                headers = curl_slist_append(headers, "Content-Encoding: gzip");
 797                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
 798                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(gzip_size));
 799
 800                if (options.verbosity > 1) {
 801                        fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
 802                                rpc->service_name,
 803                                (unsigned long)rpc->len, (unsigned long)gzip_size);
 804                        fflush(stderr);
 805                }
 806        } else {
 807                /* We know the complete request size in advance, use the
 808                 * more normal Content-Length approach.
 809                 */
 810                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
 811                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE_LARGE, xcurl_off_t(rpc->len));
 812                if (options.verbosity > 1) {
 813                        fprintf(stderr, "POST %s (%lu bytes)\n",
 814                                rpc->service_name, (unsigned long)rpc->len);
 815                        fflush(stderr);
 816                }
 817        }
 818
 819        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
 820        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
 821        rpc_in_data.rpc = rpc;
 822        rpc_in_data.slot = slot;
 823        curl_easy_setopt(slot->curl, CURLOPT_FILE, &rpc_in_data);
 824        curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
 825
 826
 827        rpc->any_written = 0;
 828        err = run_slot(slot, NULL);
 829        if (err == HTTP_REAUTH && !large_request) {
 830                credential_fill(&http_auth);
 831                goto retry;
 832        }
 833        if (err != HTTP_OK)
 834                err = -1;
 835
 836        if (!rpc->any_written)
 837                err = -1;
 838
 839        curl_slist_free_all(headers);
 840        free(gzip_body);
 841        return err;
 842}
 843
 844static int rpc_service(struct rpc_state *rpc, struct discovery *heads,
 845                       const char **client_argv, const struct strbuf *preamble,
 846                       struct strbuf *rpc_result)
 847{
 848        const char *svc = rpc->service_name;
 849        struct strbuf buf = STRBUF_INIT;
 850        struct child_process client = CHILD_PROCESS_INIT;
 851        int err = 0;
 852
 853        client.in = -1;
 854        client.out = -1;
 855        client.git_cmd = 1;
 856        client.argv = client_argv;
 857        if (start_command(&client))
 858                exit(1);
 859        write_or_die(client.in, preamble->buf, preamble->len);
 860        if (heads)
 861                write_or_die(client.in, heads->buf, heads->len);
 862
 863        rpc->alloc = http_post_buffer;
 864        rpc->buf = xmalloc(rpc->alloc);
 865        rpc->in = client.in;
 866        rpc->out = client.out;
 867
 868        strbuf_addf(&buf, "%s%s", url.buf, svc);
 869        rpc->service_url = strbuf_detach(&buf, NULL);
 870
 871        strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
 872        rpc->hdr_content_type = strbuf_detach(&buf, NULL);
 873
 874        strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
 875        rpc->hdr_accept = strbuf_detach(&buf, NULL);
 876
 877        if (get_protocol_http_header(heads->version, &buf))
 878                rpc->protocol_header = strbuf_detach(&buf, NULL);
 879        else
 880                rpc->protocol_header = NULL;
 881
 882        while (!err) {
 883                int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
 884                if (!n)
 885                        break;
 886                rpc->pos = 0;
 887                rpc->len = n;
 888                err |= post_rpc(rpc);
 889        }
 890
 891        close(client.in);
 892        client.in = -1;
 893        if (!err) {
 894                strbuf_read(rpc_result, client.out, 0);
 895        } else {
 896                char buf[4096];
 897                for (;;)
 898                        if (xread(client.out, buf, sizeof(buf)) <= 0)
 899                                break;
 900        }
 901
 902        close(client.out);
 903        client.out = -1;
 904
 905        err |= finish_command(&client);
 906        free(rpc->service_url);
 907        free(rpc->hdr_content_type);
 908        free(rpc->hdr_accept);
 909        free(rpc->protocol_header);
 910        free(rpc->buf);
 911        strbuf_release(&buf);
 912        return err;
 913}
 914
 915static int fetch_dumb(int nr_heads, struct ref **to_fetch)
 916{
 917        struct walker *walker;
 918        char **targets;
 919        int ret, i;
 920
 921        ALLOC_ARRAY(targets, nr_heads);
 922        if (options.depth || options.deepen_since)
 923                die("dumb http transport does not support shallow capabilities");
 924        for (i = 0; i < nr_heads; i++)
 925                targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
 926
 927        walker = get_http_walker(url.buf);
 928        walker->get_verbosely = options.verbosity >= 3;
 929        walker->get_recover = 0;
 930        ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
 931        walker_free(walker);
 932
 933        for (i = 0; i < nr_heads; i++)
 934                free(targets[i]);
 935        free(targets);
 936
 937        return ret ? error("fetch failed.") : 0;
 938}
 939
 940static int fetch_git(struct discovery *heads,
 941        int nr_heads, struct ref **to_fetch)
 942{
 943        struct rpc_state rpc;
 944        struct strbuf preamble = STRBUF_INIT;
 945        int i, err;
 946        struct argv_array args = ARGV_ARRAY_INIT;
 947        struct strbuf rpc_result = STRBUF_INIT;
 948
 949        argv_array_pushl(&args, "fetch-pack", "--stateless-rpc",
 950                         "--stdin", "--lock-pack", NULL);
 951        if (options.followtags)
 952                argv_array_push(&args, "--include-tag");
 953        if (options.thin)
 954                argv_array_push(&args, "--thin");
 955        if (options.verbosity >= 3)
 956                argv_array_pushl(&args, "-v", "-v", NULL);
 957        if (options.check_self_contained_and_connected)
 958                argv_array_push(&args, "--check-self-contained-and-connected");
 959        if (options.cloning)
 960                argv_array_push(&args, "--cloning");
 961        if (options.update_shallow)
 962                argv_array_push(&args, "--update-shallow");
 963        if (!options.progress)
 964                argv_array_push(&args, "--no-progress");
 965        if (options.depth)
 966                argv_array_pushf(&args, "--depth=%lu", options.depth);
 967        if (options.deepen_since)
 968                argv_array_pushf(&args, "--shallow-since=%s", options.deepen_since);
 969        for (i = 0; i < options.deepen_not.nr; i++)
 970                argv_array_pushf(&args, "--shallow-exclude=%s",
 971                                 options.deepen_not.items[i].string);
 972        if (options.deepen_relative && options.depth)
 973                argv_array_push(&args, "--deepen-relative");
 974        if (options.from_promisor)
 975                argv_array_push(&args, "--from-promisor");
 976        if (options.no_dependents)
 977                argv_array_push(&args, "--no-dependents");
 978        if (options.filter)
 979                argv_array_pushf(&args, "--filter=%s", options.filter);
 980        argv_array_push(&args, url.buf);
 981
 982        for (i = 0; i < nr_heads; i++) {
 983                struct ref *ref = to_fetch[i];
 984                if (!*ref->name)
 985                        die("cannot fetch by sha1 over smart http");
 986                packet_buf_write(&preamble, "%s %s\n",
 987                                 oid_to_hex(&ref->old_oid), ref->name);
 988        }
 989        packet_buf_flush(&preamble);
 990
 991        memset(&rpc, 0, sizeof(rpc));
 992        rpc.service_name = "git-upload-pack",
 993        rpc.gzip_request = 1;
 994
 995        err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
 996        if (rpc_result.len)
 997                write_or_die(1, rpc_result.buf, rpc_result.len);
 998        strbuf_release(&rpc_result);
 999        strbuf_release(&preamble);
1000        argv_array_clear(&args);
1001        return err;
1002}
1003
1004static int fetch(int nr_heads, struct ref **to_fetch)
1005{
1006        struct discovery *d = discover_refs("git-upload-pack", 0);
1007        if (d->proto_git)
1008                return fetch_git(d, nr_heads, to_fetch);
1009        else
1010                return fetch_dumb(nr_heads, to_fetch);
1011}
1012
1013static void parse_fetch(struct strbuf *buf)
1014{
1015        struct ref **to_fetch = NULL;
1016        struct ref *list_head = NULL;
1017        struct ref **list = &list_head;
1018        int alloc_heads = 0, nr_heads = 0;
1019
1020        do {
1021                const char *p;
1022                if (skip_prefix(buf->buf, "fetch ", &p)) {
1023                        const char *name;
1024                        struct ref *ref;
1025                        struct object_id old_oid;
1026
1027                        if (get_oid_hex(p, &old_oid))
1028                                die("protocol error: expected sha/ref, got %s'", p);
1029                        if (p[GIT_SHA1_HEXSZ] == ' ')
1030                                name = p + GIT_SHA1_HEXSZ + 1;
1031                        else if (!p[GIT_SHA1_HEXSZ])
1032                                name = "";
1033                        else
1034                                die("protocol error: expected sha/ref, got %s'", p);
1035
1036                        ref = alloc_ref(name);
1037                        oidcpy(&ref->old_oid, &old_oid);
1038
1039                        *list = ref;
1040                        list = &ref->next;
1041
1042                        ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
1043                        to_fetch[nr_heads++] = ref;
1044                }
1045                else
1046                        die("http transport does not support %s", buf->buf);
1047
1048                strbuf_reset(buf);
1049                if (strbuf_getline_lf(buf, stdin) == EOF)
1050                        return;
1051                if (!*buf->buf)
1052                        break;
1053        } while (1);
1054
1055        if (fetch(nr_heads, to_fetch))
1056                exit(128); /* error already reported */
1057        free_refs(list_head);
1058        free(to_fetch);
1059
1060        printf("\n");
1061        fflush(stdout);
1062        strbuf_reset(buf);
1063}
1064
1065static int push_dav(int nr_spec, char **specs)
1066{
1067        struct child_process child = CHILD_PROCESS_INIT;
1068        size_t i;
1069
1070        child.git_cmd = 1;
1071        argv_array_push(&child.args, "http-push");
1072        argv_array_push(&child.args, "--helper-status");
1073        if (options.dry_run)
1074                argv_array_push(&child.args, "--dry-run");
1075        if (options.verbosity > 1)
1076                argv_array_push(&child.args, "--verbose");
1077        argv_array_push(&child.args, url.buf);
1078        for (i = 0; i < nr_spec; i++)
1079                argv_array_push(&child.args, specs[i]);
1080
1081        if (run_command(&child))
1082                die("git-http-push failed");
1083        return 0;
1084}
1085
1086static int push_git(struct discovery *heads, int nr_spec, char **specs)
1087{
1088        struct rpc_state rpc;
1089        int i, err;
1090        struct argv_array args;
1091        struct string_list_item *cas_option;
1092        struct strbuf preamble = STRBUF_INIT;
1093        struct strbuf rpc_result = STRBUF_INIT;
1094
1095        argv_array_init(&args);
1096        argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
1097                         NULL);
1098
1099        if (options.thin)
1100                argv_array_push(&args, "--thin");
1101        if (options.dry_run)
1102                argv_array_push(&args, "--dry-run");
1103        if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
1104                argv_array_push(&args, "--signed=yes");
1105        else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
1106                argv_array_push(&args, "--signed=if-asked");
1107        if (options.verbosity == 0)
1108                argv_array_push(&args, "--quiet");
1109        else if (options.verbosity > 1)
1110                argv_array_push(&args, "--verbose");
1111        for (i = 0; i < options.push_options.nr; i++)
1112                argv_array_pushf(&args, "--push-option=%s",
1113                                 options.push_options.items[i].string);
1114        argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
1115        for_each_string_list_item(cas_option, &cas_options)
1116                argv_array_push(&args, cas_option->string);
1117        argv_array_push(&args, url.buf);
1118
1119        argv_array_push(&args, "--stdin");
1120        for (i = 0; i < nr_spec; i++)
1121                packet_buf_write(&preamble, "%s\n", specs[i]);
1122        packet_buf_flush(&preamble);
1123
1124        memset(&rpc, 0, sizeof(rpc));
1125        rpc.service_name = "git-receive-pack",
1126
1127        err = rpc_service(&rpc, heads, args.argv, &preamble, &rpc_result);
1128        if (rpc_result.len)
1129                write_or_die(1, rpc_result.buf, rpc_result.len);
1130        strbuf_release(&rpc_result);
1131        strbuf_release(&preamble);
1132        argv_array_clear(&args);
1133        return err;
1134}
1135
1136static int push(int nr_spec, char **specs)
1137{
1138        struct discovery *heads = discover_refs("git-receive-pack", 1);
1139        int ret;
1140
1141        if (heads->proto_git)
1142                ret = push_git(heads, nr_spec, specs);
1143        else
1144                ret = push_dav(nr_spec, specs);
1145        free_discovery(heads);
1146        return ret;
1147}
1148
1149static void parse_push(struct strbuf *buf)
1150{
1151        char **specs = NULL;
1152        int alloc_spec = 0, nr_spec = 0, i, ret;
1153
1154        do {
1155                if (starts_with(buf->buf, "push ")) {
1156                        ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
1157                        specs[nr_spec++] = xstrdup(buf->buf + 5);
1158                }
1159                else
1160                        die("http transport does not support %s", buf->buf);
1161
1162                strbuf_reset(buf);
1163                if (strbuf_getline_lf(buf, stdin) == EOF)
1164                        goto free_specs;
1165                if (!*buf->buf)
1166                        break;
1167        } while (1);
1168
1169        ret = push(nr_spec, specs);
1170        printf("\n");
1171        fflush(stdout);
1172
1173        if (ret)
1174                exit(128); /* error already reported */
1175
1176 free_specs:
1177        for (i = 0; i < nr_spec; i++)
1178                free(specs[i]);
1179        free(specs);
1180}
1181
1182/*
1183 * Used to represent the state of a connection to an HTTP server when
1184 * communicating using git's wire-protocol version 2.
1185 */
1186struct proxy_state {
1187        char *service_name;
1188        char *service_url;
1189        struct curl_slist *headers;
1190        struct strbuf request_buffer;
1191        int in;
1192        int out;
1193        struct packet_reader reader;
1194        size_t pos;
1195        int seen_flush;
1196};
1197
1198static void proxy_state_init(struct proxy_state *p, const char *service_name,
1199                             enum protocol_version version)
1200{
1201        struct strbuf buf = STRBUF_INIT;
1202
1203        memset(p, 0, sizeof(*p));
1204        p->service_name = xstrdup(service_name);
1205
1206        p->in = 0;
1207        p->out = 1;
1208        strbuf_init(&p->request_buffer, 0);
1209
1210        strbuf_addf(&buf, "%s%s", url.buf, p->service_name);
1211        p->service_url = strbuf_detach(&buf, NULL);
1212
1213        p->headers = http_copy_default_headers();
1214
1215        strbuf_addf(&buf, "Content-Type: application/x-%s-request", p->service_name);
1216        p->headers = curl_slist_append(p->headers, buf.buf);
1217        strbuf_reset(&buf);
1218
1219        strbuf_addf(&buf, "Accept: application/x-%s-result", p->service_name);
1220        p->headers = curl_slist_append(p->headers, buf.buf);
1221        strbuf_reset(&buf);
1222
1223        p->headers = curl_slist_append(p->headers, "Transfer-Encoding: chunked");
1224
1225        /* Add the Git-Protocol header */
1226        if (get_protocol_http_header(version, &buf))
1227                p->headers = curl_slist_append(p->headers, buf.buf);
1228
1229        packet_reader_init(&p->reader, p->in, NULL, 0,
1230                           PACKET_READ_GENTLE_ON_EOF |
1231                           PACKET_READ_DIE_ON_ERR_PACKET);
1232
1233        strbuf_release(&buf);
1234}
1235
1236static void proxy_state_clear(struct proxy_state *p)
1237{
1238        free(p->service_name);
1239        free(p->service_url);
1240        curl_slist_free_all(p->headers);
1241        strbuf_release(&p->request_buffer);
1242}
1243
1244/*
1245 * CURLOPT_READFUNCTION callback function.
1246 * Attempts to copy over a single packet-line at a time into the
1247 * curl provided buffer.
1248 */
1249static size_t proxy_in(char *buffer, size_t eltsize,
1250                       size_t nmemb, void *userdata)
1251{
1252        size_t max;
1253        struct proxy_state *p = userdata;
1254        size_t avail = p->request_buffer.len - p->pos;
1255
1256
1257        if (eltsize != 1)
1258                BUG("curl read callback called with size = %"PRIuMAX" != 1",
1259                    (uintmax_t)eltsize);
1260        max = nmemb;
1261
1262        if (!avail) {
1263                if (p->seen_flush) {
1264                        p->seen_flush = 0;
1265                        return 0;
1266                }
1267
1268                strbuf_reset(&p->request_buffer);
1269                switch (packet_reader_read(&p->reader)) {
1270                case PACKET_READ_EOF:
1271                        die("unexpected EOF when reading from parent process");
1272                case PACKET_READ_NORMAL:
1273                        packet_buf_write_len(&p->request_buffer, p->reader.line,
1274                                             p->reader.pktlen);
1275                        break;
1276                case PACKET_READ_DELIM:
1277                        packet_buf_delim(&p->request_buffer);
1278                        break;
1279                case PACKET_READ_FLUSH:
1280                        packet_buf_flush(&p->request_buffer);
1281                        p->seen_flush = 1;
1282                        break;
1283                }
1284                p->pos = 0;
1285                avail = p->request_buffer.len;
1286        }
1287
1288        if (max < avail)
1289                avail = max;
1290        memcpy(buffer, p->request_buffer.buf + p->pos, avail);
1291        p->pos += avail;
1292        return avail;
1293}
1294
1295static size_t proxy_out(char *buffer, size_t eltsize,
1296                        size_t nmemb, void *userdata)
1297{
1298        size_t size;
1299        struct proxy_state *p = userdata;
1300
1301        if (eltsize != 1)
1302                BUG("curl read callback called with size = %"PRIuMAX" != 1",
1303                    (uintmax_t)eltsize);
1304        size = nmemb;
1305
1306        write_or_die(p->out, buffer, size);
1307        return size;
1308}
1309
1310/* Issues a request to the HTTP server configured in `p` */
1311static int proxy_request(struct proxy_state *p)
1312{
1313        struct active_request_slot *slot;
1314
1315        slot = get_active_slot();
1316
1317        curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
1318        curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1319        curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
1320        curl_easy_setopt(slot->curl, CURLOPT_URL, p->service_url);
1321        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, p->headers);
1322
1323        /* Setup function to read request from client */
1324        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, proxy_in);
1325        curl_easy_setopt(slot->curl, CURLOPT_READDATA, p);
1326
1327        /* Setup function to write server response to client */
1328        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, proxy_out);
1329        curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, p);
1330
1331        if (run_slot(slot, NULL) != HTTP_OK)
1332                return -1;
1333
1334        return 0;
1335}
1336
1337static int stateless_connect(const char *service_name)
1338{
1339        struct discovery *discover;
1340        struct proxy_state p;
1341
1342        /*
1343         * Run the info/refs request and see if the server supports protocol
1344         * v2.  If and only if the server supports v2 can we successfully
1345         * establish a stateless connection, otherwise we need to tell the
1346         * client to fallback to using other transport helper functions to
1347         * complete their request.
1348         */
1349        discover = discover_refs(service_name, 0);
1350        if (discover->version != protocol_v2) {
1351                printf("fallback\n");
1352                fflush(stdout);
1353                return -1;
1354        } else {
1355                /* Stateless Connection established */
1356                printf("\n");
1357                fflush(stdout);
1358        }
1359
1360        proxy_state_init(&p, service_name, discover->version);
1361
1362        /*
1363         * Dump the capability listing that we got from the server earlier
1364         * during the info/refs request.
1365         */
1366        write_or_die(p.out, discover->buf, discover->len);
1367
1368        /* Peek the next packet line.  Until we see EOF keep sending POSTs */
1369        while (packet_reader_peek(&p.reader) != PACKET_READ_EOF) {
1370                if (proxy_request(&p)) {
1371                        /* We would have an err here */
1372                        break;
1373                }
1374        }
1375
1376        proxy_state_clear(&p);
1377        return 0;
1378}
1379
1380int cmd_main(int argc, const char **argv)
1381{
1382        struct strbuf buf = STRBUF_INIT;
1383        int nongit;
1384
1385        setup_git_directory_gently(&nongit);
1386        if (argc < 2) {
1387                error("remote-curl: usage: git remote-curl <remote> [<url>]");
1388                return 1;
1389        }
1390
1391        options.verbosity = 1;
1392        options.progress = !!isatty(2);
1393        options.thin = 1;
1394        string_list_init(&options.deepen_not, 1);
1395        string_list_init(&options.push_options, 1);
1396
1397        remote = remote_get(argv[1]);
1398
1399        if (argc > 2) {
1400                end_url_with_slash(&url, argv[2]);
1401        } else {
1402                end_url_with_slash(&url, remote->url[0]);
1403        }
1404
1405        http_init(remote, url.buf, 0);
1406
1407        do {
1408                const char *arg;
1409
1410                if (strbuf_getline_lf(&buf, stdin) == EOF) {
1411                        if (ferror(stdin))
1412                                error("remote-curl: error reading command stream from git");
1413                        return 1;
1414                }
1415                if (buf.len == 0)
1416                        break;
1417                if (starts_with(buf.buf, "fetch ")) {
1418                        if (nongit)
1419                                die("remote-curl: fetch attempted without a local repo");
1420                        parse_fetch(&buf);
1421
1422                } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1423                        int for_push = !!strstr(buf.buf + 4, "for-push");
1424                        output_refs(get_refs(for_push));
1425
1426                } else if (starts_with(buf.buf, "push ")) {
1427                        parse_push(&buf);
1428
1429                } else if (skip_prefix(buf.buf, "option ", &arg)) {
1430                        char *value = strchr(arg, ' ');
1431                        int result;
1432
1433                        if (value)
1434                                *value++ = '\0';
1435                        else
1436                                value = "true";
1437
1438                        result = set_option(arg, value);
1439                        if (!result)
1440                                printf("ok\n");
1441                        else if (result < 0)
1442                                printf("error invalid value\n");
1443                        else
1444                                printf("unsupported\n");
1445                        fflush(stdout);
1446
1447                } else if (!strcmp(buf.buf, "capabilities")) {
1448                        printf("stateless-connect\n");
1449                        printf("fetch\n");
1450                        printf("option\n");
1451                        printf("push\n");
1452                        printf("check-connectivity\n");
1453                        printf("\n");
1454                        fflush(stdout);
1455                } else if (skip_prefix(buf.buf, "stateless-connect ", &arg)) {
1456                        if (!stateless_connect(arg))
1457                                break;
1458                } else {
1459                        error("remote-curl: unknown command '%s' from git", buf.buf);
1460                        return 1;
1461                }
1462                strbuf_reset(&buf);
1463        } while (1);
1464
1465        http_cleanup();
1466
1467        return 0;
1468}