9 #if LIBCURL_VERSION_NUM >= 0x070908
10 #define USE_CURL_MULTI
11 #define DEFAULT_MAX_REQUESTS 5
14 #if LIBCURL_VERSION_NUM < 0x070704
15 #define curl_global_cleanup() do { /* nothing */ } while(0)
17 #if LIBCURL_VERSION_NUM < 0x070800
18 #define curl_global_init(a) do { /* nothing */ } while(0)
21 #if LIBCURL_VERSION_NUM < 0x070c04
22 #define NO_CURL_EASY_DUPHANDLE
25 #define PREV_BUF_SIZE 4096
26 #define RANGE_HEADER_SIZE 30
28 static int got_alternates
= 0;
29 static int active_requests
= 0;
30 static int data_received
;
33 static int max_requests
= -1;
36 #ifndef NO_CURL_EASY_DUPHANDLE
37 static CURL
*curl_default
;
39 static struct curl_slist
*pragma_header
;
40 static struct curl_slist
*no_pragma_header
;
41 static struct curl_slist
*no_range_header
;
42 static char curl_errorstr
[CURL_ERROR_SIZE
];
48 struct packed_git
*packs
;
49 struct alt_base
*next
;
52 static struct alt_base
*alt
= NULL
;
61 struct transfer_request
63 unsigned char sha1
[20];
64 struct alt_base
*repo
;
66 char filename
[PATH_MAX
];
67 char tmpfile
[PATH_MAX
];
69 enum transfer_state state
;
71 char errorstr
[CURL_ERROR_SIZE
];
73 unsigned char real_sha1
[20];
78 struct active_request_slot
*slot
;
79 struct transfer_request
*next
;
82 struct active_request_slot
90 struct active_request_slot
*next
;
93 static struct transfer_request
*request_queue_head
= NULL
;
94 static struct active_request_slot
*active_queue_head
= NULL
;
96 static int curl_ssl_verify
= -1;
97 static char *ssl_cert
= NULL
;
98 #if LIBCURL_VERSION_NUM >= 0x070902
99 static char *ssl_key
= NULL
;
101 #if LIBCURL_VERSION_NUM >= 0x070908
102 static char *ssl_capath
= NULL
;
104 static char *ssl_cainfo
= NULL
;
105 static long curl_low_speed_limit
= -1;
106 static long curl_low_speed_time
= -1;
115 static int http_options(const char *var
, const char *value
)
117 if (!strcmp("http.sslverify", var
)) {
118 if (curl_ssl_verify
== -1) {
119 curl_ssl_verify
= git_config_bool(var
, value
);
124 if (!strcmp("http.sslcert", var
)) {
125 if (ssl_cert
== NULL
) {
126 ssl_cert
= xmalloc(strlen(value
)+1);
127 strcpy(ssl_cert
, value
);
131 #if LIBCURL_VERSION_NUM >= 0x070902
132 if (!strcmp("http.sslkey", var
)) {
133 if (ssl_key
== NULL
) {
134 ssl_key
= xmalloc(strlen(value
)+1);
135 strcpy(ssl_key
, value
);
140 #if LIBCURL_VERSION_NUM >= 0x070908
141 if (!strcmp("http.sslcapath", var
)) {
142 if (ssl_capath
== NULL
) {
143 ssl_capath
= xmalloc(strlen(value
)+1);
144 strcpy(ssl_capath
, value
);
149 if (!strcmp("http.sslcainfo", var
)) {
150 if (ssl_cainfo
== NULL
) {
151 ssl_cainfo
= xmalloc(strlen(value
)+1);
152 strcpy(ssl_cainfo
, value
);
157 #ifdef USE_CURL_MULTI
158 if (!strcmp("http.maxrequests", var
)) {
159 if (max_requests
== -1)
160 max_requests
= git_config_int(var
, value
);
165 if (!strcmp("http.lowspeedlimit", var
)) {
166 if (curl_low_speed_limit
== -1)
167 curl_low_speed_limit
= (long)git_config_int(var
, value
);
170 if (!strcmp("http.lowspeedtime", var
)) {
171 if (curl_low_speed_time
== -1)
172 curl_low_speed_time
= (long)git_config_int(var
, value
);
176 /* Fall back on the default ones */
177 return git_default_config(var
, value
);
180 static size_t fwrite_buffer(void *ptr
, size_t eltsize
, size_t nmemb
,
181 struct buffer
*buffer
)
183 size_t size
= eltsize
* nmemb
;
184 if (size
> buffer
->size
- buffer
->posn
)
185 size
= buffer
->size
- buffer
->posn
;
186 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
187 buffer
->posn
+= size
;
192 static size_t fwrite_buffer_dynamic(const void *ptr
, size_t eltsize
,
193 size_t nmemb
, struct buffer
*buffer
)
195 size_t size
= eltsize
* nmemb
;
196 if (size
> buffer
->size
- buffer
->posn
) {
197 buffer
->size
= buffer
->size
* 3 / 2;
198 if (buffer
->size
< buffer
->posn
+ size
)
199 buffer
->size
= buffer
->posn
+ size
;
200 buffer
->buffer
= xrealloc(buffer
->buffer
, buffer
->size
);
202 memcpy(buffer
->buffer
+ buffer
->posn
, ptr
, size
);
203 buffer
->posn
+= size
;
208 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
211 unsigned char expn
[4096];
212 size_t size
= eltsize
* nmemb
;
214 struct transfer_request
*request
= (struct transfer_request
*)data
;
216 ssize_t retval
= write(request
->local
,
217 ptr
+ posn
, size
- posn
);
221 } while (posn
< size
);
223 request
->stream
.avail_in
= size
;
224 request
->stream
.next_in
= ptr
;
226 request
->stream
.next_out
= expn
;
227 request
->stream
.avail_out
= sizeof(expn
);
228 request
->zret
= inflate(&request
->stream
, Z_SYNC_FLUSH
);
229 SHA1_Update(&request
->c
, expn
,
230 sizeof(expn
) - request
->stream
.avail_out
);
231 } while (request
->stream
.avail_in
&& request
->zret
== Z_OK
);
236 #ifdef USE_CURL_MULTI
237 static void process_curl_messages(void);
238 static void process_request_queue(void);
240 static int fetch_alternates(char *base
);
242 static CURL
* get_curl_handle(void)
244 CURL
* result
= curl_easy_init();
246 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, curl_ssl_verify
);
247 #if LIBCURL_VERSION_NUM >= 0x070907
248 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
251 if (ssl_cert
!= NULL
)
252 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
253 #if LIBCURL_VERSION_NUM >= 0x070902
255 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
257 #if LIBCURL_VERSION_NUM >= 0x070908
258 if (ssl_capath
!= NULL
)
259 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
261 if (ssl_cainfo
!= NULL
)
262 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
263 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
265 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
266 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
267 curl_low_speed_limit
);
268 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
269 curl_low_speed_time
);
275 static struct active_request_slot
*get_active_slot(void)
277 struct active_request_slot
*slot
= active_queue_head
;
278 struct active_request_slot
*newslot
;
280 #ifdef USE_CURL_MULTI
283 /* Wait for a slot to open up if the queue is full */
284 while (active_requests
>= max_requests
) {
285 curl_multi_perform(curlm
, &num_transfers
);
286 if (num_transfers
< active_requests
) {
287 process_curl_messages();
292 while (slot
!= NULL
&& slot
->in_use
) {
296 newslot
= xmalloc(sizeof(*newslot
));
297 newslot
->curl
= NULL
;
299 newslot
->next
= NULL
;
301 slot
= active_queue_head
;
303 active_queue_head
= newslot
;
305 while (slot
->next
!= NULL
) {
308 slot
->next
= newslot
;
313 if (slot
->curl
== NULL
) {
314 #ifdef NO_CURL_EASY_DUPHANDLE
315 slot
->curl
= get_curl_handle();
317 slot
->curl
= curl_easy_duphandle(curl_default
);
325 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
326 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_range_header
);
327 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
332 static int start_active_slot(struct active_request_slot
*slot
)
334 #ifdef USE_CURL_MULTI
335 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
337 if (curlm_result
!= CURLM_OK
&&
338 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
347 static void run_active_slot(struct active_request_slot
*slot
)
349 #ifdef USE_CURL_MULTI
357 struct timeval select_timeout
;
358 CURLMcode curlm_result
;
360 while (!slot
->done
) {
363 curlm_result
= curl_multi_perform(curlm
,
365 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
366 if (num_transfers
< active_requests
) {
367 process_curl_messages();
368 process_request_queue();
371 if (!data_received
&& slot
->local
!= NULL
) {
372 current_pos
= ftell(slot
->local
);
373 if (current_pos
> last_pos
)
375 last_pos
= current_pos
;
378 if (!slot
->done
&& !data_received
) {
383 select_timeout
.tv_sec
= 0;
384 select_timeout
.tv_usec
= 50000;
385 select(max_fd
, &readfds
, &writefds
,
386 &excfds
, &select_timeout
);
390 slot
->curl_result
= curl_easy_perform(slot
->curl
);
395 static void start_request(struct transfer_request
*request
)
397 char *hex
= sha1_to_hex(request
->sha1
);
398 char prevfile
[PATH_MAX
];
402 unsigned char prev_buf
[PREV_BUF_SIZE
];
403 ssize_t prev_read
= 0;
405 char range
[RANGE_HEADER_SIZE
];
406 struct curl_slist
*range_header
= NULL
;
407 struct active_request_slot
*slot
;
409 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", request
->filename
);
411 rename(request
->tmpfile
, prevfile
);
412 unlink(request
->tmpfile
);
414 request
->local
= open(request
->tmpfile
,
415 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
416 /* This could have failed due to the "lazy directory creation";
417 * try to mkdir the last path component.
419 if (request
->local
< 0 && errno
== ENOENT
) {
420 char *dir
= strrchr(request
->tmpfile
, '/');
423 mkdir(request
->tmpfile
, 0777);
426 request
->local
= open(request
->tmpfile
,
427 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
430 if (request
->local
< 0) {
431 request
->state
= ABORTED
;
432 error("Couldn't create temporary file %s for %s: %s\n",
433 request
->tmpfile
, request
->filename
, strerror(errno
));
437 memset(&request
->stream
, 0, sizeof(request
->stream
));
439 inflateInit(&request
->stream
);
441 SHA1_Init(&request
->c
);
443 url
= xmalloc(strlen(request
->repo
->base
) + 50);
444 request
->url
= xmalloc(strlen(request
->repo
->base
) + 50);
445 strcpy(url
, request
->repo
->base
);
446 posn
= url
+ strlen(request
->repo
->base
);
447 strcpy(posn
, "objects/");
449 memcpy(posn
, hex
, 2);
452 strcpy(posn
, hex
+ 2);
453 strcpy(request
->url
, url
);
455 /* If a previous temp file is present, process what was already
457 prevlocal
= open(prevfile
, O_RDONLY
);
458 if (prevlocal
!= -1) {
460 prev_read
= read(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
462 if (fwrite_sha1_file(prev_buf
,
465 request
) == prev_read
) {
466 prev_posn
+= prev_read
;
471 } while (prev_read
> 0);
476 /* Reset inflate/SHA1 if there was an error reading the previous temp
477 file; also rewind to the beginning of the local file. */
478 if (prev_read
== -1) {
479 memset(&request
->stream
, 0, sizeof(request
->stream
));
480 inflateInit(&request
->stream
);
481 SHA1_Init(&request
->c
);
484 lseek(request
->local
, SEEK_SET
, 0);
485 ftruncate(request
->local
, 0);
489 slot
= get_active_slot();
490 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, request
);
491 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
492 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, request
->errorstr
);
493 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
494 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
496 /* If we have successfully processed data from a previous fetch
497 attempt, only fetch the data we don't already have. */
501 "Resuming fetch of object %s at byte %ld\n",
503 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
504 range_header
= curl_slist_append(range_header
, range
);
505 curl_easy_setopt(slot
->curl
,
506 CURLOPT_HTTPHEADER
, range_header
);
509 /* Try to get the request started, abort the request on error */
510 if (!start_active_slot(slot
)) {
511 request
->state
= ABORTED
;
512 close(request
->local
);
517 request
->slot
= slot
;
518 request
->state
= ACTIVE
;
521 static void finish_request(struct transfer_request
*request
)
525 fchmod(request
->local
, 0444);
526 close(request
->local
);
528 if (request
->http_code
== 416) {
529 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
530 } else if (request
->curl_result
!= CURLE_OK
) {
531 if (stat(request
->tmpfile
, &st
) == 0)
533 unlink(request
->tmpfile
);
537 inflateEnd(&request
->stream
);
538 SHA1_Final(request
->real_sha1
, &request
->c
);
539 if (request
->zret
!= Z_STREAM_END
) {
540 unlink(request
->tmpfile
);
543 if (memcmp(request
->sha1
, request
->real_sha1
, 20)) {
544 unlink(request
->tmpfile
);
548 move_temp_to_file(request
->tmpfile
, request
->filename
);
550 if (request
->rename
== 0)
551 pull_say("got %s\n", sha1_to_hex(request
->sha1
));
554 static void release_request(struct transfer_request
*request
)
556 struct transfer_request
*entry
= request_queue_head
;
558 if (request
== request_queue_head
) {
559 request_queue_head
= request
->next
;
561 while (entry
->next
!= NULL
&& entry
->next
!= request
)
563 if (entry
->next
== request
)
564 entry
->next
= entry
->next
->next
;
571 #ifdef USE_CURL_MULTI
572 static void process_curl_messages(void)
575 struct active_request_slot
*slot
;
576 struct transfer_request
*request
= NULL
;
577 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
579 while (curl_message
!= NULL
) {
580 if (curl_message
->msg
== CURLMSG_DONE
) {
581 int curl_result
= curl_message
->data
.result
;
582 slot
= active_queue_head
;
583 while (slot
!= NULL
&&
584 slot
->curl
!= curl_message
->easy_handle
)
587 curl_multi_remove_handle(curlm
, slot
->curl
);
591 slot
->curl_result
= curl_result
;
592 curl_easy_getinfo(slot
->curl
,
595 request
= request_queue_head
;
596 while (request
!= NULL
&&
597 request
->slot
!= slot
)
598 request
= request
->next
;
600 fprintf(stderr
, "Received DONE message for unknown request!\n");
602 if (request
!= NULL
) {
603 request
->curl_result
= curl_result
;
604 request
->http_code
= slot
->http_code
;
605 request
->slot
= NULL
;
606 request
->state
= COMPLETE
;
608 /* Use alternates if necessary */
609 if (request
->http_code
== 404) {
610 fetch_alternates(alt
->base
);
611 if (request
->repo
->next
!= NULL
) {
614 start_request(request
);
617 finish_request(request
);
621 fprintf(stderr
, "Unknown CURL message received: %d\n",
622 (int)curl_message
->msg
);
624 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
628 static void process_request_queue(void)
630 struct transfer_request
*request
= request_queue_head
;
631 struct active_request_slot
*slot
= active_queue_head
;
634 while (active_requests
< max_requests
&& request
!= NULL
) {
635 if (request
->state
== WAITING
) {
636 if (has_sha1_file(request
->sha1
))
637 release_request(request
);
639 start_request(request
);
640 curl_multi_perform(curlm
, &num_transfers
);
642 request
= request
->next
;
645 while (slot
!= NULL
) {
646 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
647 curl_easy_cleanup(slot
->curl
);
655 void prefetch(unsigned char *sha1
)
657 struct transfer_request
*newreq
;
658 struct transfer_request
*tail
;
659 char *filename
= sha1_file_name(sha1
);
661 newreq
= xmalloc(sizeof(*newreq
));
662 memcpy(newreq
->sha1
, sha1
, 20);
666 newreq
->state
= WAITING
;
667 snprintf(newreq
->filename
, sizeof(newreq
->filename
), "%s", filename
);
668 snprintf(newreq
->tmpfile
, sizeof(newreq
->tmpfile
),
669 "%s.temp", filename
);
672 if (request_queue_head
== NULL
) {
673 request_queue_head
= newreq
;
675 tail
= request_queue_head
;
676 while (tail
->next
!= NULL
) {
681 #ifdef USE_CURL_MULTI
682 process_request_queue();
683 process_curl_messages();
687 static int fetch_index(struct alt_base
*repo
, unsigned char *sha1
)
689 char *hex
= sha1_to_hex(sha1
);
692 char tmpfile
[PATH_MAX
];
694 char range
[RANGE_HEADER_SIZE
];
695 struct curl_slist
*range_header
= NULL
;
698 struct active_request_slot
*slot
;
700 if (has_pack_index(sha1
))
704 fprintf(stderr
, "Getting index for pack %s\n", hex
);
706 url
= xmalloc(strlen(repo
->base
) + 64);
707 sprintf(url
, "%s/objects/pack/pack-%s.idx", repo
->base
, hex
);
709 filename
= sha1_pack_index_name(sha1
);
710 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
711 indexfile
= fopen(tmpfile
, "a");
713 return error("Unable to open local file %s for pack index",
716 slot
= get_active_slot();
717 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
718 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
719 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
720 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
721 slot
->local
= indexfile
;
723 /* If there is data present from a previous transfer attempt,
724 resume where it left off */
725 prev_posn
= ftell(indexfile
);
729 "Resuming fetch of index for pack %s at byte %ld\n",
731 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
732 range_header
= curl_slist_append(range_header
, range
);
733 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
736 if (start_active_slot(slot
)) {
737 run_active_slot(slot
);
738 if (slot
->curl_result
!= CURLE_OK
) {
740 return error("Unable to get pack index %s\n%s", url
,
744 return error("Unable to start request");
749 return move_temp_to_file(tmpfile
, filename
);
752 static int setup_index(struct alt_base
*repo
, unsigned char *sha1
)
754 struct packed_git
*new_pack
;
755 if (has_pack_file(sha1
))
756 return 0; // don't list this as something we can get
758 if (fetch_index(repo
, sha1
))
761 new_pack
= parse_pack_index(sha1
);
762 new_pack
->next
= repo
->packs
;
763 repo
->packs
= new_pack
;
767 static int fetch_alternates(char *base
)
770 struct buffer buffer
;
774 int http_specific
= 1;
775 struct alt_base
*tail
= alt
;
776 static const char null_byte
= '\0';
778 struct active_request_slot
*slot
;
783 data
= xmalloc(4096);
786 buffer
.buffer
= data
;
789 fprintf(stderr
, "Getting alternates list\n");
791 url
= xmalloc(strlen(base
) + 31);
792 sprintf(url
, "%s/objects/info/http-alternates", base
);
794 slot
= get_active_slot();
795 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
796 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
797 fwrite_buffer_dynamic
);
798 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
799 if (start_active_slot(slot
)) {
800 run_active_slot(slot
);
801 if (slot
->curl_result
!= CURLE_OK
|| !buffer
.posn
) {
804 sprintf(url
, "%s/objects/info/alternates", base
);
806 slot
= get_active_slot();
807 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
808 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
809 fwrite_buffer_dynamic
);
810 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
811 if (start_active_slot(slot
)) {
812 run_active_slot(slot
);
813 if (slot
->curl_result
!= CURLE_OK
) {
815 if (slot
->http_code
== 404)
826 fwrite_buffer_dynamic(&null_byte
, 1, 1, &buffer
);
828 data
= buffer
.buffer
;
830 while (i
< buffer
.posn
) {
832 while (posn
< buffer
.posn
&& data
[posn
] != '\n')
834 if (data
[posn
] == '\n') {
837 struct alt_base
*newalt
;
839 if (data
[i
] == '/') {
840 serverlen
= strchr(base
+ 8, '/') - base
;
842 } else if (!memcmp(data
+ i
, "../", 3)) {
844 serverlen
= strlen(base
);
845 while (i
+ 2 < posn
&&
846 !memcmp(data
+ i
, "../", 3)) {
849 } while (serverlen
&&
850 base
[serverlen
- 1] != '/');
853 // If the server got removed, give up.
854 okay
= strchr(base
, ':') - base
+ 3 <
856 } else if (http_specific
) {
857 char *colon
= strchr(data
+ i
, ':');
858 char *slash
= strchr(data
+ i
, '/');
859 if (colon
&& slash
&& colon
< data
+ posn
&&
860 slash
< data
+ posn
&& colon
< slash
) {
864 // skip 'objects' at end
866 target
= xmalloc(serverlen
+ posn
- i
- 6);
867 strncpy(target
, base
, serverlen
);
868 strncpy(target
+ serverlen
, data
+ i
,
870 target
[serverlen
+ posn
- i
- 7] = '\0';
873 "Also look at %s\n", target
);
874 newalt
= xmalloc(sizeof(*newalt
));
876 newalt
->base
= target
;
877 newalt
->got_indices
= 0;
878 newalt
->packs
= NULL
;
879 while (tail
->next
!= NULL
)
893 static int fetch_indices(struct alt_base
*repo
)
895 unsigned char sha1
[20];
897 struct buffer buffer
;
901 struct active_request_slot
*slot
;
903 if (repo
->got_indices
)
906 data
= xmalloc(4096);
909 buffer
.buffer
= data
;
912 fprintf(stderr
, "Getting pack list\n");
914 url
= xmalloc(strlen(repo
->base
) + 21);
915 sprintf(url
, "%s/objects/info/packs", repo
->base
);
917 slot
= get_active_slot();
918 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
919 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
920 fwrite_buffer_dynamic
);
921 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
922 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
923 if (start_active_slot(slot
)) {
924 run_active_slot(slot
);
925 if (slot
->curl_result
!= CURLE_OK
) {
927 return error("%s", curl_errorstr
);
931 return error("Unable to start request");
934 data
= buffer
.buffer
;
935 while (i
< buffer
.posn
) {
939 if (i
+ 52 < buffer
.posn
&&
940 !strncmp(data
+ i
, " pack-", 6) &&
941 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
942 get_sha1_hex(data
+ i
+ 6, sha1
);
943 setup_index(repo
, sha1
);
948 while (data
[i
] != '\n')
955 repo
->got_indices
= 1;
959 static int fetch_pack(struct alt_base
*repo
, unsigned char *sha1
)
962 struct packed_git
*target
;
963 struct packed_git
**lst
;
966 char tmpfile
[PATH_MAX
];
969 char range
[RANGE_HEADER_SIZE
];
970 struct curl_slist
*range_header
= NULL
;
972 struct active_request_slot
*slot
;
974 if (fetch_indices(repo
))
976 target
= find_sha1_pack(sha1
, repo
->packs
);
981 fprintf(stderr
, "Getting pack %s\n",
982 sha1_to_hex(target
->sha1
));
983 fprintf(stderr
, " which contains %s\n",
987 url
= xmalloc(strlen(repo
->base
) + 65);
988 sprintf(url
, "%s/objects/pack/pack-%s.pack",
989 repo
->base
, sha1_to_hex(target
->sha1
));
991 filename
= sha1_pack_name(target
->sha1
);
992 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
993 packfile
= fopen(tmpfile
, "a");
995 return error("Unable to open local file %s for pack",
998 slot
= get_active_slot();
999 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
1000 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1001 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1002 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
1003 slot
->local
= packfile
;
1005 /* If there is data present from a previous transfer attempt,
1006 resume where it left off */
1007 prev_posn
= ftell(packfile
);
1011 "Resuming fetch of pack %s at byte %ld\n",
1012 sha1_to_hex(target
->sha1
), prev_posn
);
1013 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1014 range_header
= curl_slist_append(range_header
, range
);
1015 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
1018 if (start_active_slot(slot
)) {
1019 run_active_slot(slot
);
1020 if (slot
->curl_result
!= CURLE_OK
) {
1022 return error("Unable to get pack file %s\n%s", url
,
1026 return error("Unable to start request");
1031 ret
= move_temp_to_file(tmpfile
, filename
);
1036 while (*lst
!= target
)
1037 lst
= &((*lst
)->next
);
1038 *lst
= (*lst
)->next
;
1040 if (verify_pack(target
, 0))
1042 install_packed_git(target
);
1047 static int fetch_object(struct alt_base
*repo
, unsigned char *sha1
)
1049 char *hex
= sha1_to_hex(sha1
);
1051 struct transfer_request
*request
= request_queue_head
;
1053 while (request
!= NULL
&& memcmp(request
->sha1
, sha1
, 20))
1054 request
= request
->next
;
1055 if (request
== NULL
)
1056 return error("Couldn't find request for %s in the queue", hex
);
1058 if (has_sha1_file(request
->sha1
)) {
1059 release_request(request
);
1063 #ifdef USE_CURL_MULTI
1064 while (request
->state
== WAITING
) {
1066 curl_multi_perform(curlm
, &num_transfers
);
1067 if (num_transfers
< active_requests
) {
1068 process_curl_messages();
1069 process_request_queue();
1073 start_request(request
);
1076 while (request
->state
== ACTIVE
) {
1077 run_active_slot(request
->slot
);
1078 #ifndef USE_CURL_MULTI
1079 request
->curl_result
= request
->slot
->curl_result
;
1080 request
->http_code
= request
->slot
->http_code
;
1081 request
->slot
= NULL
;
1083 /* Use alternates if necessary */
1084 if (request
->http_code
== 404) {
1085 fetch_alternates(alt
->base
);
1086 if (request
->repo
->next
!= NULL
) {
1087 request
->repo
= request
->repo
->next
;
1088 start_request(request
);
1091 finish_request(request
);
1092 request
->state
= COMPLETE
;
1097 if (request
->state
== ABORTED
) {
1098 release_request(request
);
1099 return error("Request for %s aborted", hex
);
1102 if (request
->curl_result
!= CURLE_OK
&& request
->http_code
!= 416) {
1103 if (request
->http_code
== 404)
1104 ret
= -1; /* Be silent, it is probably in a pack. */
1106 ret
= error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
1107 request
->errorstr
, request
->curl_result
,
1108 request
->http_code
, hex
);
1109 release_request(request
);
1113 if (request
->zret
!= Z_STREAM_END
) {
1114 ret
= error("File %s (%s) corrupt\n", hex
, request
->url
);
1115 release_request(request
);
1119 if (memcmp(request
->sha1
, request
->real_sha1
, 20)) {
1120 release_request(request
);
1121 return error("File %s has bad hash\n", hex
);
1124 if (request
->rename
< 0) {
1125 ret
= error("unable to write sha1 filename %s: %s",
1127 strerror(request
->rename
));
1128 release_request(request
);
1132 release_request(request
);
1136 int fetch(unsigned char *sha1
)
1138 struct alt_base
*altbase
= alt
;
1140 if (!fetch_object(altbase
, sha1
))
1143 if (!fetch_pack(altbase
, sha1
))
1145 fetch_alternates(alt
->base
);
1146 altbase
= altbase
->next
;
1148 return error("Unable to find %s under %s\n", sha1_to_hex(sha1
),
1152 static inline int needs_quote(int ch
)
1155 case '/': case '-': case '.':
1156 case 'A'...'Z': case 'a'...'z': case '0'...'9':
1163 static inline int hex(int v
)
1165 if (v
< 10) return '0' + v
;
1166 else return 'A' + v
- 10;
1169 static char *quote_ref_url(const char *base
, const char *ref
)
1173 int len
, baselen
, ch
;
1175 baselen
= strlen(base
);
1176 len
= baselen
+ 6; /* "refs/" + NUL */
1177 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
1178 if (needs_quote(ch
))
1179 len
+= 2; /* extra two hex plus replacement % */
1180 qref
= xmalloc(len
);
1181 memcpy(qref
, base
, baselen
);
1182 memcpy(qref
+ baselen
, "refs/", 5);
1183 for (cp
= ref
, dp
= qref
+ baselen
+ 5; (ch
= *cp
) != 0; cp
++) {
1184 if (needs_quote(ch
)) {
1186 *dp
++ = hex((ch
>> 4) & 0xF);
1187 *dp
++ = hex(ch
& 0xF);
1197 int fetch_ref(char *ref
, unsigned char *sha1
)
1201 struct buffer buffer
;
1202 char *base
= alt
->base
;
1203 struct active_request_slot
*slot
;
1206 buffer
.buffer
= hex
;
1209 url
= quote_ref_url(base
, ref
);
1210 slot
= get_active_slot();
1211 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1212 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1213 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1214 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1215 if (start_active_slot(slot
)) {
1216 run_active_slot(slot
);
1217 if (slot
->curl_result
!= CURLE_OK
)
1218 return error("Couldn't get %s for %s\n%s",
1219 url
, ref
, curl_errorstr
);
1221 return error("Unable to start request");
1225 get_sha1_hex(hex
, sha1
);
1229 int main(int argc
, char **argv
)
1234 struct active_request_slot
*slot
;
1235 char *low_speed_limit
;
1236 char *low_speed_time
;
1240 while (arg
< argc
&& argv
[arg
][0] == '-') {
1241 if (argv
[arg
][1] == 't') {
1243 } else if (argv
[arg
][1] == 'c') {
1245 } else if (argv
[arg
][1] == 'a') {
1249 } else if (argv
[arg
][1] == 'v') {
1251 } else if (argv
[arg
][1] == 'w') {
1252 write_ref
= argv
[arg
+ 1];
1254 } else if (!strcmp(argv
[arg
], "--recover")) {
1259 if (argc
< arg
+ 2) {
1260 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1263 commit_id
= argv
[arg
];
1264 url
= argv
[arg
+ 1];
1266 curl_global_init(CURL_GLOBAL_ALL
);
1268 #ifdef USE_CURL_MULTI
1270 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1271 if (http_max_requests
!= NULL
)
1272 max_requests
= atoi(http_max_requests
);
1275 curlm
= curl_multi_init();
1276 if (curlm
== NULL
) {
1277 fprintf(stderr
, "Error creating curl multi handle.\n");
1282 if (getenv("GIT_SSL_NO_VERIFY"))
1283 curl_ssl_verify
= 0;
1285 ssl_cert
= getenv("GIT_SSL_CERT");
1286 #if LIBCURL_VERSION_NUM >= 0x070902
1287 ssl_key
= getenv("GIT_SSL_KEY");
1289 #if LIBCURL_VERSION_NUM >= 0x070908
1290 ssl_capath
= getenv("GIT_SSL_CAPATH");
1292 ssl_cainfo
= getenv("GIT_SSL_CAINFO");
1294 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1295 if (low_speed_limit
!= NULL
)
1296 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1297 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1298 if (low_speed_time
!= NULL
)
1299 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1301 git_config(http_options
);
1303 if (curl_ssl_verify
== -1)
1304 curl_ssl_verify
= 1;
1306 #ifdef USE_CURL_MULTI
1307 if (max_requests
< 1)
1308 max_requests
= DEFAULT_MAX_REQUESTS
;
1311 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
1312 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
1313 no_range_header
= curl_slist_append(no_range_header
, "Range:");
1315 #ifndef NO_CURL_EASY_DUPHANDLE
1316 curl_default
= get_curl_handle();
1319 alt
= xmalloc(sizeof(*alt
));
1321 alt
->got_indices
= 0;
1325 if (pull(commit_id
))
1328 curl_slist_free_all(pragma_header
);
1329 curl_slist_free_all(no_pragma_header
);
1330 curl_slist_free_all(no_range_header
);
1331 #ifndef NO_CURL_EASY_DUPHANDLE
1332 curl_easy_cleanup(curl_default
);
1334 slot
= active_queue_head
;
1335 while (slot
!= NULL
) {
1337 if (get_verbosely
) {
1338 curl_easy_getinfo(slot
->curl
,
1339 CURLINFO_EFFECTIVE_URL
,
1341 fprintf(stderr
, "Waiting for %s\n", wait_url
);
1343 run_active_slot(slot
);
1345 if (slot
->curl
!= NULL
)
1346 curl_easy_cleanup(slot
->curl
);
1349 #ifdef USE_CURL_MULTI
1350 curl_multi_cleanup(curlm
);
1352 curl_global_cleanup();