http.con commit http: drop "local" member from request struct (093c44a)
   1#include "http.h"
   2#include "pack.h"
   3#include "sideband.h"
   4#include "run-command.h"
   5#include "url.h"
   6
   7int active_requests;
   8int http_is_verbose;
   9size_t http_post_buffer = 16 * LARGE_PACKET_MAX;
  10
  11#if LIBCURL_VERSION_NUM >= 0x070a06
  12#define LIBCURL_CAN_HANDLE_AUTH_ANY
  13#endif
  14
  15static int min_curl_sessions = 1;
  16static int curl_session_count;
  17#ifdef USE_CURL_MULTI
  18static int max_requests = -1;
  19static CURLM *curlm;
  20#endif
  21#ifndef NO_CURL_EASY_DUPHANDLE
  22static CURL *curl_default;
  23#endif
  24
  25#define PREV_BUF_SIZE 4096
  26#define RANGE_HEADER_SIZE 30
  27
  28char curl_errorstr[CURL_ERROR_SIZE];
  29
  30static int curl_ssl_verify = -1;
  31static const char *ssl_cert;
  32#if LIBCURL_VERSION_NUM >= 0x070903
  33static const char *ssl_key;
  34#endif
  35#if LIBCURL_VERSION_NUM >= 0x070908
  36static const char *ssl_capath;
  37#endif
  38static const char *ssl_cainfo;
  39static long curl_low_speed_limit = -1;
  40static long curl_low_speed_time = -1;
  41static int curl_ftp_no_epsv;
  42static const char *curl_http_proxy;
  43static char *user_name, *user_pass;
  44static const char *user_agent;
  45
  46#if LIBCURL_VERSION_NUM >= 0x071700
  47/* Use CURLOPT_KEYPASSWD as is */
  48#elif LIBCURL_VERSION_NUM >= 0x070903
  49#define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
  50#else
  51#define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
  52#endif
  53
  54static char *ssl_cert_password;
  55static int ssl_cert_password_required;
  56
  57static struct curl_slist *pragma_header;
  58static struct curl_slist *no_pragma_header;
  59
  60static struct active_request_slot *active_queue_head;
  61
  62size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
  63{
  64        size_t size = eltsize * nmemb;
  65        struct buffer *buffer = buffer_;
  66
  67        if (size > buffer->buf.len - buffer->posn)
  68                size = buffer->buf.len - buffer->posn;
  69        memcpy(ptr, buffer->buf.buf + buffer->posn, size);
  70        buffer->posn += size;
  71
  72        return size;
  73}
  74
  75#ifndef NO_CURL_IOCTL
  76curlioerr ioctl_buffer(CURL *handle, int cmd, void *clientp)
  77{
  78        struct buffer *buffer = clientp;
  79
  80        switch (cmd) {
  81        case CURLIOCMD_NOP:
  82                return CURLIOE_OK;
  83
  84        case CURLIOCMD_RESTARTREAD:
  85                buffer->posn = 0;
  86                return CURLIOE_OK;
  87
  88        default:
  89                return CURLIOE_UNKNOWNCMD;
  90        }
  91}
  92#endif
  93
  94size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
  95{
  96        size_t size = eltsize * nmemb;
  97        struct strbuf *buffer = buffer_;
  98
  99        strbuf_add(buffer, ptr, size);
 100        return size;
 101}
 102
 103size_t fwrite_null(char *ptr, size_t eltsize, size_t nmemb, void *strbuf)
 104{
 105        return eltsize * nmemb;
 106}
 107
 108#ifdef USE_CURL_MULTI
 109static void process_curl_messages(void)
 110{
 111        int num_messages;
 112        struct active_request_slot *slot;
 113        CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
 114
 115        while (curl_message != NULL) {
 116                if (curl_message->msg == CURLMSG_DONE) {
 117                        int curl_result = curl_message->data.result;
 118                        slot = active_queue_head;
 119                        while (slot != NULL &&
 120                               slot->curl != curl_message->easy_handle)
 121                                slot = slot->next;
 122                        if (slot != NULL) {
 123                                curl_multi_remove_handle(curlm, slot->curl);
 124                                slot->curl_result = curl_result;
 125                                finish_active_slot(slot);
 126                        } else {
 127                                fprintf(stderr, "Received DONE message for unknown request!\n");
 128                        }
 129                } else {
 130                        fprintf(stderr, "Unknown CURL message received: %d\n",
 131                                (int)curl_message->msg);
 132                }
 133                curl_message = curl_multi_info_read(curlm, &num_messages);
 134        }
 135}
 136#endif
 137
 138static int http_options(const char *var, const char *value, void *cb)
 139{
 140        if (!strcmp("http.sslverify", var)) {
 141                curl_ssl_verify = git_config_bool(var, value);
 142                return 0;
 143        }
 144        if (!strcmp("http.sslcert", var))
 145                return git_config_string(&ssl_cert, var, value);
 146#if LIBCURL_VERSION_NUM >= 0x070903
 147        if (!strcmp("http.sslkey", var))
 148                return git_config_string(&ssl_key, var, value);
 149#endif
 150#if LIBCURL_VERSION_NUM >= 0x070908
 151        if (!strcmp("http.sslcapath", var))
 152                return git_config_string(&ssl_capath, var, value);
 153#endif
 154        if (!strcmp("http.sslcainfo", var))
 155                return git_config_string(&ssl_cainfo, var, value);
 156        if (!strcmp("http.sslcertpasswordprotected", var)) {
 157                if (git_config_bool(var, value))
 158                        ssl_cert_password_required = 1;
 159                return 0;
 160        }
 161        if (!strcmp("http.minsessions", var)) {
 162                min_curl_sessions = git_config_int(var, value);
 163#ifndef USE_CURL_MULTI
 164                if (min_curl_sessions > 1)
 165                        min_curl_sessions = 1;
 166#endif
 167                return 0;
 168        }
 169#ifdef USE_CURL_MULTI
 170        if (!strcmp("http.maxrequests", var)) {
 171                max_requests = git_config_int(var, value);
 172                return 0;
 173        }
 174#endif
 175        if (!strcmp("http.lowspeedlimit", var)) {
 176                curl_low_speed_limit = (long)git_config_int(var, value);
 177                return 0;
 178        }
 179        if (!strcmp("http.lowspeedtime", var)) {
 180                curl_low_speed_time = (long)git_config_int(var, value);
 181                return 0;
 182        }
 183
 184        if (!strcmp("http.noepsv", var)) {
 185                curl_ftp_no_epsv = git_config_bool(var, value);
 186                return 0;
 187        }
 188        if (!strcmp("http.proxy", var))
 189                return git_config_string(&curl_http_proxy, var, value);
 190
 191        if (!strcmp("http.postbuffer", var)) {
 192                http_post_buffer = git_config_int(var, value);
 193                if (http_post_buffer < LARGE_PACKET_MAX)
 194                        http_post_buffer = LARGE_PACKET_MAX;
 195                return 0;
 196        }
 197
 198        if (!strcmp("http.useragent", var))
 199                return git_config_string(&user_agent, var, value);
 200
 201        /* Fall back on the default ones */
 202        return git_default_config(var, value, cb);
 203}
 204
 205static void init_curl_http_auth(CURL *result)
 206{
 207        if (user_name) {
 208                struct strbuf up = STRBUF_INIT;
 209                if (!user_pass)
 210                        user_pass = xstrdup(git_getpass("Password: "));
 211                strbuf_addf(&up, "%s:%s", user_name, user_pass);
 212                curl_easy_setopt(result, CURLOPT_USERPWD,
 213                                 strbuf_detach(&up, NULL));
 214        }
 215}
 216
 217static int has_cert_password(void)
 218{
 219        if (ssl_cert_password != NULL)
 220                return 1;
 221        if (ssl_cert == NULL || ssl_cert_password_required != 1)
 222                return 0;
 223        /* Only prompt the user once. */
 224        ssl_cert_password_required = -1;
 225        ssl_cert_password = git_getpass("Certificate Password: ");
 226        if (ssl_cert_password != NULL) {
 227                ssl_cert_password = xstrdup(ssl_cert_password);
 228                return 1;
 229        } else
 230                return 0;
 231}
 232
 233static CURL *get_curl_handle(void)
 234{
 235        CURL *result = curl_easy_init();
 236
 237        if (!curl_ssl_verify) {
 238                curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
 239                curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
 240        } else {
 241                /* Verify authenticity of the peer's certificate */
 242                curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
 243                /* The name in the cert must match whom we tried to connect */
 244                curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
 245        }
 246
 247#if LIBCURL_VERSION_NUM >= 0x070907
 248        curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
 249#endif
 250#ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
 251        curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
 252#endif
 253
 254        init_curl_http_auth(result);
 255
 256        if (ssl_cert != NULL)
 257                curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
 258        if (has_cert_password())
 259                curl_easy_setopt(result, CURLOPT_KEYPASSWD, ssl_cert_password);
 260#if LIBCURL_VERSION_NUM >= 0x070903
 261        if (ssl_key != NULL)
 262                curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
 263#endif
 264#if LIBCURL_VERSION_NUM >= 0x070908
 265        if (ssl_capath != NULL)
 266                curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
 267#endif
 268        if (ssl_cainfo != NULL)
 269                curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
 270        curl_easy_setopt(result, CURLOPT_FAILONERROR, 1);
 271
 272        if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
 273                curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
 274                                 curl_low_speed_limit);
 275                curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
 276                                 curl_low_speed_time);
 277        }
 278
 279        curl_easy_setopt(result, CURLOPT_FOLLOWLOCATION, 1);
 280#if LIBCURL_VERSION_NUM >= 0x071301
 281        curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
 282#elif LIBCURL_VERSION_NUM >= 0x071101
 283        curl_easy_setopt(result, CURLOPT_POST301, 1);
 284#endif
 285
 286        if (getenv("GIT_CURL_VERBOSE"))
 287                curl_easy_setopt(result, CURLOPT_VERBOSE, 1);
 288
 289        curl_easy_setopt(result, CURLOPT_USERAGENT,
 290                user_agent ? user_agent : GIT_HTTP_USER_AGENT);
 291
 292        if (curl_ftp_no_epsv)
 293                curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
 294
 295        if (curl_http_proxy)
 296                curl_easy_setopt(result, CURLOPT_PROXY, curl_http_proxy);
 297
 298        return result;
 299}
 300
 301static void http_auth_init(const char *url)
 302{
 303        char *at, *colon, *cp, *slash, *decoded;
 304        int len;
 305
 306        cp = strstr(url, "://");
 307        if (!cp)
 308                return;
 309
 310        /*
 311         * Ok, the URL looks like "proto://something".  Which one?
 312         * "proto://<user>:<pass>@<host>/...",
 313         * "proto://<user>@<host>/...", or just
 314         * "proto://<host>/..."?
 315         */
 316        cp += 3;
 317        at = strchr(cp, '@');
 318        colon = strchr(cp, ':');
 319        slash = strchrnul(cp, '/');
 320        if (!at || slash <= at)
 321                return; /* No credentials */
 322        if (!colon || at <= colon) {
 323                /* Only username */
 324                len = at - cp;
 325                user_name = xmalloc(len + 1);
 326                memcpy(user_name, cp, len);
 327                user_name[len] = '\0';
 328                decoded = url_decode(user_name);
 329                free(user_name);
 330                user_name = decoded;
 331                user_pass = NULL;
 332        } else {
 333                len = colon - cp;
 334                user_name = xmalloc(len + 1);
 335                memcpy(user_name, cp, len);
 336                user_name[len] = '\0';
 337                decoded = url_decode(user_name);
 338                free(user_name);
 339                user_name = decoded;
 340                len = at - (colon + 1);
 341                user_pass = xmalloc(len + 1);
 342                memcpy(user_pass, colon + 1, len);
 343                user_pass[len] = '\0';
 344                decoded = url_decode(user_pass);
 345                free(user_pass);
 346                user_pass = decoded;
 347        }
 348}
 349
 350static void set_from_env(const char **var, const char *envname)
 351{
 352        const char *val = getenv(envname);
 353        if (val)
 354                *var = val;
 355}
 356
 357void http_init(struct remote *remote)
 358{
 359        char *low_speed_limit;
 360        char *low_speed_time;
 361
 362        http_is_verbose = 0;
 363
 364        git_config(http_options, NULL);
 365
 366        curl_global_init(CURL_GLOBAL_ALL);
 367
 368        if (remote && remote->http_proxy)
 369                curl_http_proxy = xstrdup(remote->http_proxy);
 370
 371        pragma_header = curl_slist_append(pragma_header, "Pragma: no-cache");
 372        no_pragma_header = curl_slist_append(no_pragma_header, "Pragma:");
 373
 374#ifdef USE_CURL_MULTI
 375        {
 376                char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
 377                if (http_max_requests != NULL)
 378                        max_requests = atoi(http_max_requests);
 379        }
 380
 381        curlm = curl_multi_init();
 382        if (curlm == NULL) {
 383                fprintf(stderr, "Error creating curl multi handle.\n");
 384                exit(1);
 385        }
 386#endif
 387
 388        if (getenv("GIT_SSL_NO_VERIFY"))
 389                curl_ssl_verify = 0;
 390
 391        set_from_env(&ssl_cert, "GIT_SSL_CERT");
 392#if LIBCURL_VERSION_NUM >= 0x070903
 393        set_from_env(&ssl_key, "GIT_SSL_KEY");
 394#endif
 395#if LIBCURL_VERSION_NUM >= 0x070908
 396        set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
 397#endif
 398        set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
 399
 400        set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
 401
 402        low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
 403        if (low_speed_limit != NULL)
 404                curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
 405        low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
 406        if (low_speed_time != NULL)
 407                curl_low_speed_time = strtol(low_speed_time, NULL, 10);
 408
 409        if (curl_ssl_verify == -1)
 410                curl_ssl_verify = 1;
 411
 412        curl_session_count = 0;
 413#ifdef USE_CURL_MULTI
 414        if (max_requests < 1)
 415                max_requests = DEFAULT_MAX_REQUESTS;
 416#endif
 417
 418        if (getenv("GIT_CURL_FTP_NO_EPSV"))
 419                curl_ftp_no_epsv = 1;
 420
 421        if (remote && remote->url && remote->url[0]) {
 422                http_auth_init(remote->url[0]);
 423                if (!ssl_cert_password_required &&
 424                    getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
 425                    !prefixcmp(remote->url[0], "https://"))
 426                        ssl_cert_password_required = 1;
 427        }
 428
 429#ifndef NO_CURL_EASY_DUPHANDLE
 430        curl_default = get_curl_handle();
 431#endif
 432}
 433
 434void http_cleanup(void)
 435{
 436        struct active_request_slot *slot = active_queue_head;
 437
 438        while (slot != NULL) {
 439                struct active_request_slot *next = slot->next;
 440                if (slot->curl != NULL) {
 441#ifdef USE_CURL_MULTI
 442                        curl_multi_remove_handle(curlm, slot->curl);
 443#endif
 444                        curl_easy_cleanup(slot->curl);
 445                }
 446                free(slot);
 447                slot = next;
 448        }
 449        active_queue_head = NULL;
 450
 451#ifndef NO_CURL_EASY_DUPHANDLE
 452        curl_easy_cleanup(curl_default);
 453#endif
 454
 455#ifdef USE_CURL_MULTI
 456        curl_multi_cleanup(curlm);
 457#endif
 458        curl_global_cleanup();
 459
 460        curl_slist_free_all(pragma_header);
 461        pragma_header = NULL;
 462
 463        curl_slist_free_all(no_pragma_header);
 464        no_pragma_header = NULL;
 465
 466        if (curl_http_proxy) {
 467                free((void *)curl_http_proxy);
 468                curl_http_proxy = NULL;
 469        }
 470
 471        if (ssl_cert_password != NULL) {
 472                memset(ssl_cert_password, 0, strlen(ssl_cert_password));
 473                free(ssl_cert_password);
 474                ssl_cert_password = NULL;
 475        }
 476        ssl_cert_password_required = 0;
 477}
 478
 479struct active_request_slot *get_active_slot(void)
 480{
 481        struct active_request_slot *slot = active_queue_head;
 482        struct active_request_slot *newslot;
 483
 484#ifdef USE_CURL_MULTI
 485        int num_transfers;
 486
 487        /* Wait for a slot to open up if the queue is full */
 488        while (active_requests >= max_requests) {
 489                curl_multi_perform(curlm, &num_transfers);
 490                if (num_transfers < active_requests)
 491                        process_curl_messages();
 492        }
 493#endif
 494
 495        while (slot != NULL && slot->in_use)
 496                slot = slot->next;
 497
 498        if (slot == NULL) {
 499                newslot = xmalloc(sizeof(*newslot));
 500                newslot->curl = NULL;
 501                newslot->in_use = 0;
 502                newslot->next = NULL;
 503
 504                slot = active_queue_head;
 505                if (slot == NULL) {
 506                        active_queue_head = newslot;
 507                } else {
 508                        while (slot->next != NULL)
 509                                slot = slot->next;
 510                        slot->next = newslot;
 511                }
 512                slot = newslot;
 513        }
 514
 515        if (slot->curl == NULL) {
 516#ifdef NO_CURL_EASY_DUPHANDLE
 517                slot->curl = get_curl_handle();
 518#else
 519                slot->curl = curl_easy_duphandle(curl_default);
 520#endif
 521                curl_session_count++;
 522        }
 523
 524        active_requests++;
 525        slot->in_use = 1;
 526        slot->results = NULL;
 527        slot->finished = NULL;
 528        slot->callback_data = NULL;
 529        slot->callback_func = NULL;
 530        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
 531        curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
 532        curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
 533        curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
 534        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
 535        curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
 536        curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
 537        curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 538
 539        return slot;
 540}
 541
 542int start_active_slot(struct active_request_slot *slot)
 543{
 544#ifdef USE_CURL_MULTI
 545        CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
 546        int num_transfers;
 547
 548        if (curlm_result != CURLM_OK &&
 549            curlm_result != CURLM_CALL_MULTI_PERFORM) {
 550                active_requests--;
 551                slot->in_use = 0;
 552                return 0;
 553        }
 554
 555        /*
 556         * We know there must be something to do, since we just added
 557         * something.
 558         */
 559        curl_multi_perform(curlm, &num_transfers);
 560#endif
 561        return 1;
 562}
 563
 564#ifdef USE_CURL_MULTI
 565struct fill_chain {
 566        void *data;
 567        int (*fill)(void *);
 568        struct fill_chain *next;
 569};
 570
 571static struct fill_chain *fill_cfg;
 572
 573void add_fill_function(void *data, int (*fill)(void *))
 574{
 575        struct fill_chain *new = xmalloc(sizeof(*new));
 576        struct fill_chain **linkp = &fill_cfg;
 577        new->data = data;
 578        new->fill = fill;
 579        new->next = NULL;
 580        while (*linkp)
 581                linkp = &(*linkp)->next;
 582        *linkp = new;
 583}
 584
 585void fill_active_slots(void)
 586{
 587        struct active_request_slot *slot = active_queue_head;
 588
 589        while (active_requests < max_requests) {
 590                struct fill_chain *fill;
 591                for (fill = fill_cfg; fill; fill = fill->next)
 592                        if (fill->fill(fill->data))
 593                                break;
 594
 595                if (!fill)
 596                        break;
 597        }
 598
 599        while (slot != NULL) {
 600                if (!slot->in_use && slot->curl != NULL
 601                        && curl_session_count > min_curl_sessions) {
 602                        curl_easy_cleanup(slot->curl);
 603                        slot->curl = NULL;
 604                        curl_session_count--;
 605                }
 606                slot = slot->next;
 607        }
 608}
 609
 610void step_active_slots(void)
 611{
 612        int num_transfers;
 613        CURLMcode curlm_result;
 614
 615        do {
 616                curlm_result = curl_multi_perform(curlm, &num_transfers);
 617        } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
 618        if (num_transfers < active_requests) {
 619                process_curl_messages();
 620                fill_active_slots();
 621        }
 622}
 623#endif
 624
 625void run_active_slot(struct active_request_slot *slot)
 626{
 627#ifdef USE_CURL_MULTI
 628        fd_set readfds;
 629        fd_set writefds;
 630        fd_set excfds;
 631        int max_fd;
 632        struct timeval select_timeout;
 633        int finished = 0;
 634
 635        slot->finished = &finished;
 636        while (!finished) {
 637                step_active_slots();
 638
 639                if (slot->in_use) {
 640#if LIBCURL_VERSION_NUM >= 0x070f04
 641                        long curl_timeout;
 642                        curl_multi_timeout(curlm, &curl_timeout);
 643                        if (curl_timeout == 0) {
 644                                continue;
 645                        } else if (curl_timeout == -1) {
 646                                select_timeout.tv_sec  = 0;
 647                                select_timeout.tv_usec = 50000;
 648                        } else {
 649                                select_timeout.tv_sec  =  curl_timeout / 1000;
 650                                select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
 651                        }
 652#else
 653                        select_timeout.tv_sec  = 0;
 654                        select_timeout.tv_usec = 50000;
 655#endif
 656
 657                        max_fd = -1;
 658                        FD_ZERO(&readfds);
 659                        FD_ZERO(&writefds);
 660                        FD_ZERO(&excfds);
 661                        curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
 662
 663                        select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
 664                }
 665        }
 666#else
 667        while (slot->in_use) {
 668                slot->curl_result = curl_easy_perform(slot->curl);
 669                finish_active_slot(slot);
 670        }
 671#endif
 672}
 673
 674static void closedown_active_slot(struct active_request_slot *slot)
 675{
 676        active_requests--;
 677        slot->in_use = 0;
 678}
 679
 680static void release_active_slot(struct active_request_slot *slot)
 681{
 682        closedown_active_slot(slot);
 683        if (slot->curl && curl_session_count > min_curl_sessions) {
 684#ifdef USE_CURL_MULTI
 685                curl_multi_remove_handle(curlm, slot->curl);
 686#endif
 687                curl_easy_cleanup(slot->curl);
 688                slot->curl = NULL;
 689                curl_session_count--;
 690        }
 691#ifdef USE_CURL_MULTI
 692        fill_active_slots();
 693#endif
 694}
 695
 696void finish_active_slot(struct active_request_slot *slot)
 697{
 698        closedown_active_slot(slot);
 699        curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
 700
 701        if (slot->finished != NULL)
 702                (*slot->finished) = 1;
 703
 704        /* Store slot results so they can be read after the slot is reused */
 705        if (slot->results != NULL) {
 706                slot->results->curl_result = slot->curl_result;
 707                slot->results->http_code = slot->http_code;
 708        }
 709
 710        /* Run callback if appropriate */
 711        if (slot->callback_func != NULL)
 712                slot->callback_func(slot->callback_data);
 713}
 714
 715void finish_all_active_slots(void)
 716{
 717        struct active_request_slot *slot = active_queue_head;
 718
 719        while (slot != NULL)
 720                if (slot->in_use) {
 721                        run_active_slot(slot);
 722                        slot = active_queue_head;
 723                } else {
 724                        slot = slot->next;
 725                }
 726}
 727
 728/* Helpers for modifying and creating URLs */
 729static inline int needs_quote(int ch)
 730{
 731        if (((ch >= 'A') && (ch <= 'Z'))
 732                        || ((ch >= 'a') && (ch <= 'z'))
 733                        || ((ch >= '0') && (ch <= '9'))
 734                        || (ch == '/')
 735                        || (ch == '-')
 736                        || (ch == '.'))
 737                return 0;
 738        return 1;
 739}
 740
 741static inline int hex(int v)
 742{
 743        if (v < 10)
 744                return '0' + v;
 745        else
 746                return 'A' + v - 10;
 747}
 748
 749static char *quote_ref_url(const char *base, const char *ref)
 750{
 751        struct strbuf buf = STRBUF_INIT;
 752        const char *cp;
 753        int ch;
 754
 755        end_url_with_slash(&buf, base);
 756
 757        for (cp = ref; (ch = *cp) != 0; cp++)
 758                if (needs_quote(ch))
 759                        strbuf_addf(&buf, "%%%02x", ch);
 760                else
 761                        strbuf_addch(&buf, *cp);
 762
 763        return strbuf_detach(&buf, NULL);
 764}
 765
 766void append_remote_object_url(struct strbuf *buf, const char *url,
 767                              const char *hex,
 768                              int only_two_digit_prefix)
 769{
 770        end_url_with_slash(buf, url);
 771
 772        strbuf_addf(buf, "objects/%.*s/", 2, hex);
 773        if (!only_two_digit_prefix)
 774                strbuf_addf(buf, "%s", hex+2);
 775}
 776
 777char *get_remote_object_url(const char *url, const char *hex,
 778                            int only_two_digit_prefix)
 779{
 780        struct strbuf buf = STRBUF_INIT;
 781        append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
 782        return strbuf_detach(&buf, NULL);
 783}
 784
 785/* http_request() targets */
 786#define HTTP_REQUEST_STRBUF     0
 787#define HTTP_REQUEST_FILE       1
 788
 789static int http_request(const char *url, void *result, int target, int options)
 790{
 791        struct active_request_slot *slot;
 792        struct slot_results results;
 793        struct curl_slist *headers = NULL;
 794        struct strbuf buf = STRBUF_INIT;
 795        int ret;
 796
 797        slot = get_active_slot();
 798        slot->results = &results;
 799        curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
 800
 801        if (result == NULL) {
 802                curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
 803        } else {
 804                curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
 805                curl_easy_setopt(slot->curl, CURLOPT_FILE, result);
 806
 807                if (target == HTTP_REQUEST_FILE) {
 808                        long posn = ftell(result);
 809                        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
 810                                         fwrite);
 811                        if (posn > 0) {
 812                                strbuf_addf(&buf, "Range: bytes=%ld-", posn);
 813                                headers = curl_slist_append(headers, buf.buf);
 814                                strbuf_reset(&buf);
 815                        }
 816                } else
 817                        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
 818                                         fwrite_buffer);
 819        }
 820
 821        strbuf_addstr(&buf, "Pragma:");
 822        if (options & HTTP_NO_CACHE)
 823                strbuf_addstr(&buf, " no-cache");
 824
 825        headers = curl_slist_append(headers, buf.buf);
 826
 827        curl_easy_setopt(slot->curl, CURLOPT_URL, url);
 828        curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
 829
 830        if (start_active_slot(slot)) {
 831                run_active_slot(slot);
 832                if (results.curl_result == CURLE_OK)
 833                        ret = HTTP_OK;
 834                else if (missing_target(&results))
 835                        ret = HTTP_MISSING_TARGET;
 836                else if (results.http_code == 401) {
 837                        if (user_name) {
 838                                ret = HTTP_NOAUTH;
 839                        } else {
 840                                /*
 841                                 * git_getpass is needed here because its very likely stdin/stdout are
 842                                 * pipes to our parent process.  So we instead need to use /dev/tty,
 843                                 * but that is non-portable.  Using git_getpass() can at least be stubbed
 844                                 * on other platforms with a different implementation if/when necessary.
 845                                 */
 846                                user_name = xstrdup(git_getpass("Username: "));
 847                                init_curl_http_auth(slot->curl);
 848                                ret = HTTP_REAUTH;
 849                        }
 850                } else
 851                        ret = HTTP_ERROR;
 852        } else {
 853                error("Unable to start HTTP request for %s", url);
 854                ret = HTTP_START_FAILED;
 855        }
 856
 857        curl_slist_free_all(headers);
 858        strbuf_release(&buf);
 859
 860        return ret;
 861}
 862
 863int http_get_strbuf(const char *url, struct strbuf *result, int options)
 864{
 865        int http_ret = http_request(url, result, HTTP_REQUEST_STRBUF, options);
 866        if (http_ret == HTTP_REAUTH) {
 867                http_ret = http_request(url, result, HTTP_REQUEST_STRBUF, options);
 868        }
 869        return http_ret;
 870}
 871
 872/*
 873 * Downloads an url and stores the result in the given file.
 874 *
 875 * If a previous interrupted download is detected (i.e. a previous temporary
 876 * file is still around) the download is resumed.
 877 */
 878static int http_get_file(const char *url, const char *filename, int options)
 879{
 880        int ret;
 881        struct strbuf tmpfile = STRBUF_INIT;
 882        FILE *result;
 883
 884        strbuf_addf(&tmpfile, "%s.temp", filename);
 885        result = fopen(tmpfile.buf, "a");
 886        if (! result) {
 887                error("Unable to open local file %s", tmpfile.buf);
 888                ret = HTTP_ERROR;
 889                goto cleanup;
 890        }
 891
 892        ret = http_request(url, result, HTTP_REQUEST_FILE, options);
 893        fclose(result);
 894
 895        if ((ret == HTTP_OK) && move_temp_to_file(tmpfile.buf, filename))
 896                ret = HTTP_ERROR;
 897cleanup:
 898        strbuf_release(&tmpfile);
 899        return ret;
 900}
 901
 902int http_error(const char *url, int ret)
 903{
 904        /* http_request has already handled HTTP_START_FAILED. */
 905        if (ret != HTTP_START_FAILED)
 906                error("%s while accessing %s\n", curl_errorstr, url);
 907
 908        return ret;
 909}
 910
 911int http_fetch_ref(const char *base, struct ref *ref)
 912{
 913        char *url;
 914        struct strbuf buffer = STRBUF_INIT;
 915        int ret = -1;
 916
 917        url = quote_ref_url(base, ref->name);
 918        if (http_get_strbuf(url, &buffer, HTTP_NO_CACHE) == HTTP_OK) {
 919                strbuf_rtrim(&buffer);
 920                if (buffer.len == 40)
 921                        ret = get_sha1_hex(buffer.buf, ref->old_sha1);
 922                else if (!prefixcmp(buffer.buf, "ref: ")) {
 923                        ref->symref = xstrdup(buffer.buf + 5);
 924                        ret = 0;
 925                }
 926        }
 927
 928        strbuf_release(&buffer);
 929        free(url);
 930        return ret;
 931}
 932
 933/* Helpers for fetching packs */
 934static char *fetch_pack_index(unsigned char *sha1, const char *base_url)
 935{
 936        char *url, *tmp;
 937        struct strbuf buf = STRBUF_INIT;
 938
 939        if (http_is_verbose)
 940                fprintf(stderr, "Getting index for pack %s\n", sha1_to_hex(sha1));
 941
 942        end_url_with_slash(&buf, base_url);
 943        strbuf_addf(&buf, "objects/pack/pack-%s.idx", sha1_to_hex(sha1));
 944        url = strbuf_detach(&buf, NULL);
 945
 946        strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(sha1));
 947        tmp = strbuf_detach(&buf, NULL);
 948
 949        if (http_get_file(url, tmp, 0) != HTTP_OK) {
 950                error("Unable to get pack index %s\n", url);
 951                free(tmp);
 952                tmp = NULL;
 953        }
 954
 955        free(url);
 956        return tmp;
 957}
 958
 959static int fetch_and_setup_pack_index(struct packed_git **packs_head,
 960        unsigned char *sha1, const char *base_url)
 961{
 962        struct packed_git *new_pack;
 963        char *tmp_idx = NULL;
 964        int ret;
 965
 966        if (has_pack_index(sha1)) {
 967                new_pack = parse_pack_index(sha1, NULL);
 968                if (!new_pack)
 969                        return -1; /* parse_pack_index() already issued error message */
 970                goto add_pack;
 971        }
 972
 973        tmp_idx = fetch_pack_index(sha1, base_url);
 974        if (!tmp_idx)
 975                return -1;
 976
 977        new_pack = parse_pack_index(sha1, tmp_idx);
 978        if (!new_pack) {
 979                unlink(tmp_idx);
 980                free(tmp_idx);
 981
 982                return -1; /* parse_pack_index() already issued error message */
 983        }
 984
 985        ret = verify_pack_index(new_pack);
 986        if (!ret) {
 987                close_pack_index(new_pack);
 988                ret = move_temp_to_file(tmp_idx, sha1_pack_index_name(sha1));
 989        }
 990        free(tmp_idx);
 991        if (ret)
 992                return -1;
 993
 994add_pack:
 995        new_pack->next = *packs_head;
 996        *packs_head = new_pack;
 997        return 0;
 998}
 999
