remote-curl.con commit Merge branch 'sb/submodule-config-doc-drop-path' into maint (a42539f)
   1#include "cache.h"
   2#include "remote.h"
   3#include "strbuf.h"
   4#include "walker.h"
   5#include "http.h"
   6#include "exec_cmd.h"
   7#include "run-command.h"
   8#include "pkt-line.h"
   9#include "string-list.h"
  10#include "sideband.h"
  11#include "argv-array.h"
  12#include "credential.h"
  13#include "sha1-array.h"
  14#include "send-pack.h"
  15
  16static struct remote *remote;
  17/* always ends with a trailing slash */
  18static struct strbuf url = STRBUF_INIT;
  19
  20struct options {
  21        int verbosity;
  22        unsigned long depth;
  23        unsigned progress : 1,
  24                check_self_contained_and_connected : 1,
  25                cloning : 1,
  26                update_shallow : 1,
  27                followtags : 1,
  28                dry_run : 1,
  29                thin : 1,
  30                /* One of the SEND_PACK_PUSH_CERT_* constants. */
  31                push_cert : 2;
  32};
  33static struct options options;
  34static struct string_list cas_options = STRING_LIST_INIT_DUP;
  35
  36static int set_option(const char *name, const char *value)
  37{
  38        if (!strcmp(name, "verbosity")) {
  39                char *end;
  40                int v = strtol(value, &end, 10);
  41                if (value == end || *end)
  42                        return -1;
  43                options.verbosity = v;
  44                return 0;
  45        }
  46        else if (!strcmp(name, "progress")) {
  47                if (!strcmp(value, "true"))
  48                        options.progress = 1;
  49                else if (!strcmp(value, "false"))
  50                        options.progress = 0;
  51                else
  52                        return -1;
  53                return 0;
  54        }
  55        else if (!strcmp(name, "depth")) {
  56                char *end;
  57                unsigned long v = strtoul(value, &end, 10);
  58                if (value == end || *end)
  59                        return -1;
  60                options.depth = v;
  61                return 0;
  62        }
  63        else if (!strcmp(name, "followtags")) {
  64                if (!strcmp(value, "true"))
  65                        options.followtags = 1;
  66                else if (!strcmp(value, "false"))
  67                        options.followtags = 0;
  68                else
  69                        return -1;
  70                return 0;
  71        }
  72        else if (!strcmp(name, "dry-run")) {
  73                if (!strcmp(value, "true"))
  74                        options.dry_run = 1;
  75                else if (!strcmp(value, "false"))
  76                        options.dry_run = 0;
  77                else
  78                        return -1;
  79                return 0;
  80        }
  81        else if (!strcmp(name, "check-connectivity")) {
  82                if (!strcmp(value, "true"))
  83                        options.check_self_contained_and_connected = 1;
  84                else if (!strcmp(value, "false"))
  85                        options.check_self_contained_and_connected = 0;
  86                else
  87                        return -1;
  88                return 0;
  89        }
  90        else if (!strcmp(name, "cas")) {
  91                struct strbuf val = STRBUF_INIT;
  92                strbuf_addf(&val, "--" CAS_OPT_NAME "=%s", value);
  93                string_list_append(&cas_options, val.buf);
  94                strbuf_release(&val);
  95                return 0;
  96        } else if (!strcmp(name, "cloning")) {
  97                if (!strcmp(value, "true"))
  98                        options.cloning = 1;
  99                else if (!strcmp(value, "false"))
 100                        options.cloning = 0;
 101                else
 102                        return -1;
 103                return 0;
 104        } else if (!strcmp(name, "update-shallow")) {
 105                if (!strcmp(value, "true"))
 106                        options.update_shallow = 1;
 107                else if (!strcmp(value, "false"))
 108                        options.update_shallow = 0;
 109                else
 110                        return -1;
 111                return 0;
 112        } else if (!strcmp(name, "pushcert")) {
 113                if (!strcmp(value, "true"))
 114                        options.push_cert = SEND_PACK_PUSH_CERT_ALWAYS;
 115                else if (!strcmp(value, "false"))
 116                        options.push_cert = SEND_PACK_PUSH_CERT_NEVER;
 117                else if (!strcmp(value, "if-asked"))
 118                        options.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED;
 119                else
 120                        return -1;
 121                return 0;
 122
 123#if LIBCURL_VERSION_NUM >= 0x070a08
 124        } else if (!strcmp(name, "family")) {
 125                if (!strcmp(value, "ipv4"))
 126                        git_curl_ipresolve = CURL_IPRESOLVE_V4;
 127                else if (!strcmp(value, "ipv6"))
 128                        git_curl_ipresolve = CURL_IPRESOLVE_V6;
 129                else if (!strcmp(value, "all"))
 130                        git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
 131                else
 132                        return -1;
 133                return 0;
 134#endif /* LIBCURL_VERSION_NUM >= 0x070a08 */
 135        } else {
 136                return 1 /* unsupported */;
 137        }
 138}
 139
 140struct discovery {
 141        const char *service;
 142        char *buf_alloc;
 143        char *buf;
 144        size_t len;
 145        struct ref *refs;
 146        struct sha1_array shallow;
 147        unsigned proto_git : 1;
 148};
 149static struct discovery *last_discovery;
 150
 151static struct ref *parse_git_refs(struct discovery *heads, int for_push)
 152{
 153        struct ref *list = NULL;
 154        get_remote_heads(-1, heads->buf, heads->len, &list,
 155                         for_push ? REF_NORMAL : 0, NULL, &heads->shallow);
 156        return list;
 157}
 158
 159static struct ref *parse_info_refs(struct discovery *heads)
 160{
 161        char *data, *start, *mid;
 162        char *ref_name;
 163        int i = 0;
 164
 165        struct ref *refs = NULL;
 166        struct ref *ref = NULL;
 167        struct ref *last_ref = NULL;
 168
 169        data = heads->buf;
 170        start = NULL;
 171        mid = data;
 172        while (i < heads->len) {
 173                if (!start) {
 174                        start = &data[i];
 175                }
 176                if (data[i] == '\t')
 177                        mid = &data[i];
 178                if (data[i] == '\n') {
 179                        if (mid - start != 40)
 180                                die("%sinfo/refs not valid: is this a git repository?",
 181                                    url.buf);
 182                        data[i] = 0;
 183                        ref_name = mid + 1;
 184                        ref = alloc_ref(ref_name);
 185                        get_oid_hex(start, &ref->old_oid);
 186                        if (!refs)
 187                                refs = ref;
 188                        if (last_ref)
 189                                last_ref->next = ref;
 190                        last_ref = ref;
 191                        start = NULL;
 192                }
 193                i++;
 194        }
 195
 196        ref = alloc_ref("HEAD");
 197        if (!http_fetch_ref(url.buf, ref) &&
 198            !resolve_remote_symref(ref, refs)) {
 199                ref->next = refs;
 200                refs = ref;
 201        } else {
 202                free(ref);
 203        }
 204
 205        return refs;
 206}
 207
 208static void free_discovery(struct discovery *d)
 209{
 210        if (d) {
 211                if (d == last_discovery)
 212                        last_discovery = NULL;
 213                free(d->shallow.sha1);
 214                free(d->buf_alloc);
 215                free_refs(d->refs);
 216                free(d);
 217        }
 218}
 219
 220static int show_http_message(struct strbuf *type, struct strbuf *charset,
 221                             struct strbuf *msg)
 222{
 223        const char *p, *eol;
 224
 225        /*
 226         * We only show text/plain parts, as other types are likely
 227         * to be ugly to look at on the user's terminal.
 228         */
 229        if (strcmp(type->buf, "text/plain"))
 230                return -1;
 231        if (charset->len)
 232                strbuf_reencode(msg, charset->buf, get_log_output_encoding());
 233
 234        strbuf_trim(msg);
 235        if (!msg->len)
 236                return -1;
 237
 238        p = msg->buf;
 239        do {
 240                eol = strchrnul(p, '\n');
 241                fprintf(stderr, "remote: %.*s\n", (int)(eol - p), p);
 242                p = eol + 1;
 243        } while(*eol);
 244        return 0;
 245}
 246
 247static struct discovery *discover_refs(const char *service, int for_push)
 248{
 249        struct strbuf exp = STRBUF_INIT;
 250        struct strbuf type = STRBUF_INIT;
 251        struct strbuf charset = STRBUF_INIT;
 252        struct strbuf buffer = STRBUF_INIT;
 253        struct strbuf refs_url = STRBUF_INIT;
 254        struct strbuf effective_url = STRBUF_INIT;
 255        struct discovery *last = last_discovery;
 256        int http_ret, maybe_smart = 0;
 257        struct http_get_options options;
 258
 259        if (last && !strcmp(service, last->service))
 260                return last;
 261        free_discovery(last);
 262
 263        strbuf_addf(&refs_url, "%sinfo/refs", url.buf);
 264        if ((starts_with(url.buf, "http://") || starts_with(url.buf, "https://")) &&
 265             git_env_bool("GIT_SMART_HTTP", 1)) {
 266                maybe_smart = 1;
 267                if (!strchr(url.buf, '?'))
 268                        strbuf_addch(&refs_url, '?');
 269                else
 270                        strbuf_addch(&refs_url, '&');
 271                strbuf_addf(&refs_url, "service=%s", service);
 272        }
 273
 274        memset(&options, 0, sizeof(options));
 275        options.content_type = &type;
 276        options.charset = &charset;
 277        options.effective_url = &effective_url;
 278        options.base_url = &url;
 279        options.no_cache = 1;
 280        options.keep_error = 1;
 281
 282        http_ret = http_get_strbuf(refs_url.buf, &buffer, &options);
 283        switch (http_ret) {
 284        case HTTP_OK:
 285                break;
 286        case HTTP_MISSING_TARGET:
 287                show_http_message(&type, &charset, &buffer);
 288                die("repository '%s' not found", url.buf);
 289        case HTTP_NOAUTH:
 290                show_http_message(&type, &charset, &buffer);
 291                die("Authentication failed for '%s'", url.buf);
 292        default:
 293                show_http_message(&type, &charset, &buffer);
 294                die("unable to access '%s': %s", url.buf, curl_errorstr);
 295        }
 296
 297        last= xcalloc(1, sizeof(*last_discovery));
 298        last->service = service;
 299        last->buf_alloc = strbuf_detach(&buffer, &last->len);
 300        last->buf = last->buf_alloc;
 301
 302        strbuf_addf(&exp, "application/x-%s-advertisement", service);
 303        if (maybe_smart &&
 304            (5 <= last->len && last->buf[4] == '#') &&
 305            !strbuf_cmp(&exp, &type)) {
 306                char *line;
 307
 308                /*
 309                 * smart HTTP response; validate that the service
 310                 * pkt-line matches our request.
 311                 */
 312                line = packet_read_line_buf(&last->buf, &last->len, NULL);
 313
 314                strbuf_reset(&exp);
 315                strbuf_addf(&exp, "# service=%s", service);
 316                if (strcmp(line, exp.buf))
 317                        die("invalid server response; got '%s'", line);
 318                strbuf_release(&exp);
 319
 320                /* The header can include additional metadata lines, up
 321                 * until a packet flush marker.  Ignore these now, but
 322                 * in the future we might start to scan them.
 323                 */
 324                while (packet_read_line_buf(&last->buf, &last->len, NULL))
 325                        ;
 326
 327                last->proto_git = 1;
 328        }
 329
 330        if (last->proto_git)
 331                last->refs = parse_git_refs(last, for_push);
 332        else
 333                last->refs = parse_info_refs(last);
 334
 335        strbuf_release(&refs_url);
 336        strbuf_release(&exp);
 337        strbuf_release(&type);
 338        strbuf_release(&charset);
 339        strbuf_release(&effective_url);
 340        strbuf_release(&buffer);
 341        last_discovery = last;
 342        return last;
 343}
 344
 345static struct ref *get_refs(int for_push)
 346{
 347        struct discovery *heads;
 348
 349        if (for_push)
 350                heads = discover_refs("git-receive-pack", for_push);
 351        else
 352                heads = discover_refs("git-upload-pack", for_push);
 353
 354        return heads->refs;
 355}
 356
 357static void output_refs(struct ref *refs)
 358{
 359        struct ref *posn;
 360        for (posn = refs; posn; posn = posn->next) {
 361                if (posn->symref)
 362                        printf("@%s %s\n", posn->symref, posn->name);
 363                else
 364                        printf("%s %s\n", oid_to_hex(&posn->old_oid), posn->name);
 365        }
 366        printf("\n");
 367        fflush(stdout);
 368}
 369
 370struct rpc_state {
 371        const char *service_name;
 372        const char **argv;
 373        struct strbuf *stdin_preamble;
 374        char *service_url;
 375        char *hdr_content_type;
 376        char *hdr_accept;
 377        char *buf;
 378        size_t alloc;
 379        size_t len;
 380        size_t pos;
 381        int in;
 382        int out;
 383        struct strbuf result;
 384        unsigned gzip_request : 1;
 385        unsigned initial_buffer : 1;
 386};
 387
 388static size_t rpc_out(void *ptr, size_t eltsize,
 389                size_t nmemb, void *buffer_)
 390{
 391        size_t max = eltsize * nmemb;
 392        struct rpc_state *rpc = buffer_;
 393        size_t avail = rpc->len - rpc->pos;
 394
 395        if (!avail) {
 396                rpc->initial_buffer = 0;
 397                avail = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
 398                if (!avail)
 399                        return 0;
 400                rpc->pos = 0;
 401                rpc->len = avail;
 402        }
 403
 404        if (max < avail)
 405                avail = max;
 406        memcpy(ptr, rpc->buf + rpc->pos, avail);
 407        rpc->pos += avail;
 408        return avail;
 409}
 410
 411#ifndef NO_CURL_IOCTL
 412static curlioerr rpc_ioctl(CURL *handle, int cmd, void *clientp)
 413{
 414        struct rpc_state *rpc = clientp;
 415
 416        switch (cmd) {
 417        case CURLIOCMD_NOP:
 418                return CURLIOE_OK;
 419
 420        case CURLIOCMD_RESTARTREAD:
 421                if (rpc->initial_buffer) {
 422                        rpc->pos = 0;
 423                        return CURLIOE_OK;
 424                }
 425                error("unable to rewind rpc post data - try increasing http.postBuffer");
 426                return CURLIOE_FAILRESTART;
 427
 428        default:
 429                return CURLIOE_UNKNOWNCMD;
 430        }
 431}
 432#endif
 433
 434static size_t rpc_in(char *ptr, size_t eltsize,
 435                size_t nmemb, void *buffer_)
 436{
 437        size_t size = eltsize * nmemb;
 438        struct rpc_state *rpc = buffer_;
 439        write_or_die(rpc->in, ptr, size);
 440        return size;
 441}
 442
 443static int run_slot(struct active_request_slot *slot,
 444                    struct slot_results *results)
 445{
 446        int err;
 447        struct slot_results results_buf;
 448
 449        if (!results)
 450                results = &results_buf;
 451
 452        err = run_one_slot(slot, results);
 453
 454        if (err != HTTP_OK && err != HTTP_REAUTH) {
 455                struct strbuf msg = STRBUF_INIT;
 456                if (results->http_code && results->http_code != 200)
 457                        strbuf_addf(&msg, "HTTP %ld", results->http_code);
 458                if (results->curl_result != CURLE_OK) {
 459                        if (msg.len)
 460                                strbuf_addch(&msg, ' ');
 461                        strbuf_addf(&msg, "curl %d", results->curl_result);
 462                        if (curl_errorstr[0]) {
 463                                strbuf_addch(&msg, ' ');
 464                                strbuf_addstr(&msg, curl_errorstr);
 465                        }
 466                }
 467                error("RPC failed; %s", msg.buf);
 468                strbuf_release(&msg);
 469        }
 470
 471        return err;
 472}
 473
 474static int probe_rpc(struct rpc_state *rpc, struct slot_results *results)
 475{
 476        struct active_request_slot *slot;
 477        struct curl_slist *headers = http_copy_default_headers();
 478        struct strbuf buf = STRBUF_INIT;
 479        int err;
 480
 481        slot = get_active_slot();
 482
 483        headers = curl_slist_append(headers, rpc->hdr_content_type);
 484        headers = curl_slist_append(headers, rpc->hdr_accept);
 485
 486        curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
 487        curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
 488        curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
 489        curl_easy_setopt(slot->curl, CURLOPT_ENCODING, NULL);
 490        curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, "0000");
 491        curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, 4);
 492        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
 493        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 494        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buf);
 495
 496        err = run_slot(slot, results);
 497
 498        curl_slist_free_all(headers);
 499        strbuf_release(&buf);
 500        return err;
 501}
 502
 503static int post_rpc(struct rpc_state *rpc)
 504{
 505        struct active_request_slot *slot;
 506        struct curl_slist *headers = http_copy_default_headers();
 507        int use_gzip = rpc->gzip_request;
 508        char *gzip_body = NULL;
 509        size_t gzip_size = 0;
 510        int err, large_request = 0;
 511        int needs_100_continue = 0;
 512
 513        /* Try to load the entire request, if we can fit it into the
 514         * allocated buffer space we can use HTTP/1.0 and avoid the
 515         * chunked encoding mess.
 516         */
 517        while (1) {
 518                size_t left = rpc->alloc - rpc->len;
 519                char *buf = rpc->buf + rpc->len;
 520                int n;
 521
 522                if (left < LARGE_PACKET_MAX) {
 523                        large_request = 1;
 524                        use_gzip = 0;
 525                        break;
 526                }
 527
 528                n = packet_read(rpc->out, NULL, NULL, buf, left, 0);
 529                if (!n)
 530                        break;
 531                rpc->len += n;
 532        }
 533
 534        if (large_request) {
 535                struct slot_results results;
 536
 537                do {
 538                        err = probe_rpc(rpc, &results);
 539                        if (err == HTTP_REAUTH)
 540                                credential_fill(&http_auth);
 541                } while (err == HTTP_REAUTH);
 542                if (err != HTTP_OK)
 543                        return -1;
 544
 545                if (results.auth_avail & CURLAUTH_GSSNEGOTIATE)
 546                        needs_100_continue = 1;
 547        }
 548
 549        headers = curl_slist_append(headers, rpc->hdr_content_type);
 550        headers = curl_slist_append(headers, rpc->hdr_accept);
 551        headers = curl_slist_append(headers, needs_100_continue ?
 552                "Expect: 100-continue" : "Expect:");
 553
 554retry:
 555        slot = get_active_slot();
 556
 557        curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
 558        curl_easy_setopt(slot->curl, CURLOPT_POST, 1);
 559        curl_easy_setopt(slot->curl, CURLOPT_URL, rpc->service_url);
 560        curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
 561
 562        if (large_request) {
 563                /* The request body is large and the size cannot be predicted.
 564                 * We must use chunked encoding to send it.
 565                 */
 566                headers = curl_slist_append(headers, "Transfer-Encoding: chunked");
 567                rpc->initial_buffer = 1;
 568                curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, rpc_out);
 569                curl_easy_setopt(slot->curl, CURLOPT_INFILE, rpc);
 570#ifndef NO_CURL_IOCTL
 571                curl_easy_setopt(slot->curl, CURLOPT_IOCTLFUNCTION, rpc_ioctl);
 572                curl_easy_setopt(slot->curl, CURLOPT_IOCTLDATA, rpc);
 573#endif
 574                if (options.verbosity > 1) {
 575                        fprintf(stderr, "POST %s (chunked)\n", rpc->service_name);
 576                        fflush(stderr);
 577                }
 578
 579        } else if (gzip_body) {
 580                /*
 581                 * If we are looping to retry authentication, then the previous
 582                 * run will have set up the headers and gzip buffer already,
 583                 * and we just need to send it.
 584                 */
 585                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
 586                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
 587
 588        } else if (use_gzip && 1024 < rpc->len) {
 589                /* The client backend isn't giving us compressed data so
 590                 * we can try to deflate it ourselves, this may save on.
 591                 * the transfer time.
 592                 */
 593                git_zstream stream;
 594                int ret;
 595
 596                git_deflate_init_gzip(&stream, Z_BEST_COMPRESSION);
 597                gzip_size = git_deflate_bound(&stream, rpc->len);
 598                gzip_body = xmalloc(gzip_size);
 599
 600                stream.next_in = (unsigned char *)rpc->buf;
 601                stream.avail_in = rpc->len;
 602                stream.next_out = (unsigned char *)gzip_body;
 603                stream.avail_out = gzip_size;
 604
 605                ret = git_deflate(&stream, Z_FINISH);
 606                if (ret != Z_STREAM_END)
 607                        die("cannot deflate request; zlib deflate error %d", ret);
 608
 609                ret = git_deflate_end_gently(&stream);
 610                if (ret != Z_OK)
 611                        die("cannot deflate request; zlib end error %d", ret);
 612
 613                gzip_size = stream.total_out;
 614
 615                headers = curl_slist_append(headers, "Content-Encoding: gzip");
 616                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, gzip_body);
 617                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, gzip_size);
 618
 619                if (options.verbosity > 1) {
 620                        fprintf(stderr, "POST %s (gzip %lu to %lu bytes)\n",
 621                                rpc->service_name,
 622                                (unsigned long)rpc->len, (unsigned long)gzip_size);
 623                        fflush(stderr);
 624                }
 625        } else {
 626                /* We know the complete request size in advance, use the
 627                 * more normal Content-Length approach.
 628                 */
 629                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, rpc->buf);
 630                curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDSIZE, rpc->len);
 631                if (options.verbosity > 1) {
 632                        fprintf(stderr, "POST %s (%lu bytes)\n",
 633                                rpc->service_name, (unsigned long)rpc->len);
 634                        fflush(stderr);
 635                }
 636        }
 637
 638        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
 639        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, rpc_in);
 640        curl_easy_setopt(slot->curl, CURLOPT_FILE, rpc);
 641
 642        err = run_slot(slot, NULL);
 643        if (err == HTTP_REAUTH && !large_request) {
 644                credential_fill(&http_auth);
 645                goto retry;
 646        }
 647        if (err != HTTP_OK)
 648                err = -1;
 649
 650        curl_slist_free_all(headers);
 651        free(gzip_body);
 652        return err;
 653}
 654
 655static int rpc_service(struct rpc_state *rpc, struct discovery *heads)
 656{
 657        const char *svc = rpc->service_name;
 658        struct strbuf buf = STRBUF_INIT;
 659        struct strbuf *preamble = rpc->stdin_preamble;
 660        struct child_process client = CHILD_PROCESS_INIT;
 661        int err = 0;
 662
 663        client.in = -1;
 664        client.out = -1;
 665        client.git_cmd = 1;
 666        client.argv = rpc->argv;
 667        if (start_command(&client))
 668                exit(1);
 669        if (preamble)
 670                write_or_die(client.in, preamble->buf, preamble->len);
 671        if (heads)
 672                write_or_die(client.in, heads->buf, heads->len);
 673
 674        rpc->alloc = http_post_buffer;
 675        rpc->buf = xmalloc(rpc->alloc);
 676        rpc->in = client.in;
 677        rpc->out = client.out;
 678        strbuf_init(&rpc->result, 0);
 679
 680        strbuf_addf(&buf, "%s%s", url.buf, svc);
 681        rpc->service_url = strbuf_detach(&buf, NULL);
 682
 683        strbuf_addf(&buf, "Content-Type: application/x-%s-request", svc);
 684        rpc->hdr_content_type = strbuf_detach(&buf, NULL);
 685
 686        strbuf_addf(&buf, "Accept: application/x-%s-result", svc);
 687        rpc->hdr_accept = strbuf_detach(&buf, NULL);
 688
 689        while (!err) {
 690                int n = packet_read(rpc->out, NULL, NULL, rpc->buf, rpc->alloc, 0);
 691                if (!n)
 692                        break;
 693                rpc->pos = 0;
 694                rpc->len = n;
 695                err |= post_rpc(rpc);
 696        }
 697
 698        close(client.in);
 699        client.in = -1;
 700        if (!err) {
 701                strbuf_read(&rpc->result, client.out, 0);
 702        } else {
 703                char buf[4096];
 704                for (;;)
 705                        if (xread(client.out, buf, sizeof(buf)) <= 0)
 706                                break;
 707        }
 708
 709        close(client.out);
 710        client.out = -1;
 711
 712        err |= finish_command(&client);
 713        free(rpc->service_url);
 714        free(rpc->hdr_content_type);
 715        free(rpc->hdr_accept);
 716        free(rpc->buf);
 717        strbuf_release(&buf);
 718        return err;
 719}
 720
 721static int fetch_dumb(int nr_heads, struct ref **to_fetch)
 722{
 723        struct walker *walker;
 724        char **targets;
 725        int ret, i;
 726
 727        ALLOC_ARRAY(targets, nr_heads);
 728        if (options.depth)
 729                die("dumb http transport does not support --depth");
 730        for (i = 0; i < nr_heads; i++)
 731                targets[i] = xstrdup(oid_to_hex(&to_fetch[i]->old_oid));
 732
 733        walker = get_http_walker(url.buf);
 734        walker->get_all = 1;
 735        walker->get_tree = 1;
 736        walker->get_history = 1;
 737        walker->get_verbosely = options.verbosity >= 3;
 738        walker->get_recover = 0;
 739        ret = walker_fetch(walker, nr_heads, targets, NULL, NULL);
 740        walker_free(walker);
 741
 742        for (i = 0; i < nr_heads; i++)
 743                free(targets[i]);
 744        free(targets);
 745
 746        return ret ? error("fetch failed.") : 0;
 747}
 748
 749static int fetch_git(struct discovery *heads,
 750        int nr_heads, struct ref **to_fetch)
 751{
 752        struct rpc_state rpc;
 753        struct strbuf preamble = STRBUF_INIT;
 754        char *depth_arg = NULL;
 755        int argc = 0, i, err;
 756        const char *argv[17];
 757
 758        argv[argc++] = "fetch-pack";
 759        argv[argc++] = "--stateless-rpc";
 760        argv[argc++] = "--stdin";
 761        argv[argc++] = "--lock-pack";
 762        if (options.followtags)
 763                argv[argc++] = "--include-tag";
 764        if (options.thin)
 765                argv[argc++] = "--thin";
 766        if (options.verbosity >= 3) {
 767                argv[argc++] = "-v";
 768                argv[argc++] = "-v";
 769        }
 770        if (options.check_self_contained_and_connected)
 771                argv[argc++] = "--check-self-contained-and-connected";
 772        if (options.cloning)
 773                argv[argc++] = "--cloning";
 774        if (options.update_shallow)
 775                argv[argc++] = "--update-shallow";
 776        if (!options.progress)
 777                argv[argc++] = "--no-progress";
 778        if (options.depth) {
 779                struct strbuf buf = STRBUF_INIT;
 780                strbuf_addf(&buf, "--depth=%lu", options.depth);
 781                depth_arg = strbuf_detach(&buf, NULL);
 782                argv[argc++] = depth_arg;
 783        }
 784        argv[argc++] = url.buf;
 785        argv[argc++] = NULL;
 786
 787        for (i = 0; i < nr_heads; i++) {
 788                struct ref *ref = to_fetch[i];
 789                if (!*ref->name)
 790                        die("cannot fetch by sha1 over smart http");
 791                packet_buf_write(&preamble, "%s %s\n",
 792                                 oid_to_hex(&ref->old_oid), ref->name);
 793        }
 794        packet_buf_flush(&preamble);
 795
 796        memset(&rpc, 0, sizeof(rpc));
 797        rpc.service_name = "git-upload-pack",
 798        rpc.argv = argv;
 799        rpc.stdin_preamble = &preamble;
 800        rpc.gzip_request = 1;
 801
 802        err = rpc_service(&rpc, heads);
 803        if (rpc.result.len)
 804                write_or_die(1, rpc.result.buf, rpc.result.len);
 805        strbuf_release(&rpc.result);
 806        strbuf_release(&preamble);
 807        free(depth_arg);
 808        return err;
 809}
 810
 811static int fetch(int nr_heads, struct ref **to_fetch)
 812{
 813        struct discovery *d = discover_refs("git-upload-pack", 0);
 814        if (d->proto_git)
 815                return fetch_git(d, nr_heads, to_fetch);
 816        else
 817                return fetch_dumb(nr_heads, to_fetch);
 818}
 819
 820static void parse_fetch(struct strbuf *buf)
 821{
 822        struct ref **to_fetch = NULL;
 823        struct ref *list_head = NULL;
 824        struct ref **list = &list_head;
 825        int alloc_heads = 0, nr_heads = 0;
 826
 827        do {
 828                const char *p;
 829                if (skip_prefix(buf->buf, "fetch ", &p)) {
 830                        const char *name;
 831                        struct ref *ref;
 832                        struct object_id old_oid;
 833
 834                        if (get_oid_hex(p, &old_oid))
 835                                die("protocol error: expected sha/ref, got %s'", p);
 836                        if (p[GIT_SHA1_HEXSZ] == ' ')
 837                                name = p + GIT_SHA1_HEXSZ + 1;
 838                        else if (!p[GIT_SHA1_HEXSZ])
 839                                name = "";
 840                        else
 841                                die("protocol error: expected sha/ref, got %s'", p);
 842
 843                        ref = alloc_ref(name);
 844                        oidcpy(&ref->old_oid, &old_oid);
 845
 846                        *list = ref;
 847                        list = &ref->next;
 848
 849                        ALLOC_GROW(to_fetch, nr_heads + 1, alloc_heads);
 850                        to_fetch[nr_heads++] = ref;
 851                }
 852                else
 853                        die("http transport does not support %s", buf->buf);
 854
 855                strbuf_reset(buf);
 856                if (strbuf_getline_lf(buf, stdin) == EOF)
 857                        return;
 858                if (!*buf->buf)
 859                        break;
 860        } while (1);
 861
 862        if (fetch(nr_heads, to_fetch))
 863                exit(128); /* error already reported */
 864        free_refs(list_head);
 865        free(to_fetch);
 866
 867        printf("\n");
 868        fflush(stdout);
 869        strbuf_reset(buf);
 870}
 871
 872static int push_dav(int nr_spec, char **specs)
 873{
 874        struct child_process child = CHILD_PROCESS_INIT;
 875        size_t i;
 876
 877        child.git_cmd = 1;
 878        argv_array_push(&child.args, "http-push");
 879        argv_array_push(&child.args, "--helper-status");
 880        if (options.dry_run)
 881                argv_array_push(&child.args, "--dry-run");
 882        if (options.verbosity > 1)
 883                argv_array_push(&child.args, "--verbose");
 884        argv_array_push(&child.args, url.buf);
 885        for (i = 0; i < nr_spec; i++)
 886                argv_array_push(&child.args, specs[i]);
 887
 888        if (run_command(&child))
 889                die("git-http-push failed");
 890        return 0;
 891}
 892
 893static int push_git(struct discovery *heads, int nr_spec, char **specs)
 894{
 895        struct rpc_state rpc;
 896        int i, err;
 897        struct argv_array args;
 898        struct string_list_item *cas_option;
 899        struct strbuf preamble = STRBUF_INIT;
 900
 901        argv_array_init(&args);
 902        argv_array_pushl(&args, "send-pack", "--stateless-rpc", "--helper-status",
 903                         NULL);
 904
 905        if (options.thin)
 906                argv_array_push(&args, "--thin");
 907        if (options.dry_run)
 908                argv_array_push(&args, "--dry-run");
 909        if (options.push_cert == SEND_PACK_PUSH_CERT_ALWAYS)
 910                argv_array_push(&args, "--signed=yes");
 911        else if (options.push_cert == SEND_PACK_PUSH_CERT_IF_ASKED)
 912                argv_array_push(&args, "--signed=if-asked");
 913        if (options.verbosity == 0)
 914                argv_array_push(&args, "--quiet");
 915        else if (options.verbosity > 1)
 916                argv_array_push(&args, "--verbose");
 917        argv_array_push(&args, options.progress ? "--progress" : "--no-progress");
 918        for_each_string_list_item(cas_option, &cas_options)
 919                argv_array_push(&args, cas_option->string);
 920        argv_array_push(&args, url.buf);
 921
 922        argv_array_push(&args, "--stdin");
 923        for (i = 0; i < nr_spec; i++)
 924                packet_buf_write(&preamble, "%s\n", specs[i]);
 925        packet_buf_flush(&preamble);
 926
 927        memset(&rpc, 0, sizeof(rpc));
 928        rpc.service_name = "git-receive-pack",
 929        rpc.argv = args.argv;
 930        rpc.stdin_preamble = &preamble;
 931
 932        err = rpc_service(&rpc, heads);
 933        if (rpc.result.len)
 934                write_or_die(1, rpc.result.buf, rpc.result.len);
 935        strbuf_release(&rpc.result);
 936        strbuf_release(&preamble);
 937        argv_array_clear(&args);
 938        return err;
 939}
 940
 941static int push(int nr_spec, char **specs)
 942{
 943        struct discovery *heads = discover_refs("git-receive-pack", 1);
 944        int ret;
 945
 946        if (heads->proto_git)
 947                ret = push_git(heads, nr_spec, specs);
 948        else
 949                ret = push_dav(nr_spec, specs);
 950        free_discovery(heads);
 951        return ret;
 952}
 953
 954static void parse_push(struct strbuf *buf)
 955{
 956        char **specs = NULL;
 957        int alloc_spec = 0, nr_spec = 0, i, ret;
 958
 959        do {
 960                if (starts_with(buf->buf, "push ")) {
 961                        ALLOC_GROW(specs, nr_spec + 1, alloc_spec);
 962                        specs[nr_spec++] = xstrdup(buf->buf + 5);
 963                }
 964                else
 965                        die("http transport does not support %s", buf->buf);
 966
 967                strbuf_reset(buf);
 968                if (strbuf_getline_lf(buf, stdin) == EOF)
 969                        goto free_specs;
 970                if (!*buf->buf)
 971                        break;
 972        } while (1);
 973
 974        ret = push(nr_spec, specs);
 975        printf("\n");
 976        fflush(stdout);
 977
 978        if (ret)
 979                exit(128); /* error already reported */
 980
 981 free_specs:
 982        for (i = 0; i < nr_spec; i++)
 983                free(specs[i]);
 984        free(specs);
 985}
 986
 987int cmd_main(int argc, const char **argv)
 988{
 989        struct strbuf buf = STRBUF_INIT;
 990        int nongit;
 991
 992        setup_git_directory_gently(&nongit);
 993        if (argc < 2) {
 994                error("remote-curl: usage: git remote-curl <remote> [<url>]");
 995                return 1;
 996        }
 997
 998        options.verbosity = 1;
 999        options.progress = !!isatty(2);
1000        options.thin = 1;
1001
1002        remote = remote_get(argv[1]);
1003
1004        if (argc > 2) {
1005                end_url_with_slash(&url, argv[2]);
1006        } else {
1007                end_url_with_slash(&url, remote->url[0]);
1008        }
1009
1010        http_init(remote, url.buf, 0);
1011
1012        do {
1013                const char *arg;
1014
1015                if (strbuf_getline_lf(&buf, stdin) == EOF) {
1016                        if (ferror(stdin))
1017                                error("remote-curl: error reading command stream from git");
1018                        return 1;
1019                }
1020                if (buf.len == 0)
1021                        break;
1022                if (starts_with(buf.buf, "fetch ")) {
1023                        if (nongit)
1024                                die("remote-curl: fetch attempted without a local repo");
1025                        parse_fetch(&buf);
1026
1027                } else if (!strcmp(buf.buf, "list") || starts_with(buf.buf, "list ")) {
1028                        int for_push = !!strstr(buf.buf + 4, "for-push");
1029                        output_refs(get_refs(for_push));
1030
1031                } else if (starts_with(buf.buf, "push ")) {
1032                        parse_push(&buf);
1033
1034                } else if (skip_prefix(buf.buf, "option ", &arg)) {
1035                        char *value = strchr(arg, ' ');
1036                        int result;
1037
1038                        if (value)
1039                                *value++ = '\0';
1040                        else
1041                                value = "true";
1042
1043                        result = set_option(arg, value);
1044                        if (!result)
1045                                printf("ok\n");
1046                        else if (result < 0)
1047                                printf("error invalid value\n");
1048                        else
1049                                printf("unsupported\n");
1050                        fflush(stdout);
1051
1052                } else if (!strcmp(buf.buf, "capabilities")) {
1053                        printf("fetch\n");
1054                        printf("option\n");
1055                        printf("push\n");
1056                        printf("check-connectivity\n");
1057                        printf("\n");
1058                        fflush(stdout);
1059                } else {
1060                        error("remote-curl: unknown command '%s' from git", buf.buf);
1061                        return 1;
1062                }
1063                strbuf_reset(&buf);
1064        } while (1);
1065
1066        http_cleanup();
1067
1068        return 0;
1069}