http-walker.con commit Merge branch 'bw/attr-pathspec' (f6c64c6)
   1#include "cache.h"
   2#include "commit.h"
   3#include "walker.h"
   4#include "http.h"
   5#include "list.h"
   6#include "transport.h"
   7
   8struct alt_base {
   9        char *base;
  10        int got_indices;
  11        struct packed_git *packs;
  12        struct alt_base *next;
  13};
  14
  15enum object_request_state {
  16        WAITING,
  17        ABORTED,
  18        ACTIVE,
  19        COMPLETE
  20};
  21
  22struct object_request {
  23        struct walker *walker;
  24        unsigned char sha1[20];
  25        struct alt_base *repo;
  26        enum object_request_state state;
  27        struct http_object_request *req;
  28        struct list_head node;
  29};
  30
  31struct alternates_request {
  32        struct walker *walker;
  33        const char *base;
  34        struct strbuf *url;
  35        struct strbuf *buffer;
  36        struct active_request_slot *slot;
  37        int http_specific;
  38};
  39
  40struct walker_data {
  41        const char *url;
  42        int got_alternates;
  43        struct alt_base *alt;
  44};
  45
  46static LIST_HEAD(object_queue_head);
  47
  48static void fetch_alternates(struct walker *walker, const char *base);
  49
  50static void process_object_response(void *callback_data);
  51
  52static void start_object_request(struct walker *walker,
  53                                 struct object_request *obj_req)
  54{
  55        struct active_request_slot *slot;
  56        struct http_object_request *req;
  57
  58        req = new_http_object_request(obj_req->repo->base, obj_req->sha1);
  59        if (req == NULL) {
  60                obj_req->state = ABORTED;
  61                return;
  62        }
  63        obj_req->req = req;
  64
  65        slot = req->slot;
  66        slot->callback_func = process_object_response;
  67        slot->callback_data = obj_req;
  68
  69        /* Try to get the request started, abort the request on error */
  70        obj_req->state = ACTIVE;
  71        if (!start_active_slot(slot)) {
  72                obj_req->state = ABORTED;
  73                release_http_object_request(req);
  74                return;
  75        }
  76}
  77
  78static void finish_object_request(struct object_request *obj_req)
  79{
  80        if (finish_http_object_request(obj_req->req))
  81                return;
  82
  83        if (obj_req->req->rename == 0)
  84                walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
  85}
  86
  87static void process_object_response(void *callback_data)
  88{
  89        struct object_request *obj_req =
  90                (struct object_request *)callback_data;
  91        struct walker *walker = obj_req->walker;
  92        struct walker_data *data = walker->data;
  93        struct alt_base *alt = data->alt;
  94
  95        process_http_object_request(obj_req->req);
  96        obj_req->state = COMPLETE;
  97
  98        /* Use alternates if necessary */
  99        if (missing_target(obj_req->req)) {
 100                fetch_alternates(walker, alt->base);
 101                if (obj_req->repo->next != NULL) {
 102                        obj_req->repo =
 103                                obj_req->repo->next;
 104                        release_http_object_request(obj_req->req);
 105                        start_object_request(walker, obj_req);
 106                        return;
 107                }
 108        }
 109
 110        finish_object_request(obj_req);
 111}
 112
 113static void release_object_request(struct object_request *obj_req)
 114{
 115        if (obj_req->req !=NULL && obj_req->req->localfile != -1)
 116                error("fd leakage in release: %d", obj_req->req->localfile);
 117
 118        list_del(&obj_req->node);
 119        free(obj_req);
 120}
 121
 122#ifdef USE_CURL_MULTI
 123static int fill_active_slot(struct walker *walker)
 124{
 125        struct object_request *obj_req;
 126        struct list_head *pos, *tmp, *head = &object_queue_head;
 127
 128        list_for_each_safe(pos, tmp, head) {
 129                obj_req = list_entry(pos, struct object_request, node);
 130                if (obj_req->state == WAITING) {
 131                        if (has_sha1_file(obj_req->sha1))
 132                                obj_req->state = COMPLETE;
 133                        else {
 134                                start_object_request(walker, obj_req);
 135                                return 1;
 136                        }
 137                }
 138        }
 139        return 0;
 140}
 141#endif
 142
 143static void prefetch(struct walker *walker, unsigned char *sha1)
 144{
 145        struct object_request *newreq;
 146        struct walker_data *data = walker->data;
 147
 148        newreq = xmalloc(sizeof(*newreq));
 149        newreq->walker = walker;
 150        hashcpy(newreq->sha1, sha1);
 151        newreq->repo = data->alt;
 152        newreq->state = WAITING;
 153        newreq->req = NULL;
 154
 155        http_is_verbose = walker->get_verbosely;
 156        list_add_tail(&newreq->node, &object_queue_head);
 157
 158#ifdef USE_CURL_MULTI
 159        fill_active_slots();
 160        step_active_slots();
 161#endif
 162}
 163
 164static int is_alternate_allowed(const char *url)
 165{
 166        const char *protocols[] = {
 167                "http", "https", "ftp", "ftps"
 168        };
 169        int i;
 170
 171        if (http_follow_config != HTTP_FOLLOW_ALWAYS) {
 172                warning("alternate disabled by http.followRedirects: %s", url);
 173                return 0;
 174        }
 175
 176        for (i = 0; i < ARRAY_SIZE(protocols); i++) {
 177                const char *end;
 178                if (skip_prefix(url, protocols[i], &end) &&
 179                    starts_with(end, "://"))
 180                        break;
 181        }
 182
 183        if (i >= ARRAY_SIZE(protocols)) {
 184                warning("ignoring alternate with unknown protocol: %s", url);
 185                return 0;
 186        }
 187        if (!is_transport_allowed(protocols[i], 0)) {
 188                warning("ignoring alternate with restricted protocol: %s", url);
 189                return 0;
 190        }
 191
 192        return 1;
 193}
 194
 195static void process_alternates_response(void *callback_data)
 196{
 197        struct alternates_request *alt_req =
 198                (struct alternates_request *)callback_data;
 199        struct walker *walker = alt_req->walker;
 200        struct walker_data *cdata = walker->data;
 201        struct active_request_slot *slot = alt_req->slot;
 202        struct alt_base *tail = cdata->alt;
 203        const char *base = alt_req->base;
 204        const char null_byte = '\0';
 205        char *data;
 206        int i = 0;
 207
 208        if (alt_req->http_specific) {
 209                if (slot->curl_result != CURLE_OK ||
 210                    !alt_req->buffer->len) {
 211
 212                        /* Try reusing the slot to get non-http alternates */
 213                        alt_req->http_specific = 0;
 214                        strbuf_reset(alt_req->url);
 215                        strbuf_addf(alt_req->url, "%s/objects/info/alternates",
 216                                    base);
 217                        curl_easy_setopt(slot->curl, CURLOPT_URL,
 218                                         alt_req->url->buf);
 219                        active_requests++;
 220                        slot->in_use = 1;
 221                        if (slot->finished != NULL)
 222                                (*slot->finished) = 0;
 223                        if (!start_active_slot(slot)) {
 224                                cdata->got_alternates = -1;
 225                                slot->in_use = 0;
 226                                if (slot->finished != NULL)
 227                                        (*slot->finished) = 1;
 228                        }
 229                        return;
 230                }
 231        } else if (slot->curl_result != CURLE_OK) {
 232                if (!missing_target(slot)) {
 233                        cdata->got_alternates = -1;
 234                        return;
 235                }
 236        }
 237
 238        fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer);
 239        alt_req->buffer->len--;
 240        data = alt_req->buffer->buf;
 241
 242        while (i < alt_req->buffer->len) {
 243                int posn = i;
 244                while (posn < alt_req->buffer->len && data[posn] != '\n')
 245                        posn++;
 246                if (data[posn] == '\n') {
 247                        int okay = 0;
 248                        int serverlen = 0;
 249                        struct alt_base *newalt;
 250                        if (data[i] == '/') {
 251                                /*
 252                                 * This counts
 253                                 * http://git.host/pub/scm/linux.git/
 254                                 * -----------here^
 255                                 * so memcpy(dst, base, serverlen) will
 256                                 * copy up to "...git.host".
 257                                 */
 258                                const char *colon_ss = strstr(base,"://");
 259                                if (colon_ss) {
 260                                        serverlen = (strchr(colon_ss + 3, '/')
 261                                                     - base);
 262                                        okay = 1;
 263                                }
 264                        } else if (!memcmp(data + i, "../", 3)) {
 265                                /*
 266                                 * Relative URL; chop the corresponding
 267                                 * number of subpath from base (and ../
 268                                 * from data), and concatenate the result.
 269                                 *
 270                                 * The code first drops ../ from data, and
 271                                 * then drops one ../ from data and one path
 272                                 * from base.  IOW, one extra ../ is dropped
 273                                 * from data than path is dropped from base.
 274                                 *
 275                                 * This is not wrong.  The alternate in
 276                                 *     http://git.host/pub/scm/linux.git/
 277                                 * to borrow from
 278                                 *     http://git.host/pub/scm/linus.git/
 279                                 * is ../../linus.git/objects/.  You need
 280                                 * two ../../ to borrow from your direct
 281                                 * neighbour.
 282                                 */
 283                                i += 3;
 284                                serverlen = strlen(base);
 285                                while (i + 2 < posn &&
 286                                       !memcmp(data + i, "../", 3)) {
 287                                        do {
 288                                                serverlen--;
 289                                        } while (serverlen &&
 290                                                 base[serverlen - 1] != '/');
 291                                        i += 3;
 292                                }
 293                                /* If the server got removed, give up. */
 294                                okay = strchr(base, ':') - base + 3 <
 295                                       serverlen;
 296                        } else if (alt_req->http_specific) {
 297                                char *colon = strchr(data + i, ':');
 298                                char *slash = strchr(data + i, '/');
 299                                if (colon && slash && colon < data + posn &&
 300                                    slash < data + posn && colon < slash) {
 301                                        okay = 1;
 302                                }
 303                        }
 304                        /* skip "objects\n" at end */
 305                        if (okay) {
 306                                struct strbuf target = STRBUF_INIT;
 307                                strbuf_add(&target, base, serverlen);
 308                                strbuf_add(&target, data + i, posn - i - 7);
 309
 310                                if (is_alternate_allowed(target.buf)) {
 311                                        warning("adding alternate object store: %s",
 312                                                target.buf);
 313                                        newalt = xmalloc(sizeof(*newalt));
 314                                        newalt->next = NULL;
 315                                        newalt->base = strbuf_detach(&target, NULL);
 316                                        newalt->got_indices = 0;
 317                                        newalt->packs = NULL;
 318
 319                                        while (tail->next != NULL)
 320                                                tail = tail->next;
 321                                        tail->next = newalt;
 322                                } else {
 323                                        strbuf_release(&target);
 324                                }
 325                        }
 326                }
 327                i = posn + 1;
 328        }
 329
 330        cdata->got_alternates = 1;
 331}
 332
 333static void fetch_alternates(struct walker *walker, const char *base)
 334{
 335        struct strbuf buffer = STRBUF_INIT;
 336        struct strbuf url = STRBUF_INIT;
 337        struct active_request_slot *slot;
 338        struct alternates_request alt_req;
 339        struct walker_data *cdata = walker->data;
 340
 341        /*
 342         * If another request has already started fetching alternates,
 343         * wait for them to arrive and return to processing this request's
 344         * curl message
 345         */
 346#ifdef USE_CURL_MULTI
 347        while (cdata->got_alternates == 0) {
 348                step_active_slots();
 349        }
 350#endif
 351
 352        /* Nothing to do if they've already been fetched */
 353        if (cdata->got_alternates == 1)
 354                return;
 355
 356        /* Start the fetch */
 357        cdata->got_alternates = 0;
 358
 359        if (walker->get_verbosely)
 360                fprintf(stderr, "Getting alternates list for %s\n", base);
 361
 362        strbuf_addf(&url, "%s/objects/info/http-alternates", base);
 363
 364        /*
 365         * Use a callback to process the result, since another request
 366         * may fail and need to have alternates loaded before continuing
 367         */
 368        slot = get_active_slot();
 369        slot->callback_func = process_alternates_response;
 370        alt_req.walker = walker;
 371        slot->callback_data = &alt_req;
 372
 373        curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
 374        curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
 375        curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf);
 376
 377        alt_req.base = base;
 378        alt_req.url = &url;
 379        alt_req.buffer = &buffer;
 380        alt_req.http_specific = 1;
 381        alt_req.slot = slot;
 382
 383        if (start_active_slot(slot))
 384                run_active_slot(slot);
 385        else
 386                cdata->got_alternates = -1;
 387
 388        strbuf_release(&buffer);
 389        strbuf_release(&url);
 390}
 391
 392static int fetch_indices(struct walker *walker, struct alt_base *repo)
 393{
 394        int ret;
 395
 396        if (repo->got_indices)
 397                return 0;
 398
 399        if (walker->get_verbosely)
 400                fprintf(stderr, "Getting pack list for %s\n", repo->base);
 401
 402        switch (http_get_info_packs(repo->base, &repo->packs)) {
 403        case HTTP_OK:
 404        case HTTP_MISSING_TARGET:
 405                repo->got_indices = 1;
 406                ret = 0;
 407                break;
 408        default:
 409                repo->got_indices = 0;
 410                ret = -1;
 411        }
 412
 413        return ret;
 414}
 415
 416static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
 417{
 418        struct packed_git *target;
 419        int ret;
 420        struct slot_results results;
 421        struct http_pack_request *preq;
 422
 423        if (fetch_indices(walker, repo))
 424                return -1;
 425        target = find_sha1_pack(sha1, repo->packs);
 426        if (!target)
 427                return -1;
 428
 429        if (walker->get_verbosely) {
 430                fprintf(stderr, "Getting pack %s\n",
 431                        sha1_to_hex(target->sha1));
 432                fprintf(stderr, " which contains %s\n",
 433                        sha1_to_hex(sha1));
 434        }
 435
 436        preq = new_http_pack_request(target, repo->base);
 437        if (preq == NULL)
 438                goto abort;
 439        preq->lst = &repo->packs;
 440        preq->slot->results = &results;
 441
 442        if (start_active_slot(preq->slot)) {
 443                run_active_slot(preq->slot);
 444                if (results.curl_result != CURLE_OK) {
 445                        error("Unable to get pack file %s\n%s", preq->url,
 446                              curl_errorstr);
 447                        goto abort;
 448                }
 449        } else {
 450                error("Unable to start request");
 451                goto abort;
 452        }
 453
 454        ret = finish_http_pack_request(preq);
 455        release_http_pack_request(preq);
 456        if (ret)
 457                return ret;
 458
 459        return 0;
 460
 461abort:
 462        return -1;
 463}
 464
 465static void abort_object_request(struct object_request *obj_req)
 466{
 467        release_object_request(obj_req);
 468}
 469
 470static int fetch_object(struct walker *walker, unsigned char *sha1)
 471{
 472        char *hex = sha1_to_hex(sha1);
 473        int ret = 0;
 474        struct object_request *obj_req = NULL;
 475        struct http_object_request *req;
 476        struct list_head *pos, *head = &object_queue_head;
 477
 478        list_for_each(pos, head) {
 479                obj_req = list_entry(pos, struct object_request, node);
 480                if (!hashcmp(obj_req->sha1, sha1))
 481                        break;
 482        }
 483        if (obj_req == NULL)
 484                return error("Couldn't find request for %s in the queue", hex);
 485
 486        if (has_sha1_file(obj_req->sha1)) {
 487                if (obj_req->req != NULL)
 488                        abort_http_object_request(obj_req->req);
 489                abort_object_request(obj_req);
 490                return 0;
 491        }
 492
 493#ifdef USE_CURL_MULTI
 494        while (obj_req->state == WAITING)
 495                step_active_slots();
 496#else
 497        start_object_request(walker, obj_req);
 498#endif
 499
 500        /*
 501         * obj_req->req might change when fetching alternates in the callback
 502         * process_object_response; therefore, the "shortcut" variable, req,
 503         * is used only after we're done with slots.
 504         */
 505        while (obj_req->state == ACTIVE)
 506                run_active_slot(obj_req->req->slot);
 507
 508        req = obj_req->req;
 509
 510        if (req->localfile != -1) {
 511                close(req->localfile);
 512                req->localfile = -1;
 513        }
 514
 515        /*
 516         * we turned off CURLOPT_FAILONERROR to avoid losing a
 517         * persistent connection and got CURLE_OK.
 518         */
 519        if (req->http_code >= 300 && req->curl_result == CURLE_OK &&
 520                        (starts_with(req->url, "http://") ||
 521                         starts_with(req->url, "https://"))) {
 522                req->curl_result = CURLE_HTTP_RETURNED_ERROR;
 523                xsnprintf(req->errorstr, sizeof(req->errorstr),
 524                          "HTTP request failed");
 525        }
 526
 527        if (obj_req->state == ABORTED) {
 528                ret = error("Request for %s aborted", hex);
 529        } else if (req->curl_result != CURLE_OK &&
 530                   req->http_code != 416) {
 531                if (missing_target(req))
 532                        ret = -1; /* Be silent, it is probably in a pack. */
 533                else
 534                        ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
 535                                    req->errorstr, req->curl_result,
 536                                    req->http_code, hex);
 537        } else if (req->zret != Z_STREAM_END) {
 538                walker->corrupt_object_found++;
 539                ret = error("File %s (%s) corrupt", hex, req->url);
 540        } else if (hashcmp(obj_req->sha1, req->real_sha1)) {
 541                ret = error("File %s has bad hash", hex);
 542        } else if (req->rename < 0) {
 543                ret = error("unable to write sha1 filename %s",
 544                            sha1_file_name(req->sha1));
 545        }
 546
 547        release_http_object_request(req);
 548        release_object_request(obj_req);
 549        return ret;
 550}
 551
 552static int fetch(struct walker *walker, unsigned char *sha1)
 553{
 554        struct walker_data *data = walker->data;
 555        struct alt_base *altbase = data->alt;
 556
 557        if (!fetch_object(walker, sha1))
 558                return 0;
 559        while (altbase) {
 560                if (!http_fetch_pack(walker, altbase, sha1))
 561                        return 0;
 562                fetch_alternates(walker, data->alt->base);
 563                altbase = altbase->next;
 564        }
 565        return error("Unable to find %s under %s", sha1_to_hex(sha1),
 566                     data->alt->base);
 567}
 568
 569static int fetch_ref(struct walker *walker, struct ref *ref)
 570{
 571        struct walker_data *data = walker->data;
 572        return http_fetch_ref(data->alt->base, ref);
 573}
 574
 575static void cleanup(struct walker *walker)
 576{
 577        struct walker_data *data = walker->data;
 578        struct alt_base *alt, *alt_next;
 579
 580        if (data) {
 581                alt = data->alt;
 582                while (alt) {
 583                        alt_next = alt->next;
 584
 585                        free(alt->base);
 586                        free(alt);
 587
 588                        alt = alt_next;
 589                }
 590                free(data);
 591                walker->data = NULL;
 592        }
 593}
 594
 595struct walker *get_http_walker(const char *url)
 596{
 597        char *s;
 598        struct walker_data *data = xmalloc(sizeof(struct walker_data));
 599        struct walker *walker = xmalloc(sizeof(struct walker));
 600
 601        data->alt = xmalloc(sizeof(*data->alt));
 602        data->alt->base = xstrdup(url);
 603        for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
 604                *s = 0;
 605
 606        data->alt->got_indices = 0;
 607        data->alt->packs = NULL;
 608        data->alt->next = NULL;
 609        data->got_alternates = -1;
 610
 611        walker->corrupt_object_found = 0;
 612        walker->fetch = fetch;
 613        walker->fetch_ref = fetch_ref;
 614        walker->prefetch = prefetch;
 615        walker->cleanup = cleanup;
 616        walker->data = data;
 617
 618#ifdef USE_CURL_MULTI
 619        add_fill_function(walker, (int (*)(void *)) fill_active_slot);
 620#endif
 621
 622        return walker;
 623}