1000int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
1001{
1002        int ret = 0, i = 0;
1003        char *url, *data;
1004        struct strbuf buf = STRBUF_INIT;
1005        unsigned char sha1[20];
1006
1007        end_url_with_slash(&buf, base_url);
1008        strbuf_addstr(&buf, "objects/info/packs");
1009        url = strbuf_detach(&buf, NULL);
1010
1011        ret = http_get_strbuf(url, &buf, HTTP_NO_CACHE);
1012        if (ret != HTTP_OK)
1013                goto cleanup;
1014
1015        data = buf.buf;
1016        while (i < buf.len) {
1017                switch (data[i]) {
1018                case 'P':
1019                        i++;
1020                        if (i + 52 <= buf.len &&
1021                            !prefixcmp(data + i, " pack-") &&
1022                            !prefixcmp(data + i + 46, ".pack\n")) {
1023                                get_sha1_hex(data + i + 6, sha1);
1024                                fetch_and_setup_pack_index(packs_head, sha1,
1025                                                      base_url);
1026                                i += 51;
1027                                break;
1028                        }
1029                default:
1030                        while (i < buf.len && data[i] != '\n')
1031                                i++;
1032                }
1033                i++;
1034        }
1035
1036cleanup:
1037        free(url);
1038        return ret;
1039}
1040
1041void release_http_pack_request(struct http_pack_request *preq)
1042{
1043        if (preq->packfile != NULL) {
1044                fclose(preq->packfile);
1045                preq->packfile = NULL;
1046        }
1047        if (preq->range_header != NULL) {
1048                curl_slist_free_all(preq->range_header);
1049                preq->range_header = NULL;
1050        }
1051        preq->slot = NULL;
1052        free(preq->url);
1053}
1054
1055int finish_http_pack_request(struct http_pack_request *preq)
1056{
1057        struct packed_git **lst;
1058        struct packed_git *p = preq->target;
1059        char *tmp_idx;
1060        struct child_process ip;
1061        const char *ip_argv[8];
1062
1063        close_pack_index(p);
1064
1065        fclose(preq->packfile);
1066        preq->packfile = NULL;
1067
1068        lst = preq->lst;
1069        while (*lst != p)
1070                lst = &((*lst)->next);
1071        *lst = (*lst)->next;
1072
1073        tmp_idx = xstrdup(preq->tmpfile);
1074        strcpy(tmp_idx + strlen(tmp_idx) - strlen(".pack.temp"),
1075               ".idx.temp");
1076
1077        ip_argv[0] = "index-pack";
1078        ip_argv[1] = "-o";
1079        ip_argv[2] = tmp_idx;
1080        ip_argv[3] = preq->tmpfile;
1081        ip_argv[4] = NULL;
1082
1083        memset(&ip, 0, sizeof(ip));
1084        ip.argv = ip_argv;
1085        ip.git_cmd = 1;
1086        ip.no_stdin = 1;
1087        ip.no_stdout = 1;
1088
1089        if (run_command(&ip)) {
1090                unlink(preq->tmpfile);
1091                unlink(tmp_idx);
1092                free(tmp_idx);
1093                return -1;
1094        }
1095
1096        unlink(sha1_pack_index_name(p->sha1));
1097
1098        if (move_temp_to_file(preq->tmpfile, sha1_pack_name(p->sha1))
1099         || move_temp_to_file(tmp_idx, sha1_pack_index_name(p->sha1))) {
1100                free(tmp_idx);
1101                return -1;
1102        }
1103
1104        install_packed_git(p);
1105        free(tmp_idx);
1106        return 0;
1107}
1108
1109struct http_pack_request *new_http_pack_request(
1110        struct packed_git *target, const char *base_url)
1111{
1112        long prev_posn = 0;
1113        char range[RANGE_HEADER_SIZE];
1114        struct strbuf buf = STRBUF_INIT;
1115        struct http_pack_request *preq;
1116
1117        preq = xmalloc(sizeof(*preq));
1118        preq->target = target;
1119        preq->range_header = NULL;
1120
1121        end_url_with_slash(&buf, base_url);
1122        strbuf_addf(&buf, "objects/pack/pack-%s.pack",
1123                sha1_to_hex(target->sha1));
1124        preq->url = strbuf_detach(&buf, NULL);
1125
1126        snprintf(preq->tmpfile, sizeof(preq->tmpfile), "%s.temp",
1127                sha1_pack_name(target->sha1));
1128        preq->packfile = fopen(preq->tmpfile, "a");
1129        if (!preq->packfile) {
1130                error("Unable to open local file %s for pack",
1131                      preq->tmpfile);
1132                goto abort;
1133        }
1134
1135        preq->slot = get_active_slot();
1136        curl_easy_setopt(preq->slot->curl, CURLOPT_FILE, preq->packfile);
1137        curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
1138        curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
1139        curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1140                no_pragma_header);
1141
1142        /*
1143         * If there is data present from a previous transfer attempt,
1144         * resume where it left off
1145         */
1146        prev_posn = ftell(preq->packfile);
1147        if (prev_posn>0) {
1148                if (http_is_verbose)
1149                        fprintf(stderr,
1150                                "Resuming fetch of pack %s at byte %ld\n",
1151                                sha1_to_hex(target->sha1), prev_posn);
1152                sprintf(range, "Range: bytes=%ld-", prev_posn);
1153                preq->range_header = curl_slist_append(NULL, range);
1154                curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
1155                        preq->range_header);
1156        }
1157
1158        return preq;
1159
1160abort:
1161        free(preq->url);
1162        free(preq);
1163        return NULL;
1164}
1165
1166/* Helpers for fetching objects (loose) */
1167static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
1168                               void *data)
1169{
1170        unsigned char expn[4096];
1171        size_t size = eltsize * nmemb;
1172        int posn = 0;
1173        struct http_object_request *freq =
1174                (struct http_object_request *)data;
1175        do {
1176                ssize_t retval = xwrite(freq->localfile,
1177                                        (char *) ptr + posn, size - posn);
1178                if (retval < 0)
1179                        return posn;
1180                posn += retval;
1181        } while (posn < size);
1182
1183        freq->stream.avail_in = size;
1184        freq->stream.next_in = (void *)ptr;
1185        do {
1186                freq->stream.next_out = expn;
1187                freq->stream.avail_out = sizeof(expn);
1188                freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
1189                git_SHA1_Update(&freq->c, expn,
1190                                sizeof(expn) - freq->stream.avail_out);
1191        } while (freq->stream.avail_in && freq->zret == Z_OK);
1192        return size;
1193}
1194
1195struct http_object_request *new_http_object_request(const char *base_url,
1196        unsigned char *sha1)
1197{
1198        char *hex = sha1_to_hex(sha1);
1199        char *filename;
1200        char prevfile[PATH_MAX];
1201        int prevlocal;
1202        char prev_buf[PREV_BUF_SIZE];
1203        ssize_t prev_read = 0;
1204        long prev_posn = 0;
1205        char range[RANGE_HEADER_SIZE];
1206        struct curl_slist *range_header = NULL;
1207        struct http_object_request *freq;
1208
1209        freq = xmalloc(sizeof(*freq));
1210        hashcpy(freq->sha1, sha1);
1211        freq->localfile = -1;
1212
1213        filename = sha1_file_name(sha1);
1214        snprintf(freq->tmpfile, sizeof(freq->tmpfile),
1215                 "%s.temp", filename);
1216
1217        snprintf(prevfile, sizeof(prevfile), "%s.prev", filename);
1218        unlink_or_warn(prevfile);
1219        rename(freq->tmpfile, prevfile);
1220        unlink_or_warn(freq->tmpfile);
1221
1222        if (freq->localfile != -1)
1223                error("fd leakage in start: %d", freq->localfile);
1224        freq->localfile = open(freq->tmpfile,
1225                               O_WRONLY | O_CREAT | O_EXCL, 0666);
1226        /*
1227         * This could have failed due to the "lazy directory creation";
1228         * try to mkdir the last path component.
1229         */
1230        if (freq->localfile < 0 && errno == ENOENT) {
1231                char *dir = strrchr(freq->tmpfile, '/');
1232                if (dir) {
1233                        *dir = 0;
1234                        mkdir(freq->tmpfile, 0777);
1235                        *dir = '/';
1236                }
1237                freq->localfile = open(freq->tmpfile,
1238                                       O_WRONLY | O_CREAT | O_EXCL, 0666);
1239        }
1240
1241        if (freq->localfile < 0) {
1242                error("Couldn't create temporary file %s: %s",
1243                      freq->tmpfile, strerror(errno));
1244                goto abort;
1245        }
1246
1247        memset(&freq->stream, 0, sizeof(freq->stream));
1248
1249        git_inflate_init(&freq->stream);
1250
1251        git_SHA1_Init(&freq->c);
1252
1253        freq->url = get_remote_object_url(base_url, hex, 0);
1254
1255        /*
1256         * If a previous temp file is present, process what was already
1257         * fetched.
1258         */
1259        prevlocal = open(prevfile, O_RDONLY);
1260        if (prevlocal != -1) {
1261                do {
1262                        prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
1263                        if (prev_read>0) {
1264                                if (fwrite_sha1_file(prev_buf,
1265                                                     1,
1266                                                     prev_read,
1267                                                     freq) == prev_read) {
1268                                        prev_posn += prev_read;
1269                                } else {
1270                                        prev_read = -1;
1271                                }
1272                        }
1273                } while (prev_read > 0);
1274                close(prevlocal);
1275        }
1276        unlink_or_warn(prevfile);
1277
1278        /*
1279         * Reset inflate/SHA1 if there was an error reading the previous temp
1280         * file; also rewind to the beginning of the local file.
1281         */
1282        if (prev_read == -1) {
1283                memset(&freq->stream, 0, sizeof(freq->stream));
1284                git_inflate_init(&freq->stream);
1285                git_SHA1_Init(&freq->c);
1286                if (prev_posn>0) {
1287                        prev_posn = 0;
1288                        lseek(freq->localfile, 0, SEEK_SET);
1289                        if (ftruncate(freq->localfile, 0) < 0) {
1290                                error("Couldn't truncate temporary file %s: %s",
1291                                          freq->tmpfile, strerror(errno));
1292                                goto abort;
1293                        }
1294                }
1295        }
1296
1297        freq->slot = get_active_slot();
1298
1299        curl_easy_setopt(freq->slot->curl, CURLOPT_FILE, freq);
1300        curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
1301        curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
1302        curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
1303        curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
1304
1305        /*
1306         * If we have successfully processed data from a previous fetch
1307         * attempt, only fetch the data we don't already have.
1308         */
1309        if (prev_posn>0) {
1310                if (http_is_verbose)
1311                        fprintf(stderr,
1312                                "Resuming fetch of object %s at byte %ld\n",
1313                                hex, prev_posn);
1314                sprintf(range, "Range: bytes=%ld-", prev_posn);
1315                range_header = curl_slist_append(range_header, range);
1316                curl_easy_setopt(freq->slot->curl,
1317                                 CURLOPT_HTTPHEADER, range_header);
1318        }
1319
1320        return freq;
1321
1322abort:
1323        free(filename);
1324        free(freq->url);
1325        free(freq);
1326        return NULL;
1327}
1328
1329void process_http_object_request(struct http_object_request *freq)
1330{
1331        if (freq->slot == NULL)
1332                return;
1333        freq->curl_result = freq->slot->curl_result;
1334        freq->http_code = freq->slot->http_code;
1335        freq->slot = NULL;
1336}
1337
1338int finish_http_object_request(struct http_object_request *freq)
1339{
1340        struct stat st;
1341
1342        close(freq->localfile);
1343        freq->localfile = -1;
1344
1345        process_http_object_request(freq);
1346
1347        if (freq->http_code == 416) {
1348                warning("requested range invalid; we may already have all the data.");
1349        } else if (freq->curl_result != CURLE_OK) {
1350                if (stat(freq->tmpfile, &st) == 0)
1351                        if (st.st_size == 0)
1352                                unlink_or_warn(freq->tmpfile);
1353                return -1;
1354        }
1355
1356        git_inflate_end(&freq->stream);
1357        git_SHA1_Final(freq->real_sha1, &freq->c);
1358        if (freq->zret != Z_STREAM_END) {
1359                unlink_or_warn(freq->tmpfile);
1360                return -1;
1361        }
1362        if (hashcmp(freq->sha1, freq->real_sha1)) {
1363                unlink_or_warn(freq->tmpfile);
1364                return -1;
1365        }
1366        freq->rename =
1367                move_temp_to_file(freq->tmpfile, sha1_file_name(freq->sha1));
1368
1369        return freq->rename;
1370}
1371
1372void abort_http_object_request(struct http_object_request *freq)
1373{
1374        unlink_or_warn(freq->tmpfile);
1375
1376        release_http_object_request(freq);
1377}
1378
1379void release_http_object_request(struct http_object_request *freq)
1380{
1381        if (freq->localfile != -1) {
1382                close(freq->localfile);
1383                freq->localfile = -1;
1384        }
1385        if (freq->url != NULL) {
1386                free(freq->url);
1387                freq->url = NULL;
1388        }
1389        if (freq->slot != NULL) {
1390                freq->slot->callback_func = NULL;
1391                freq->slot->callback_data = NULL;
1392                release_active_slot(freq->slot);
1393                freq->slot = NULL;
1394        }
1395}