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