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