12 struct packed_git *packs;
13 struct alt_base *next;
16 enum object_request_state {
23 struct object_request {
24 struct walker *walker;
25 unsigned char sha1[20];
26 struct alt_base *repo;
27 enum object_request_state state;
28 struct http_object_request *req;
29 struct list_head node;
32 struct alternates_request {
33 struct walker *walker;
36 struct strbuf *buffer;
37 struct active_request_slot *slot;
47 static LIST_HEAD(object_queue_head);
49 static void fetch_alternates(struct walker *walker, const char *base);
51 static void process_object_response(void *callback_data);
53 static void start_object_request(struct walker *walker,
54 struct object_request *obj_req)
56 struct active_request_slot *slot;
57 struct http_object_request *req;
59 req = new_http_object_request(obj_req->repo->base, obj_req->sha1);
61 obj_req->state = ABORTED;
67 slot->callback_func = process_object_response;
68 slot->callback_data = obj_req;
70 /* Try to get the request started, abort the request on error */
71 obj_req->state = ACTIVE;
72 if (!start_active_slot(slot)) {
73 obj_req->state = ABORTED;
74 release_http_object_request(req);
79 static void finish_object_request(struct object_request *obj_req)
81 if (finish_http_object_request(obj_req->req))
84 if (obj_req->req->rename == 0)
85 walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
88 static void process_object_response(void *callback_data)
90 struct object_request *obj_req =
91 (struct object_request *)callback_data;
92 struct walker *walker = obj_req->walker;
93 struct walker_data *data = walker->data;
94 struct alt_base *alt = data->alt;
96 process_http_object_request(obj_req->req);
97 obj_req->state = COMPLETE;
99 /* Use alternates if necessary */
100 if (missing_target(obj_req->req)) {
101 fetch_alternates(walker, alt->base);
102 if (obj_req->repo->next != NULL) {
105 release_http_object_request(obj_req->req);
106 start_object_request(walker, obj_req);
111 finish_object_request(obj_req);
114 static void release_object_request(struct object_request *obj_req)
116 if (obj_req->req !=NULL && obj_req->req->localfile != -1)
117 error("fd leakage in release: %d", obj_req->req->localfile);
119 list_del(&obj_req->node);
123 static int fill_active_slot(struct walker *walker)
125 struct object_request *obj_req;
126 struct list_head *pos, *tmp, *head = &object_queue_head;
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;
134 start_object_request(walker, obj_req);
142 static void prefetch(struct walker *walker, unsigned char *sha1)
144 struct object_request *newreq;
145 struct walker_data *data = walker->data;
147 newreq = xmalloc(sizeof(*newreq));
148 newreq->walker = walker;
149 hashcpy(newreq->sha1, sha1);
150 newreq->repo = data->alt;
151 newreq->state = WAITING;
154 http_is_verbose = walker->get_verbosely;
155 list_add_tail(&newreq->node, &object_queue_head);
161 static int is_alternate_allowed(const char *url)
163 const char *protocols[] = {
164 "http", "https", "ftp", "ftps"
168 if (http_follow_config != HTTP_FOLLOW_ALWAYS) {
169 warning("alternate disabled by http.followRedirects: %s", url);
173 for (i = 0; i < ARRAY_SIZE(protocols); i++) {
175 if (skip_prefix(url, protocols[i], &end) &&
176 starts_with(end, "://"))
180 if (i >= ARRAY_SIZE(protocols)) {
181 warning("ignoring alternate with unknown protocol: %s", url);
184 if (!is_transport_allowed(protocols[i], 0)) {
185 warning("ignoring alternate with restricted protocol: %s", url);
192 static void process_alternates_response(void *callback_data)
194 struct alternates_request *alt_req =
195 (struct alternates_request *)callback_data;
196 struct walker *walker = alt_req->walker;
197 struct walker_data *cdata = walker->data;
198 struct active_request_slot *slot = alt_req->slot;
199 struct alt_base *tail = cdata->alt;
200 const char *base = alt_req->base;
201 const char null_byte = '\0';
205 if (alt_req->http_specific) {
206 if (slot->curl_result != CURLE_OK ||
207 !alt_req->buffer->len) {
209 /* Try reusing the slot to get non-http alternates */
210 alt_req->http_specific = 0;
211 strbuf_reset(alt_req->url);
212 strbuf_addf(alt_req->url, "%s/objects/info/alternates",
214 curl_easy_setopt(slot->curl, CURLOPT_URL,
218 if (slot->finished != NULL)
219 (*slot->finished) = 0;
220 if (!start_active_slot(slot)) {
221 cdata->got_alternates = -1;
223 if (slot->finished != NULL)
224 (*slot->finished) = 1;
228 } else if (slot->curl_result != CURLE_OK) {
229 if (!missing_target(slot)) {
230 cdata->got_alternates = -1;
235 fwrite_buffer((char *)&null_byte, 1, 1, alt_req->buffer);
236 alt_req->buffer->len--;
237 data = alt_req->buffer->buf;
239 while (i < alt_req->buffer->len) {
241 while (posn < alt_req->buffer->len && data[posn] != '\n')
243 if (data[posn] == '\n') {
246 struct alt_base *newalt;
247 if (data[i] == '/') {
250 * http://git.host/pub/scm/linux.git/
252 * so memcpy(dst, base, serverlen) will
253 * copy up to "...git.host".
255 const char *colon_ss = strstr(base,"://");
257 serverlen = (strchr(colon_ss + 3, '/')
261 } else if (!memcmp(data + i, "../", 3)) {
263 * Relative URL; chop the corresponding
264 * number of subpath from base (and ../
265 * from data), and concatenate the result.
267 * The code first drops ../ from data, and
268 * then drops one ../ from data and one path
269 * from base. IOW, one extra ../ is dropped
270 * from data than path is dropped from base.
272 * This is not wrong. The alternate in
273 * http://git.host/pub/scm/linux.git/
275 * http://git.host/pub/scm/linus.git/
276 * is ../../linus.git/objects/. You need
277 * two ../../ to borrow from your direct
281 serverlen = strlen(base);
282 while (i + 2 < posn &&
283 !memcmp(data + i, "../", 3)) {
286 } while (serverlen &&
287 base[serverlen - 1] != '/');
290 /* If the server got removed, give up. */
291 okay = strchr(base, ':') - base + 3 <
293 } else if (alt_req->http_specific) {
294 char *colon = strchr(data + i, ':');
295 char *slash = strchr(data + i, '/');
296 if (colon && slash && colon < data + posn &&
297 slash < data + posn && colon < slash) {
302 struct strbuf target = STRBUF_INIT;
303 strbuf_add(&target, base, serverlen);
304 strbuf_add(&target, data + i, posn - i);
305 if (!strbuf_strip_suffix(&target, "objects")) {
306 warning("ignoring alternate that does"
307 " not end in 'objects': %s",
309 strbuf_release(&target);
310 } else if (is_alternate_allowed(target.buf)) {
311 warning("adding alternate object store: %s",
313 newalt = xmalloc(sizeof(*newalt));
315 newalt->base = strbuf_detach(&target, NULL);
316 newalt->got_indices = 0;
317 newalt->packs = NULL;
319 while (tail->next != NULL)
323 strbuf_release(&target);
330 cdata->got_alternates = 1;
333 static void fetch_alternates(struct walker *walker, const char *base)
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;
342 * If another request has already started fetching alternates,
343 * wait for them to arrive and return to processing this request's
346 while (cdata->got_alternates == 0) {
350 /* Nothing to do if they've already been fetched */
351 if (cdata->got_alternates == 1)
354 /* Start the fetch */
355 cdata->got_alternates = 0;
357 if (walker->get_verbosely)
358 fprintf(stderr, "Getting alternates list for %s\n", base);
360 strbuf_addf(&url, "%s/objects/info/http-alternates", base);
363 * Use a callback to process the result, since another request
364 * may fail and need to have alternates loaded before continuing
366 slot = get_active_slot();
367 slot->callback_func = process_alternates_response;
368 alt_req.walker = walker;
369 slot->callback_data = &alt_req;
371 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
372 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
373 curl_easy_setopt(slot->curl, CURLOPT_URL, url.buf);
377 alt_req.buffer = &buffer;
378 alt_req.http_specific = 1;
381 if (start_active_slot(slot))
382 run_active_slot(slot);
384 cdata->got_alternates = -1;
386 strbuf_release(&buffer);
387 strbuf_release(&url);
390 static int fetch_indices(struct walker *walker, struct alt_base *repo)
394 if (repo->got_indices)
397 if (walker->get_verbosely)
398 fprintf(stderr, "Getting pack list for %s\n", repo->base);
400 switch (http_get_info_packs(repo->base, &repo->packs)) {
402 case HTTP_MISSING_TARGET:
403 repo->got_indices = 1;
407 repo->got_indices = 0;
414 static int http_fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
416 struct packed_git *target;
418 struct slot_results results;
419 struct http_pack_request *preq;
421 if (fetch_indices(walker, repo))
423 target = find_sha1_pack(sha1, repo->packs);
427 if (walker->get_verbosely) {
428 fprintf(stderr, "Getting pack %s\n",
429 sha1_to_hex(target->sha1));
430 fprintf(stderr, " which contains %s\n",
434 preq = new_http_pack_request(target, repo->base);
437 preq->lst = &repo->packs;
438 preq->slot->results = &results;
440 if (start_active_slot(preq->slot)) {
441 run_active_slot(preq->slot);
442 if (results.curl_result != CURLE_OK) {
443 error("Unable to get pack file %s\n%s", preq->url,
448 error("Unable to start request");
452 ret = finish_http_pack_request(preq);
453 release_http_pack_request(preq);
463 static void abort_object_request(struct object_request *obj_req)
465 release_object_request(obj_req);
468 static int fetch_object(struct walker *walker, unsigned char *sha1)
470 char *hex = sha1_to_hex(sha1);
472 struct object_request *obj_req = NULL;
473 struct http_object_request *req;
474 struct list_head *pos, *head = &object_queue_head;
476 list_for_each(pos, head) {
477 obj_req = list_entry(pos, struct object_request, node);
478 if (!hashcmp(obj_req->sha1, sha1))
482 return error("Couldn't find request for %s in the queue", hex);
484 if (has_sha1_file(obj_req->sha1)) {
485 if (obj_req->req != NULL)
486 abort_http_object_request(obj_req->req);
487 abort_object_request(obj_req);
491 while (obj_req->state == WAITING)
495 * obj_req->req might change when fetching alternates in the callback
496 * process_object_response; therefore, the "shortcut" variable, req,
497 * is used only after we're done with slots.
499 while (obj_req->state == ACTIVE)
500 run_active_slot(obj_req->req->slot);
504 if (req->localfile != -1) {
505 close(req->localfile);
510 * we turned off CURLOPT_FAILONERROR to avoid losing a
511 * persistent connection and got CURLE_OK.
513 if (req->http_code >= 300 && req->curl_result == CURLE_OK &&
514 (starts_with(req->url, "http://") ||
515 starts_with(req->url, "https://"))) {
516 req->curl_result = CURLE_HTTP_RETURNED_ERROR;
517 xsnprintf(req->errorstr, sizeof(req->errorstr),
518 "HTTP request failed");
521 if (obj_req->state == ABORTED) {
522 ret = error("Request for %s aborted", hex);
523 } else if (req->curl_result != CURLE_OK &&
524 req->http_code != 416) {
525 if (missing_target(req))
526 ret = -1; /* Be silent, it is probably in a pack. */
528 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
529 req->errorstr, req->curl_result,
530 req->http_code, hex);
531 } else if (req->zret != Z_STREAM_END) {
532 walker->corrupt_object_found++;
533 ret = error("File %s (%s) corrupt", hex, req->url);
534 } else if (hashcmp(obj_req->sha1, req->real_sha1)) {
535 ret = error("File %s has bad hash", hex);
536 } else if (req->rename < 0) {
537 struct strbuf buf = STRBUF_INIT;
538 sha1_file_name(&buf, req->sha1);
539 ret = error("unable to write sha1 filename %s", buf.buf);
540 strbuf_release(&buf);
543 release_http_object_request(req);
544 release_object_request(obj_req);
548 static int fetch(struct walker *walker, unsigned char *sha1)
550 struct walker_data *data = walker->data;
551 struct alt_base *altbase = data->alt;
553 if (!fetch_object(walker, sha1))
556 if (!http_fetch_pack(walker, altbase, sha1))
558 fetch_alternates(walker, data->alt->base);
559 altbase = altbase->next;
561 return error("Unable to find %s under %s", sha1_to_hex(sha1),
565 static int fetch_ref(struct walker *walker, struct ref *ref)
567 struct walker_data *data = walker->data;
568 return http_fetch_ref(data->alt->base, ref);
571 static void cleanup(struct walker *walker)
573 struct walker_data *data = walker->data;
574 struct alt_base *alt, *alt_next;
579 alt_next = alt->next;
591 struct walker *get_http_walker(const char *url)
594 struct walker_data *data = xmalloc(sizeof(struct walker_data));
595 struct walker *walker = xmalloc(sizeof(struct walker));
597 data->alt = xmalloc(sizeof(*data->alt));
598 data->alt->base = xstrdup(url);
599 for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
602 data->alt->got_indices = 0;
603 data->alt->packs = NULL;
604 data->alt->next = NULL;
605 data->got_alternates = -1;
607 walker->corrupt_object_found = 0;
608 walker->fetch = fetch;
609 walker->fetch_ref = fetch_ref;
610 walker->prefetch = prefetch;
611 walker->cleanup = cleanup;
614 add_fill_function(walker, (int (*)(void *)) fill_active_slot);