10 struct packed_git
*packs
;
11 struct alt_base
*next
;
14 enum object_request_state
{
21 struct object_request
{
22 struct walker
*walker
;
23 unsigned char sha1
[20];
24 struct alt_base
*repo
;
25 enum object_request_state state
;
26 struct http_object_request
*req
;
27 struct list_head node
;
30 struct alternates_request
{
31 struct walker
*walker
;
34 struct strbuf
*buffer
;
35 struct active_request_slot
*slot
;
45 static LIST_HEAD(object_queue_head
);
47 static void fetch_alternates(struct walker
*walker
, const char *base
);
49 static void process_object_response(void *callback_data
);
51 static void start_object_request(struct walker
*walker
,
52 struct object_request
*obj_req
)
54 struct active_request_slot
*slot
;
55 struct http_object_request
*req
;
57 req
= new_http_object_request(obj_req
->repo
->base
, obj_req
->sha1
);
59 obj_req
->state
= ABORTED
;
65 slot
->callback_func
= process_object_response
;
66 slot
->callback_data
= obj_req
;
68 /* Try to get the request started, abort the request on error */
69 obj_req
->state
= ACTIVE
;
70 if (!start_active_slot(slot
)) {
71 obj_req
->state
= ABORTED
;
72 release_http_object_request(req
);
77 static void finish_object_request(struct object_request
*obj_req
)
79 if (finish_http_object_request(obj_req
->req
))
82 if (obj_req
->req
->rename
== 0)
83 walker_say(obj_req
->walker
, "got %s\n", sha1_to_hex(obj_req
->sha1
));
86 static void process_object_response(void *callback_data
)
88 struct object_request
*obj_req
=
89 (struct object_request
*)callback_data
;
90 struct walker
*walker
= obj_req
->walker
;
91 struct walker_data
*data
= walker
->data
;
92 struct alt_base
*alt
= data
->alt
;
94 process_http_object_request(obj_req
->req
);
95 obj_req
->state
= COMPLETE
;
97 /* Use alternates if necessary */
98 if (missing_target(obj_req
->req
)) {
99 fetch_alternates(walker
, alt
->base
);
100 if (obj_req
->repo
->next
!= NULL
) {
103 release_http_object_request(obj_req
->req
);
104 start_object_request(walker
, obj_req
);
109 finish_object_request(obj_req
);
112 static void release_object_request(struct object_request
*obj_req
)
114 if (obj_req
->req
!=NULL
&& obj_req
->req
->localfile
!= -1)
115 error("fd leakage in release: %d", obj_req
->req
->localfile
);
117 list_del(&obj_req
->node
);
121 #ifdef USE_CURL_MULTI
122 static int fill_active_slot(struct walker
*walker
)
124 struct object_request
*obj_req
;
125 struct list_head
*pos
, *tmp
, *head
= &object_queue_head
;
127 list_for_each_safe(pos
, tmp
, head
) {
128 obj_req
= list_entry(pos
, struct object_request
, node
);
129 if (obj_req
->state
== WAITING
) {
130 if (has_sha1_file(obj_req
->sha1
))
131 obj_req
->state
= COMPLETE
;
133 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
);
157 #ifdef USE_CURL_MULTI
163 static void process_alternates_response(void *callback_data
)
165 struct alternates_request
*alt_req
=
166 (struct alternates_request
*)callback_data
;
167 struct walker
*walker
= alt_req
->walker
;
168 struct walker_data
*cdata
= walker
->data
;
169 struct active_request_slot
*slot
= alt_req
->slot
;
170 struct alt_base
*tail
= cdata
->alt
;
171 const char *base
= alt_req
->base
;
172 const char null_byte
= '\0';
176 if (alt_req
->http_specific
) {
177 if (slot
->curl_result
!= CURLE_OK
||
178 !alt_req
->buffer
->len
) {
180 /* Try reusing the slot to get non-http alternates */
181 alt_req
->http_specific
= 0;
182 strbuf_reset(alt_req
->url
);
183 strbuf_addf(alt_req
->url
, "%s/objects/info/alternates",
185 curl_easy_setopt(slot
->curl
, CURLOPT_URL
,
189 if (slot
->finished
!= NULL
)
190 (*slot
->finished
) = 0;
191 if (!start_active_slot(slot
)) {
192 cdata
->got_alternates
= -1;
194 if (slot
->finished
!= NULL
)
195 (*slot
->finished
) = 1;
199 } else if (slot
->curl_result
!= CURLE_OK
) {
200 if (!missing_target(slot
)) {
201 cdata
->got_alternates
= -1;
206 fwrite_buffer((char *)&null_byte
, 1, 1, alt_req
->buffer
);
207 alt_req
->buffer
->len
--;
208 data
= alt_req
->buffer
->buf
;
210 while (i
< alt_req
->buffer
->len
) {
212 while (posn
< alt_req
->buffer
->len
&& data
[posn
] != '\n')
214 if (data
[posn
] == '\n') {
217 struct alt_base
*newalt
;
218 if (data
[i
] == '/') {
221 * http://git.host/pub/scm/linux.git/
223 * so memcpy(dst, base, serverlen) will
224 * copy up to "...git.host".
226 const char *colon_ss
= strstr(base
,"://");
228 serverlen
= (strchr(colon_ss
+ 3, '/')
232 } else if (!memcmp(data
+ i
, "../", 3)) {
234 * Relative URL; chop the corresponding
235 * number of subpath from base (and ../
236 * from data), and concatenate the result.
238 * The code first drops ../ from data, and
239 * then drops one ../ from data and one path
240 * from base. IOW, one extra ../ is dropped
241 * from data than path is dropped from base.
243 * This is not wrong. The alternate in
244 * http://git.host/pub/scm/linux.git/
246 * http://git.host/pub/scm/linus.git/
247 * is ../../linus.git/objects/. You need
248 * two ../../ to borrow from your direct
252 serverlen
= strlen(base
);
253 while (i
+ 2 < posn
&&
254 !memcmp(data
+ i
, "../", 3)) {
257 } while (serverlen
&&
258 base
[serverlen
- 1] != '/');
261 /* If the server got removed, give up. */
262 okay
= strchr(base
, ':') - base
+ 3 <
264 } else if (alt_req
->http_specific
) {
265 char *colon
= strchr(data
+ i
, ':');
266 char *slash
= strchr(data
+ i
, '/');
267 if (colon
&& slash
&& colon
< data
+ posn
&&
268 slash
< data
+ posn
&& colon
< slash
) {
272 /* skip "objects\n" at end */
274 struct strbuf target
= STRBUF_INIT
;
275 strbuf_add(&target
, base
, serverlen
);
276 strbuf_add(&target
, data
+ i
, posn
- i
- 7);
277 warning("adding alternate object store: %s",
279 newalt
= xmalloc(sizeof(*newalt
));
281 newalt
->base
= strbuf_detach(&target
, NULL
);
282 newalt
->got_indices
= 0;
283 newalt
->packs
= NULL
;
285 while (tail
->next
!= NULL
)
293 cdata
->got_alternates
= 1;
296 static void fetch_alternates(struct walker
*walker
, const char *base
)
298 struct strbuf buffer
= STRBUF_INIT
;
299 struct strbuf url
= STRBUF_INIT
;
300 struct active_request_slot
*slot
;
301 struct alternates_request alt_req
;
302 struct walker_data
*cdata
= walker
->data
;
304 if (http_follow_config
!= HTTP_FOLLOW_ALWAYS
)
308 * If another request has already started fetching alternates,
309 * wait for them to arrive and return to processing this request's
312 #ifdef USE_CURL_MULTI
313 while (cdata
->got_alternates
== 0) {
318 /* Nothing to do if they've already been fetched */
319 if (cdata
->got_alternates
== 1)
322 /* Start the fetch */
323 cdata
->got_alternates
= 0;
325 if (walker
->get_verbosely
)
326 fprintf(stderr
, "Getting alternates list for %s\n", base
);
328 strbuf_addf(&url
, "%s/objects/info/http-alternates", base
);
331 * Use a callback to process the result, since another request
332 * may fail and need to have alternates loaded before continuing
334 slot
= get_active_slot();
335 slot
->callback_func
= process_alternates_response
;
336 alt_req
.walker
= walker
;
337 slot
->callback_data
= &alt_req
;
339 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
340 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
341 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
.buf
);
345 alt_req
.buffer
= &buffer
;
346 alt_req
.http_specific
= 1;
349 if (start_active_slot(slot
))
350 run_active_slot(slot
);
352 cdata
->got_alternates
= -1;
354 strbuf_release(&buffer
);
355 strbuf_release(&url
);
358 static int fetch_indices(struct walker
*walker
, struct alt_base
*repo
)
362 if (repo
->got_indices
)
365 if (walker
->get_verbosely
)
366 fprintf(stderr
, "Getting pack list for %s\n", repo
->base
);
368 switch (http_get_info_packs(repo
->base
, &repo
->packs
)) {
370 case HTTP_MISSING_TARGET
:
371 repo
->got_indices
= 1;
375 repo
->got_indices
= 0;
382 static int http_fetch_pack(struct walker
*walker
, struct alt_base
*repo
, unsigned char *sha1
)
384 struct packed_git
*target
;
386 struct slot_results results
;
387 struct http_pack_request
*preq
;
389 if (fetch_indices(walker
, repo
))
391 target
= find_sha1_pack(sha1
, repo
->packs
);
395 if (walker
->get_verbosely
) {
396 fprintf(stderr
, "Getting pack %s\n",
397 sha1_to_hex(target
->sha1
));
398 fprintf(stderr
, " which contains %s\n",
402 preq
= new_http_pack_request(target
, repo
->base
);
405 preq
->lst
= &repo
->packs
;
406 preq
->slot
->results
= &results
;
408 if (start_active_slot(preq
->slot
)) {
409 run_active_slot(preq
->slot
);
410 if (results
.curl_result
!= CURLE_OK
) {
411 error("Unable to get pack file %s\n%s", preq
->url
,
416 error("Unable to start request");
420 ret
= finish_http_pack_request(preq
);
421 release_http_pack_request(preq
);
431 static void abort_object_request(struct object_request
*obj_req
)
433 release_object_request(obj_req
);
436 static int fetch_object(struct walker
*walker
, unsigned char *sha1
)
438 char *hex
= sha1_to_hex(sha1
);
440 struct object_request
*obj_req
= NULL
;
441 struct http_object_request
*req
;
442 struct list_head
*pos
, *head
= &object_queue_head
;
444 list_for_each(pos
, head
) {
445 obj_req
= list_entry(pos
, struct object_request
, node
);
446 if (!hashcmp(obj_req
->sha1
, sha1
))
450 return error("Couldn't find request for %s in the queue", hex
);
452 if (has_sha1_file(obj_req
->sha1
)) {
453 if (obj_req
->req
!= NULL
)
454 abort_http_object_request(obj_req
->req
);
455 abort_object_request(obj_req
);
459 #ifdef USE_CURL_MULTI
460 while (obj_req
->state
== WAITING
)
463 start_object_request(walker
, obj_req
);
467 * obj_req->req might change when fetching alternates in the callback
468 * process_object_response; therefore, the "shortcut" variable, req,
469 * is used only after we're done with slots.
471 while (obj_req
->state
== ACTIVE
)
472 run_active_slot(obj_req
->req
->slot
);
476 if (req
->localfile
!= -1) {
477 close(req
->localfile
);
482 * we turned off CURLOPT_FAILONERROR to avoid losing a
483 * persistent connection and got CURLE_OK.
485 if (req
->http_code
>= 300 && req
->curl_result
== CURLE_OK
&&
486 (starts_with(req
->url
, "http://") ||
487 starts_with(req
->url
, "https://"))) {
488 req
->curl_result
= CURLE_HTTP_RETURNED_ERROR
;
489 xsnprintf(req
->errorstr
, sizeof(req
->errorstr
),
490 "HTTP request failed");
493 if (obj_req
->state
== ABORTED
) {
494 ret
= error("Request for %s aborted", hex
);
495 } else if (req
->curl_result
!= CURLE_OK
&&
496 req
->http_code
!= 416) {
497 if (missing_target(req
))
498 ret
= -1; /* Be silent, it is probably in a pack. */
500 ret
= error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
501 req
->errorstr
, req
->curl_result
,
502 req
->http_code
, hex
);
503 } else if (req
->zret
!= Z_STREAM_END
) {
504 walker
->corrupt_object_found
++;
505 ret
= error("File %s (%s) corrupt", hex
, req
->url
);
506 } else if (hashcmp(obj_req
->sha1
, req
->real_sha1
)) {
507 ret
= error("File %s has bad hash", hex
);
508 } else if (req
->rename
< 0) {
509 ret
= error("unable to write sha1 filename %s",
510 sha1_file_name(req
->sha1
));
513 release_http_object_request(req
);
514 release_object_request(obj_req
);
518 static int fetch(struct walker
*walker
, unsigned char *sha1
)
520 struct walker_data
*data
= walker
->data
;
521 struct alt_base
*altbase
= data
->alt
;
523 if (!fetch_object(walker
, sha1
))
526 if (!http_fetch_pack(walker
, altbase
, sha1
))
528 fetch_alternates(walker
, data
->alt
->base
);
529 altbase
= altbase
->next
;
531 return error("Unable to find %s under %s", sha1_to_hex(sha1
),
535 static int fetch_ref(struct walker
*walker
, struct ref
*ref
)
537 struct walker_data
*data
= walker
->data
;
538 return http_fetch_ref(data
->alt
->base
, ref
);
541 static void cleanup(struct walker
*walker
)
543 struct walker_data
*data
= walker
->data
;
544 struct alt_base
*alt
, *alt_next
;
549 alt_next
= alt
->next
;
561 struct walker
*get_http_walker(const char *url
)
564 struct walker_data
*data
= xmalloc(sizeof(struct walker_data
));
565 struct walker
*walker
= xmalloc(sizeof(struct walker
));
567 data
->alt
= xmalloc(sizeof(*data
->alt
));
568 data
->alt
->base
= xstrdup(url
);
569 for (s
= data
->alt
->base
+ strlen(data
->alt
->base
) - 1; *s
== '/'; --s
)
572 data
->alt
->got_indices
= 0;
573 data
->alt
->packs
= NULL
;
574 data
->alt
->next
= NULL
;
575 data
->got_alternates
= -1;
577 walker
->corrupt_object_found
= 0;
578 walker
->fetch
= fetch
;
579 walker
->fetch_ref
= fetch_ref
;
580 walker
->prefetch
= prefetch
;
581 walker
->cleanup
= cleanup
;
584 #ifdef USE_CURL_MULTI
585 add_fill_function(walker
, (int (*)(void *)) fill_active_slot
);