10 struct packed_git *packs;
11 struct alt_base *next;
14 enum object_request_state {
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 object_request *next;
31 struct alternates_request {
32 struct walker *walker;
35 struct strbuf *buffer;
36 struct active_request_slot *slot;
46 static struct object_request *object_queue_head;
48 static void fetch_alternates(struct walker *walker, const char *base);
50 static void process_object_response(void *callback_data);
52 static void start_object_request(struct walker *walker,
53 struct object_request *obj_req)
55 struct active_request_slot *slot;
56 struct http_object_request *req;
58 req = new_http_object_request(obj_req->repo->base, obj_req->sha1);
60 obj_req->state = ABORTED;
66 slot->callback_func = process_object_response;
67 slot->callback_data = obj_req;
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);
78 static void finish_object_request(struct object_request *obj_req)
80 if (finish_http_object_request(obj_req->req))
83 if (obj_req->req->rename == 0)
84 walker_say(obj_req->walker, "got %s\n", sha1_to_hex(obj_req->sha1));
87 static void process_object_response(void *callback_data)
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;
95 process_http_object_request(obj_req->req);
96 obj_req->state = COMPLETE;
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) {
104 release_http_object_request(obj_req->req);
105 start_object_request(walker, obj_req);
110 finish_object_request(obj_req);
113 static void release_object_request(struct object_request *obj_req)
115 struct object_request *entry = object_queue_head;
117 if (obj_req->req !=NULL && obj_req->req->localfile != -1)
118 error("fd leakage in release: %d", obj_req->req->localfile);
119 if (obj_req == object_queue_head) {
120 object_queue_head = obj_req->next;
122 while (entry->next != NULL && entry->next != obj_req)
124 if (entry->next == obj_req)
125 entry->next = entry->next->next;
131 #ifdef USE_CURL_MULTI
132 static int fill_active_slot(struct walker *walker)
134 struct object_request *obj_req;
136 for (obj_req = object_queue_head; obj_req; obj_req = obj_req->next) {
137 if (obj_req->state == WAITING) {
138 if (has_sha1_file(obj_req->sha1))
139 obj_req->state = COMPLETE;
141 start_object_request(walker, obj_req);
150 static void prefetch(struct walker *walker, unsigned char *sha1)
152 struct object_request *newreq;
153 struct object_request *tail;
154 struct walker_data *data = walker->data;
156 newreq = xmalloc(sizeof(*newreq));
157 newreq->walker = walker;
158 hashcpy(newreq->sha1, sha1);
159 newreq->repo = data->alt;
160 newreq->state = WAITING;
164 http_is_verbose = walker->get_verbosely;
166 if (object_queue_head == NULL) {
167 object_queue_head = newreq;
169 tail = object_queue_head;
170 while (tail->next != NULL)
175 #ifdef USE_CURL_MULTI
181 static void process_alternates_response(void *callback_data)
183 struct alternates_request *alt_req =
184 (struct alternates_request *)callback_data;
185 struct walker *walker = alt_req->walker;
186 struct walker_data *cdata = walker->data;
187 struct active_request_slot *slot = alt_req->slot;
188 struct alt_base *tail = cdata->alt;
189 const char *base = alt_req->base;
190 static const char null_byte = '\0';
194 if (alt_req->http_specific) {
195 if (slot->curl_result != CURLE_OK ||
196 !alt_req->buffer->len) {
198 /* Try reusing the slot to get non-http alternates */
199 alt_req->http_specific = 0;
200 sprintf(alt_req->url, "%s/objects/info/alternates",
202 curl_easy_setopt(slot->curl, CURLOPT_URL,
206 if (slot->finished != NULL)
207 (*slot->finished) = 0;
208 if (!start_active_slot(slot)) {
209 cdata->got_alternates = -1;
211 if (slot->finished != NULL)
212 (*slot->finished) = 1;
216 } else if (slot->curl_result != CURLE_OK) {
217 if (!missing_target(slot)) {
218 cdata->got_alternates = -1;
223 fwrite_buffer(&null_byte, 1, 1, alt_req->buffer);
224 alt_req->buffer->len--;
225 data = alt_req->buffer->buf;
227 while (i < alt_req->buffer->len) {
229 while (posn < alt_req->buffer->len && data[posn] != '\n')
231 if (data[posn] == '\n') {
234 struct alt_base *newalt;
236 if (data[i] == '/') {
239 * http://git.host/pub/scm/linux.git/
241 * so memcpy(dst, base, serverlen) will
242 * copy up to "...git.host".
244 const char *colon_ss = strstr(base,"://");
246 serverlen = (strchr(colon_ss + 3, '/')
250 } else if (!memcmp(data + i, "../", 3)) {
252 * Relative URL; chop the corresponding
253 * number of subpath from base (and ../
254 * from data), and concatenate the result.
256 * The code first drops ../ from data, and
257 * then drops one ../ from data and one path
258 * from base. IOW, one extra ../ is dropped
259 * from data than path is dropped from base.
261 * This is not wrong. The alternate in
262 * http://git.host/pub/scm/linux.git/
264 * http://git.host/pub/scm/linus.git/
265 * is ../../linus.git/objects/. You need
266 * two ../../ to borrow from your direct
270 serverlen = strlen(base);
271 while (i + 2 < posn &&
272 !memcmp(data + i, "../", 3)) {
275 } while (serverlen &&
276 base[serverlen - 1] != '/');
279 /* If the server got removed, give up. */
280 okay = strchr(base, ':') - base + 3 <
282 } else if (alt_req->http_specific) {
283 char *colon = strchr(data + i, ':');
284 char *slash = strchr(data + i, '/');
285 if (colon && slash && colon < data + posn &&
286 slash < data + posn && colon < slash) {
290 /* skip "objects\n" at end */
292 target = xmalloc(serverlen + posn - i - 6);
293 memcpy(target, base, serverlen);
294 memcpy(target + serverlen, data + i,
296 target[serverlen + posn - i - 7] = 0;
297 if (walker->get_verbosely)
299 "Also look at %s\n", target);
300 newalt = xmalloc(sizeof(*newalt));
302 newalt->base = target;
303 newalt->got_indices = 0;
304 newalt->packs = NULL;
306 while (tail->next != NULL)
314 cdata->got_alternates = 1;
317 static void fetch_alternates(struct walker *walker, const char *base)
319 struct strbuf buffer = STRBUF_INIT;
321 struct active_request_slot *slot;
322 struct alternates_request alt_req;
323 struct walker_data *cdata = walker->data;
326 * If another request has already started fetching alternates,
327 * wait for them to arrive and return to processing this request's
330 #ifdef USE_CURL_MULTI
331 while (cdata->got_alternates == 0) {
336 /* Nothing to do if they've already been fetched */
337 if (cdata->got_alternates == 1)
340 /* Start the fetch */
341 cdata->got_alternates = 0;
343 if (walker->get_verbosely)
344 fprintf(stderr, "Getting alternates list for %s\n", base);
346 url = xmalloc(strlen(base) + 31);
347 sprintf(url, "%s/objects/info/http-alternates", base);
350 * Use a callback to process the result, since another request
351 * may fail and need to have alternates loaded before continuing
353 slot = get_active_slot();
354 slot->callback_func = process_alternates_response;
355 alt_req.walker = walker;
356 slot->callback_data = &alt_req;
358 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
359 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
360 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
364 alt_req.buffer = &buffer;
365 alt_req.http_specific = 1;
368 if (start_active_slot(slot))
369 run_active_slot(slot);
371 cdata->got_alternates = -1;
373 strbuf_release(&buffer);
377 static int fetch_indices(struct walker *walker, struct alt_base *repo)
381 if (repo->got_indices)
384 if (walker->get_verbosely)
385 fprintf(stderr, "Getting pack list for %s\n", repo->base);
387 switch (http_get_info_packs(repo->base, &repo->packs)) {
389 case HTTP_MISSING_TARGET:
390 repo->got_indices = 1;
394 repo->got_indices = 0;
401 static int fetch_pack(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
403 struct packed_git *target;
405 struct slot_results results;
406 struct http_pack_request *preq;
408 if (fetch_indices(walker, repo))
410 target = find_sha1_pack(sha1, repo->packs);
414 if (walker->get_verbosely) {
415 fprintf(stderr, "Getting pack %s\n",
416 sha1_to_hex(target->sha1));
417 fprintf(stderr, " which contains %s\n",
421 preq = new_http_pack_request(target, repo->base);
424 preq->lst = &repo->packs;
425 preq->slot->results = &results;
427 if (start_active_slot(preq->slot)) {
428 run_active_slot(preq->slot);
429 if (results.curl_result != CURLE_OK) {
430 error("Unable to get pack file %s\n%s", preq->url,
435 error("Unable to start request");
439 ret = finish_http_pack_request(preq);
440 release_http_pack_request(preq);
450 static void abort_object_request(struct object_request *obj_req)
452 release_object_request(obj_req);
455 static int fetch_object(struct walker *walker, struct alt_base *repo, unsigned char *sha1)
457 char *hex = sha1_to_hex(sha1);
459 struct object_request *obj_req = object_queue_head;
460 struct http_object_request *req;
462 while (obj_req != NULL && hashcmp(obj_req->sha1, sha1))
463 obj_req = obj_req->next;
465 return error("Couldn't find request for %s in the queue", hex);
467 if (has_sha1_file(obj_req->sha1)) {
468 if (obj_req->req != NULL)
469 abort_http_object_request(obj_req->req);
470 abort_object_request(obj_req);
474 #ifdef USE_CURL_MULTI
475 while (obj_req->state == WAITING)
478 start_object_request(walker, obj_req);
482 * obj_req->req might change when fetching alternates in the callback
483 * process_object_response; therefore, the "shortcut" variable, req,
484 * is used only after we're done with slots.
486 while (obj_req->state == ACTIVE)
487 run_active_slot(obj_req->req->slot);
491 if (req->localfile != -1) {
492 close(req->localfile);
496 if (obj_req->state == ABORTED) {
497 ret = error("Request for %s aborted", hex);
498 } else if (req->curl_result != CURLE_OK &&
499 req->http_code != 416) {
500 if (missing_target(req))
501 ret = -1; /* Be silent, it is probably in a pack. */
503 ret = error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
504 req->errorstr, req->curl_result,
505 req->http_code, hex);
506 } else if (req->zret != Z_STREAM_END) {
507 walker->corrupt_object_found++;
508 ret = error("File %s (%s) corrupt", hex, req->url);
509 } else if (hashcmp(obj_req->sha1, req->real_sha1)) {
510 ret = error("File %s has bad hash", hex);
511 } else if (req->rename < 0) {
512 ret = error("unable to write sha1 filename %s",
516 release_http_object_request(req);
517 release_object_request(obj_req);
521 static int fetch(struct walker *walker, unsigned char *sha1)
523 struct walker_data *data = walker->data;
524 struct alt_base *altbase = data->alt;
526 if (!fetch_object(walker, altbase, sha1))
529 if (!fetch_pack(walker, altbase, sha1))
531 fetch_alternates(walker, data->alt->base);
532 altbase = altbase->next;
534 return error("Unable to find %s under %s", sha1_to_hex(sha1),
538 static int fetch_ref(struct walker *walker, struct ref *ref)
540 struct walker_data *data = walker->data;
541 return http_fetch_ref(data->alt->base, ref);
544 static void cleanup(struct walker *walker)
549 struct walker *get_http_walker(const char *url, struct remote *remote)
552 struct walker_data *data = xmalloc(sizeof(struct walker_data));
553 struct walker *walker = xmalloc(sizeof(struct walker));
557 data->alt = xmalloc(sizeof(*data->alt));
558 data->alt->base = xmalloc(strlen(url) + 1);
559 strcpy(data->alt->base, url);
560 for (s = data->alt->base + strlen(data->alt->base) - 1; *s == '/'; --s)
563 data->alt->got_indices = 0;
564 data->alt->packs = NULL;
565 data->alt->next = NULL;
566 data->got_alternates = -1;
568 walker->corrupt_object_found = 0;
569 walker->fetch = fetch;
570 walker->fetch_ref = fetch_ref;
571 walker->prefetch = prefetch;
572 walker->cleanup = cleanup;
575 #ifdef USE_CURL_MULTI
576 add_fill_function(walker, (int (*)(void *)) fill_active_slot);