9 static int max_requests
= -1;
12 #ifndef NO_CURL_EASY_DUPHANDLE
13 static CURL
*curl_default
;
16 #define PREV_BUF_SIZE 4096
17 #define RANGE_HEADER_SIZE 30
19 char curl_errorstr
[CURL_ERROR_SIZE
];
21 static int curl_ssl_verify
= -1;
22 static const char *ssl_cert
;
23 #if LIBCURL_VERSION_NUM >= 0x070903
24 static const char *ssl_key
;
26 #if LIBCURL_VERSION_NUM >= 0x070908
27 static const char *ssl_capath
;
29 static const char *ssl_cainfo
;
30 static long curl_low_speed_limit
= -1;
31 static long curl_low_speed_time
= -1;
32 static int curl_ftp_no_epsv
;
33 static const char *curl_http_proxy
;
34 static char *user_name
, *user_pass
;
36 #if LIBCURL_VERSION_NUM >= 0x071700
37 /* Use CURLOPT_KEYPASSWD as is */
38 #elif LIBCURL_VERSION_NUM >= 0x070903
39 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
41 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
44 static char *ssl_cert_password
;
45 static int ssl_cert_password_required
;
47 static struct curl_slist
*pragma_header
;
48 static struct curl_slist
*no_pragma_header
;
50 static struct active_request_slot
*active_queue_head
;
52 size_t fread_buffer(void *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
54 size_t size
= eltsize
* nmemb
;
55 struct buffer
*buffer
= buffer_
;
57 if (size
> buffer
->buf
.len
- buffer
->posn
)
58 size
= buffer
->buf
.len
- buffer
->posn
;
59 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
66 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
68 struct buffer
*buffer
= clientp
;
74 case CURLIOCMD_RESTARTREAD
:
79 return CURLIOE_UNKNOWNCMD
;
84 size_t fwrite_buffer(const void *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
86 size_t size
= eltsize
* nmemb
;
87 struct strbuf
*buffer
= buffer_
;
89 strbuf_add(buffer
, ptr
, size
);
94 size_t fwrite_null(const void *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
97 return eltsize
* nmemb
;
100 static void finish_active_slot(struct active_request_slot
*slot
);
102 #ifdef USE_CURL_MULTI
103 static void process_curl_messages(void)
106 struct active_request_slot
*slot
;
107 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
109 while (curl_message
!= NULL
) {
110 if (curl_message
->msg
== CURLMSG_DONE
) {
111 int curl_result
= curl_message
->data
.result
;
112 slot
= active_queue_head
;
113 while (slot
!= NULL
&&
114 slot
->curl
!= curl_message
->easy_handle
)
117 curl_multi_remove_handle(curlm
, slot
->curl
);
118 slot
->curl_result
= curl_result
;
119 finish_active_slot(slot
);
121 fprintf(stderr
, "Received DONE message for unknown request!\n");
124 fprintf(stderr
, "Unknown CURL message received: %d\n",
125 (int)curl_message
->msg
);
127 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
132 static int http_options(const char *var
, const char *value
, void *cb
)
134 if (!strcmp("http.sslverify", var
)) {
135 curl_ssl_verify
= git_config_bool(var
, value
);
138 if (!strcmp("http.sslcert", var
))
139 return git_config_string(&ssl_cert
, var
, value
);
140 #if LIBCURL_VERSION_NUM >= 0x070903
141 if (!strcmp("http.sslkey", var
))
142 return git_config_string(&ssl_key
, var
, value
);
144 #if LIBCURL_VERSION_NUM >= 0x070908
145 if (!strcmp("http.sslcapath", var
))
146 return git_config_string(&ssl_capath
, var
, value
);
148 if (!strcmp("http.sslcainfo", var
))
149 return git_config_string(&ssl_cainfo
, var
, value
);
150 if (!strcmp("http.sslcertpasswordprotected", var
)) {
151 if (git_config_bool(var
, value
))
152 ssl_cert_password_required
= 1;
155 #ifdef USE_CURL_MULTI
156 if (!strcmp("http.maxrequests", var
)) {
157 max_requests
= git_config_int(var
, value
);
161 if (!strcmp("http.lowspeedlimit", var
)) {
162 curl_low_speed_limit
= (long)git_config_int(var
, value
);
165 if (!strcmp("http.lowspeedtime", var
)) {
166 curl_low_speed_time
= (long)git_config_int(var
, value
);
170 if (!strcmp("http.noepsv", var
)) {
171 curl_ftp_no_epsv
= git_config_bool(var
, value
);
174 if (!strcmp("http.proxy", var
))
175 return git_config_string(&curl_http_proxy
, var
, value
);
177 /* Fall back on the default ones */
178 return git_default_config(var
, value
, cb
);
181 static void init_curl_http_auth(CURL
*result
)
184 struct strbuf up
= STRBUF_INIT
;
186 user_pass
= xstrdup(getpass("Password: "));
187 strbuf_addf(&up
, "%s:%s", user_name
, user_pass
);
188 curl_easy_setopt(result
, CURLOPT_USERPWD
,
189 strbuf_detach(&up
, NULL
));
193 static int has_cert_password(void)
195 if (ssl_cert_password
!= NULL
)
197 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
199 /* Only prompt the user once. */
200 ssl_cert_password_required
= -1;
201 ssl_cert_password
= getpass("Certificate Password: ");
202 if (ssl_cert_password
!= NULL
) {
203 ssl_cert_password
= xstrdup(ssl_cert_password
);
209 static CURL
*get_curl_handle(void)
211 CURL
*result
= curl_easy_init();
213 if (!curl_ssl_verify
) {
214 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
215 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
217 /* Verify authenticity of the peer's certificate */
218 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
219 /* The name in the cert must match whom we tried to connect */
220 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
223 #if LIBCURL_VERSION_NUM >= 0x070907
224 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
227 init_curl_http_auth(result
);
229 if (ssl_cert
!= NULL
)
230 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
231 if (has_cert_password())
232 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, ssl_cert_password
);
233 #if LIBCURL_VERSION_NUM >= 0x070903
235 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
237 #if LIBCURL_VERSION_NUM >= 0x070908
238 if (ssl_capath
!= NULL
)
239 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
241 if (ssl_cainfo
!= NULL
)
242 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
243 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
245 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
246 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
247 curl_low_speed_limit
);
248 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
249 curl_low_speed_time
);
252 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
254 if (getenv("GIT_CURL_VERBOSE"))
255 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
257 curl_easy_setopt(result
, CURLOPT_USERAGENT
, GIT_USER_AGENT
);
259 if (curl_ftp_no_epsv
)
260 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
263 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
268 static void http_auth_init(const char *url
)
270 char *at
, *colon
, *cp
, *slash
;
273 cp
= strstr(url
, "://");
278 * Ok, the URL looks like "proto://something". Which one?
279 * "proto://<user>:<pass>@<host>/...",
280 * "proto://<user>@<host>/...", or just
281 * "proto://<host>/..."?
284 at
= strchr(cp
, '@');
285 colon
= strchr(cp
, ':');
286 slash
= strchrnul(cp
, '/');
287 if (!at
|| slash
<= at
)
288 return; /* No credentials */
289 if (!colon
|| at
<= colon
) {
292 user_name
= xmalloc(len
+ 1);
293 memcpy(user_name
, cp
, len
);
294 user_name
[len
] = '\0';
298 user_name
= xmalloc(len
+ 1);
299 memcpy(user_name
, cp
, len
);
300 user_name
[len
] = '\0';
301 len
= at
- (colon
+ 1);
302 user_pass
= xmalloc(len
+ 1);
303 memcpy(user_pass
, colon
+ 1, len
);
304 user_pass
[len
] = '\0';
308 static void set_from_env(const char **var
, const char *envname
)
310 const char *val
= getenv(envname
);
315 void http_init(struct remote
*remote
)
317 char *low_speed_limit
;
318 char *low_speed_time
;
322 git_config(http_options
, NULL
);
324 curl_global_init(CURL_GLOBAL_ALL
);
326 if (remote
&& remote
->http_proxy
)
327 curl_http_proxy
= xstrdup(remote
->http_proxy
);
329 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
330 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
332 #ifdef USE_CURL_MULTI
334 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
335 if (http_max_requests
!= NULL
)
336 max_requests
= atoi(http_max_requests
);
339 curlm
= curl_multi_init();
341 fprintf(stderr
, "Error creating curl multi handle.\n");
346 if (getenv("GIT_SSL_NO_VERIFY"))
349 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
350 #if LIBCURL_VERSION_NUM >= 0x070903
351 set_from_env(&ssl_key
, "GIT_SSL_KEY");
353 #if LIBCURL_VERSION_NUM >= 0x070908
354 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
356 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
358 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
359 if (low_speed_limit
!= NULL
)
360 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
361 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
362 if (low_speed_time
!= NULL
)
363 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
365 if (curl_ssl_verify
== -1)
368 #ifdef USE_CURL_MULTI
369 if (max_requests
< 1)
370 max_requests
= DEFAULT_MAX_REQUESTS
;
373 if (getenv("GIT_CURL_FTP_NO_EPSV"))
374 curl_ftp_no_epsv
= 1;
376 if (remote
&& remote
->url
&& remote
->url
[0]) {
377 http_auth_init(remote
->url
[0]);
378 if (!ssl_cert_password_required
&&
379 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
380 !prefixcmp(remote
->url
[0], "https://"))
381 ssl_cert_password_required
= 1;
384 #ifndef NO_CURL_EASY_DUPHANDLE
385 curl_default
= get_curl_handle();
389 void http_cleanup(void)
391 struct active_request_slot
*slot
= active_queue_head
;
393 while (slot
!= NULL
) {
394 struct active_request_slot
*next
= slot
->next
;
395 if (slot
->curl
!= NULL
) {
396 #ifdef USE_CURL_MULTI
397 curl_multi_remove_handle(curlm
, slot
->curl
);
399 curl_easy_cleanup(slot
->curl
);
404 active_queue_head
= NULL
;
406 #ifndef NO_CURL_EASY_DUPHANDLE
407 curl_easy_cleanup(curl_default
);
410 #ifdef USE_CURL_MULTI
411 curl_multi_cleanup(curlm
);
413 curl_global_cleanup();
415 curl_slist_free_all(pragma_header
);
416 pragma_header
= NULL
;
418 curl_slist_free_all(no_pragma_header
);
419 no_pragma_header
= NULL
;
421 if (curl_http_proxy
) {
422 free((void *)curl_http_proxy
);
423 curl_http_proxy
= NULL
;
426 if (ssl_cert_password
!= NULL
) {
427 memset(ssl_cert_password
, 0, strlen(ssl_cert_password
));
428 free(ssl_cert_password
);
429 ssl_cert_password
= NULL
;
431 ssl_cert_password_required
= 0;
434 struct active_request_slot
*get_active_slot(void)
436 struct active_request_slot
*slot
= active_queue_head
;
437 struct active_request_slot
*newslot
;
439 #ifdef USE_CURL_MULTI
442 /* Wait for a slot to open up if the queue is full */
443 while (active_requests
>= max_requests
) {
444 curl_multi_perform(curlm
, &num_transfers
);
445 if (num_transfers
< active_requests
)
446 process_curl_messages();
450 while (slot
!= NULL
&& slot
->in_use
)
454 newslot
= xmalloc(sizeof(*newslot
));
455 newslot
->curl
= NULL
;
457 newslot
->next
= NULL
;
459 slot
= active_queue_head
;
461 active_queue_head
= newslot
;
463 while (slot
->next
!= NULL
)
465 slot
->next
= newslot
;
470 if (slot
->curl
== NULL
) {
471 #ifdef NO_CURL_EASY_DUPHANDLE
472 slot
->curl
= get_curl_handle();
474 slot
->curl
= curl_easy_duphandle(curl_default
);
481 slot
->results
= NULL
;
482 slot
->finished
= NULL
;
483 slot
->callback_data
= NULL
;
484 slot
->callback_func
= NULL
;
485 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
486 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
487 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
488 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
489 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
490 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
491 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
496 int start_active_slot(struct active_request_slot
*slot
)
498 #ifdef USE_CURL_MULTI
499 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
502 if (curlm_result
!= CURLM_OK
&&
503 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
510 * We know there must be something to do, since we just added
513 curl_multi_perform(curlm
, &num_transfers
);
518 #ifdef USE_CURL_MULTI
522 struct fill_chain
*next
;
525 static struct fill_chain
*fill_cfg
;
527 void add_fill_function(void *data
, int (*fill
)(void *))
529 struct fill_chain
*new = xmalloc(sizeof(*new));
530 struct fill_chain
**linkp
= &fill_cfg
;
535 linkp
= &(*linkp
)->next
;
539 void fill_active_slots(void)
541 struct active_request_slot
*slot
= active_queue_head
;
543 while (active_requests
< max_requests
) {
544 struct fill_chain
*fill
;
545 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
546 if (fill
->fill(fill
->data
))
553 while (slot
!= NULL
) {
554 if (!slot
->in_use
&& slot
->curl
!= NULL
) {
555 curl_easy_cleanup(slot
->curl
);
562 void step_active_slots(void)
565 CURLMcode curlm_result
;
568 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
569 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
570 if (num_transfers
< active_requests
) {
571 process_curl_messages();
577 void run_active_slot(struct active_request_slot
*slot
)
579 #ifdef USE_CURL_MULTI
586 struct timeval select_timeout
;
589 slot
->finished
= &finished
;
594 if (!data_received
&& slot
->local
!= NULL
) {
595 current_pos
= ftell(slot
->local
);
596 if (current_pos
> last_pos
)
598 last_pos
= current_pos
;
601 if (slot
->in_use
&& !data_received
) {
606 select_timeout
.tv_sec
= 0;
607 select_timeout
.tv_usec
= 50000;
608 select(max_fd
, &readfds
, &writefds
,
609 &excfds
, &select_timeout
);
613 while (slot
->in_use
) {
614 slot
->curl_result
= curl_easy_perform(slot
->curl
);
615 finish_active_slot(slot
);
620 static void closedown_active_slot(struct active_request_slot
*slot
)
626 void release_active_slot(struct active_request_slot
*slot
)
628 closedown_active_slot(slot
);
630 #ifdef USE_CURL_MULTI
631 curl_multi_remove_handle(curlm
, slot
->curl
);
633 curl_easy_cleanup(slot
->curl
);
636 #ifdef USE_CURL_MULTI
641 static void finish_active_slot(struct active_request_slot
*slot
)
643 closedown_active_slot(slot
);
644 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
646 if (slot
->finished
!= NULL
)
647 (*slot
->finished
) = 1;
649 /* Store slot results so they can be read after the slot is reused */
650 if (slot
->results
!= NULL
) {
651 slot
->results
->curl_result
= slot
->curl_result
;
652 slot
->results
->http_code
= slot
->http_code
;
655 /* Run callback if appropriate */
656 if (slot
->callback_func
!= NULL
)
657 slot
->callback_func(slot
->callback_data
);
660 void finish_all_active_slots(void)
662 struct active_request_slot
*slot
= active_queue_head
;
666 run_active_slot(slot
);
667 slot
= active_queue_head
;
673 /* Helpers for modifying and creating URLs */
674 static inline int needs_quote(int ch
)
676 if (((ch
>= 'A') && (ch
<= 'Z'))
677 || ((ch
>= 'a') && (ch
<= 'z'))
678 || ((ch
>= '0') && (ch
<= '9'))
686 static inline int hex(int v
)
694 static void end_url_with_slash(struct strbuf
*buf
, const char *url
)
696 strbuf_addstr(buf
, url
);
697 if (buf
->len
&& buf
->buf
[buf
->len
- 1] != '/')
698 strbuf_addstr(buf
, "/");
701 static char *quote_ref_url(const char *base
, const char *ref
)
703 struct strbuf buf
= STRBUF_INIT
;
707 end_url_with_slash(&buf
, base
);
709 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
711 strbuf_addf(&buf
, "%%%02x", ch
);
713 strbuf_addch(&buf
, *cp
);
715 return strbuf_detach(&buf
, NULL
);
718 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
720 int only_two_digit_prefix
)
722 end_url_with_slash(buf
, url
);
724 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
725 if (!only_two_digit_prefix
)
726 strbuf_addf(buf
, "%s", hex
+2);
729 char *get_remote_object_url(const char *url
, const char *hex
,
730 int only_two_digit_prefix
)
732 struct strbuf buf
= STRBUF_INIT
;
733 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
734 return strbuf_detach(&buf
, NULL
);
737 /* http_request() targets */
738 #define HTTP_REQUEST_STRBUF 0
739 #define HTTP_REQUEST_FILE 1
741 static int http_request(const char *url
, void *result
, int target
, int options
)
743 struct active_request_slot
*slot
;
744 struct slot_results results
;
745 struct curl_slist
*headers
= NULL
;
746 struct strbuf buf
= STRBUF_INIT
;
749 slot
= get_active_slot();
750 slot
->results
= &results
;
751 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
753 if (result
== NULL
) {
754 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
756 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
757 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
759 if (target
== HTTP_REQUEST_FILE
) {
760 long posn
= ftell(result
);
761 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
764 strbuf_addf(&buf
, "Range: bytes=%ld-", posn
);
765 headers
= curl_slist_append(headers
, buf
.buf
);
768 slot
->local
= result
;
770 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
774 strbuf_addstr(&buf
, "Pragma:");
775 if (options
& HTTP_NO_CACHE
)
776 strbuf_addstr(&buf
, " no-cache");
778 headers
= curl_slist_append(headers
, buf
.buf
);
780 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
781 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
783 if (start_active_slot(slot
)) {
784 run_active_slot(slot
);
785 if (results
.curl_result
== CURLE_OK
)
787 else if (missing_target(&results
))
788 ret
= HTTP_MISSING_TARGET
;
792 error("Unable to start HTTP request for %s", url
);
793 ret
= HTTP_START_FAILED
;
797 curl_slist_free_all(headers
);
798 strbuf_release(&buf
);
803 int http_get_strbuf(const char *url
, struct strbuf
*result
, int options
)
805 return http_request(url
, result
, HTTP_REQUEST_STRBUF
, options
);
808 int http_get_file(const char *url
, const char *filename
, int options
)
811 struct strbuf tmpfile
= STRBUF_INIT
;
814 strbuf_addf(&tmpfile
, "%s.temp", filename
);
815 result
= fopen(tmpfile
.buf
, "a");
817 error("Unable to open local file %s", tmpfile
.buf
);
822 ret
= http_request(url
, result
, HTTP_REQUEST_FILE
, options
);
825 if ((ret
== HTTP_OK
) && move_temp_to_file(tmpfile
.buf
, filename
))
828 strbuf_release(&tmpfile
);
832 int http_error(const char *url
, int ret
)
834 /* http_request has already handled HTTP_START_FAILED. */
835 if (ret
!= HTTP_START_FAILED
)
836 error("%s while accessing %s\n", curl_errorstr
, url
);
841 int http_fetch_ref(const char *base
, struct ref
*ref
)
844 struct strbuf buffer
= STRBUF_INIT
;
847 url
= quote_ref_url(base
, ref
->name
);
848 if (http_get_strbuf(url
, &buffer
, HTTP_NO_CACHE
) == HTTP_OK
) {
849 strbuf_rtrim(&buffer
);
850 if (buffer
.len
== 40)
851 ret
= get_sha1_hex(buffer
.buf
, ref
->old_sha1
);
852 else if (!prefixcmp(buffer
.buf
, "ref: ")) {
853 ref
->symref
= xstrdup(buffer
.buf
+ 5);
858 strbuf_release(&buffer
);
863 /* Helpers for fetching packs */
864 static int fetch_pack_index(unsigned char *sha1
, const char *base_url
)
867 char *hex
= xstrdup(sha1_to_hex(sha1
));
870 struct strbuf buf
= STRBUF_INIT
;
872 /* Don't use the index if the pack isn't there */
873 end_url_with_slash(&buf
, base_url
);
874 strbuf_addf(&buf
, "objects/pack/pack-%s.pack", hex
);
875 url
= strbuf_detach(&buf
, 0);
877 if (http_get_strbuf(url
, NULL
, 0)) {
878 ret
= error("Unable to verify pack %s is available",
883 if (has_pack_index(sha1
)) {
889 fprintf(stderr
, "Getting index for pack %s\n", hex
);
891 end_url_with_slash(&buf
, base_url
);
892 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", hex
);
893 url
= strbuf_detach(&buf
, NULL
);
895 filename
= sha1_pack_index_name(sha1
);
896 if (http_get_file(url
, filename
, 0) != HTTP_OK
)
897 ret
= error("Unable to get pack index %s\n", url
);
905 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
906 unsigned char *sha1
, const char *base_url
)
908 struct packed_git
*new_pack
;
910 if (fetch_pack_index(sha1
, base_url
))
913 new_pack
= parse_pack_index(sha1
);
915 return -1; /* parse_pack_index() already issued error message */
916 new_pack
->next
= *packs_head
;
917 *packs_head
= new_pack
;
921 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
925 struct strbuf buf
= STRBUF_INIT
;
926 unsigned char sha1
[20];
928 end_url_with_slash(&buf
, base_url
);
929 strbuf_addstr(&buf
, "objects/info/packs");
930 url
= strbuf_detach(&buf
, NULL
);
932 ret
= http_get_strbuf(url
, &buf
, HTTP_NO_CACHE
);
937 while (i
< buf
.len
) {
941 if (i
+ 52 <= buf
.len
&&
942 !prefixcmp(data
+ i
, " pack-") &&
943 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
944 get_sha1_hex(data
+ i
+ 6, sha1
);
945 fetch_and_setup_pack_index(packs_head
, sha1
,
951 while (i
< buf
.len
&& data
[i
] != '\n')
962 void release_http_pack_request(struct http_pack_request
*preq
)
964 if (preq
->packfile
!= NULL
) {
965 fclose(preq
->packfile
);
966 preq
->packfile
= NULL
;
967 preq
->slot
->local
= NULL
;
969 if (preq
->range_header
!= NULL
) {
970 curl_slist_free_all(preq
->range_header
);
971 preq
->range_header
= NULL
;
977 int finish_http_pack_request(struct http_pack_request
*preq
)
980 struct packed_git
**lst
;
982 preq
->target
->pack_size
= ftell(preq
->packfile
);
984 if (preq
->packfile
!= NULL
) {
985 fclose(preq
->packfile
);
986 preq
->packfile
= NULL
;
987 preq
->slot
->local
= NULL
;
990 ret
= move_temp_to_file(preq
->tmpfile
, preq
->filename
);
995 while (*lst
!= preq
->target
)
996 lst
= &((*lst
)->next
);
999 if (verify_pack(preq
->target
))
1001 install_packed_git(preq
->target
);
1006 struct http_pack_request
*new_http_pack_request(
1007 struct packed_git
*target
, const char *base_url
)
1012 char range
[RANGE_HEADER_SIZE
];
1013 struct strbuf buf
= STRBUF_INIT
;
1014 struct http_pack_request
*preq
;
1016 preq
= xmalloc(sizeof(*preq
));
1017 preq
->target
= target
;
1018 preq
->range_header
= NULL
;
1020 end_url_with_slash(&buf
, base_url
);
1021 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
1022 sha1_to_hex(target
->sha1
));
1023 url
= strbuf_detach(&buf
, NULL
);
1024 preq
->url
= xstrdup(url
);
1026 filename
= sha1_pack_name(target
->sha1
);
1027 snprintf(preq
->filename
, sizeof(preq
->filename
), "%s", filename
);
1028 snprintf(preq
->tmpfile
, sizeof(preq
->tmpfile
), "%s.temp", filename
);
1029 preq
->packfile
= fopen(preq
->tmpfile
, "a");
1030 if (!preq
->packfile
) {
1031 error("Unable to open local file %s for pack",
1036 preq
->slot
= get_active_slot();
1037 preq
->slot
->local
= preq
->packfile
;
1038 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
1039 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1040 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, url
);
1041 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1045 * If there is data present from a previous transfer attempt,
1046 * resume where it left off
1048 prev_posn
= ftell(preq
->packfile
);
1050 if (http_is_verbose
)
1052 "Resuming fetch of pack %s at byte %ld\n",
1053 sha1_to_hex(target
->sha1
), prev_posn
);
1054 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1055 preq
->range_header
= curl_slist_append(NULL
, range
);
1056 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1057 preq
->range_header
);
1067 /* Helpers for fetching objects (loose) */
1068 static size_t fwrite_sha1_file(void *ptr
, size_t eltsize
, size_t nmemb
,
1071 unsigned char expn
[4096];
1072 size_t size
= eltsize
* nmemb
;
1074 struct http_object_request
*freq
=
1075 (struct http_object_request
*)data
;
1077 ssize_t retval
= xwrite(freq
->localfile
,
1078 (char *) ptr
+ posn
, size
- posn
);
1082 } while (posn
< size
);
1084 freq
->stream
.avail_in
= size
;
1085 freq
->stream
.next_in
= ptr
;
1087 freq
->stream
.next_out
= expn
;
1088 freq
->stream
.avail_out
= sizeof(expn
);
1089 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
1090 git_SHA1_Update(&freq
->c
, expn
,
1091 sizeof(expn
) - freq
->stream
.avail_out
);
1092 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
1097 struct http_object_request
*new_http_object_request(const char *base_url
,
1098 unsigned char *sha1
)
1100 char *hex
= sha1_to_hex(sha1
);
1102 char prevfile
[PATH_MAX
];
1105 unsigned char prev_buf
[PREV_BUF_SIZE
];
1106 ssize_t prev_read
= 0;
1108 char range
[RANGE_HEADER_SIZE
];
1109 struct curl_slist
*range_header
= NULL
;
1110 struct http_object_request
*freq
;
1112 freq
= xmalloc(sizeof(*freq
));
1113 hashcpy(freq
->sha1
, sha1
);
1114 freq
->localfile
= -1;
1116 filename
= sha1_file_name(sha1
);
1117 snprintf(freq
->filename
, sizeof(freq
->filename
), "%s", filename
);
1118 snprintf(freq
->tmpfile
, sizeof(freq
->tmpfile
),
1119 "%s.temp", filename
);
1121 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
);
1122 unlink_or_warn(prevfile
);
1123 rename(freq
->tmpfile
, prevfile
);
1124 unlink_or_warn(freq
->tmpfile
);
1126 if (freq
->localfile
!= -1)
1127 error("fd leakage in start: %d", freq
->localfile
);
1128 freq
->localfile
= open(freq
->tmpfile
,
1129 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1131 * This could have failed due to the "lazy directory creation";
1132 * try to mkdir the last path component.
1134 if (freq
->localfile
< 0 && errno
== ENOENT
) {
1135 char *dir
= strrchr(freq
->tmpfile
, '/');
1138 mkdir(freq
->tmpfile
, 0777);
1141 freq
->localfile
= open(freq
->tmpfile
,
1142 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1145 if (freq
->localfile
< 0) {
1146 error("Couldn't create temporary file %s for %s: %s",
1147 freq
->tmpfile
, freq
->filename
, strerror(errno
));
1151 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
1153 git_inflate_init(&freq
->stream
);
1155 git_SHA1_Init(&freq
->c
);
1157 url
= get_remote_object_url(base_url
, hex
, 0);
1158 freq
->url
= xstrdup(url
);
1161 * If a previous temp file is present, process what was already
1164 prevlocal
= open(prevfile
, O_RDONLY
);
1165 if (prevlocal
!= -1) {
1167 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
1169 if (fwrite_sha1_file(prev_buf
,
1172 freq
) == prev_read
) {
1173 prev_posn
+= prev_read
;
1178 } while (prev_read
> 0);
1181 unlink_or_warn(prevfile
);
1184 * Reset inflate/SHA1 if there was an error reading the previous temp
1185 * file; also rewind to the beginning of the local file.
1187 if (prev_read
== -1) {
1188 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
1189 git_inflate_init(&freq
->stream
);
1190 git_SHA1_Init(&freq
->c
);
1193 lseek(freq
->localfile
, 0, SEEK_SET
);
1194 ftruncate(freq
->localfile
, 0);
1198 freq
->slot
= get_active_slot();
1200 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
1201 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
1202 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
1203 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, url
);
1204 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
1207 * If we have successfully processed data from a previous fetch
1208 * attempt, only fetch the data we don't already have.
1211 if (http_is_verbose
)
1213 "Resuming fetch of object %s at byte %ld\n",
1215 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1216 range_header
= curl_slist_append(range_header
, range
);
1217 curl_easy_setopt(freq
->slot
->curl
,
1218 CURLOPT_HTTPHEADER
, range_header
);
1230 void process_http_object_request(struct http_object_request
*freq
)
1232 if (freq
->slot
== NULL
)
1234 freq
->curl_result
= freq
->slot
->curl_result
;
1235 freq
->http_code
= freq
->slot
->http_code
;
1239 int finish_http_object_request(struct http_object_request
*freq
)
1243 close(freq
->localfile
);
1244 freq
->localfile
= -1;
1246 process_http_object_request(freq
);
1248 if (freq
->http_code
== 416) {
1249 fprintf(stderr
, "Warning: requested range invalid; we may already have all the data.\n");
1250 } else if (freq
->curl_result
!= CURLE_OK
) {
1251 if (stat(freq
->tmpfile
, &st
) == 0)
1252 if (st
.st_size
== 0)
1253 unlink_or_warn(freq
->tmpfile
);
1257 git_inflate_end(&freq
->stream
);
1258 git_SHA1_Final(freq
->real_sha1
, &freq
->c
);
1259 if (freq
->zret
!= Z_STREAM_END
) {
1260 unlink_or_warn(freq
->tmpfile
);
1263 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
1264 unlink_or_warn(freq
->tmpfile
);
1268 move_temp_to_file(freq
->tmpfile
, freq
->filename
);
1270 return freq
->rename
;
1273 void abort_http_object_request(struct http_object_request
*freq
)
1275 unlink_or_warn(freq
->tmpfile
);
1277 release_http_object_request(freq
);
1280 void release_http_object_request(struct http_object_request
*freq
)
1282 if (freq
->localfile
!= -1) {
1283 close(freq
->localfile
);
1284 freq
->localfile
= -1;
1286 if (freq
->url
!= NULL
) {
1290 if (freq
->slot
!= NULL
) {
1291 freq
->slot
->callback_func
= NULL
;
1292 freq
->slot
->callback_data
= NULL
;
1293 release_active_slot(freq
->slot
);