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