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
)
523 fchmod(request
->local
, 0444);
524 close(request
->local
);
526 if (request
->http_code
== 416) {
527 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
528 } else if (request
->curl_result
!= CURLE_OK
) {
532 inflateEnd(&request
->stream
);
533 SHA1_Final(request
->real_sha1
, &request
->c
);
534 if (request
->zret
!= Z_STREAM_END
) {
535 unlink(request
->tmpfile
);
538 if (memcmp(request
->sha1
, request
->real_sha1
, 20)) {
539 unlink(request
->tmpfile
);
543 move_temp_to_file(request
->tmpfile
, request
->filename
);
545 if (request
->rename
== 0)
546 pull_say("got %s\n", sha1_to_hex(request
->sha1
));
549 static void release_request(struct transfer_request
*request
)
551 struct transfer_request
*entry
= request_queue_head
;
553 if (request
== request_queue_head
) {
554 request_queue_head
= request
->next
;
556 while (entry
->next
!= NULL
&& entry
->next
!= request
)
558 if (entry
->next
== request
)
559 entry
->next
= entry
->next
->next
;
566 #ifdef USE_CURL_MULTI
567 void process_curl_messages(void)
570 struct active_request_slot
*slot
;
571 struct transfer_request
*request
= NULL
;
572 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
574 while (curl_message
!= NULL
) {
575 if (curl_message
->msg
== CURLMSG_DONE
) {
576 slot
= active_queue_head
;
577 while (slot
!= NULL
&&
578 slot
->curl
!= curl_message
->easy_handle
)
581 curl_multi_remove_handle(curlm
, slot
->curl
);
585 slot
->curl_result
= curl_message
->data
.result
;
586 curl_easy_getinfo(slot
->curl
,
589 request
= request_queue_head
;
590 while (request
!= NULL
&&
591 request
->slot
!= slot
)
592 request
= request
->next
;
594 fprintf(stderr
, "Received DONE message for unknown request!\n");
596 if (request
!= NULL
) {
597 request
->curl_result
=
598 curl_message
->data
.result
;
599 request
->http_code
= slot
->http_code
;
600 request
->slot
= NULL
;
601 request
->state
= COMPLETE
;
603 /* Use alternates if necessary */
604 if (request
->http_code
== 404) {
605 fetch_alternates(alt
->base
);
606 if (request
->repo
->next
!= NULL
) {
609 start_request(request
);
612 finish_request(request
);
616 fprintf(stderr
, "Unknown CURL message received: %d\n",
617 (int)curl_message
->msg
);
619 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
623 void process_request_queue(void)
625 struct transfer_request
*request
= request_queue_head
;
626 struct active_request_slot
*slot
= active_queue_head
;
629 while (active_requests
< max_requests
&& request
!= NULL
) {
630 if (request
->state
== WAITING
) {
631 if (has_sha1_file(request
->sha1
))
632 release_request(request
);
634 start_request(request
);
635 curl_multi_perform(curlm
, &num_transfers
);
637 request
= request
->next
;
640 while (slot
!= NULL
) {
641 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
642 curl_easy_cleanup(slot
->curl
);
650 void prefetch(unsigned char *sha1
)
652 struct transfer_request
*newreq
;
653 struct transfer_request
*tail
;
654 char *filename
= sha1_file_name(sha1
);
656 newreq
= xmalloc(sizeof(*newreq
));
657 memcpy(newreq
->sha1
, sha1
, 20);
661 newreq
->state
= WAITING
;
662 snprintf(newreq
->filename
, sizeof(newreq
->filename
), "%s", filename
);
663 snprintf(newreq
->tmpfile
, sizeof(newreq
->tmpfile
),
664 "%s.temp", filename
);
667 if (request_queue_head
== NULL
) {
668 request_queue_head
= newreq
;
670 tail
= request_queue_head
;
671 while (tail
->next
!= NULL
) {
676 #ifdef USE_CURL_MULTI
677 process_request_queue();
678 process_curl_messages();
682 static int fetch_index(struct alt_base
*repo
, unsigned char *sha1
)
684 char *hex
= sha1_to_hex(sha1
);
687 char tmpfile
[PATH_MAX
];
689 char range
[RANGE_HEADER_SIZE
];
690 struct curl_slist
*range_header
= NULL
;
693 struct active_request_slot
*slot
;
695 if (has_pack_index(sha1
))
699 fprintf(stderr
, "Getting index for pack %s\n", hex
);
701 url
= xmalloc(strlen(repo
->base
) + 64);
702 sprintf(url
, "%s/objects/pack/pack-%s.idx", repo
->base
, hex
);
704 filename
= sha1_pack_index_name(sha1
);
705 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
706 indexfile
= fopen(tmpfile
, "a");
708 return error("Unable to open local file %s for pack index",
711 slot
= get_active_slot();
712 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, indexfile
);
713 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
714 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
715 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
716 slot
->local
= indexfile
;
718 /* If there is data present from a previous transfer attempt,
719 resume where it left off */
720 prev_posn
= ftell(indexfile
);
724 "Resuming fetch of index for pack %s at byte %ld\n",
726 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
727 range_header
= curl_slist_append(range_header
, range
);
728 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
731 if (start_active_slot(slot
)) {
732 run_active_slot(slot
);
733 if (slot
->curl_result
!= CURLE_OK
) {
735 return error("Unable to get pack index %s\n%s", url
,
739 return error("Unable to start request");
744 return move_temp_to_file(tmpfile
, filename
);
747 static int setup_index(struct alt_base
*repo
, unsigned char *sha1
)
749 struct packed_git
*new_pack
;
750 if (has_pack_file(sha1
))
751 return 0; // don't list this as something we can get
753 if (fetch_index(repo
, sha1
))
756 new_pack
= parse_pack_index(sha1
);
757 new_pack
->next
= repo
->packs
;
758 repo
->packs
= new_pack
;
762 static int fetch_alternates(char *base
)
765 struct buffer buffer
;
769 int http_specific
= 1;
770 struct alt_base
*tail
= alt
;
771 static const char null_byte
= '\0';
773 struct active_request_slot
*slot
;
778 data
= xmalloc(4096);
781 buffer
.buffer
= data
;
784 fprintf(stderr
, "Getting alternates list\n");
786 url
= xmalloc(strlen(base
) + 31);
787 sprintf(url
, "%s/objects/info/http-alternates", base
);
789 slot
= get_active_slot();
790 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
791 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
792 fwrite_buffer_dynamic
);
793 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
794 if (start_active_slot(slot
)) {
795 run_active_slot(slot
);
796 if (slot
->curl_result
!= CURLE_OK
|| !buffer
.posn
) {
799 sprintf(url
, "%s/objects/info/alternates", base
);
801 slot
= get_active_slot();
802 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
803 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
804 fwrite_buffer_dynamic
);
805 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
806 if (start_active_slot(slot
)) {
807 run_active_slot(slot
);
808 if (slot
->curl_result
!= CURLE_OK
) {
810 if (slot
->http_code
== 404)
821 fwrite_buffer_dynamic(&null_byte
, 1, 1, &buffer
);
823 data
= buffer
.buffer
;
825 while (i
< buffer
.posn
) {
827 while (posn
< buffer
.posn
&& data
[posn
] != '\n')
829 if (data
[posn
] == '\n') {
832 struct alt_base
*newalt
;
834 if (data
[i
] == '/') {
835 serverlen
= strchr(base
+ 8, '/') - base
;
837 } else if (!memcmp(data
+ i
, "../", 3)) {
839 serverlen
= strlen(base
);
840 while (i
+ 2 < posn
&&
841 !memcmp(data
+ i
, "../", 3)) {
844 } while (serverlen
&&
845 base
[serverlen
- 1] != '/');
848 // If the server got removed, give up.
849 okay
= strchr(base
, ':') - base
+ 3 <
851 } else if (http_specific
) {
852 char *colon
= strchr(data
+ i
, ':');
853 char *slash
= strchr(data
+ i
, '/');
854 if (colon
&& slash
&& colon
< data
+ posn
&&
855 slash
< data
+ posn
&& colon
< slash
) {
859 // skip 'objects' at end
861 target
= xmalloc(serverlen
+ posn
- i
- 6);
862 strncpy(target
, base
, serverlen
);
863 strncpy(target
+ serverlen
, data
+ i
,
865 target
[serverlen
+ posn
- i
- 7] = '\0';
868 "Also look at %s\n", target
);
869 newalt
= xmalloc(sizeof(*newalt
));
871 newalt
->base
= target
;
872 newalt
->got_indices
= 0;
873 newalt
->packs
= NULL
;
874 while (tail
->next
!= NULL
)
888 static int fetch_indices(struct alt_base
*repo
)
890 unsigned char sha1
[20];
892 struct buffer buffer
;
896 struct active_request_slot
*slot
;
898 if (repo
->got_indices
)
901 data
= xmalloc(4096);
904 buffer
.buffer
= data
;
907 fprintf(stderr
, "Getting pack list\n");
909 url
= xmalloc(strlen(repo
->base
) + 21);
910 sprintf(url
, "%s/objects/info/packs", repo
->base
);
912 slot
= get_active_slot();
913 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
914 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
915 fwrite_buffer_dynamic
);
916 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
917 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
918 if (start_active_slot(slot
)) {
919 run_active_slot(slot
);
920 if (slot
->curl_result
!= CURLE_OK
) {
922 return error("%s", curl_errorstr
);
926 return error("Unable to start request");
929 data
= buffer
.buffer
;
930 while (i
< buffer
.posn
) {
934 if (i
+ 52 < buffer
.posn
&&
935 !strncmp(data
+ i
, " pack-", 6) &&
936 !strncmp(data
+ i
+ 46, ".pack\n", 6)) {
937 get_sha1_hex(data
+ i
+ 6, sha1
);
938 setup_index(repo
, sha1
);
943 while (data
[i
] != '\n')
950 repo
->got_indices
= 1;
954 static int fetch_pack(struct alt_base
*repo
, unsigned char *sha1
)
957 struct packed_git
*target
;
958 struct packed_git
**lst
;
961 char tmpfile
[PATH_MAX
];
964 char range
[RANGE_HEADER_SIZE
];
965 struct curl_slist
*range_header
= NULL
;
967 struct active_request_slot
*slot
;
969 if (fetch_indices(repo
))
971 target
= find_sha1_pack(sha1
, repo
->packs
);
976 fprintf(stderr
, "Getting pack %s\n",
977 sha1_to_hex(target
->sha1
));
978 fprintf(stderr
, " which contains %s\n",
982 url
= xmalloc(strlen(repo
->base
) + 65);
983 sprintf(url
, "%s/objects/pack/pack-%s.pack",
984 repo
->base
, sha1_to_hex(target
->sha1
));
986 filename
= sha1_pack_name(target
->sha1
);
987 snprintf(tmpfile
, sizeof(tmpfile
), "%s.temp", filename
);
988 packfile
= fopen(tmpfile
, "a");
990 return error("Unable to open local file %s for pack",
993 slot
= get_active_slot();
994 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, packfile
);
995 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
996 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
997 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
998 slot
->local
= packfile
;
1000 /* If there is data present from a previous transfer attempt,
1001 resume where it left off */
1002 prev_posn
= ftell(packfile
);
1006 "Resuming fetch of pack %s at byte %ld\n",
1007 sha1_to_hex(target
->sha1
), prev_posn
);
1008 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1009 range_header
= curl_slist_append(range_header
, range
);
1010 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, range_header
);
1013 if (start_active_slot(slot
)) {
1014 run_active_slot(slot
);
1015 if (slot
->curl_result
!= CURLE_OK
) {
1017 return error("Unable to get pack file %s\n%s", url
,
1021 return error("Unable to start request");
1026 ret
= move_temp_to_file(tmpfile
, filename
);
1031 while (*lst
!= target
)
1032 lst
= &((*lst
)->next
);
1033 *lst
= (*lst
)->next
;
1035 if (verify_pack(target
, 0))
1037 install_packed_git(target
);
1042 static int fetch_object(struct alt_base
*repo
, unsigned char *sha1
)
1044 char *hex
= sha1_to_hex(sha1
);
1046 struct transfer_request
*request
= request_queue_head
;
1048 while (request
!= NULL
&& memcmp(request
->sha1
, sha1
, 20))
1049 request
= request
->next
;
1050 if (request
== NULL
)
1051 return error("Couldn't find request for %s in the queue", hex
);
1053 if (has_sha1_file(request
->sha1
)) {
1054 release_request(request
);
1058 #ifdef USE_CURL_MULTI
1059 while (request
->state
== WAITING
) {
1061 curl_multi_perform(curlm
, &num_transfers
);
1062 if (num_transfers
< active_requests
) {
1063 process_curl_messages();
1064 process_request_queue();
1068 start_request(request
);
1071 while (request
->state
== ACTIVE
) {
1072 run_active_slot(request
->slot
);
1073 #ifndef USE_CURL_MULTI
1074 request
->curl_result
= request
->slot
->curl_result
;
1075 request
->http_code
= request
->slot
->http_code
;
1076 request
->slot
= NULL
;
1078 /* Use alternates if necessary */
1079 if (request
->http_code
== 404) {
1080 fetch_alternates(alt
->base
);
1081 if (request
->repo
->next
!= NULL
) {
1082 request
->repo
= request
->repo
->next
;
1083 start_request(request
);
1086 finish_request(request
);
1087 request
->state
= COMPLETE
;
1092 if (request
->state
== ABORTED
) {
1093 release_request(request
);
1094 return error("Request for %s aborted", hex
);
1097 if (request
->curl_result
!= CURLE_OK
&& request
->http_code
!= 416) {
1098 ret
= error("%s (curl_result = %d, http_code = %ld, sha1 = %s)",
1099 request
->errorstr
, request
->curl_result
,
1100 request
->http_code
, hex
);
1101 release_request(request
);
1105 if (request
->zret
!= Z_STREAM_END
) {
1106 ret
= error("File %s (%s) corrupt\n", hex
, request
->url
);
1107 release_request(request
);
1111 if (memcmp(request
->sha1
, request
->real_sha1
, 20)) {
1112 release_request(request
);
1113 return error("File %s has bad hash\n", hex
);
1116 if (request
->rename
< 0) {
1117 ret
= error("unable to write sha1 filename %s: %s",
1119 strerror(request
->rename
));
1120 release_request(request
);
1124 release_request(request
);
1128 int fetch(unsigned char *sha1
)
1130 struct alt_base
*altbase
= alt
;
1132 if (!fetch_object(altbase
, sha1
))
1135 if (!fetch_pack(altbase
, sha1
))
1137 fetch_alternates(alt
->base
);
1138 altbase
= altbase
->next
;
1140 return error("Unable to find %s under %s\n", sha1_to_hex(sha1
),
1144 static inline int needs_quote(int ch
)
1147 case '/': case '-': case '.':
1148 case 'A'...'Z': case 'a'...'z': case '0'...'9':
1155 static inline int hex(int v
)
1157 if (v
< 10) return '0' + v
;
1158 else return 'A' + v
- 10;
1161 static char *quote_ref_url(const char *base
, const char *ref
)
1165 int len
, baselen
, ch
;
1167 baselen
= strlen(base
);
1168 len
= baselen
+ 6; /* "refs/" + NUL */
1169 for (cp
= ref
; (ch
= *cp
) != 0; cp
++, len
++)
1170 if (needs_quote(ch
))
1171 len
+= 2; /* extra two hex plus replacement % */
1172 qref
= xmalloc(len
);
1173 memcpy(qref
, base
, baselen
);
1174 memcpy(qref
+ baselen
, "refs/", 5);
1175 for (cp
= ref
, dp
= qref
+ baselen
+ 5; (ch
= *cp
) != 0; cp
++) {
1176 if (needs_quote(ch
)) {
1178 *dp
++ = hex((ch
>> 4) & 0xF);
1179 *dp
++ = hex(ch
& 0xF);
1189 int fetch_ref(char *ref
, unsigned char *sha1
)
1193 struct buffer buffer
;
1194 char *base
= alt
->base
;
1195 struct active_request_slot
*slot
;
1198 buffer
.buffer
= hex
;
1201 url
= quote_ref_url(base
, ref
);
1202 slot
= get_active_slot();
1203 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
1204 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
1205 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
1206 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1207 if (start_active_slot(slot
)) {
1208 run_active_slot(slot
);
1209 if (slot
->curl_result
!= CURLE_OK
)
1210 return error("Couldn't get %s for %s\n%s",
1211 url
, ref
, curl_errorstr
);
1213 return error("Unable to start request");
1217 get_sha1_hex(hex
, sha1
);
1221 int main(int argc
, char **argv
)
1226 struct active_request_slot
*slot
;
1227 char *low_speed_limit
;
1228 char *low_speed_time
;
1232 while (arg
< argc
&& argv
[arg
][0] == '-') {
1233 if (argv
[arg
][1] == 't') {
1235 } else if (argv
[arg
][1] == 'c') {
1237 } else if (argv
[arg
][1] == 'a') {
1241 } else if (argv
[arg
][1] == 'v') {
1243 } else if (argv
[arg
][1] == 'w') {
1244 write_ref
= argv
[arg
+ 1];
1246 } else if (!strcmp(argv
[arg
], "--recover")) {
1251 if (argc
< arg
+ 2) {
1252 usage("git-http-fetch [-c] [-t] [-a] [-d] [-v] [--recover] [-w ref] commit-id url");
1255 commit_id
= argv
[arg
];
1256 url
= argv
[arg
+ 1];
1258 curl_global_init(CURL_GLOBAL_ALL
);
1260 #ifdef USE_CURL_MULTI
1262 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1263 if (http_max_requests
!= NULL
)
1264 max_requests
= atoi(http_max_requests
);
1267 curlm
= curl_multi_init();
1268 if (curlm
== NULL
) {
1269 fprintf(stderr
, "Error creating curl multi handle.\n");
1274 if (getenv("GIT_SSL_NO_VERIFY"))
1275 curl_ssl_verify
= 0;
1277 ssl_cert
= getenv("GIT_SSL_CERT");
1278 #if LIBCURL_VERSION_NUM >= 0x070902
1279 ssl_key
= getenv("GIT_SSL_KEY");
1281 #if LIBCURL_VERSION_NUM >= 0x070908
1282 ssl_capath
= getenv("GIT_SSL_CAPATH");
1284 ssl_cainfo
= getenv("GIT_SSL_CAINFO");
1286 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1287 if (low_speed_limit
!= NULL
)
1288 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1289 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1290 if (low_speed_time
!= NULL
)
1291 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1293 git_config(http_options
);
1295 if (curl_ssl_verify
== -1)
1296 curl_ssl_verify
= 1;
1298 #ifdef USE_CURL_MULTI
1299 if (max_requests
< 1)
1300 max_requests
= DEFAULT_MAX_REQUESTS
;
1303 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
1304 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
1305 no_range_header
= curl_slist_append(no_range_header
, "Range:");
1307 #ifndef NO_CURL_EASY_DUPHANDLE
1308 curl_default
= get_curl_handle();
1311 alt
= xmalloc(sizeof(*alt
));
1313 alt
->got_indices
= 0;
1317 if (pull(commit_id
))
1320 curl_slist_free_all(pragma_header
);
1321 curl_slist_free_all(no_pragma_header
);
1322 curl_slist_free_all(no_range_header
);
1323 #ifndef NO_CURL_EASY_DUPHANDLE
1324 curl_easy_cleanup(curl_default
);
1326 slot
= active_queue_head
;
1327 while (slot
!= NULL
) {
1329 if (get_verbosely
) {
1330 curl_easy_getinfo(slot
->curl
,
1331 CURLINFO_EFFECTIVE_URL
,
1333 fprintf(stderr
, "Waiting for %s\n", wait_url
);
1335 run_active_slot(slot
);
1337 if (slot
->curl
!= NULL
)
1338 curl_easy_cleanup(slot
->curl
);
1341 #ifdef USE_CURL_MULTI
1342 curl_multi_cleanup(curlm
);
1344 curl_global_cleanup();