http.con commit Makefile: introduce NO_PTHREADS (46059cc)
   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        if (!curl_ssl_verify) {
 169                curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
 170                curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
 171        } else {
 172                /* Verify authenticity of the peer's certificate */
 173                curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
 174                /* The name in the cert must match whom we tried to connect */
 175                curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
 176        }
 177
 178#if LIBCURL_VERSION_NUM >= 0x070907
 179        curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
 180#endif
 181
 182        if (ssl_cert != NULL)
 183                curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
 184#if LIBCURL_VERSION_NUM >= 0x070902
 185        if (ssl_key != NULL)
 186                curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
 187#endif
 188#if LIBCURL_VERSION_NUM >= 0x070908
 189        if (ssl_capath != NULL)
 190                curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
 191#endif
 192        if (ssl_cainfo != NULL)
 193                curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
 194        curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
 195
 196        if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
 197                curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
 198                                 curl_low_speed_limit);
 199                curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
 200                                 curl_low_speed_time);
 201        }
 202
 203        curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
 204
 205        if (getenv("GIT_CURL_VERBOSE"))
 206                curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
 207
 208        curl_easy_setopt(result, CURLOPT_USERAGENT, GIT_USER_AGENT);
 209
 210        if (curl_ftp_no_epsv)
 211                curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
 212
 213        if (curl_http_proxy)
 214                curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
 215
 216        return result;
 217}
 218
 219void http_init(struct remote *remote)
 220{
 221        char *low_speed_limit;
 222        char *low_speed_time;
 223
 224        curl_global_init(CURL_GLOBAL_ALL);
 225
 226        if (remote && remote->http_proxy)
 227                curl_http_proxy = xstrdup(remote->http_proxy);
 228
 229        pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
 230
 231#ifdef USE_CURL_MULTI
 232        {
 233                char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
 234                if (http_max_requests != NULL)
 235                        max_requests = atoi(http_max_requests);
 236        }
 237
 238        curlm = curl_multi_init();
 239        if (curlm == NULL) {
 240                fprintf(stderr, "Error creating curl multi handle.\n");
 241                exit(1);
 242        }
 243#endif
 244
 245        if (getenv("GIT_SSL_NO_VERIFY"))
 246                curl_ssl_verify = 0;
 247
 248        ssl_cert = getenv("GIT_SSL_CERT");
 249#if LIBCURL_VERSION_NUM >= 0x070902
 250        ssl_key = getenv("GIT_SSL_KEY");
 251#endif
 252#if LIBCURL_VERSION_NUM >= 0x070908
 253        ssl_capath = getenv("GIT_SSL_CAPATH");
 254#endif
 255        ssl_cainfo = getenv("GIT_SSL_CAINFO");
 256
 257        low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
 258        if (low_speed_limit != NULL)
 259                curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
 260        low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
 261        if (low_speed_time != NULL)
 262                curl_low_speed_time = strtol(low_speed_time, NULL, 10);
 263
 264        git_config(http_options, NULL);
 265
 266        if (curl_ssl_verify == -1)
 267                curl_ssl_verify = 1;
 268
 269#ifdef USE_CURL_MULTI
 270        if (max_requests < 1)
 271                max_requests = DEFAULT_MAX_REQUESTS;
 272#endif
 273
 274        if (getenv("GIT_CURL_FTP_NO_EPSV"))
 275                curl_ftp_no_epsv = 1;
 276
 277#ifndef NO_CURL_EASY_DUPHANDLE
 278        curl_default = get_curl_handle();
 279#endif
 280}
 281
 282void http_cleanup(void)
 283{
 284        struct active_request_slot *slot = active_queue_head;
 285
 286        while (slot != NULL) {
 287                struct active_request_slot *next = slot->next;
 288                if (slot->curl != NULL) {
 289#ifdef USE_CURL_MULTI
 290                        curl_multi_remove_handle(curlm, slot->curl);
 291#endif
 292                        curl_easy_cleanup(slot->curl);
 293                }
 294                free(slot);
 295                slot = next;
 296        }
 297        active_queue_head = NULL;
 298
 299#ifndef NO_CURL_EASY_DUPHANDLE
 300        curl_easy_cleanup(curl_default);
 301#endif
 302
 303#ifdef USE_CURL_MULTI
 304        curl_multi_cleanup(curlm);
 305#endif
 306        curl_global_cleanup();
 307
 308        curl_slist_free_all(pragma_header);
 309        pragma_header = NULL;
 310
 311        if (curl_http_proxy) {
 312                free(curl_http_proxy);
 313                curl_http_proxy = NULL;
 314        }
 315}
 316
 317struct active_request_slot *get_active_slot(void)
 318{
 319        struct active_request_slot *slot = active_queue_head;
 320        struct active_request_slot *newslot;
 321
 322#ifdef USE_CURL_MULTI
 323        int num_transfers;
 324
 325        /* Wait for a slot to open up if the queue is full */
 326        while (active_requests >= max_requests) {
 327                curl_multi_perform(curlm, &num_transfers);
 328                if (num_transfers < active_requests) {
 329                        process_curl_messages();
 330                }
 331        }
 332#endif
 333
 334        while (slot != NULL && slot->in_use) {
 335                slot = slot->next;
 336        }
 337        if (slot == NULL) {
 338                newslot = xmalloc(sizeof(*newslot));
 339                newslot->curl = NULL;
 340                newslot->in_use = 0;
 341                newslot->next = NULL;
 342
 343                slot = active_queue_head;
 344                if (slot == NULL) {
 345                        active_queue_head = newslot;
 346                } else {
 347                        while (slot->next != NULL) {
 348                                slot = slot->next;
 349                        }
 350                        slot->next = newslot;
 351                }
 352                slot = newslot;
 353        }
 354
 355        if (slot->curl == NULL) {
 356#ifdef NO_CURL_EASY_DUPHANDLE
 357                slot->curl = get_curl_handle();
 358#else
 359                slot->curl = curl_easy_duphandle(curl_default);
 360#endif
 361        }
 362
 363        active_requests++;
 364        slot->in_use = 1;
 365        slot->local = NULL;
 366        slot->results = NULL;
 367        slot->finished = NULL;
 368        slot->callback_data = NULL;
 369        slot->callback_func = NULL;
 370        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
 371        curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 372        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
 373        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
 374        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
 375        curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
 376        curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 377
 378        return slot;
 379}
 380
 381int start_active_slot(struct active_request_slot *slot)
 382{
 383#ifdef USE_CURL_MULTI
 384        CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
 385        int num_transfers;
 386
 387        if (curlm_result != CURLM_OK &&
 388            curlm_result != CURLM_CALL_MULTI_PERFORM) {
 389                active_requests--;
 390                slot->in_use = 0;
 391                return 0;
 392        }
 393
 394        /*
 395         * We know there must be something to do, since we just added
 396         * something.
 397         */
 398        curl_multi_perform(curlm, &num_transfers);
 399#endif
 400        return 1;
 401}
 402
 403#ifdef USE_CURL_MULTI
 404struct fill_chain {
 405        void *data;
 406        int (*fill)(void *);
 407        struct fill_chain *next;
 408};
 409
 410static struct fill_chain *fill_cfg = NULL;
 411
 412void add_fill_function(void *data, int (*fill)(void *))
 413{
 414        struct fill_chain *new = xmalloc(sizeof(*new));
 415        struct fill_chain **linkp = &fill_cfg;
 416        new->data = data;
 417        new->fill = fill;
 418        new->next = NULL;
 419        while (*linkp)
 420                linkp = &(*linkp)->next;
 421        *linkp = new;
 422}
 423
 424void fill_active_slots(void)
 425{
 426        struct active_request_slot *slot = active_queue_head;
 427
 428        while (active_requests < max_requests) {
 429                struct fill_chain *fill;
 430                for (fill = fill_cfg; fill; fill = fill->next)
 431                        if (fill->fill(fill->data))
 432                                break;
 433
 434                if (!fill)
 435                        break;
 436        }
 437
 438        while (slot != NULL) {
 439                if (!slot->in_use && slot->curl != NULL) {
 440                        curl_easy_cleanup(slot->curl);
 441                        slot->curl = NULL;
 442                }
 443                slot = slot->next;
 444        }
 445}
 446
 447void step_active_slots(void)
 448{
 449        int num_transfers;
 450        CURLMcode curlm_result;
 451
 452        do {
 453                curlm_result = curl_multi_perform(curlm, &num_transfers);
 454        } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
 455        if (num_transfers < active_requests) {
 456                process_curl_messages();
 457                fill_active_slots();
 458        }
 459}
 460#endif
 461
 462void run_active_slot(struct active_request_slot *slot)
 463{
 464#ifdef USE_CURL_MULTI
 465        long last_pos = 0;
 466        long current_pos;
 467        fd_set readfds;
 468        fd_set writefds;
 469        fd_set excfds;
 470        int max_fd;
 471        struct timeval select_timeout;
 472        int finished = 0;
 473
 474        slot->finished = &finished;
 475        while (!finished) {
 476                data_received = 0;
 477                step_active_slots();
 478
 479                if (!data_received && slot->local != NULL) {
 480                        current_pos = ftell(slot->local);
 481                        if (current_pos > last_pos)
 482                                data_received++;
 483                        last_pos = current_pos;
 484                }
 485
 486                if (slot->in_use && !data_received) {
 487                        max_fd = 0;
 488                        FD_ZERO(&readfds);
 489                        FD_ZERO(&writefds);
 490                        FD_ZERO(&excfds);
 491                        select_timeout.tv_sec = 0;
 492                        select_timeout.tv_usec = 50000;
 493                        select(max_fd, &readfds, &writefds,
 494                               &excfds, &select_timeout);
 495                }
 496        }
 497#else
 498        while (slot->in_use) {
 499                slot->curl_result = curl_easy_perform(slot->curl);
 500                finish_active_slot(slot);
 501        }
 502#endif
 503}
 504
 505static void closedown_active_slot(struct active_request_slot *slot)
 506{
 507        active_requests--;
 508        slot->in_use = 0;
 509}
 510
 511void release_active_slot(struct active_request_slot *slot)
 512{
 513        closedown_active_slot(slot);
 514        if (slot->curl) {
 515#ifdef USE_CURL_MULTI
 516                curl_multi_remove_handle(curlm, slot->curl);
 517#endif
 518                curl_easy_cleanup(slot->curl);
 519                slot->curl = NULL;
 520        }
 521#ifdef USE_CURL_MULTI
 522        fill_active_slots();
 523#endif
 524}
 525
 526static void finish_active_slot(struct active_request_slot *slot)
 527{
 528        closedown_active_slot(slot);
 529        curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
 530
 531        if (slot->finished != NULL)
 532                (*slot->finished) = 1;
 533
 534        /* Store slot results so they can be read after the slot is reused */
 535        if (slot->results != NULL) {
 536                slot->results->curl_result = slot->curl_result;
 537                slot->results->http_code = slot->http_code;
 538        }
 539
 540        /* Run callback if appropriate */
 541        if (slot->callback_func != NULL) {
 542                slot->callback_func(slot->callback_data);
 543        }
 544}
 545
 546void finish_all_active_slots(void)
 547{
 548        struct active_request_slot *slot = active_queue_head;
 549
 550        while (slot != NULL)
 551                if (slot->in_use) {
 552                        run_active_slot(slot);
 553                        slot = active_queue_head;
 554                } else {
 555                        slot = slot->next;
 556                }
 557}
 558
 559static inline int needs_quote(int ch)
 560{
 561        if (((ch >= 'A') && (ch <= 'Z'))
 562                        || ((ch >= 'a') && (ch <= 'z'))
 563                        || ((ch >= '0') && (ch <= '9'))
 564                        || (ch == '/')
 565                        || (ch == '-')
 566                        || (ch == '.'))
 567                return 0;
 568        return 1;
 569}
 570
 571static inline int hex(int v)
 572{
 573        if (v < 10) return '0' + v;
 574        else return 'A' + v - 10;
 575}
 576
 577static char *quote_ref_url(const char *base, const char *ref)
 578{
 579        const char *cp;
 580        char *dp, *qref;
 581        int len, baselen, ch;
 582
 583        baselen = strlen(base);
 584        len = baselen + 2; /* '/' after base and terminating NUL */
 585        for (cp = ref; (ch = *cp) != 0; cp++, len++)
 586                if (needs_quote(ch))
 587                        len += 2; /* extra two hex plus replacement % */
 588        qref = xmalloc(len);
 589        memcpy(qref, base, baselen);
 590        dp = qref + baselen;
 591        *(dp++) = '/';
 592        for (cp = ref; (ch = *cp) != 0; cp++) {
 593                if (needs_quote(ch)) {
 594                        *dp++ = '%';
 595                        *dp++ = hex((ch >> 4) & 0xF);
 596                        *dp++ = hex(ch & 0xF);
 597                }
 598                else
 599                        *dp++ = ch;
 600        }
 601        *dp = 0;
 602
 603        return qref;
 604}
 605
 606int http_fetch_ref(const char *base, struct ref *ref)
 607{
 608        char *url;
 609        struct strbuf buffer = STRBUF_INIT;
 610        struct active_request_slot *slot;
 611        struct slot_results results;
 612        int ret;
 613
 614        url = quote_ref_url(base, ref->name);
 615        slot = get_active_slot();
 616        slot->results = &results;
 617        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
 618        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 619        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
 620        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 621        if (start_active_slot(slot)) {
 622                run_active_slot(slot);
 623                if (results.curl_result == CURLE_OK) {
 624                        strbuf_rtrim(&buffer);
 625                        if (buffer.len == 40)
 626                                ret = get_sha1_hex(buffer.buf, ref->old_sha1);
 627                        else if (!prefixcmp(buffer.buf, "ref: ")) {
 628                                ref->symref = xstrdup(buffer.buf + 5);
 629                                ret = 0;
 630                        } else
 631                                ret = 1;
 632                } else {
 633                        ret = error("Couldn't get %s for %s\n%s",
 634                                    url, ref->name, curl_errorstr);
 635                }
 636        } else {
 637                ret = error("Unable to start request");
 638        }
 639
 640        strbuf_release(&buffer);
 641        free(url);
 642        return ret;
 643}