11 struct packed_git
*packs
;
12 struct alt_base
*next
;
15 enum object_request_state
{
22 struct object_request
{
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 list_head node
;
31 struct alternates_request
{
32 struct walker
*walker
;
35 struct strbuf
*buffer
;
36 struct active_request_slot
*slot
;
46 static LIST_HEAD(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 if (obj_req
->req
!=NULL
&& obj_req
->req
->localfile
!= -1)
116 error("fd leakage in release: %d", obj_req
->req
->localfile
);
118 list_del(&obj_req
->node
);
122 #ifdef USE_CURL_MULTI
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
);
143 static void prefetch(struct walker
*walker
, unsigned char *sha1
)
145 struct object_request
*newreq
;
146 struct walker_data
*data
= walker
->data
;
148 newreq
= xmalloc(sizeof(*newreq
));
149 newreq
->walker
= walker
;
150 hashcpy(newreq
->sha1
, sha1
);
151 newreq
->repo
= data
->alt
;
152 newreq
->state
= WAITING
;
155 http_is_verbose
= walker
->get_verbosely
;
156 list_add_tail(&newreq
->node
, &object_queue_head
);
158 #ifdef USE_CURL_MULTI
164 static int is_alternate_allowed(const char *url
)
166 const char *protocols
[] = {
167 "http", "https", "ftp", "ftps"
171 for (i
= 0; i
< ARRAY_SIZE(protocols
); i
++) {
173 if (skip_prefix(url
, protocols
[i
], &end
) &&
174 starts_with(end
, "://"))
178 if (i
>= ARRAY_SIZE(protocols
)) {
179 warning("ignoring alternate with unknown protocol: %s", url
);
182 if (!is_transport_allowed(protocols
[i
], 0)) {
183 warning("ignoring alternate with restricted protocol: %s", url
);
190 static void process_alternates_response(void *callback_data
)
192 struct alternates_request
*alt_req
=
193 (struct alternates_request
*)callback_data
;
194 struct walker
*walker
= alt_req
->walker
;
195 struct walker_data
*cdata
= walker
->data
;
196 struct active_request_slot
*slot
= alt_req
->slot
;
197 struct alt_base
*tail
= cdata
->alt
;
198 const char *base
= alt_req
->base
;
199 const char null_byte
= '\0';
203 if (alt_req
->http_specific
) {
204 if (slot
->curl_result
!= CURLE_OK
||
205 !alt_req
->buffer
->len
) {
207 /* Try reusing the slot to get non-http alternates */
208 alt_req
->http_specific
= 0;
209 strbuf_reset(alt_req
->url
);
210 strbuf_addf(alt_req
->url
, "%s/objects/info/alternates",
212 curl_easy_setopt(slot
->curl
, CURLOPT_URL
,
216 if (slot
->finished
!= NULL
)
217 (*slot
->finished
) = 0;
218 if (!start_active_slot(slot
)) {
219 cdata
->got_alternates
= -1;
221 if (slot
->finished
!= NULL
)
222 (*slot
->finished
) = 1;
226 } else if (slot
->curl_result
!= CURLE_OK
) {
227 if (!missing_target(slot
)) {
228 cdata
->got_alternates
= -1;
233 fwrite_buffer((char *)&null_byte
, 1, 1, alt_req
->buffer
);
234 alt_req
->buffer
->len
--;
235 data
= alt_req
->buffer
->buf
;
237 while (i
< alt_req
->buffer
->len
) {
239 while (posn
< alt_req
->buffer
->len
&& data
[posn
] != '\n')
241 if (data
[posn
] == '\n') {
244 struct alt_base
*newalt
;
245 if (data
[i
] == '/') {
248 * http://git.host/pub/scm/linux.git/
250 * so memcpy(dst, base, serverlen) will
251 * copy up to "...git.host".
253 const char *colon_ss
= strstr(base
,"://");
255 serverlen
= (strchr(colon_ss
+ 3, '/')
259 } else if (!memcmp(data
+ i
, "../", 3)) {
261 * Relative URL; chop the corresponding
262 * number of subpath from base (and ../
263 * from data), and concatenate the result.
265 * The code first drops ../ from data, and
266 * then drops one ../ from data and one path
267 * from base. IOW, one extra ../ is dropped
268 * from data than path is dropped from base.
270 * This is not wrong. The alternate in
271 * http://git.host/pub/scm/linux.git/
273 * http://git.host/pub/scm/linus.git/
274 * is ../../linus.git/objects/. You need
275 * two ../../ to borrow from your direct
279 serverlen
= strlen(base
);
280 while (i
+ 2 < posn
&&
281 !memcmp(data
+ i
, "../", 3)) {
284 } while (serverlen
&&
285 base
[serverlen
- 1] != '/');
288 /* If the server got removed, give up. */
289 okay
= strchr(base
, ':') - base
+ 3 <
291 } else if (alt_req
->http_specific
) {
292 char *colon
= strchr(data
+ i
, ':');
293 char *slash
= strchr(data
+ i
, '/');
294 if (colon
&& slash
&& colon
< data
+ posn
&&
295 slash
< data
+ posn
&& colon
< slash
) {
300 struct strbuf target
= STRBUF_INIT
;
301 strbuf_add(&target
, base
, serverlen
);
302 strbuf_add(&target
, data
+ i
, posn
- i
);
303 if (!strbuf_strip_suffix(&target
, "objects")) {
304 warning("ignoring alternate that does"
305 " not end in 'objects': %s",
307 strbuf_release(&target
);
308 } else if (is_alternate_allowed(target
.buf
)) {
309 warning("adding alternate object store: %s",
311 newalt
= xmalloc(sizeof(*newalt
));
313 newalt
->base
= strbuf_detach(&target
, NULL
);
314 newalt
->got_indices
= 0;
315 newalt
->packs
= NULL
;
317 while (tail
->next
!= NULL
)
326 cdata
->got_alternates
= 1;
329 static void fetch_alternates(struct walker
*walker
, const char *base
)
331 struct strbuf buffer
= STRBUF_INIT
;
332 struct strbuf url
= STRBUF_INIT
;
333 struct active_request_slot
*slot
;
334 struct alternates_request alt_req
;
335 struct walker_data
*cdata
= walker
->data
;
337 if (http_follow_config
!= HTTP_FOLLOW_ALWAYS
)
341 * If another request has already started fetching alternates,
342 * wait for them to arrive and return to processing this request's
345 #ifdef USE_CURL_MULTI
346 while (cdata
->got_alternates
== 0) {
351 /* Nothing to do if they've already been fetched */
352 if (cdata
->got_alternates
== 1)
355 /* Start the fetch */
356 cdata
->got_alternates
= 0;
358 if (walker
->get_verbosely
)
359 fprintf(stderr
, "Getting alternates list for %s\n", base
);
361 strbuf_addf(&url
, "%s/objects/info/http-alternates", base
);
364 * Use a callback to process the result, since another request
365 * may fail and need to have alternates loaded before continuing
367 slot
= get_active_slot();
368 slot
->callback_func
= process_alternates_response
;
369 alt_req
.walker
= walker
;
370 slot
->callback_data
= &alt_req
;
372 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
373 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
374 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
.buf
);
378 alt_req
.buffer
= &buffer
;
379 alt_req
.http_specific
= 1;
382 if (start_active_slot(slot
))
383 run_active_slot(slot
);
385 cdata
->got_alternates
= -1;
387 strbuf_release(&buffer
);
388 strbuf_release(&url
);
391 static int fetch_indices(struct walker
*walker
, struct alt_base
*repo
)
395 if (repo
->got_indices
)
398 if (walker
->get_verbosely
)
399 fprintf(stderr
, "Getting pack list for %s\n", repo
->base
);
401 switch (http_get_info_packs(repo
->base
, &repo
->packs
)) {
403 case HTTP_MISSING_TARGET
:
404 repo
->got_indices
= 1;
408 repo
->got_indices
= 0;
415 static int http_fetch_pack(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
417 struct packed_git
*target
;
419 struct slot_results results
;
420 struct http_pack_request
*preq
;
422 if (fetch_indices(walker
, repo
))
424 target
= find_sha1_pack(sha1
, repo
->packs
);
428 if (walker
->get_verbosely
) {
429 fprintf(stderr
, "Getting pack %s\n",
430 sha1_to_hex(target
->sha1
));
431 fprintf(stderr
, " which contains %s\n",
435 preq
= new_http_pack_request(target
, repo
->base
);
438 preq
->lst
= &repo
->packs
;
439 preq
->slot
->results
= &results
;
441 if (start_active_slot(preq
->slot
)) {
442 run_active_slot(preq
->slot
);
443 if (results
.curl_result
!= CURLE_OK
) {
444 error("Unable to get pack file %s\n%s", preq
->url
,
449 error("Unable to start request");
453 ret
= finish_http_pack_request(preq
);
454 release_http_pack_request(preq
);
464 static void abort_object_request(struct object_request
*obj_req
)
466 release_object_request(obj_req
);
469 static int fetch_object(struct walker
*walker
, unsigned char *sha1
)
471 char *hex
= sha1_to_hex(sha1
);
473 struct object_request
*obj_req
= NULL
;
474 struct http_object_request
*req
;
475 struct list_head
*pos
, *head
= &object_queue_head
;
477 list_for_each(pos
, head
) {
478 obj_req
= list_entry(pos
, struct object_request
, node
);
479 if (!hashcmp(obj_req
->sha1
, sha1
))
483 return error("Couldn't find request for %s in the queue", hex
);
485 if (has_sha1_file(obj_req
->sha1
)) {
486 if (obj_req
->req
!= NULL
)
487 abort_http_object_request(obj_req
->req
);
488 abort_object_request(obj_req
);
492 #ifdef USE_CURL_MULTI
493 while (obj_req
->state
== WAITING
)
496 start_object_request(walker
, obj_req
);
500 * obj_req->req might change when fetching alternates in the callback
501 * process_object_response; therefore, the "shortcut" variable, req,
502 * is used only after we're done with slots.
504 while (obj_req
->state
== ACTIVE
)
505 run_active_slot(obj_req
->req
->slot
);
509 if (req
->localfile
!= -1) {
510 close(req
->localfile
);
515 * we turned off CURLOPT_FAILONERROR to avoid losing a
516 * persistent connection and got CURLE_OK.
518 if (req
->http_code
>= 300 && req
->curl_result
== CURLE_OK
&&
519 (starts_with(req
->url
, "http://") ||
520 starts_with(req
->url
, "https://"))) {
521 req
->curl_result
= CURLE_HTTP_RETURNED_ERROR
;
522 xsnprintf(req
->errorstr
, sizeof(req
->errorstr
),
523 "HTTP request failed");
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. */
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 (hashcmp(obj_req
->sha1
, req
->real_sha1
)) {
540 ret
= error("File %s has bad hash", hex
);
541 } else if (req
->rename
< 0) {
542 ret
= error("unable to write sha1 filename %s",
543 sha1_file_name(req
->sha1
));
546 release_http_object_request(req
);
547 release_object_request(obj_req
);
551 static int fetch(struct walker
*walker
, unsigned char *sha1
)
553 struct walker_data
*data
= walker
->data
;
554 struct alt_base
*altbase
= data
->alt
;
556 if (!fetch_object(walker
, sha1
))
559 if (!http_fetch_pack(walker
, altbase
, sha1
))
561 fetch_alternates(walker
, data
->alt
->base
);
562 altbase
= altbase
->next
;
564 return error("Unable to find %s under %s", sha1_to_hex(sha1
),
568 static int fetch_ref(struct walker
*walker
, struct ref
*ref
)
570 struct walker_data
*data
= walker
->data
;
571 return http_fetch_ref(data
->alt
->base
, ref
);
574 static void cleanup(struct walker
*walker
)
576 struct walker_data
*data
= walker
->data
;
577 struct alt_base
*alt
, *alt_next
;
582 alt_next
= alt
->next
;
594 struct walker
*get_http_walker(const char *url
)
597 struct walker_data
*data
= xmalloc(sizeof(struct walker_data
));
598 struct walker
*walker
= xmalloc(sizeof(struct walker
));
600 data
->alt
= xmalloc(sizeof(*data
->alt
));
601 data
->alt
->base
= xstrdup(url
);
602 for (s
= data
->alt
->base
+ strlen(data
->alt
->base
) - 1; *s
== '/'; --s
)
605 data
->alt
->got_indices
= 0;
606 data
->alt
->packs
= NULL
;
607 data
->alt
->next
= NULL
;
608 data
->got_alternates
= -1;
610 walker
->corrupt_object_found
= 0;
611 walker
->fetch
= fetch
;
612 walker
->fetch_ref
= fetch_ref
;
613 walker
->prefetch
= prefetch
;
614 walker
->cleanup
= cleanup
;
617 #ifdef USE_CURL_MULTI
618 add_fill_function(walker
, (int (*)(void *)) fill_active_slot
);