http.con commit Merge branch 'db/send-pack-user-signingkey' into maint (7e7ce32)
   1#include "git-compat-util.h"
   2#include "http.h"
   3#include "pack.h"
   4#include "sideband.h"
   5#include "run-command.h"
   6#include "url.h"
   7#include "urlmatch.h"
   8#include "credential.h"
   9#include "version.h"
  10#include "pkt-line.h"
  11#include "gettext.h"
  12
  13int active_requests;
  14int http_is_verbose;
  15size_t http_post_buffer = 16 * LARGE_PACKET_MAX;
  16
  17#if LIBCURL_VERSION_NUM >= 0x070a06
  18#define LIBCURL_CAN_HANDLE_AUTH_ANY
  19#endif
  20
  21static int min_curl_sessions = 1;
  22static int curl_session_count;
  23#ifdef USE_CURL_MULTI
  24static int max_requests = -1;
  25static CURLM *curlm;
  26#endif
  27#ifndef NO_CURL_EASY_DUPHANDLE
  28static CURL *curl_default;
  29#endif
  30
  31#define PREV_BUF_SIZE 4096
  32#define RANGE_HEADER_SIZE 30
  33
  34char curl_errorstr[CURL_ERROR_SIZE];
  35
  36static int curl_ssl_verify = -1;
  37static int curl_ssl_try;
  38static const char *ssl_cert;
  39static const char *ssl_cipherlist;
  40#if LIBCURL_VERSION_NUM >= 0x070903
  41static const char *ssl_key;
  42#endif
  43#if LIBCURL_VERSION_NUM >= 0x070908
  44static const char *ssl_capath;
  45#endif
  46static const char *ssl_cainfo;
  47static long curl_low_speed_limit = -1;
  48static long curl_low_speed_time = -1;
  49static int curl_ftp_no_epsv;
  50static const char *curl_http_proxy;
  51static const char *curl_cookie_file;
  52static int curl_save_cookies;
  53struct credential http_auth = CREDENTIAL_INIT;
  54static int http_proactive_auth;
  55static const char *user_agent;
  56
  57#if LIBCURL_VERSION_NUM >= 0x071700
  58/* Use CURLOPT_KEYPASSWD as is */
  59#elif LIBCURL_VERSION_NUM >= 0x070903
  60#define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
  61#else
  62#define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
  63#endif
  64
  65static struct credential cert_auth = CREDENTIAL_INIT;
  66static int ssl_cert_password_required;
  67#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
  68static unsigned long http_auth_methods = CURLAUTH_ANY;
  69#endif
  70
  71static struct curl_slist *pragma_header;
  72static struct curl_slist *no_pragma_header;
  73
  74static struct active_request_slot *active_queue_head;
  75
  76static char *cached_accept_language;
  77
  78size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
  79{
  80        size_t size = eltsize * nmemb;
  81        struct buffer *buffer = buffer_;
  82
  83        if (size > buffer->buf.len - buffer->posn)
  84                size = buffer->buf.len - buffer->posn;
  85        memcpy(ptr, buffer->buf.buf + buffer->posn, size);
  86        buffer->posn += size;
  87
  88        return size;
  89}
  90
  91#ifndef NO_CURL_IOCTL
  92curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
  93{
  94        struct buffer *buffer = clientp;
  95
  96        switch (cmd) {
  97        case CURLIOCMD_NOP:
  98                return CURLIOE_OK;
  99
 100        case CURLIOCMD_RESTARTREAD:
 101                buffer->posn = 0;
 102                return CURLIOE_OK;
 103
 104        default:
 105                return CURLIOE_UNKNOWNCMD;
 106        }
 107}
 108#endif
 109
 110size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
 111{
 112        size_t size = eltsize * nmemb;
 113        struct strbuf *buffer = buffer_;
 114
 115        strbuf_add(buffer, ptr, size);
 116        return size;
 117}
 118
 119size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
 120{
 121        return eltsize * nmemb;
 122}
 123
 124static void closedown_active_slot(struct active_request_slot *slot)
 125{
 126        active_requests--;
 127        slot->in_use = 0;
 128}
 129
 130static void finish_active_slot(struct active_request_slot *slot)
 131{
 132        closedown_active_slot(slot);
 133        curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
 134
 135        if (slot->finished != NULL)
 136                (*slot->finished) = 1;
 137
 138        /* Store slot results so they can be read after the slot is reused */
 139        if (slot->results != NULL) {
 140                slot->results->curl_result = slot->curl_result;
 141                slot->results->http_code = slot->http_code;
 142#if LIBCURL_VERSION_NUM >= 0x070a08
 143                curl_easy_getinfo(slot->curl, CURLINFO_HTTPAUTH_AVAIL,
 144                                  &slot->results->auth_avail);
 145#else
 146                slot->results->auth_avail = 0;
 147#endif
 148        }
 149
 150        /* Run callback if appropriate */
 151        if (slot->callback_func != NULL)
 152                slot->callback_func(slot->callback_data);
 153}
 154
 155#ifdef USE_CURL_MULTI
 156static void process_curl_messages(void)
 157{
 158        int num_messages;
 159        struct active_request_slot *slot;
 160        CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
 161
 162        while (curl_message != NULL) {
 163                if (curl_message->msg == CURLMSG_DONE) {
 164                        int curl_result = curl_message->data.result;
 165                        slot = active_queue_head;
 166                        while (slot != NULL &&
 167                               slot->curl != curl_message->easy_handle)
 168                                slot = slot->next;
 169                        if (slot != NULL) {
 170                                curl_multi_remove_handle(curlm, slot->curl);
 171                                slot->curl_result = curl_result;
 172                                finish_active_slot(slot);
 173                        } else {
 174                                fprintf(stderr, "Received DONE message for unknown request!\n");
 175                        }
 176                } else {
 177                        fprintf(stderr, "Unknown CURL message received: %d\n",
 178                                (int)curl_message->msg);
 179                }
 180                curl_message = curl_multi_info_read(curlm, &num_messages);
 181        }
 182}
 183#endif
 184
 185static int http_options(const char *var, const char *value, void *cb)
 186{
 187        if (!strcmp("http.sslverify", var)) {
 188                curl_ssl_verify = git_config_bool(var, value);
 189                return 0;
 190        }
 191        if (!strcmp("http.sslcipherlist", var))
 192                return git_config_string(&ssl_cipherlist, var, value);
 193        if (!strcmp("http.sslcert", var))
 194                return git_config_string(&ssl_cert, var, value);
 195#if LIBCURL_VERSION_NUM >= 0x070903
 196        if (!strcmp("http.sslkey", var))
 197                return git_config_string(&ssl_key, var, value);
 198#endif
 199#if LIBCURL_VERSION_NUM >= 0x070908
 200        if (!strcmp("http.sslcapath", var))
 201                return git_config_string(&ssl_capath, var, value);
 202#endif
 203        if (!strcmp("http.sslcainfo", var))
 204                return git_config_string(&ssl_cainfo, var, value);
 205        if (!strcmp("http.sslcertpasswordprotected", var)) {
 206                ssl_cert_password_required = git_config_bool(var, value);
 207                return 0;
 208        }
 209        if (!strcmp("http.ssltry", var)) {
 210                curl_ssl_try = git_config_bool(var, value);
 211                return 0;
 212        }
 213        if (!strcmp("http.minsessions", var)) {
 214                min_curl_sessions = git_config_int(var, value);
 215#ifndef USE_CURL_MULTI
 216                if (min_curl_sessions > 1)
 217                        min_curl_sessions = 1;
 218#endif
 219                return 0;
 220        }
 221#ifdef USE_CURL_MULTI
 222        if (!strcmp("http.maxrequests", var)) {
 223                max_requests = git_config_int(var, value);
 224                return 0;
 225        }
 226#endif
 227        if (!strcmp("http.lowspeedlimit", var)) {
 228                curl_low_speed_limit = (long)git_config_int(var, value);
 229                return 0;
 230        }
 231        if (!strcmp("http.lowspeedtime", var)) {
 232                curl_low_speed_time = (long)git_config_int(var, value);
 233                return 0;
 234        }
 235
 236        if (!strcmp("http.noepsv", var)) {
 237                curl_ftp_no_epsv = git_config_bool(var, value);
 238                return 0;
 239        }
 240        if (!strcmp("http.proxy", var))
 241                return git_config_string(&curl_http_proxy, var, value);
 242
 243        if (!strcmp("http.cookiefile", var))
 244                return git_config_string(&curl_cookie_file, var, value);
 245        if (!strcmp("http.savecookies", var)) {
 246                curl_save_cookies = git_config_bool(var, value);
 247                return 0;
 248        }
 249
 250        if (!strcmp("http.postbuffer", var)) {
 251                http_post_buffer = git_config_int(var, value);
 252                if (http_post_buffer < LARGE_PACKET_MAX)
 253                        http_post_buffer = LARGE_PACKET_MAX;
 254                return 0;
 255        }
 256
 257        if (!strcmp("http.useragent", var))
 258                return git_config_string(&user_agent, var, value);
 259
 260        /* Fall back on the default ones */
 261        return git_default_config(var, value, cb);
 262}
 263
 264static void init_curl_http_auth(CURL *result)
 265{
 266        if (!http_auth.username)
 267                return;
 268
 269        credential_fill(&http_auth);
 270
 271#if LIBCURL_VERSION_NUM >= 0x071301
 272        curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username);
 273        curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password);
 274#else
 275        {
 276                static struct strbuf up = STRBUF_INIT;
 277                /*
 278                 * Note that we assume we only ever have a single set of
 279                 * credentials in a given program run, so we do not have
 280                 * to worry about updating this buffer, only setting its
 281                 * initial value.
 282                 */
 283                if (!up.len)
 284                        strbuf_addf(&up, "%s:%s",
 285                                http_auth.username, http_auth.password);
 286                curl_easy_setopt(result, CURLOPT_USERPWD, up.buf);
 287        }
 288#endif
 289}
 290
 291static int has_cert_password(void)
 292{
 293        if (ssl_cert == NULL || ssl_cert_password_required != 1)
 294                return 0;
 295        if (!cert_auth.password) {
 296                cert_auth.protocol = xstrdup("cert");
 297                cert_auth.username = xstrdup("");
 298                cert_auth.path = xstrdup(ssl_cert);
 299                credential_fill(&cert_auth);
 300        }
 301        return 1;
 302}
 303
 304#if LIBCURL_VERSION_NUM >= 0x071900
 305static void set_curl_keepalive(CURL *c)
 306{
 307        curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
 308}
 309
 310#elif LIBCURL_VERSION_NUM >= 0x071000
 311static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
 312{
 313        int ka = 1;
 314        int rc;
 315        socklen_t len = (socklen_t)sizeof(ka);
 316
 317        if (type != CURLSOCKTYPE_IPCXN)
 318                return 0;
 319
 320        rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
 321        if (rc < 0)
 322                warning("unable to set SO_KEEPALIVE on socket %s",
 323                        strerror(errno));
 324
 325        return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
 326}
 327
 328static void set_curl_keepalive(CURL *c)
 329{
 330        curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
 331}
 332
 333#else
 334static void set_curl_keepalive(CURL *c)
 335{
 336        /* not supported on older curl versions */
 337}
 338#endif
 339
 340static CURL *get_curl_handle(void)
 341{
 342        CURL *result = curl_easy_init();
 343
 344        if (!result)
 345                die("curl_easy_init failed");
 346
 347        if (!curl_ssl_verify) {
 348                curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
 349                curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
 350        } else {
 351                /* Verify authenticity of the peer's certificate */
 352                curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
 353                /* The name in the cert must match whom we tried to connect */
 354                curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
 355        }
 356
 357#if LIBCURL_VERSION_NUM >= 0x070907
 358        curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
 359#endif
 360#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
 361        curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
 362#endif
 363
 364        if (http_proactive_auth)
 365                init_curl_http_auth(result);
 366
 367        if (getenv("GIT_SSL_CIPHER_LIST"))
 368                ssl_cipherlist = getenv("GIT_SSL_CIPHER_LIST");
 369
 370        if (ssl_cipherlist != NULL && *ssl_cipherlist)
 371                curl_easy_setopt(result, CURLOPT_SSL_CIPHER_LIST,
 372                                ssl_cipherlist);
 373
 374        if (ssl_cert != NULL)
 375                curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
 376        if (has_cert_password())
 377                curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password);
 378#if LIBCURL_VERSION_NUM >= 0x070903
 379        if (ssl_key != NULL)
 380                curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
 381#endif
 382#if LIBCURL_VERSION_NUM >= 0x070908
 383        if (ssl_capath != NULL)
 384                curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
 385#endif
 386        if (ssl_cainfo != NULL)
 387                curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
 388
 389        if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
 390                curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
 391                                 curl_low_speed_limit);
 392                curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
 393                                 curl_low_speed_time);
 394        }
 395
 396        curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
 397#if LIBCURL_VERSION_NUM >= 0x071301
 398        curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
 399#elif LIBCURL_VERSION_NUM >= 0x071101
 400        curl_easy_setopt(result, CURLOPT_POST301, 1);
 401#endif
 402
 403        if (getenv("GIT_CURL_VERBOSE"))
 404                curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
 405
 406        curl_easy_setopt(result, CURLOPT_USERAGENT,
 407                user_agent ? user_agent : git_user_agent());
 408
 409        if (curl_ftp_no_epsv)
 410                curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
 411
 412#ifdef CURLOPT_USE_SSL
 413        if (curl_ssl_try)
 414                curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
 415#endif
 416
 417        if (curl_http_proxy) {
 418                curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
 419        }
 420#if LIBCURL_VERSION_NUM >= 0x070a07
 421        curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
 422#endif
 423
 424        set_curl_keepalive(result);
 425
 426        return result;
 427}
 428
 429static void set_from_env(const char **var, const char *envname)
 430{
 431        const char *val = getenv(envname);
 432        if (val)
 433                *var = val;
 434}
 435
 436void http_init(struct remote *remote, const char *url, int proactive_auth)
 437{
 438        char *low_speed_limit;
 439        char *low_speed_time;
 440        char *normalized_url;
 441        struct urlmatch_config config = { STRING_LIST_INIT_DUP };
 442
 443        config.section = "http";
 444        config.key = NULL;
 445        config.collect_fn = http_options;
 446        config.cascade_fn = git_default_config;
 447        config.cb = NULL;
 448
 449        http_is_verbose = 0;
 450        normalized_url = url_normalize(url, &config.url);
 451
 452        git_config(urlmatch_config_entry, &config);
 453        free(normalized_url);
 454
 455        if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
 456                die("curl_global_init failed");
 457
 458        http_proactive_auth = proactive_auth;
 459
 460        if (remote && remote->http_proxy)
 461                curl_http_proxy = xstrdup(remote->http_proxy);
 462
 463        pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
 464        no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
 465
 466#ifdef USE_CURL_MULTI
 467        {
 468                char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
 469                if (http_max_requests != NULL)
 470                        max_requests = atoi(http_max_requests);
 471        }
 472
 473        curlm = curl_multi_init();
 474        if (!curlm)
 475                die("curl_multi_init failed");
 476#endif
 477
 478        if (getenv("GIT_SSL_NO_VERIFY"))
 479                curl_ssl_verify = 0;
 480
 481        set_from_env(&ssl_cert, "GIT_SSL_CERT");
 482#if LIBCURL_VERSION_NUM >= 0x070903
 483        set_from_env(&ssl_key, "GIT_SSL_KEY");
 484#endif
 485#if LIBCURL_VERSION_NUM >= 0x070908
 486        set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
 487#endif
 488        set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
 489
 490        set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
 491
 492        low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
 493        if (low_speed_limit != NULL)
 494                curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
 495        low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
 496        if (low_speed_time != NULL)
 497                curl_low_speed_time = strtol(low_speed_time, NULL, 10);
 498
 499        if (curl_ssl_verify == -1)
 500                curl_ssl_verify = 1;
 501
 502        curl_session_count = 0;
 503#ifdef USE_CURL_MULTI
 504        if (max_requests < 1)
 505                max_requests = DEFAULT_MAX_REQUESTS;
 506#endif
 507
 508        if (getenv("GIT_CURL_FTP_NO_EPSV"))
 509                curl_ftp_no_epsv = 1;
 510
 511        if (url) {
 512                credential_from_url(&http_auth, url);
 513                if (!ssl_cert_password_required &&
 514                    getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
 515                    starts_with(url, "https://"))
 516                        ssl_cert_password_required = 1;
 517        }
 518
 519#ifndef NO_CURL_EASY_DUPHANDLE
 520        curl_default = get_curl_handle();
 521#endif
 522}
 523
 524void http_cleanup(void)
 525{
 526        struct active_request_slot *slot = active_queue_head;
 527
 528        while (slot != NULL) {
 529                struct active_request_slot *next = slot->next;
 530                if (slot->curl != NULL) {
 531#ifdef USE_CURL_MULTI
 532                        curl_multi_remove_handle(curlm, slot->curl);
 533#endif
 534                        curl_easy_cleanup(slot->curl);
 535                }
 536                free(slot);
 537                slot = next;
 538        }
 539        active_queue_head = NULL;
 540
 541#ifndef NO_CURL_EASY_DUPHANDLE
 542        curl_easy_cleanup(curl_default);
 543#endif
 544
 545#ifdef USE_CURL_MULTI
 546        curl_multi_cleanup(curlm);
 547#endif
 548        curl_global_cleanup();
 549
 550        curl_slist_free_all(pragma_header);
 551        pragma_header = NULL;
 552
 553        curl_slist_free_all(no_pragma_header);
 554        no_pragma_header = NULL;
 555
 556        if (curl_http_proxy) {
 557                free((void *)curl_http_proxy);
 558                curl_http_proxy = NULL;
 559        }
 560
 561        if (cert_auth.password != NULL) {
 562                memset(cert_auth.password, 0, strlen(cert_auth.password));
 563                free(cert_auth.password);
 564                cert_auth.password = NULL;
 565        }
 566        ssl_cert_password_required = 0;
 567
 568        free(cached_accept_language);
 569        cached_accept_language = NULL;
 570}
 571
 572struct active_request_slot *get_active_slot(void)
 573{
 574        struct active_request_slot *slot = active_queue_head;
 575        struct active_request_slot *newslot;
 576
 577#ifdef USE_CURL_MULTI
 578        int num_transfers;
 579
 580        /* Wait for a slot to open up if the queue is full */
 581        while (active_requests >= max_requests) {
 582                curl_multi_perform(curlm, &num_transfers);
 583                if (num_transfers < active_requests)
 584                        process_curl_messages();
 585        }
 586#endif
 587
 588        while (slot != NULL && slot->in_use)
 589                slot = slot->next;
 590
 591        if (slot == NULL) {
 592                newslot = xmalloc(sizeof(*newslot));
 593                newslot->curl = NULL;
 594                newslot->in_use = 0;
 595                newslot->next = NULL;
 596
 597                slot = active_queue_head;
 598                if (slot == NULL) {
 599                        active_queue_head = newslot;
 600                } else {
 601                        while (slot->next != NULL)
 602                                slot = slot->next;
 603                        slot->next = newslot;
 604                }
 605                slot = newslot;
 606        }
 607
 608        if (slot->curl == NULL) {
 609#ifdef NO_CURL_EASY_DUPHANDLE
 610                slot->curl = get_curl_handle();
 611#else
 612                slot->curl = curl_easy_duphandle(curl_default);
 613#endif
 614                curl_session_count++;
 615        }
 616
 617        active_requests++;
 618        slot->in_use = 1;
 619        slot->results = NULL;
 620        slot->finished = NULL;
 621        slot->callback_data = NULL;
 622        slot->callback_func = NULL;
 623        curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
 624        if (curl_save_cookies)
 625                curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
 626        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
 627        curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 628        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
 629        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
 630        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
 631        curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
 632        curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
 633        curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 634        curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
 635#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
 636        curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
 637#endif
 638        if (http_auth.password)
 639                init_curl_http_auth(slot->curl);
 640
 641        return slot;
 642}
 643
 644int start_active_slot(struct active_request_slot *slot)
 645{
 646#ifdef USE_CURL_MULTI
 647        CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
 648        int num_transfers;
 649
 650        if (curlm_result != CURLM_OK &&
 651            curlm_result != CURLM_CALL_MULTI_PERFORM) {
 652                active_requests--;
 653                slot->in_use = 0;
 654                return 0;
 655        }
 656
 657        /*
 658         * We know there must be something to do, since we just added
 659         * something.
 660         */
 661        curl_multi_perform(curlm, &num_transfers);
 662#endif
 663        return 1;
 664}
 665
 666#ifdef USE_CURL_MULTI
 667struct fill_chain {
 668        void *data;
 669        int (*fill)(void *);
 670        struct fill_chain *next;
 671};
 672
 673static struct fill_chain *fill_cfg;
 674
 675void add_fill_function(void *data, int (*fill)(void *))
 676{
 677        struct fill_chain *new = xmalloc(sizeof(*new));
 678        struct fill_chain **linkp = &fill_cfg;
 679        new->data = data;
 680        new->fill = fill;
 681        new->next = NULL;
 682        while (*linkp)
 683                linkp = &(*linkp)->next;
 684        *linkp = new;
 685}
 686
 687void fill_active_slots(void)
 688{
 689        struct active_request_slot *slot = active_queue_head;
 690
 691        while (active_requests < max_requests) {
 692                struct fill_chain *fill;
 693                for (fill = fill_cfg; fill; fill = fill->next)
 694                        if (fill->fill(fill->data))
 695                                break;
 696
 697                if (!fill)
 698                        break;
 699        }
 700
 701        while (slot != NULL) {
 702                if (!slot->in_use && slot->curl != NULL
 703                        && curl_session_count > min_curl_sessions) {
 704                        curl_easy_cleanup(slot->curl);
 705                        slot->curl = NULL;
 706                        curl_session_count--;
 707                }
 708                slot = slot->next;
 709        }
 710}
 711
 712void step_active_slots(void)
 713{
 714        int num_transfers;
 715        CURLMcode curlm_result;
 716
 717        do {
 718                curlm_result = curl_multi_perform(curlm, &num_transfers);
 719        } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
 720        if (num_transfers < active_requests) {
 721                process_curl_messages();
 722                fill_active_slots();
 723        }
 724}
 725#endif
 726
 727void run_active_slot(struct active_request_slot *slot)
 728{
 729#ifdef USE_CURL_MULTI
 730        fd_set readfds;
 731        fd_set writefds;
 732        fd_set excfds;
 733        int max_fd;
 734        struct timeval select_timeout;
 735        int finished = 0;
 736
 737        slot->finished = &finished;
 738        while (!finished) {
 739                step_active_slots();
 740
 741                if (slot->in_use) {
 742#if LIBCURL_VERSION_NUM >= 0x070f04
 743                        long curl_timeout;
 744                        curl_multi_timeout(curlm, &curl_timeout);
 745                        if (curl_timeout == 0) {
 746                                continue;
 747                        } else if (curl_timeout == -1) {
 748                                select_timeout.tv_sec  = 0;
 749                                select_timeout.tv_usec = 50000;
 750                        } else {
 751                                select_timeout.tv_sec  =  curl_timeout / 1000;
 752                                select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
 753                        }
 754#else
 755                        select_timeout.tv_sec  = 0;
 756                        select_timeout.tv_usec = 50000;
 757#endif
 758
 759                        max_fd = -1;
 760                        FD_ZERO(&readfds);
 761                        FD_ZERO(&writefds);
 762                        FD_ZERO(&excfds);
 763                        curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
 764
 765                        /*
 766                         * It can happen that curl_multi_timeout returns a pathologically
 767                         * long timeout when curl_multi_fdset returns no file descriptors
 768                         * to read.  See commit message for more details.
 769                         */
 770                        if (max_fd < 0 &&
 771                            (select_timeout.tv_sec > 0 ||
 772                             select_timeout.tv_usec > 50000)) {
 773                                select_timeout.tv_sec  = 0;
 774                                select_timeout.tv_usec = 50000;
 775                        }
 776
 777                        select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
 778                }
 779        }
 780#else
 781        while (slot->in_use) {
 782                slot->curl_result = curl_easy_perform(slot->curl);
 783                finish_active_slot(slot);
 784        }
 785#endif
 786}
 787
 788static void release_active_slot(struct active_request_slot *slot)
 789{
 790        closedown_active_slot(slot);
 791        if (slot->curl && curl_session_count > min_curl_sessions) {
 792#ifdef USE_CURL_MULTI
 793                curl_multi_remove_handle(curlm, slot->curl);
 794#endif
 795                curl_easy_cleanup(slot->curl);
 796                slot->curl = NULL;
 797                curl_session_count--;
 798        }
 799#ifdef USE_CURL_MULTI
 800        fill_active_slots();
 801#endif
 802}
 803
 804void finish_all_active_slots(void)
 805{
 806        struct active_request_slot *slot = active_queue_head;
 807
 808        while (slot != NULL)
 809                if (slot->in_use) {
 810                        run_active_slot(slot);
 811                        slot = active_queue_head;
 812                } else {
 813                        slot = slot->next;
 814                }
 815}
 816
 817/* Helpers for modifying and creating URLs */
 818static inline int needs_quote(int ch)
 819{
 820        if (((ch >= 'A') && (ch <= 'Z'))
 821                        || ((ch >= 'a') && (ch <= 'z'))
 822                        || ((ch >= '0') && (ch <= '9'))
 823                        || (ch == '/')
 824                        || (ch == '-')
 825                        || (ch == '.'))
 826                return 0;
 827        return 1;
 828}
 829
 830static char *quote_ref_url(const char *base, const char *ref)
 831{
 832        struct strbuf buf = STRBUF_INIT;
 833        const char *cp;
 834        int ch;
 835
 836        end_url_with_slash(&buf, base);
 837
 838        for (cp = ref; (ch = *cp) != 0; cp++)
 839                if (needs_quote(ch))
 840                        strbuf_addf(&buf, "%%%02x", ch);
 841                else
 842                        strbuf_addch(&buf, *cp);
 843
 844        return strbuf_detach(&buf, NULL);
 845}
 846
 847void append_remote_object_url(struct strbuf *buf, const char *url,
 848                              const char *hex,
 849                              int only_two_digit_prefix)
 850{
 851        end_url_with_slash(buf, url);
 852
 853        strbuf_addf(buf, "objects/%.*s/", 2, hex);
 854        if (!only_two_digit_prefix)
 855                strbuf_addf(buf, "%s", hex+2);
 856}
 857
 858char *get_remote_object_url(const char *url, const char *hex,
 859                            int only_two_digit_prefix)
 860{
 861        struct strbuf buf = STRBUF_INIT;
 862        append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
 863        return strbuf_detach(&buf, NULL);
 864}
 865
 866static int handle_curl_result(struct slot_results *results)
 867{
 868        /*
 869         * If we see a failing http code with CURLE_OK, we have turned off
 870         * FAILONERROR (to keep the server's custom error response), and should
 871         * translate the code into failure here.
 872         */
 873        if (results->curl_result == CURLE_OK &&
 874            results->http_code >= 400) {
 875                results->curl_result = CURLE_HTTP_RETURNED_ERROR;
 876                /*
 877                 * Normally curl will already have put the "reason phrase"
 878                 * from the server into curl_errorstr; unfortunately without
 879                 * FAILONERROR it is lost, so we can give only the numeric
 880                 * status code.
 881                 */
 882                snprintf(curl_errorstr, sizeof(curl_errorstr),
 883                         "The requested URL returned error: %ld",
 884                         results->http_code);
 885        }
 886
 887        if (results->curl_result == CURLE_OK) {
 888                credential_approve(&http_auth);
 889                return HTTP_OK;
 890        } else if (missing_target(results))
 891                return HTTP_MISSING_TARGET;
 892        else if (results->http_code == 401) {
 893                if (http_auth.username && http_auth.password) {
 894                        credential_reject(&http_auth);
 895                        return HTTP_NOAUTH;
 896                } else {
 897#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
 898                        http_auth_methods &= ~CURLAUTH_GSSNEGOTIATE;
 899#endif
 900                        return HTTP_REAUTH;
 901                }
 902        } else {
 903#if LIBCURL_VERSION_NUM >= 0x070c00
 904                if (!curl_errorstr[0])
 905                        strlcpy(curl_errorstr,
 906                                curl_easy_strerror(results->curl_result),
 907                                sizeof(curl_errorstr));
 908#endif
 909                return HTTP_ERROR;
 910        }
 911}
 912
 913int run_one_slot(struct active_request_slot *slot,
 914                 struct slot_results *results)
 915{
 916        slot->results = results;
 917        if (!start_active_slot(slot)) {
 918                snprintf(curl_errorstr, sizeof(curl_errorstr),
 919                         "failed to start HTTP request");
 920                return HTTP_START_FAILED;
 921        }
 922
 923        run_active_slot(slot);
 924        return handle_curl_result(results);
 925}
 926
 927static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
 928{
 929        char *ptr;
 930        CURLcode ret;
 931
 932        strbuf_reset(buf);
 933        ret = curl_easy_getinfo(curl, info, &ptr);
 934        if (!ret && ptr)
 935                strbuf_addstr(buf, ptr);
 936        return ret;
 937}
 938
 939/*
 940 * Check for and extract a content-type parameter. "raw"
 941 * should be positioned at the start of the potential
 942 * parameter, with any whitespace already removed.
 943 *
 944 * "name" is the name of the parameter. The value is appended
 945 * to "out".
 946 */
 947static int extract_param(const char *raw, const char *name,
 948                         struct strbuf *out)
 949{
 950        size_t len = strlen(name);
 951
 952        if (strncasecmp(raw, name, len))
 953                return -1;
 954        raw += len;
 955
 956        if (*raw != '=')
 957                return -1;
 958        raw++;
 959
 960        while (*raw && !isspace(*raw) && *raw != ';')
 961                strbuf_addch(out, *raw++);
 962        return 0;
 963}
 964
 965/*
 966 * Extract a normalized version of the content type, with any
 967 * spaces suppressed, all letters lowercased, and no trailing ";"
 968 * or parameters.
 969 *
 970 * Note that we will silently remove even invalid whitespace. For
 971 * example, "text / plain" is specifically forbidden by RFC 2616,
 972 * but "text/plain" is the only reasonable output, and this keeps
 973 * our code simple.
 974 *
 975 * If the "charset" argument is not NULL, store the value of any
 976 * charset parameter there.
 977 *
 978 * Example:
 979 *   "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
 980 *   "text / plain" -> "text/plain"
 981 */
 982static void extract_content_type(struct strbuf *raw, struct strbuf *type,
 983                                 struct strbuf *charset)
 984{
 985        const char *p;
 986
 987        strbuf_reset(type);
 988        strbuf_grow(type, raw->len);
 989        for (p = raw->buf; *p; p++) {
 990                if (isspace(*p))
 991                        continue;
 992                if (*p == ';') {
 993                        p++;
 994                        break;
 995                }
 996                strbuf_addch(type, tolower(*p));
 997        }
 998
 999        if (!charset)
1000                return;
1001
1002        strbuf_reset(charset);
1003        while (*p) {
1004                while (isspace(*p) || *p == ';')
1005                        p++;
1006                if (!extract_param(p, "charset", charset))
1007                        return;
1008                while (*p && !isspace(*p))
1009                        p++;
1010        }
1011
1012        if (!charset->len && starts_with(type->buf, "text/"))
1013                strbuf_addstr(charset, "ISO-8859-1");
1014}
1015
1016static void write_accept_language(struct strbuf *buf)
1017{
1018        /*
1019         * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1020         * that, q-value will be smaller than 0.001, the minimum q-value the
1021         * HTTP specification allows. See
1022         * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1023         */
1024        const int MAX_DECIMAL_PLACES = 3;
1025        const int MAX_LANGUAGE_TAGS = 1000;
1026        const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE = 4000;
1027        char **language_tags = NULL;
1028        int num_langs = 0;
1029        const char *s = get_preferred_languages();
1030        int i;
1031        struct strbuf tag = STRBUF_INIT;
1032
1033        /* Don't add Accept-Language header if no language is preferred. */
1034        if (!s)
1035                return;
1036
1037        /*
1038         * Split the colon-separated string of preferred languages into
1039         * language_tags array.
1040         */
1041        do {
1042                /* collect language tag */
1043                for (; *s && (isalnum(*s) || *s == '_'); s++)
1044                        strbuf_addch(&tag, *s == '_' ? '-' : *s);
1045
1046                /* skip .codeset, @modifier and any other unnecessary parts */
1047                while (*s && *s != ':')
1048                        s++;
1049
1050                if (tag.len) {
1051                        num_langs++;
1052                        REALLOC_ARRAY(language_tags, num_langs);
1053                        language_tags[num_langs - 1] = strbuf_detach(&tag, NULL);
1054                        if (num_langs >= MAX_LANGUAGE_TAGS - 1) /* -1 for '*' */
1055                                break;
1056                }
1057        } while (*s++);
1058
1059        /* write Accept-Language header into buf */
1060        if (num_langs) {
1061                int last_buf_len = 0;
1062                int max_q;
1063                int decimal_places;
1064                char q_format[32];
1065
1066                /* add '*' */
1067                REALLOC_ARRAY(language_tags, num_langs + 1);
1068                language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
1069
1070                /* compute decimal_places */
1071                for (max_q = 1, decimal_places = 0;
1072                     max_q < num_langs && decimal_places <= MAX_DECIMAL_PLACES;
1073                     decimal_places++, max_q *= 10)
1074                        ;
1075
1076                sprintf(q_format, ";q=0.%%0%dd", decimal_places);
1077
1078                strbuf_addstr(buf, "Accept-Language: ");
1079
1080                for (i = 0; i < num_langs; i++) {
1081                        if (i > 0)
1082                                strbuf_addstr(buf, ", ");
1083
1084                        strbuf_addstr(buf, language_tags[i]);
1085
1086                        if (i > 0)
1087                                strbuf_addf(buf, q_format, max_q - i);
1088
1089                        if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) {
1090                                strbuf_remove(buf, last_buf_len, buf->len - last_buf_len);
1091                                break;
1092                        }
1093
1094                        last_buf_len = buf->len;
1095                }
1096        }
1097
1098        /* free language tags -- last one is a static '*' */
1099        for (i = 0; i < num_langs - 1; i++)
1100                free(language_tags[i]);
1101        free(language_tags);
1102}
1103
1104/*
1105 * Get an Accept-Language header which indicates user's preferred languages.
1106 *
1107 * Examples:
1108 *   LANGUAGE= -> ""
1109 *   LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1110 *   LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1111 *   LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1112 *   LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1113 *   LANGUAGE= LANG=C -> ""
1114 */
1115static const char *get_accept_language(void)
1116{
1117        if (!cached_accept_language) {
1118                struct strbuf buf = STRBUF_INIT;
1119                write_accept_language(&buf);
1120                if (buf.len > 0)
1121                        cached_accept_language = strbuf_detach(&buf, NULL);
1122        }
1123
1124        return cached_accept_language;
1125}
1126
1127/* http_request() targets */
1128#define HTTP_REQUEST_STRBUF     0
1129#define HTTP_REQUEST_FILE       1
1130
1131static int http_request(const char *url,
1132                        void *result, int target,
1133                        const struct http_get_options *options)
1134{
1135        struct active_request_slot *slot;
1136        struct slot_results results;
1137        struct curl_slist *headers = NULL;
1138        struct strbuf buf = STRBUF_INIT;
1139        const char *accept_language;
1140        int ret;
1141
1142        slot = get_active_slot();
1143        curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1144
1145        if (result == NULL) {
1146                curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
1147        } else {
1148                curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
1149                curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
1150
1151                if (target == HTTP_REQUEST_FILE) {
1152                        long posn = ftell(result);
1153                        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1154                                         fwrite);
1155                        if (posn > 0) {
1156                                strbuf_addf(&buf, "Range: bytes=%ld-", posn);
1157                                headers = curl_slist_append(headers, buf.buf);
1158                                strbuf_reset(&buf);
1159                        }
1160                } else
1161                        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
1162                                         fwrite_buffer);
1163        }
1164
1165        accept_language = get_accept_language();
1166
1167        if (accept_language)
1168                headers = curl_slist_append(headers, accept_language);
1169
1170        strbuf_addstr(&buf, "Pragma:");
1171        if (options && options->no_cache)
1172                strbuf_addstr(&buf, " no-cache");
1173        if (options && options->keep_error)
1174                curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
1175
1176        headers = curl_slist_append(headers, buf.buf);
1177
1178        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1179        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
1180        curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "gzip");
1181
1182        ret = run_one_slot(slot, &results);
1183
1184        if (options && options->content_type) {
1185                struct strbuf raw = STRBUF_INIT;
1186                curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw);
1187                extract_content_type(&raw, options->content_type,
1188                                     options->charset);
1189                strbuf_release(&raw);
1190        }
1191
1192        if (options && options->effective_url)
1193                curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
1194                                options->effective_url);
1195
1196        curl_slist_free_all(headers);
1197        strbuf_release(&buf);
1198
1199        return ret;
1200}
1201
1202/*
1203 * Update the "base" url to a more appropriate value, as deduced by
1204 * redirects seen when requesting a URL starting with "url".
1205 *
1206 * The "asked" parameter is a URL that we asked curl to access, and must begin
1207 * with "base".
1208 *
1209 * The "got" parameter is the URL that curl reported to us as where we ended
1210 * up.
1211 *
1212 * Returns 1 if we updated the base url, 0 otherwise.
1213 *
1214 * Our basic strategy is to compare "base" and "asked" to find the bits
1215 * specific to our request. We then strip those bits off of "got" to yield the
1216 * new base. So for example, if our base is "http://example.com/foo.git",
1217 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1218 * with "https://other.example.com/foo.git/info/refs". We would want the
1219 * new URL to become "https://other.example.com/foo.git".
1220 *
1221 * Note that this assumes a sane redirect scheme. It's entirely possible
1222 * in the example above to end up at a URL that does not even end in
1223 * "info/refs".  In such a case we simply punt, as there is not much we can
1224 * do (and such a scheme is unlikely to represent a real git repository,
1225 * which means we are likely about to abort anyway).
1226 */
1227static int update_url_from_redirect(struct strbuf *base,
1228                                    const char *asked,
1229                                    const struct strbuf *got)
1230{
1231        const char *tail;
1232        size_t tail_len;
1233
1234        if (!strcmp(asked, got->buf))
1235                return 0;
1236
1237        if (!skip_prefix(asked, base->buf, &tail))
1238                die("BUG: update_url_from_redirect: %s is not a superset of %s",
1239                    asked, base->buf);
1240
1241        tail_len = strlen(tail);
1242
1243        if (got->len < tail_len ||
1244            strcmp(tail, got->buf + got->len - tail_len))
1245                return 0; /* insane redirect scheme */
1246
1247        strbuf_reset(base);
1248        strbuf_add(base, got->buf, got->len - tail_len);
1249        return 1;
1250}
1251
1252static int http_request_reauth(const char *url,
1253                               void *result, int target,
1254                               struct http_get_options *options)
1255{
1256        int ret = http_request(url, result, target, options);
1257
1258        if (options && options->effective_url && options->base_url) {
1259                if (update_url_from_redirect(options->base_url,
1260                                             url, options->effective_url)) {
1261                        credential_from_url(&http_auth, options->base_url->buf);
1262                        url = options->effective_url->buf;
1263                }
1264        }
1265
1266        if (ret != HTTP_REAUTH)
1267                return ret;
1268
1269        /*
1270         * If we are using KEEP_ERROR, the previous request may have
1271         * put cruft into our output stream; we should clear it out before
1272         * making our next request. We only know how to do this for
1273         * the strbuf case, but that is enough to satisfy current callers.
1274         */
1275        if (options && options->keep_error) {
1276                switch (target) {
1277                case HTTP_REQUEST_STRBUF:
1278                        strbuf_reset(result);
1279                        break;
1280                default:
1281                        die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
1282                }
1283        }
1284
1285        credential_fill(&http_auth);
1286
1287        return http_request(url, result, target, options);
1288}
1289
1290int http_get_strbuf(const char *url,
1291                    struct strbuf *result,
1292                    struct http_get_options *options)
1293{
1294        return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
1295}
1296
1297/*
1298 * Downloads a URL and stores the result in the given file.
1299 *
1300 * If a previous interrupted download is detected (i.e. a previous temporary
1301 * file is still around) the download is resumed.
1302 */
1303static int http_get_file(const char *url, const char *filename,
1304                         struct http_get_options *options)
1305{
1306        int ret;
1307        struct strbuf tmpfile = STRBUF_INIT;
1308        FILE *result;
1309
1310        strbuf_addf(&tmpfile, "%s.temp", filename);
1311        result = fopen(tmpfile.buf, "a");
1312        if (!result) {
1313                error("Unable to open local file %s", tmpfile.buf);
1314                ret = HTTP_ERROR;
1315                goto cleanup;
1316        }
1317
1318        ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
1319        fclose(result);
1320
1321        if (ret == HTTP_OK && move_temp_to_file(tmpfile.buf, filename))
1322                ret = HTTP_ERROR;
1323cleanup:
1324        strbuf_release(&tmpfile);
1325        return ret;
1326}
1327
1328int http_fetch_ref(const char *base, struct ref *ref)
1329{
1330        struct http_get_options options = {0};
1331        char *url;
1332        struct strbuf buffer = STRBUF_INIT;
1333        int ret = -1;
1334
1335        options.no_cache = 1;
1336
1337        url = quote_ref_url(base, ref->name);
1338        if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
1339                strbuf_rtrim(&buffer);
1340                if (buffer.len == 40)
1341                        ret = get_sha1_hex(buffer.buf, ref->old_sha1);
1342                else if (starts_with(buffer.buf, "ref: ")) {
1343                        ref->symref = xstrdup(buffer.buf + 5);
1344                        ret = 0;
1345                }
1346        }
1347
1348        strbuf_release(&buffer);
1349        free(url);
1350        return ret;
1351}
1352
1353/* Helpers for fetching packs */
1354static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
1355{
1356        char *url, *tmp;
1357        struct strbuf buf = STRBUF_INIT;
1358
1359        if (http_is_verbose)
1360                fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1));
1361
1362        end_url_with_slash(&buf, base_url);
1363        strbuf_addf(&buf, "objects/pack/pack-%s.idx", sha1_to_hex(sha1));
1364        url = strbuf_detach(&buf, NULL);
1365
1366        strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1));
1367        tmp = strbuf_detach(&buf, NULL);
1368
1369        if (http_get_file(url, tmp, NULL) != HTTP_OK) {
1370                error("Unable to get pack index %s", url);
1371                free(tmp);
1372                tmp = NULL;
1373        }
1374
1375        free(url);
1376        return tmp;
1377}
1378
1379static int fetch_and_setup_pack_index(struct packed_git **packs_head,
1380        unsigned char *sha1, const char *base_url)
1381{
1382        struct packed_git *new_pack;
1383        char *tmp_idx = NULL;
1384        int ret;
1385
1386        if (has_pack_index(sha1)) {
1387                new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1));
1388                if (!new_pack)
1389                        return -1; /* parse_pack_index() already issued error message */
1390                goto add_pack;
1391        }
1392
1393        tmp_idx = fetch_pack_index(sha1, base_url);
1394        if (!tmp_idx)
1395                return -1;
1396
1397        new_pack = parse_pack_index(sha1, tmp_idx);
1398        if (!new_pack) {
1399                unlink(tmp_idx);
1400                free(tmp_idx);
1401
1402                return -1; /* parse_pack_index() already issued error message */
1403        }
1404
1405        ret = verify_pack_index(new_pack);
1406        if (!ret) {
1407                close_pack_index(new_pack);
1408                ret = move_temp_to_file(tmp_idx, sha1_pack_index_name(sha1));
1409        }
1410        free(tmp_idx);
1411        if (ret)
1412                return -1;
1413
1414add_pack:
1415        new_pack->next = *packs_head;
1416        *packs_head = new_pack;
1417        return 0;
1418}
1419
1420int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
1421{
1422        struct http_get_options options = {0};
1423        int ret = 0, i = 0;
1424        char *url, *data;
1425        struct strbuf buf = STRBUF_INIT;
1426        unsigned char sha1[20];
1427
1428        end_url_with_slash(&buf, base_url);
1429        strbuf_addstr(&buf, "objects/info/packs");
1430        url = strbuf_detach(&buf, NULL);
1431
1432        options.no_cache = 1;
1433        ret = http_get_strbuf(url, &buf, &options);
1434        if (ret != HTTP_OK)
1435                goto cleanup;
1436
1437        data = buf.buf;
1438        while (i < buf.len) {
1439                switch (data[i]) {
1440                case 'P':
1441                        i++;
1442                        if (i + 52 <= buf.len &&
1443                            starts_with(data + i, " pack-") &&
1444                            starts_with(data + i + 46, ".pack\n")) {
1445                                get_sha1_hex(data + i + 6, sha1);
1446                                fetch_and_setup_pack_index(packs_head, sha1,
1447                                                      base_url);
1448                                i += 51;
1449                                break;
1450                        }
1451                default:
1452                        while (i < buf.len && data[i] != '\n')
1453                                i++;
1454                }
1455                i++;
1456        }
1457
1458cleanup:
1459        free(url);
1460        return ret;
1461}
1462
1463void release_http_pack_request(struct http_pack_request *preq)
1464{
1465        if (preq->packfile != NULL) {
1466                fclose(preq->packfile);
1467                preq->packfile = NULL;
1468        }
1469        if (preq->range_header != NULL) {
1470                curl_slist_free_all(preq->range_header);
1471                preq->range_header = NULL;
1472        }
1473        preq->slot = NULL;
1474        free(preq->url);
1475        free(preq);
1476}
1477
1478int finish_http_pack_request(struct http_pack_request *preq)
1479{
1480        struct packed_git **lst;
1481        struct packed_git *p = preq->target;
1482        char *tmp_idx;
1483        struct child_process ip = CHILD_PROCESS_INIT;
1484        const char *ip_argv[8];
1485
1486        close_pack_index(p);
1487
1488        fclose(preq->packfile);
1489        preq->packfile = NULL;
1490
1491        lst = preq->lst;
1492        while (*lst != p)
1493                lst = &((*lst)->next);
1494        *lst = (*lst)->next;
1495
1496        tmp_idx = xstrdup(preq->tmpfile);
1497        strcpy(tmp_idx + strlen(tmp_idx) - strlen(".pack.temp"),
1498               ".idx.temp");
1499
1500        ip_argv[0] = "index-pack";
1501        ip_argv[1] = "-o";
1502        ip_argv[2] = tmp_idx;
1503        ip_argv[3] = preq->tmpfile;
1504        ip_argv[4] = NULL;
1505
1506        ip.argv = ip_argv;
1507        ip.git_cmd = 1;
1508        ip.no_stdin = 1;
1509        ip.no_stdout = 1;
1510
1511        if (run_command(&ip)) {
1512                unlink(preq->tmpfile);
1513                unlink(tmp_idx);
1514                free(tmp_idx);
1515                return -1;
1516        }
1517
1518        unlink(sha1_pack_index_name(p->sha1));
1519
1520        if (move_temp_to_file(preq->tmpfile, sha1_pack_name(p->sha1))
1521         || move_temp_to_file(tmp_idx, sha1_pack_index_name(p->sha1))) {
1522                free(tmp_idx);
1523                return -1;
1524        }
1525
1526        install_packed_git(p);
1527        free(tmp_idx);
1528        return 0;
1529}
1530
1531struct http_pack_request *new_http_pack_request(
1532        struct packed_git *target, const char *base_url)
1533{
1534        long prev_posn = 0;
1535        char range[RANGE_HEADER_SIZE];
1536        struct strbuf buf = STRBUF_INIT;
1537        struct http_pack_request *preq;
1538
1539        preq = xcalloc(1, sizeof(*preq));
1540        preq->target = target;
1541
1542        end_url_with_slash(&buf, base_url);
1543        strbuf_addf(&buf, "objects/pack/pack-%s.pack",
1544                sha1_to_hex(target->sha1));
1545        preq->url = strbuf_detach(&buf, NULL);
1546
1547        snprintf(preq->tmpfile, sizeof(preq->tmpfile), "%s.temp",
1548                sha1_pack_name(target->sha1));
1549        preq->packfile = fopen(preq->tmpfile, "a");
1550        if (!preq->packfile) {
1551                error("Unable to open local file %s for pack",
1552                      preq->tmpfile);
1553                goto abort;
1554        }
1555
1556        preq->slot = get_active_slot();
1557        curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile);
1558        curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
1559        curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
1560        curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1561                no_pragma_header);
1562
1563        /*
1564         * If there is data present from a previous transfer attempt,
1565         * resume where it left off
1566         */
1567        prev_posn = ftell(preq->packfile);
1568        if (prev_posn>0) {
1569                if (http_is_verbose)
1570                        fprintf(stderr,
1571                                "Resuming fetch of pack %s at byte %ld\n",
1572                                sha1_to_hex(target->sha1), prev_posn);
1573                sprintf(range, "Range: bytes=%ld-", prev_posn);
1574                preq->range_header = curl_slist_append(NULL, range);
1575                curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1576                        preq->range_header);
1577        }
1578
1579        return preq;
1580
1581abort:
1582        free(preq->url);
1583        free(preq);
1584        return NULL;
1585}
1586
1587/* Helpers for fetching objects (loose) */
1588static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
1589                               void *data)
1590{
1591        unsigned char expn[4096];
1592        size_t size = eltsize * nmemb;
1593        int posn = 0;
1594        struct http_object_request *freq =
1595                (struct http_object_request *)data;
1596        do {
1597                ssize_t retval = xwrite(freq->localfile,
1598                                        (char *) ptr + posn, size - posn);
1599                if (retval < 0)
1600                        return posn;
1601                posn += retval;
1602        } while (posn < size);
1603
1604        freq->stream.avail_in = size;
1605        freq->stream.next_in = (void *)ptr;
1606        do {
1607                freq->stream.next_out = expn;
1608                freq->stream.avail_out = sizeof(expn);
1609                freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
1610                git_SHA1_Update(&freq->c, expn,
1611                                sizeof(expn) - freq->stream.avail_out);
1612        } while (freq->stream.avail_in && freq->zret == Z_OK);
1613        return size;
1614}
1615
1616struct http_object_request *new_http_object_request(const char *base_url,
1617        unsigned char *sha1)
1618{
1619        char *hex = sha1_to_hex(sha1);
1620        const char *filename;
1621        char prevfile[PATH_MAX];
1622        int prevlocal;
1623        char prev_buf[PREV_BUF_SIZE];
1624        ssize_t prev_read = 0;
1625        long prev_posn = 0;
1626        char range[RANGE_HEADER_SIZE];
1627        struct curl_slist *range_header = NULL;
1628        struct http_object_request *freq;
1629
1630        freq = xcalloc(1, sizeof(*freq));
1631        hashcpy(freq->sha1, sha1);
1632        freq->localfile = -1;
1633
1634        filename = sha1_file_name(sha1);
1635        snprintf(freq->tmpfile, sizeof(freq->tmpfile),
1636                 "%s.temp", filename);
1637
1638        snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
1639        unlink_or_warn(prevfile);
1640        rename(freq->tmpfile, prevfile);
1641        unlink_or_warn(freq->tmpfile);
1642
1643        if (freq->localfile != -1)
1644                error("fd leakage in start: %d", freq->localfile);
1645        freq->localfile = open(freq->tmpfile,
1646                               O_WRONLY | O_CREAT | O_EXCL, 0666);
1647        /*
1648         * This could have failed due to the "lazy directory creation";
1649         * try to mkdir the last path component.
1650         */
1651        if (freq->localfile < 0 && errno == ENOENT) {
1652                char *dir = strrchr(freq->tmpfile, '/');
1653                if (dir) {
1654                        *dir = 0;
1655                        mkdir(freq->tmpfile, 0777);
1656                        *dir = '/';
1657                }
1658                freq->localfile = open(freq->tmpfile,
1659                                       O_WRONLY | O_CREAT | O_EXCL, 0666);
1660        }
1661
1662        if (freq->localfile < 0) {
1663                error("Couldn't create temporary file %s: %s",
1664                      freq->tmpfile, strerror(errno));
1665                goto abort;
1666        }
1667
1668        git_inflate_init(&freq->stream);
1669
1670        git_SHA1_Init(&freq->c);
1671
1672        freq->url = get_remote_object_url(base_url, hex, 0);
1673
1674        /*
1675         * If a previous temp file is present, process what was already
1676         * fetched.
1677         */
1678        prevlocal = open(prevfile, O_RDONLY);
1679        if (prevlocal != -1) {
1680                do {
1681                        prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
1682                        if (prev_read>0) {
1683                                if (fwrite_sha1_file(prev_buf,
1684                                                     1,
1685                                                     prev_read,
1686                                                     freq) == prev_read) {
1687                                        prev_posn += prev_read;
1688                                } else {
1689                                        prev_read = -1;
1690                                }
1691                        }
1692                } while (prev_read > 0);
1693                close(prevlocal);
1694        }
1695        unlink_or_warn(prevfile);
1696
1697        /*
1698         * Reset inflate/SHA1 if there was an error reading the previous temp
1699         * file; also rewind to the beginning of the local file.
1700         */
1701        if (prev_read == -1) {
1702                memset(&freq->stream, 0, sizeof(freq->stream));
1703                git_inflate_init(&freq->stream);
1704                git_SHA1_Init(&freq->c);
1705                if (prev_posn>0) {
1706                        prev_posn = 0;
1707                        lseek(freq->localfile, 0, SEEK_SET);
1708                        if (ftruncate(freq->localfile, 0) < 0) {
1709                                error("Couldn't truncate temporary file %s: %s",
1710                                          freq->tmpfile, strerror(errno));
1711                                goto abort;
1712                        }
1713                }
1714        }
1715
1716        freq->slot = get_active_slot();
1717
1718        curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
1719        curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
1720        curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
1721        curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
1722        curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1723
1724        /*
1725         * If we have successfully processed data from a previous fetch
1726         * attempt, only fetch the data we don't already have.
1727         */
1728        if (prev_posn>0) {
1729                if (http_is_verbose)
1730                        fprintf(stderr,
1731                                "Resuming fetch of object %s at byte %ld\n",
1732                                hex, prev_posn);
1733                sprintf(range, "Range: bytes=%ld-", prev_posn);
1734                range_header = curl_slist_append(range_header, range);
1735                curl_easy_setopt(freq->slot->curl,
1736                                 CURLOPT_HTTPHEADER, range_header);
1737        }
1738
1739        return freq;
1740
1741abort:
1742        free(freq->url);
1743        free(freq);
1744        return NULL;
1745}
1746
1747void process_http_object_request(struct http_object_request *freq)
1748{
1749        if (freq->slot == NULL)
1750                return;
1751        freq->curl_result = freq->slot->curl_result;
1752        freq->http_code = freq->slot->http_code;
1753        freq->slot = NULL;
1754}
1755
1756int finish_http_object_request(struct http_object_request *freq)
1757{
1758        struct stat st;
1759
1760        close(freq->localfile);
1761        freq->localfile = -1;
1762
1763        process_http_object_request(freq);
1764
1765        if (freq->http_code == 416) {
1766                warning("requested range invalid; we may already have all the data.");
1767        } else if (freq->curl_result != CURLE_OK) {
1768                if (stat(freq->tmpfile, &st) == 0)
1769                        if (st.st_size == 0)
1770                                unlink_or_warn(freq->tmpfile);
1771                return -1;
1772        }
1773
1774        git_inflate_end(&freq->stream);
1775        git_SHA1_Final(freq->real_sha1, &freq->c);
1776        if (freq->zret != Z_STREAM_END) {
1777                unlink_or_warn(freq->tmpfile);
1778                return -1;
1779        }
1780        if (hashcmp(freq->sha1, freq->real_sha1)) {
1781                unlink_or_warn(freq->tmpfile);
1782                return -1;
1783        }
1784        freq->rename =
1785                move_temp_to_file(freq->tmpfile, sha1_file_name(freq->sha1));
1786
1787        return freq->rename;
1788}
1789
1790void abort_http_object_request(struct http_object_request *freq)
1791{
1792        unlink_or_warn(freq->tmpfile);
1793
1794        release_http_object_request(freq);
1795}
1796
1797void release_http_object_request(struct http_object_request *freq)
1798{
1799        if (freq->localfile != -1) {
1800                close(freq->localfile);
1801                freq->localfile = -1;
1802        }
1803        if (freq->url != NULL) {
1804                free(freq->url);
1805                freq->url = NULL;
1806        }
1807        if (freq->slot != NULL) {
1808                freq->slot->callback_func = NULL;
1809                freq->slot->callback_data = NULL;
1810                release_active_slot(freq->slot);
1811                freq->slot = NULL;
1812        }
1813}