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