12 struct packed_git
*packs
;
13 struct alt_base
*next
;
16 enum object_request_state
{
23 struct object_request
{
24 struct walker
*walker
;
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
->oid
.hash
);
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", oid_to_hex(&obj_req
->oid
));
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 #ifdef USE_CURL_MULTI
124 static int fill_active_slot(struct walker
*walker
)
126 struct object_request
*obj_req
;
127 struct list_head
*pos
, *tmp
, *head
= &object_queue_head
;
129 list_for_each_safe(pos
, tmp
, head
) {
130 obj_req
= list_entry(pos
, struct object_request
, node
);
131 if (obj_req
->state
== WAITING
) {
132 if (has_sha1_file(obj_req
->oid
.hash
))
133 obj_req
->state
= COMPLETE
;
135 start_object_request(walker
, obj_req
);
144 static void prefetch(struct walker
*walker
, unsigned char *sha1
)
146 struct object_request
*newreq
;
147 struct walker_data
*data
= walker
->data
;
149 newreq
= xmalloc(sizeof(*newreq
));
150 newreq
->walker
= walker
;
151 hashcpy(newreq
->oid
.hash
, sha1
);
152 newreq
->repo
= data
->alt
;
153 newreq
->state
= WAITING
;
156 http_is_verbose
= walker
->get_verbosely
;
157 list_add_tail(&newreq
->node
, &object_queue_head
);
159 #ifdef USE_CURL_MULTI
165 static int is_alternate_allowed(const char *url
)
167 const char *protocols
[] = {
168 "http", "https", "ftp", "ftps"
172 if (http_follow_config
!= HTTP_FOLLOW_ALWAYS
) {
173 warning("alternate disabled by http.followRedirects: %s", url
);
177 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++) {
179 if (skip_prefix(url
, protocols
[i
], &end
) &&
180 starts_with(end
, "://"))
184 if (i
>= ARRAY_SIZE(protocols
)) {
185 warning("ignoring alternate with unknown protocol: %s", url
);
188 if (!is_transport_allowed(protocols
[i
], 0)) {
189 warning("ignoring alternate with restricted protocol: %s", url
);
196 static void process_alternates_response(void *callback_data
)
198 struct alternates_request
*alt_req
=
199 (struct alternates_request
*)callback_data
;
200 struct walker
*walker
= alt_req
->walker
;
201 struct walker_data
*cdata
= walker
->data
;
202 struct active_request_slot
*slot
= alt_req
->slot
;
203 struct alt_base
*tail
= cdata
->alt
;
204 const char *base
= alt_req
->base
;
205 const char null_byte
= '\0';
209 if (alt_req
->http_specific
) {
210 if (slot
->curl_result
!= CURLE_OK
||
211 !alt_req
->buffer
->len
) {
213 /* Try reusing the slot to get non-http alternates */
214 alt_req
->http_specific
= 0;
215 strbuf_reset(alt_req
->url
);
216 strbuf_addf(alt_req
->url
, "%s/objects/info/alternates",
218 curl_easy_setopt(slot
->curl
, CURLOPT_URL
,
222 if (slot
->finished
!= NULL
)
223 (*slot
->finished
) = 0;
224 if (!start_active_slot(slot
)) {
225 cdata
->got_alternates
= -1;
227 if (slot
->finished
!= NULL
)
228 (*slot
->finished
) = 1;
232 } else if (slot
->curl_result
!= CURLE_OK
) {
233 if (!missing_target(slot
)) {
234 cdata
->got_alternates
= -1;
239 fwrite_buffer((char *)&null_byte
, 1, 1, alt_req
->buffer
);
240 alt_req
->buffer
->len
--;
241 data
= alt_req
->buffer
->buf
;
243 while (i
< alt_req
->buffer
->len
) {
245 while (posn
< alt_req
->buffer
->len
&& data
[posn
] != '\n')
247 if (data
[posn
] == '\n') {
250 struct alt_base
*newalt
;
251 if (data
[i
] == '/') {
254 * http://git.host/pub/scm/linux.git/
256 * so memcpy(dst, base, serverlen) will
257 * copy up to "...git.host".
259 const char *colon_ss
= strstr(base
,"://");
261 serverlen
= (strchr(colon_ss
+ 3, '/')
265 } else if (!memcmp(data
+ i
, "../", 3)) {
267 * Relative URL; chop the corresponding
268 * number of subpath from base (and ../
269 * from data), and concatenate the result.
271 * The code first drops ../ from data, and
272 * then drops one ../ from data and one path
273 * from base. IOW, one extra ../ is dropped
274 * from data than path is dropped from base.
276 * This is not wrong. The alternate in
277 * http://git.host/pub/scm/linux.git/
279 * http://git.host/pub/scm/linus.git/
280 * is ../../linus.git/objects/. You need
281 * two ../../ to borrow from your direct
285 serverlen
= strlen(base
);
286 while (i
+ 2 < posn
&&
287 !memcmp(data
+ i
, "../", 3)) {
290 } while (serverlen
&&
291 base
[serverlen
- 1] != '/');
294 /* If the server got removed, give up. */
295 okay
= strchr(base
, ':') - base
+ 3 <
297 } else if (alt_req
->http_specific
) {
298 char *colon
= strchr(data
+ i
, ':');
299 char *slash
= strchr(data
+ i
, '/');
300 if (colon
&& slash
&& colon
< data
+ posn
&&
301 slash
< data
+ posn
&& colon
< slash
) {
306 struct strbuf target
= STRBUF_INIT
;
307 strbuf_add(&target
, base
, serverlen
);
308 strbuf_add(&target
, data
+ i
, posn
- i
);
309 if (!strbuf_strip_suffix(&target
, "objects")) {
310 warning("ignoring alternate that does"
311 " not end in 'objects': %s",
313 strbuf_release(&target
);
314 } else if (is_alternate_allowed(target
.buf
)) {
315 warning("adding alternate object store: %s",
317 newalt
= xmalloc(sizeof(*newalt
));
319 newalt
->base
= strbuf_detach(&target
, NULL
);
320 newalt
->got_indices
= 0;
321 newalt
->packs
= NULL
;
323 while (tail
->next
!= NULL
)
327 strbuf_release(&target
);
334 cdata
->got_alternates
= 1;
337 static void fetch_alternates(struct walker
*walker
, const char *base
)
339 struct strbuf buffer
= STRBUF_INIT
;
340 struct strbuf url
= STRBUF_INIT
;
341 struct active_request_slot
*slot
;
342 struct alternates_request alt_req
;
343 struct walker_data
*cdata
= walker
->data
;
346 * If another request has already started fetching alternates,
347 * wait for them to arrive and return to processing this request's
350 #ifdef USE_CURL_MULTI
351 while (cdata
->got_alternates
== 0) {
356 /* Nothing to do if they've already been fetched */
357 if (cdata
->got_alternates
== 1)
360 /* Start the fetch */
361 cdata
->got_alternates
= 0;
363 if (walker
->get_verbosely
)
364 fprintf(stderr
, "Getting alternates list for %s\n", base
);
366 strbuf_addf(&url
, "%s/objects/info/http-alternates", base
);
369 * Use a callback to process the result, since another request
370 * may fail and need to have alternates loaded before continuing
372 slot
= get_active_slot();
373 slot
->callback_func
= process_alternates_response
;
374 alt_req
.walker
= walker
;
375 slot
->callback_data
= &alt_req
;
377 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
378 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
379 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
.buf
);
383 alt_req
.buffer
= &buffer
;
384 alt_req
.http_specific
= 1;
387 if (start_active_slot(slot
))
388 run_active_slot(slot
);
390 cdata
->got_alternates
= -1;
392 strbuf_release(&buffer
);
393 strbuf_release(&url
);
396 static int fetch_indices(struct walker
*walker
, struct alt_base
*repo
)
400 if (repo
->got_indices
)
403 if (walker
->get_verbosely
)
404 fprintf(stderr
, "Getting pack list for %s\n", repo
->base
);
406 switch (http_get_info_packs(repo
->base
, &repo
->packs
)) {
408 case HTTP_MISSING_TARGET
:
409 repo
->got_indices
= 1;
413 repo
->got_indices
= 0;
420 static int http_fetch_pack(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
422 struct packed_git
*target
;
424 struct slot_results results
;
425 struct http_pack_request
*preq
;
427 if (fetch_indices(walker
, repo
))
429 target
= find_sha1_pack(sha1
, repo
->packs
);
433 if (walker
->get_verbosely
) {
434 fprintf(stderr
, "Getting pack %s\n",
435 sha1_to_hex(target
->sha1
));
436 fprintf(stderr
, " which contains %s\n",
440 preq
= new_http_pack_request(target
, repo
->base
);
443 preq
->lst
= &repo
->packs
;
444 preq
->slot
->results
= &results
;
446 if (start_active_slot(preq
->slot
)) {
447 run_active_slot(preq
->slot
);
448 if (results
.curl_result
!= CURLE_OK
) {
449 error("Unable to get pack file %s\n%s", preq
->url
,
454 error("Unable to start request");
458 ret
= finish_http_pack_request(preq
);
459 release_http_pack_request(preq
);
469 static void abort_object_request(struct object_request
*obj_req
)
471 release_object_request(obj_req
);
474 static int fetch_object(struct walker
*walker
, unsigned char *sha1
)
476 char *hex
= sha1_to_hex(sha1
);
478 struct object_request
*obj_req
= NULL
;
479 struct http_object_request
*req
;
480 struct list_head
*pos
, *head
= &object_queue_head
;
482 list_for_each(pos
, head
) {
483 obj_req
= list_entry(pos
, struct object_request
, node
);
484 if (!hashcmp(obj_req
->oid
.hash
, sha1
))
488 return error("Couldn't find request for %s in the queue", hex
);
490 if (has_sha1_file(obj_req
->oid
.hash
)) {
491 if (obj_req
->req
!= NULL
)
492 abort_http_object_request(obj_req
->req
);
493 abort_object_request(obj_req
);
497 #ifdef USE_CURL_MULTI
498 while (obj_req
->state
== WAITING
)
501 start_object_request(walker
, obj_req
);
505 * obj_req->req might change when fetching alternates in the callback
506 * process_object_response; therefore, the "shortcut" variable, req,
507 * is used only after we're done with slots.
509 while (obj_req
->state
== ACTIVE
)
510 run_active_slot(obj_req
->req
->slot
);
514 if (req
->localfile
!= -1) {
515 close(req
->localfile
);
520 * we turned off CURLOPT_FAILONERROR to avoid losing a
521 * persistent connection and got CURLE_OK.
523 if (req
->http_code
>= 300 && req
->curl_result
== CURLE_OK
&&
524 (starts_with(req
->url
, "http://") ||
525 starts_with(req
->url
, "https://"))) {
526 req
->curl_result
= CURLE_HTTP_RETURNED_ERROR
;
527 xsnprintf(req
->errorstr
, sizeof(req
->errorstr
),
528 "HTTP request failed");
531 if (obj_req
->state
== ABORTED
) {
532 ret
= error("Request for %s aborted", hex
);
533 } else if (req
->curl_result
!= CURLE_OK
&&
534 req
->http_code
!= 416) {
535 if (missing_target(req
))
536 ret
= -1; /* Be silent, it is probably in a pack. */
538 ret
= error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
539 req
->errorstr
, req
->curl_result
,
540 req
->http_code
, hex
);
541 } else if (req
->zret
!= Z_STREAM_END
) {
542 walker
->corrupt_object_found
++;
543 ret
= error("File %s (%s) corrupt", hex
, req
->url
);
544 } else if (hashcmp(obj_req
->oid
.hash
, req
->real_sha1
)) {
545 ret
= error("File %s has bad hash", hex
);
546 } else if (req
->rename
< 0) {
547 struct strbuf buf
= STRBUF_INIT
;
548 sha1_file_name(&buf
, req
->sha1
);
549 ret
= error("unable to write sha1 filename %s", buf
.buf
);
550 strbuf_release(&buf
);
553 release_http_object_request(req
);
554 release_object_request(obj_req
);
558 static int fetch(struct walker
*walker
, unsigned char *sha1
)
560 struct walker_data
*data
= walker
->data
;
561 struct alt_base
*altbase
= data
->alt
;
563 if (!fetch_object(walker
, sha1
))
566 if (!http_fetch_pack(walker
, altbase
, sha1
))
568 fetch_alternates(walker
, data
->alt
->base
);
569 altbase
= altbase
->next
;
571 return error("Unable to find %s under %s", sha1_to_hex(sha1
),
575 static int fetch_ref(struct walker
*walker
, struct ref
*ref
)
577 struct walker_data
*data
= walker
->data
;
578 return http_fetch_ref(data
->alt
->base
, ref
);
581 static void cleanup(struct walker
*walker
)
583 struct walker_data
*data
= walker
->data
;
584 struct alt_base
*alt
, *alt_next
;
589 alt_next
= alt
->next
;
601 struct walker
*get_http_walker(const char *url
)
604 struct walker_data
*data
= xmalloc(sizeof(struct walker_data
));
605 struct walker
*walker
= xmalloc(sizeof(struct walker
));
607 data
->alt
= xmalloc(sizeof(*data
->alt
));
608 data
->alt
->base
= xstrdup(url
);
609 for (s
= data
->alt
->base
+ strlen(data
->alt
->base
) - 1; *s
== '/'; --s
)
612 data
->alt
->got_indices
= 0;
613 data
->alt
->packs
= NULL
;
614 data
->alt
->next
= NULL
;
615 data
->got_alternates
= -1;
617 walker
->corrupt_object_found
= 0;
618 walker
->fetch
= fetch
;
619 walker
->fetch_ref
= fetch_ref
;
620 walker
->prefetch
= prefetch
;
621 walker
->cleanup
= cleanup
;
624 #ifdef USE_CURL_MULTI
625 add_fill_function(walker
, (int (*)(void *)) fill_active_slot
);