http.con commit shell: do not play duplicated definition games to shrink the executable (4cfc24a)
   1#include "http.h"
   2
   3int data_received;
   4int active_requests = 0;
   5
   6#ifdef USE_CURL_MULTI
   7static int max_requests = -1;
   8static CURLM *curlm;
   9#endif
  10#ifndef NO_CURL_EASY_DUPHANDLE
  11static CURL *curl_default;
  12#endif
  13char curl_errorstr[CURL_ERROR_SIZE];
  14
  15static int curl_ssl_verify = -1;
  16static const char *ssl_cert = NULL;
  17#if LIBCURL_VERSION_NUM >= 0x070902
  18static const char *ssl_key = NULL;
  19#endif
  20#if LIBCURL_VERSION_NUM >= 0x070908
  21static const char *ssl_capath = NULL;
  22#endif
  23static const char *ssl_cainfo = NULL;
  24static long curl_low_speed_limit = -1;
  25static long curl_low_speed_time = -1;
  26static int curl_ftp_no_epsv = 0;
  27static char *curl_http_proxy = NULL;
  28
  29static struct curl_slist *pragma_header;
  30
  31static struct active_request_slot *active_queue_head = NULL;
  32
  33size_t fread_buffer(void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
  34{
  35        size_t size = eltsize * nmemb;
  36        struct buffer *buffer = buffer_;
  37
  38        if (size > buffer->buf.len - buffer->posn)
  39                size = buffer->buf.len - buffer->posn;
  40        memcpy(ptr, buffer->buf.buf + buffer->posn, size);
  41        buffer->posn += size;
  42
  43        return size;
  44}
  45
  46size_t fwrite_buffer(const void *ptr, size_t eltsize, size_t nmemb, void *buffer_)
  47{
  48        size_t size = eltsize * nmemb;
  49        struct strbuf *buffer = buffer_;
  50
  51        strbuf_add(buffer, ptr, size);
  52        data_received++;
  53        return size;
  54}
  55
  56size_t fwrite_null(const void *ptr, size_t eltsize, size_t nmemb, void *strbuf)
  57{
  58        data_received++;
  59        return eltsize * nmemb;
  60}
  61
  62static void finish_active_slot(struct active_request_slot *slot);
  63
  64#ifdef USE_CURL_MULTI
  65static void process_curl_messages(void)
  66{
  67        int num_messages;
  68        struct active_request_slot *slot;
  69        CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
  70
  71        while (curl_message != NULL) {
  72                if (curl_message->msg == CURLMSG_DONE) {
  73                        int curl_result = curl_message->data.result;
  74                        slot = active_queue_head;
  75                        while (slot != NULL &&
  76                               slot->curl != curl_message->easy_handle)
  77                                slot = slot->next;
  78                        if (slot != NULL) {
  79                                curl_multi_remove_handle(curlm, slot->curl);
  80                                slot->curl_result = curl_result;
  81                                finish_active_slot(slot);
  82                        } else {
  83                                fprintf(stderr, "Received DONE message for unknown request!\n");
  84                        }
  85                } else {
  86                        fprintf(stderr, "Unknown CURL message received: %d\n",
  87                                (int)curl_message->msg);
  88                }
  89                curl_message = curl_multi_info_read(curlm, &num_messages);
  90        }
  91}
  92#endif
  93
  94static int http_options(const char *var, const char *value, void *cb)
  95{
  96        if (!strcmp("http.sslverify", var)) {
  97                if (curl_ssl_verify == -1) {
  98                        curl_ssl_verify = git_config_bool(var, value);
  99                }
 100                return 0;
 101        }
 102
 103        if (!strcmp("http.sslcert", var)) {
 104                if (ssl_cert == NULL)
 105                        return git_config_string(&ssl_cert, var, value);
 106                return 0;
 107        }
 108#if LIBCURL_VERSION_NUM >= 0x070902
 109        if (!strcmp("http.sslkey", var)) {
 110                if (ssl_key == NULL)
 111                        return git_config_string(&ssl_key, var, value);
 112                return 0;
 113        }
 114#endif
 115#if LIBCURL_VERSION_NUM >= 0x070908
 116        if (!strcmp("http.sslcapath", var)) {
 117                if (ssl_capath == NULL)
 118                        return git_config_string(&ssl_capath, var, value);
 119                return 0;
 120        }
 121#endif
 122        if (!strcmp("http.sslcainfo", var)) {
 123                if (ssl_cainfo == NULL)
 124                        return git_config_string(&ssl_cainfo, var, value);
 125                return 0;
 126        }
 127
 128#ifdef USE_CURL_MULTI
 129        if (!strcmp("http.maxrequests", var)) {
 130                if (max_requests == -1)
 131                        max_requests = git_config_int(var, value);
 132                return 0;
 133        }
 134#endif
 135
 136        if (!strcmp("http.lowspeedlimit", var)) {
 137                if (curl_low_speed_limit == -1)
 138                        curl_low_speed_limit = (long)git_config_int(var, value);
 139                return 0;
 140        }
 141        if (!strcmp("http.lowspeedtime", var)) {
 142                if (curl_low_speed_time == -1)
 143                        curl_low_speed_time = (long)git_config_int(var, value);
 144                return 0;
 145        }
 146
 147        if (!strcmp("http.noepsv", var)) {
 148                curl_ftp_no_epsv = git_config_bool(var, value);
 149                return 0;
 150        }
 151        if (!strcmp("http.proxy", var)) {
 152                if (curl_http_proxy == NULL) {
 153                        if (!value)
 154                                return config_error_nonbool(var);
 155                        curl_http_proxy = xstrdup(value);
 156                }
 157                return 0;
 158        }
 159
 160        /* Fall back on the default ones */
 161        return git_default_config(var, value, cb);
 162}
 163
 164static CURL* get_curl_handle(void)
 165{
 166        CURL* result = curl_easy_init();
 167
 168        curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, curl_ssl_verify);
 169#if LIBCURL_VERSION_NUM >= 0x070907
 170        curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
 171#endif
 172
 173        if (ssl_cert != NULL)
 174                curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
 175#if LIBCURL_VERSION_NUM >= 0x070902
 176        if (ssl_key != NULL)
 177                curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
 178#endif
 179#if LIBCURL_VERSION_NUM >= 0x070908
 180        if (ssl_capath != NULL)
 181                curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
 182#endif
 183        if (ssl_cainfo != NULL)
 184                curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
 185        curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
 186
 187        if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
 188                curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
 189                                 curl_low_speed_limit);
 190                curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
 191                                 curl_low_speed_time);
 192        }
 193
 194        curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
 195
 196        if (getenv("GIT_CURL_VERBOSE"))
 197                curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
 198
 199        curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
 200
 201        if (curl_ftp_no_epsv)
 202                curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
 203
 204        if (curl_http_proxy)
 205                curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
 206
 207        return result;
 208}
 209
 210void http_init(struct remote *remote)
 211{
 212        char *low_speed_limit;
 213        char *low_speed_time;
 214
 215        curl_global_init(CURL_GLOBAL_ALL);
 216
 217        if (remote && remote->http_proxy)
 218                curl_http_proxy = xstrdup(remote->http_proxy);
 219
 220        pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
 221
 222#ifdef USE_CURL_MULTI
 223        {
 224                char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
 225                if (http_max_requests != NULL)
 226                        max_requests = atoi(http_max_requests);
 227        }
 228
 229        curlm = curl_multi_init();
 230        if (curlm == NULL) {
 231                fprintf(stderr, "Error creating curl multi handle.\n");
 232                exit(1);
 233        }
 234#endif
 235
 236        if (getenv("GIT_SSL_NO_VERIFY"))
 237                curl_ssl_verify = 0;
 238
 239        ssl_cert = getenv("GIT_SSL_CERT");
 240#if LIBCURL_VERSION_NUM >= 0x070902
 241        ssl_key = getenv("GIT_SSL_KEY");
 242#endif
 243#if LIBCURL_VERSION_NUM >= 0x070908
 244        ssl_capath = getenv("GIT_SSL_CAPATH");
 245#endif
 246        ssl_cainfo = getenv("GIT_SSL_CAINFO");
 247
 248        low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
 249        if (low_speed_limit != NULL)
 250                curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
 251        low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
 252        if (low_speed_time != NULL)
 253                curl_low_speed_time = strtol(low_speed_time, NULL, 10);
 254
 255        git_config(http_options, NULL);
 256
 257        if (curl_ssl_verify == -1)
 258                curl_ssl_verify = 1;
 259
 260#ifdef USE_CURL_MULTI
 261        if (max_requests < 1)
 262                max_requests = DEFAULT_MAX_REQUESTS;
 263#endif
 264
 265        if (getenv("GIT_CURL_FTP_NO_EPSV"))
 266                curl_ftp_no_epsv = 1;
 267
 268#ifndef NO_CURL_EASY_DUPHANDLE
 269        curl_default = get_curl_handle();
 270#endif
 271}
 272
 273void http_cleanup(void)
 274{
 275        struct active_request_slot *slot = active_queue_head;
 276
 277        while (slot != NULL) {
 278                struct active_request_slot *next = slot->next;
 279                if (slot->curl != NULL) {
 280#ifdef USE_CURL_MULTI
 281                        curl_multi_remove_handle(curlm, slot->curl);
 282#endif
 283                        curl_easy_cleanup(slot->curl);
 284                }
 285                free(slot);
 286                slot = next;
 287        }
 288        active_queue_head = NULL;
 289
 290#ifndef NO_CURL_EASY_DUPHANDLE
 291        curl_easy_cleanup(curl_default);
 292#endif
 293
 294#ifdef USE_CURL_MULTI
 295        curl_multi_cleanup(curlm);
 296#endif
 297        curl_global_cleanup();
 298
 299        curl_slist_free_all(pragma_header);
 300        pragma_header = NULL;
 301
 302        if (curl_http_proxy) {
 303                free(curl_http_proxy);
 304                curl_http_proxy = NULL;
 305        }
 306}
 307
 308struct active_request_slot *get_active_slot(void)
 309{
 310        struct active_request_slot *slot = active_queue_head;
 311        struct active_request_slot *newslot;
 312
 313#ifdef USE_CURL_MULTI
 314        int num_transfers;
 315
 316        /* Wait for a slot to open up if the queue is full */
 317        while (active_requests >= max_requests) {
 318                curl_multi_perform(curlm, &num_transfers);
 319                if (num_transfers < active_requests) {
 320                        process_curl_messages();
 321                }
 322        }
 323#endif
 324
 325        while (slot != NULL && slot->in_use) {
 326                slot = slot->next;
 327        }
 328        if (slot == NULL) {
 329                newslot = xmalloc(sizeof(*newslot));
 330                newslot->curl = NULL;
 331                newslot->in_use = 0;
 332                newslot->next = NULL;
 333
 334                slot = active_queue_head;
 335                if (slot == NULL) {
 336                        active_queue_head = newslot;
 337                } else {
 338                        while (slot->next != NULL) {
 339                                slot = slot->next;
 340                        }
 341                        slot->next = newslot;
 342                }
 343                slot = newslot;
 344        }
 345
 346        if (slot->curl == NULL) {
 347#ifdef NO_CURL_EASY_DUPHANDLE
 348                slot->curl = get_curl_handle();
 349#else
 350                slot->curl = curl_easy_duphandle(curl_default);
 351#endif
 352        }
 353
 354        active_requests++;
 355        slot->in_use = 1;
 356        slot->local = NULL;
 357        slot->results = NULL;
 358        slot->finished = NULL;
 359        slot->callback_data = NULL;
 360        slot->callback_func = NULL;
 361        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
 362        curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 363        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
 364        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
 365        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
 366        curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
 367        curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 368
 369        return slot;
 370}
 371
 372int start_active_slot(struct active_request_slot *slot)
 373{
 374#ifdef USE_CURL_MULTI
 375        CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
 376        int num_transfers;
 377
 378        if (curlm_result != CURLM_OK &&
 379            curlm_result != CURLM_CALL_MULTI_PERFORM) {
 380                active_requests--;
 381                slot->in_use = 0;
 382                return 0;
 383        }
 384
 385        /*
 386         * We know there must be something to do, since we just added
 387         * something.
 388         */
 389        curl_multi_perform(curlm, &num_transfers);
 390#endif
 391        return 1;
 392}
 393
 394#ifdef USE_CURL_MULTI
 395struct fill_chain {
 396        void *data;
 397        int (*fill)(void *);
 398        struct fill_chain *next;
 399};
 400
 401static struct fill_chain *fill_cfg = NULL;
 402
 403void add_fill_function(void *data, int (*fill)(void *))
 404{
 405        struct fill_chain *new = malloc(sizeof(*new));
 406        struct fill_chain **linkp = &fill_cfg;
 407        new->data = data;
 408        new->fill = fill;
 409        new->next = NULL;
 410        while (*linkp)
 411                linkp = &(*linkp)->next;
 412        *linkp = new;
 413}
 414
 415void fill_active_slots(void)
 416{
 417        struct active_request_slot *slot = active_queue_head;
 418
 419        while (active_requests < max_requests) {
 420                struct fill_chain *fill;
 421                for (fill = fill_cfg; fill; fill = fill->next)
 422                        if (fill->fill(fill->data))
 423                                break;
 424
 425                if (!fill)
 426                        break;
 427        }
 428
 429        while (slot != NULL) {
 430                if (!slot->in_use && slot->curl != NULL) {
 431                        curl_easy_cleanup(slot->curl);
 432                        slot->curl = NULL;
 433                }
 434                slot = slot->next;
 435        }
 436}
 437
 438void step_active_slots(void)
 439{
 440        int num_transfers;
 441        CURLMcode curlm_result;
 442
 443        do {
 444                curlm_result = curl_multi_perform(curlm, &num_transfers);
 445        } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
 446        if (num_transfers < active_requests) {
 447                process_curl_messages();
 448                fill_active_slots();
 449        }
 450}
 451#endif
 452
 453void run_active_slot(struct active_request_slot *slot)
 454{
 455#ifdef USE_CURL_MULTI
 456        long last_pos = 0;
 457        long current_pos;
 458        fd_set readfds;
 459        fd_set writefds;
 460        fd_set excfds;
 461        int max_fd;
 462        struct timeval select_timeout;
 463        int finished = 0;
 464
 465        slot->finished = &finished;
 466        while (!finished) {
 467                data_received = 0;
 468                step_active_slots();
 469
 470                if (!data_received && slot->local != NULL) {
 471                        current_pos = ftell(slot->local);
 472                        if (current_pos > last_pos)
 473                                data_received++;
 474                        last_pos = current_pos;
 475                }
 476
 477                if (slot->in_use && !data_received) {
 478                        max_fd = 0;
 479                        FD_ZERO(&readfds);
 480                        FD_ZERO(&writefds);
 481                        FD_ZERO(&excfds);
 482                        select_timeout.tv_sec = 0;
 483                        select_timeout.tv_usec = 50000;
 484                        select(max_fd, &readfds, &writefds,
 485                               &excfds, &select_timeout);
 486                }
 487        }
 488#else
 489        while (slot->in_use) {
 490                slot->curl_result = curl_easy_perform(slot->curl);
 491                finish_active_slot(slot);
 492        }
 493#endif
 494}
 495
 496static void closedown_active_slot(struct active_request_slot *slot)
 497{
 498        active_requests--;
 499        slot->in_use = 0;
 500}
 501
 502void release_active_slot(struct active_request_slot *slot)
 503{
 504        closedown_active_slot(slot);
 505        if (slot->curl) {
 506#ifdef USE_CURL_MULTI
 507                curl_multi_remove_handle(curlm, slot->curl);
 508#endif
 509                curl_easy_cleanup(slot->curl);
 510                slot->curl = NULL;
 511        }
 512#ifdef USE_CURL_MULTI
 513        fill_active_slots();
 514#endif
 515}
 516
 517static void finish_active_slot(struct active_request_slot *slot)
 518{
 519        closedown_active_slot(slot);
 520        curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
 521
 522        if (slot->finished != NULL)
 523                (*slot->finished) = 1;
 524
 525        /* Store slot results so they can be read after the slot is reused */
 526        if (slot->results != NULL) {
 527                slot->results->curl_result = slot->curl_result;
 528                slot->results->http_code = slot->http_code;
 529        }
 530
 531        /* Run callback if appropriate */
 532        if (slot->callback_func != NULL) {
 533                slot->callback_func(slot->callback_data);
 534        }
 535}
 536
 537void finish_all_active_slots(void)
 538{
 539        struct active_request_slot *slot = active_queue_head;
 540
 541        while (slot != NULL)
 542                if (slot->in_use) {
 543                        run_active_slot(slot);
 544                        slot = active_queue_head;
 545                } else {
 546                        slot = slot->next;
 547                }
 548}
 549
 550static inline int needs_quote(int ch)
 551{
 552        if (((ch >= 'A') && (ch <= 'Z'))
 553                        || ((ch >= 'a') && (ch <= 'z'))
 554                        || ((ch >= '0') && (ch <= '9'))
 555                        || (ch == '/')
 556                        || (ch == '-')
 557                        || (ch == '.'))
 558                return 0;
 559        return 1;
 560}
 561
 562static inline int hex(int v)
 563{
 564        if (v < 10) return '0' + v;
 565        else return 'A' + v - 10;
 566}
 567
 568static char *quote_ref_url(const char *base, const char *ref)
 569{
 570        const char *cp;
 571        char *dp, *qref;
 572        int len, baselen, ch;
 573
 574        baselen = strlen(base);
 575        len = baselen + 2; /* '/' after base and terminating NUL */
 576        for (cp = ref; (ch = *cp) != 0; cp++, len++)
 577                if (needs_quote(ch))
 578                        len += 2; /* extra two hex plus replacement % */
 579        qref = xmalloc(len);
 580        memcpy(qref, base, baselen);
 581        dp = qref + baselen;
 582        *(dp++) = '/';
 583        for (cp = ref; (ch = *cp) != 0; cp++) {
 584                if (needs_quote(ch)) {
 585                        *dp++ = '%';
 586                        *dp++ = hex((ch >> 4) & 0xF);
 587                        *dp++ = hex(ch & 0xF);
 588                }
 589                else
 590                        *dp++ = ch;
 591        }
 592        *dp = 0;
 593
 594        return qref;
 595}
 596
 597int http_fetch_ref(const char *base, struct ref *ref)
 598{
 599        char *url;
 600        struct strbuf buffer = STRBUF_INIT;
 601        struct active_request_slot *slot;
 602        struct slot_results results;
 603        int ret;
 604
 605        url = quote_ref_url(base, ref->name);
 606        slot = get_active_slot();
 607        slot->results = &results;
 608        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
 609        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 610        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
 611        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 612        if (start_active_slot(slot)) {
 613                run_active_slot(slot);
 614                if (results.curl_result == CURLE_OK) {
 615                        strbuf_rtrim(&buffer);
 616                        if (buffer.len == 40)
 617                                ret = get_sha1_hex(buffer.buf, ref->old_sha1);
 618                        else if (!prefixcmp(buffer.buf, "ref: ")) {
 619                                ref->symref = xstrdup(buffer.buf + 5);
 620                                ret = 0;
 621                        } else
 622                                ret = 1;
 623                } else {
 624                        ret = error("Couldn't get %s for %s\n%s",
 625                                    url, ref->name, curl_errorstr);
 626                }
 627        } else {
 628                ret = error("Unable to start request");
 629        }
 630
 631        strbuf_release(&buffer);
 632        free(url);
 633        return ret;
 634}