4 #include "run-command.h"
11 size_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
13 #if LIBCURL_VERSION_NUM >= 0x070a06
14 #define LIBCURL_CAN_HANDLE_AUTH_ANY
17 static int min_curl_sessions
= 1;
18 static int curl_session_count
;
20 static int max_requests
= -1;
23 #ifndef NO_CURL_EASY_DUPHANDLE
24 static CURL
*curl_default
;
27 #define PREV_BUF_SIZE 4096
28 #define RANGE_HEADER_SIZE 30
30 char curl_errorstr
[CURL_ERROR_SIZE
];
32 static int curl_ssl_verify
= -1;
33 static const char *ssl_cert
;
34 #if LIBCURL_VERSION_NUM >= 0x070903
35 static const char *ssl_key
;
37 #if LIBCURL_VERSION_NUM >= 0x070908
38 static const char *ssl_capath
;
40 static const char *ssl_cainfo
;
41 static long curl_low_speed_limit
= -1;
42 static long curl_low_speed_time
= -1;
43 static int curl_ftp_no_epsv
;
44 static const char *curl_http_proxy
;
45 static const char *curl_cookie_file
;
46 static char *user_name
, *user_pass
, *description
;
47 static const char *user_agent
;
49 #if LIBCURL_VERSION_NUM >= 0x071700
50 /* Use CURLOPT_KEYPASSWD as is */
51 #elif LIBCURL_VERSION_NUM >= 0x070903
52 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
54 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
57 static char *ssl_cert_password
;
58 static int ssl_cert_password_required
;
60 static struct curl_slist
*pragma_header
;
61 static struct curl_slist
*no_pragma_header
;
63 static struct active_request_slot
*active_queue_head
;
65 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
67 size_t size
= eltsize
* nmemb
;
68 struct buffer
*buffer
= buffer_
;
70 if (size
> buffer
->buf
.len
- buffer
->posn
)
71 size
= buffer
->buf
.len
- buffer
->posn
;
72 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
79 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
81 struct buffer
*buffer
= clientp
;
87 case CURLIOCMD_RESTARTREAD
:
92 return CURLIOE_UNKNOWNCMD
;
97 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
99 size_t size
= eltsize
* nmemb
;
100 struct strbuf
*buffer
= buffer_
;
102 strbuf_add(buffer
, ptr
, size
);
107 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
110 return eltsize
* nmemb
;
113 #ifdef USE_CURL_MULTI
114 static void process_curl_messages(void)
117 struct active_request_slot
*slot
;
118 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
120 while (curl_message
!= NULL
) {
121 if (curl_message
->msg
== CURLMSG_DONE
) {
122 int curl_result
= curl_message
->data
.result
;
123 slot
= active_queue_head
;
124 while (slot
!= NULL
&&
125 slot
->curl
!= curl_message
->easy_handle
)
128 curl_multi_remove_handle(curlm
, slot
->curl
);
129 slot
->curl_result
= curl_result
;
130 finish_active_slot(slot
);
132 fprintf(stderr
, "Received DONE message for unknown request!\n");
135 fprintf(stderr
, "Unknown CURL message received: %d\n",
136 (int)curl_message
->msg
);
138 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
143 static char *git_getpass_with_description(const char *what
, const char *desc
)
145 struct strbuf prompt
= STRBUF_INIT
;
149 strbuf_addf(&prompt
, "%s for '%s': ", what
, desc
);
151 strbuf_addf(&prompt
, "%s: ", what
);
153 * NEEDSWORK: for usernames, we should do something less magical that
154 * actually echoes the characters. However, we need to read from
155 * /dev/tty and not stdio, which is not portable (but getpass will do
156 * it for us). http.c uses the same workaround.
158 r
= git_getpass(prompt
.buf
);
160 strbuf_release(&prompt
);
164 static int git_config_path(const char **result
,
165 const char *var
, const char *value
)
167 if (git_config_string(result
, var
, value
))
171 *result
= system_path((*result
) + 1);
176 static int http_options(const char *var
, const char *value
, void *cb
)
178 if (!strcmp("http.sslverify", var
)) {
179 curl_ssl_verify
= git_config_bool(var
, value
);
182 if (!strcmp("http.sslcert", var
))
183 return git_config_path(&ssl_cert
, var
, value
);
184 #if LIBCURL_VERSION_NUM >= 0x070903
185 if (!strcmp("http.sslkey", var
))
186 return git_config_path(&ssl_key
, var
, value
);
188 #if LIBCURL_VERSION_NUM >= 0x070908
189 if (!strcmp("http.sslcapath", var
))
190 return git_config_path(&ssl_capath
, var
, value
);
192 if (!strcmp("http.sslcainfo", var
))
193 return git_config_path(&ssl_cainfo
, var
, value
);
194 if (!strcmp("http.sslcertpasswordprotected", var
)) {
195 if (git_config_bool(var
, value
))
196 ssl_cert_password_required
= 1;
199 if (!strcmp("http.minsessions", var
)) {
200 min_curl_sessions
= git_config_int(var
, value
);
201 #ifndef USE_CURL_MULTI
202 if (min_curl_sessions
> 1)
203 min_curl_sessions
= 1;
207 #ifdef USE_CURL_MULTI
208 if (!strcmp("http.maxrequests", var
)) {
209 max_requests
= git_config_int(var
, value
);
213 if (!strcmp("http.lowspeedlimit", var
)) {
214 curl_low_speed_limit
= (long)git_config_int(var
, value
);
217 if (!strcmp("http.lowspeedtime", var
)) {
218 curl_low_speed_time
= (long)git_config_int(var
, value
);
222 if (!strcmp("http.noepsv", var
)) {
223 curl_ftp_no_epsv
= git_config_bool(var
, value
);
226 if (!strcmp("http.proxy", var
))
227 return git_config_string(&curl_http_proxy
, var
, value
);
229 if (!strcmp("http.cookiefile", var
))
230 return git_config_string(&curl_cookie_file
, var
, value
);
232 if (!strcmp("http.postbuffer", var
)) {
233 http_post_buffer
= git_config_int(var
, value
);
234 if (http_post_buffer
< LARGE_PACKET_MAX
)
235 http_post_buffer
= LARGE_PACKET_MAX
;
239 if (!strcmp("http.useragent", var
))
240 return git_config_string(&user_agent
, var
, value
);
242 /* Fall back on the default ones */
243 return git_default_config(var
, value
, cb
);
246 static void init_curl_http_auth(CURL
*result
)
249 struct strbuf up
= STRBUF_INIT
;
251 user_pass
= xstrdup(git_getpass_with_description("Password", description
));
252 strbuf_addf(&up
, "%s:%s", user_name
, user_pass
);
253 curl_easy_setopt(result
, CURLOPT_USERPWD
,
254 strbuf_detach(&up
, NULL
));
258 static int has_cert_password(void)
260 if (ssl_cert_password
!= NULL
)
262 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
264 /* Only prompt the user once. */
265 ssl_cert_password_required
= -1;
266 ssl_cert_password
= git_getpass_with_description("Certificate Password", description
);
267 if (ssl_cert_password
!= NULL
) {
268 ssl_cert_password
= xstrdup(ssl_cert_password
);
274 static CURL
*get_curl_handle(void)
276 CURL
*result
= curl_easy_init();
278 if (!curl_ssl_verify
) {
279 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
280 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
282 /* Verify authenticity of the peer's certificate */
283 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
284 /* The name in the cert must match whom we tried to connect */
285 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
288 #if LIBCURL_VERSION_NUM >= 0x070907
289 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
291 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
292 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
295 init_curl_http_auth(result
);
297 if (ssl_cert
!= NULL
)
298 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
299 if (has_cert_password())
300 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, ssl_cert_password
);
301 #if LIBCURL_VERSION_NUM >= 0x070903
303 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
305 #if LIBCURL_VERSION_NUM >= 0x070908
306 if (ssl_capath
!= NULL
)
307 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
309 if (ssl_cainfo
!= NULL
)
310 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
311 curl_easy_setopt(result
, CURLOPT_FAILONERROR
, 1);
313 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
314 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
315 curl_low_speed_limit
);
316 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
317 curl_low_speed_time
);
320 curl_easy_setopt(result
, CURLOPT_FOLLOWLOCATION
, 1);
321 #if LIBCURL_VERSION_NUM >= 0x071301
322 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
323 #elif LIBCURL_VERSION_NUM >= 0x071101
324 curl_easy_setopt(result
, CURLOPT_POST301
, 1);
327 if (getenv("GIT_CURL_VERBOSE"))
328 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
330 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
331 user_agent
? user_agent
: GIT_HTTP_USER_AGENT
);
333 if (curl_ftp_no_epsv
)
334 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
337 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
342 static void http_auth_init(const char *url
)
344 const char *at
, *colon
, *cp
, *slash
, *host
;
346 cp
= strstr(url
, "://");
351 * Ok, the URL looks like "proto://something". Which one?
352 * "proto://<user>:<pass>@<host>/...",
353 * "proto://<user>@<host>/...", or just
354 * "proto://<host>/..."?
357 at
= strchr(cp
, '@');
358 colon
= strchr(cp
, ':');
359 slash
= strchrnul(cp
, '/');
360 if (!at
|| slash
<= at
) {
361 /* No credentials, but we may have to ask for some later */
364 else if (!colon
|| at
<= colon
) {
366 user_name
= url_decode_mem(cp
, at
- cp
);
370 user_name
= url_decode_mem(cp
, colon
- cp
);
371 user_pass
= url_decode_mem(colon
+ 1, at
- (colon
+ 1));
375 description
= url_decode_mem(host
, slash
- host
);
378 static void set_from_env(const char **var
, const char *envname
)
380 const char *val
= getenv(envname
);
385 void http_init(struct remote
*remote
, const char *url
)
387 char *low_speed_limit
;
388 char *low_speed_time
;
392 git_config(http_options
, NULL
);
394 curl_global_init(CURL_GLOBAL_ALL
);
396 if (remote
&& remote
->http_proxy
)
397 curl_http_proxy
= xstrdup(remote
->http_proxy
);
399 pragma_header
= curl_slist_append(pragma_header
, "Pragma: no-cache");
400 no_pragma_header
= curl_slist_append(no_pragma_header
, "Pragma:");
402 #ifdef USE_CURL_MULTI
404 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
405 if (http_max_requests
!= NULL
)
406 max_requests
= atoi(http_max_requests
);
409 curlm
= curl_multi_init();
411 fprintf(stderr
, "Error creating curl multi handle.\n");
416 if (getenv("GIT_SSL_NO_VERIFY"))
419 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
420 #if LIBCURL_VERSION_NUM >= 0x070903
421 set_from_env(&ssl_key
, "GIT_SSL_KEY");
423 #if LIBCURL_VERSION_NUM >= 0x070908
424 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
426 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
428 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
430 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
431 if (low_speed_limit
!= NULL
)
432 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
433 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
434 if (low_speed_time
!= NULL
)
435 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
437 if (curl_ssl_verify
== -1)
440 curl_session_count
= 0;
441 #ifdef USE_CURL_MULTI
442 if (max_requests
< 1)
443 max_requests
= DEFAULT_MAX_REQUESTS
;
446 if (getenv("GIT_CURL_FTP_NO_EPSV"))
447 curl_ftp_no_epsv
= 1;
451 if (!ssl_cert_password_required
&&
452 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
453 !prefixcmp(url
, "https://"))
454 ssl_cert_password_required
= 1;
457 #ifndef NO_CURL_EASY_DUPHANDLE
458 curl_default
= get_curl_handle();
462 void http_cleanup(void)
464 struct active_request_slot
*slot
= active_queue_head
;
466 while (slot
!= NULL
) {
467 struct active_request_slot
*next
= slot
->next
;
468 if (slot
->curl
!= NULL
) {
469 #ifdef USE_CURL_MULTI
470 curl_multi_remove_handle(curlm
, slot
->curl
);
472 curl_easy_cleanup(slot
->curl
);
477 active_queue_head
= NULL
;
479 #ifndef NO_CURL_EASY_DUPHANDLE
480 curl_easy_cleanup(curl_default
);
483 #ifdef USE_CURL_MULTI
484 curl_multi_cleanup(curlm
);
486 curl_global_cleanup();
488 curl_slist_free_all(pragma_header
);
489 pragma_header
= NULL
;
491 curl_slist_free_all(no_pragma_header
);
492 no_pragma_header
= NULL
;
494 if (curl_http_proxy
) {
495 free((void *)curl_http_proxy
);
496 curl_http_proxy
= NULL
;
499 if (ssl_cert_password
!= NULL
) {
500 memset(ssl_cert_password
, 0, strlen(ssl_cert_password
));
501 free(ssl_cert_password
);
502 ssl_cert_password
= NULL
;
504 ssl_cert_password_required
= 0;
507 struct active_request_slot
*get_active_slot(void)
509 struct active_request_slot
*slot
= active_queue_head
;
510 struct active_request_slot
*newslot
;
512 #ifdef USE_CURL_MULTI
515 /* Wait for a slot to open up if the queue is full */
516 while (active_requests
>= max_requests
) {
517 curl_multi_perform(curlm
, &num_transfers
);
518 if (num_transfers
< active_requests
)
519 process_curl_messages();
523 while (slot
!= NULL
&& slot
->in_use
)
527 newslot
= xmalloc(sizeof(*newslot
));
528 newslot
->curl
= NULL
;
530 newslot
->next
= NULL
;
532 slot
= active_queue_head
;
534 active_queue_head
= newslot
;
536 while (slot
->next
!= NULL
)
538 slot
->next
= newslot
;
543 if (slot
->curl
== NULL
) {
544 #ifdef NO_CURL_EASY_DUPHANDLE
545 slot
->curl
= get_curl_handle();
547 slot
->curl
= curl_easy_duphandle(curl_default
);
549 curl_session_count
++;
555 slot
->results
= NULL
;
556 slot
->finished
= NULL
;
557 slot
->callback_data
= NULL
;
558 slot
->callback_func
= NULL
;
559 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
560 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
561 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
562 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
563 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
564 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
565 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
566 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
567 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
572 int start_active_slot(struct active_request_slot
*slot
)
574 #ifdef USE_CURL_MULTI
575 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
578 if (curlm_result
!= CURLM_OK
&&
579 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
586 * We know there must be something to do, since we just added
589 curl_multi_perform(curlm
, &num_transfers
);
594 #ifdef USE_CURL_MULTI
598 struct fill_chain
*next
;
601 static struct fill_chain
*fill_cfg
;
603 void add_fill_function(void *data
, int (*fill
)(void *))
605 struct fill_chain
*new = xmalloc(sizeof(*new));
606 struct fill_chain
**linkp
= &fill_cfg
;
611 linkp
= &(*linkp
)->next
;
615 void fill_active_slots(void)
617 struct active_request_slot
*slot
= active_queue_head
;
619 while (active_requests
< max_requests
) {
620 struct fill_chain
*fill
;
621 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
622 if (fill
->fill(fill
->data
))
629 while (slot
!= NULL
) {
630 if (!slot
->in_use
&& slot
->curl
!= NULL
631 && curl_session_count
> min_curl_sessions
) {
632 curl_easy_cleanup(slot
->curl
);
634 curl_session_count
--;
640 void step_active_slots(void)
643 CURLMcode curlm_result
;
646 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
647 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
648 if (num_transfers
< active_requests
) {
649 process_curl_messages();
655 void run_active_slot(struct active_request_slot
*slot
)
657 #ifdef USE_CURL_MULTI
664 struct timeval select_timeout
;
667 slot
->finished
= &finished
;
672 if (!data_received
&& slot
->local
!= NULL
) {
673 current_pos
= ftell(slot
->local
);
674 if (current_pos
> last_pos
)
676 last_pos
= current_pos
;
679 if (slot
->in_use
&& !data_received
) {
684 select_timeout
.tv_sec
= 0;
685 select_timeout
.tv_usec
= 50000;
686 select(max_fd
, &readfds
, &writefds
,
687 &excfds
, &select_timeout
);
691 while (slot
->in_use
) {
692 slot
->curl_result
= curl_easy_perform(slot
->curl
);
693 finish_active_slot(slot
);
698 static void closedown_active_slot(struct active_request_slot
*slot
)
704 static void release_active_slot(struct active_request_slot
*slot
)
706 closedown_active_slot(slot
);
707 if (slot
->curl
&& curl_session_count
> min_curl_sessions
) {
708 #ifdef USE_CURL_MULTI
709 curl_multi_remove_handle(curlm
, slot
->curl
);
711 curl_easy_cleanup(slot
->curl
);
713 curl_session_count
--;
715 #ifdef USE_CURL_MULTI
720 void finish_active_slot(struct active_request_slot
*slot
)
722 closedown_active_slot(slot
);
723 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
725 if (slot
->finished
!= NULL
)
726 (*slot
->finished
) = 1;
728 /* Store slot results so they can be read after the slot is reused */
729 if (slot
->results
!= NULL
) {
730 slot
->results
->curl_result
= slot
->curl_result
;
731 slot
->results
->http_code
= slot
->http_code
;
734 /* Run callback if appropriate */
735 if (slot
->callback_func
!= NULL
)
736 slot
->callback_func(slot
->callback_data
);
739 void finish_all_active_slots(void)
741 struct active_request_slot
*slot
= active_queue_head
;
745 run_active_slot(slot
);
746 slot
= active_queue_head
;
752 /* Helpers for modifying and creating URLs */
753 static inline int needs_quote(int ch
)
755 if (((ch
>= 'A') && (ch
<= 'Z'))
756 || ((ch
>= 'a') && (ch
<= 'z'))
757 || ((ch
>= '0') && (ch
<= '9'))
765 static inline int hex(int v
)
773 static char *quote_ref_url(const char *base
, const char *ref
)
775 struct strbuf buf
= STRBUF_INIT
;
779 end_url_with_slash(&buf
, base
);
781 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
783 strbuf_addf(&buf
, "%%%02x", ch
);
785 strbuf_addch(&buf
, *cp
);
787 return strbuf_detach(&buf
, NULL
);
790 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
792 int only_two_digit_prefix
)
794 end_url_with_slash(buf
, url
);
796 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
797 if (!only_two_digit_prefix
)
798 strbuf_addf(buf
, "%s", hex
+2);
801 char *get_remote_object_url(const char *url
, const char *hex
,
802 int only_two_digit_prefix
)
804 struct strbuf buf
= STRBUF_INIT
;
805 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
806 return strbuf_detach(&buf
, NULL
);
809 /* http_request() targets */
810 #define HTTP_REQUEST_STRBUF 0
811 #define HTTP_REQUEST_FILE 1
813 static int http_request(const char *url
, void *result
, int target
, int options
)
815 struct active_request_slot
*slot
;
816 struct slot_results results
;
817 struct curl_slist
*headers
= NULL
;
818 struct strbuf buf
= STRBUF_INIT
;
821 slot
= get_active_slot();
822 slot
->results
= &results
;
823 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
825 if (result
== NULL
) {
826 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
828 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
829 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
831 if (target
== HTTP_REQUEST_FILE
) {
832 long posn
= ftell(result
);
833 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
836 strbuf_addf(&buf
, "Range: bytes=%ld-", posn
);
837 headers
= curl_slist_append(headers
, buf
.buf
);
840 slot
->local
= result
;
842 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
846 strbuf_addstr(&buf
, "Pragma:");
847 if (options
& HTTP_NO_CACHE
)
848 strbuf_addstr(&buf
, " no-cache");
850 headers
= curl_slist_append(headers
, buf
.buf
);
852 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
853 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
855 if (start_active_slot(slot
)) {
856 run_active_slot(slot
);
857 if (results
.curl_result
== CURLE_OK
)
859 else if (missing_target(&results
))
860 ret
= HTTP_MISSING_TARGET
;
861 else if (results
.http_code
== 401) {
866 * git_getpass is needed here because its very likely stdin/stdout are
867 * pipes to our parent process. So we instead need to use /dev/tty,
868 * but that is non-portable. Using git_getpass() can at least be stubbed
869 * on other platforms with a different implementation if/when necessary.
871 user_name
= xstrdup(git_getpass_with_description("Username", description
));
872 init_curl_http_auth(slot
->curl
);
876 if (!curl_errorstr
[0])
877 strlcpy(curl_errorstr
,
878 curl_easy_strerror(results
.curl_result
),
879 sizeof(curl_errorstr
));
883 error("Unable to start HTTP request for %s", url
);
884 ret
= HTTP_START_FAILED
;
888 curl_slist_free_all(headers
);
889 strbuf_release(&buf
);
894 static int http_request_reauth(const char *url
, void *result
, int target
,
897 int ret
= http_request(url
, result
, target
, options
);
898 if (ret
!= HTTP_REAUTH
)
900 return http_request(url
, result
, target
, options
);
903 int http_get_strbuf(const char *url
, struct strbuf
*result
, int options
)
905 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
909 * Downloads an url and stores the result in the given file.
911 * If a previous interrupted download is detected (i.e. a previous temporary
912 * file is still around) the download is resumed.
914 static int http_get_file(const char *url
, const char *filename
, int options
)
917 struct strbuf tmpfile
= STRBUF_INIT
;
920 strbuf_addf(&tmpfile
, "%s.temp", filename
);
921 result
= fopen(tmpfile
.buf
, "a");
923 error("Unable to open local file %s", tmpfile
.buf
);
928 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
931 if ((ret
== HTTP_OK
) && move_temp_to_file(tmpfile
.buf
, filename
))
934 strbuf_release(&tmpfile
);
938 int http_error(const char *url
, int ret
)
940 /* http_request has already handled HTTP_START_FAILED. */
941 if (ret
!= HTTP_START_FAILED
)
942 error("%s while accessing %s", curl_errorstr
, url
);
947 int http_fetch_ref(const char *base
, struct ref
*ref
)
950 struct strbuf buffer
= STRBUF_INIT
;
953 url
= quote_ref_url(base
, ref
->name
);
954 if (http_get_strbuf(url
, &buffer
, HTTP_NO_CACHE
) == HTTP_OK
) {
955 strbuf_rtrim(&buffer
);
956 if (buffer
.len
== 40)
957 ret
= get_sha1_hex(buffer
.buf
, ref
->old_sha1
);
958 else if (!prefixcmp(buffer
.buf
, "ref: ")) {
959 ref
->symref
= xstrdup(buffer
.buf
+ 5);
964 strbuf_release(&buffer
);
969 /* Helpers for fetching packs */
970 static char *fetch_pack_index(unsigned char *sha1
, const char *base_url
)
973 struct strbuf buf
= STRBUF_INIT
;
976 fprintf(stderr
, "Getting index for pack %s\n", sha1_to_hex(sha1
));
978 end_url_with_slash(&buf
, base_url
);
979 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", sha1_to_hex(sha1
));
980 url
= strbuf_detach(&buf
, NULL
);
982 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(sha1
));
983 tmp
= strbuf_detach(&buf
, NULL
);
985 if (http_get_file(url
, tmp
, 0) != HTTP_OK
) {
986 error("Unable to get pack index %s\n", url
);
995 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
996 unsigned char *sha1
, const char *base_url
)
998 struct packed_git
*new_pack
;
999 char *tmp_idx
= NULL
;
1002 if (has_pack_index(sha1
)) {
1003 new_pack
= parse_pack_index(sha1
, NULL
);
1005 return -1; /* parse_pack_index() already issued error message */
1009 tmp_idx
= fetch_pack_index(sha1
, base_url
);
1013 new_pack
= parse_pack_index(sha1
, tmp_idx
);
1018 return -1; /* parse_pack_index() already issued error message */
1021 ret
= verify_pack_index(new_pack
);
1023 close_pack_index(new_pack
);
1024 ret
= move_temp_to_file(tmp_idx
, sha1_pack_index_name(sha1
));
1031 new_pack
->next
= *packs_head
;
1032 *packs_head
= new_pack
;
1036 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
1040 struct strbuf buf
= STRBUF_INIT
;
1041 unsigned char sha1
[20];
1043 end_url_with_slash(&buf
, base_url
);
1044 strbuf_addstr(&buf
, "objects/info/packs");
1045 url
= strbuf_detach(&buf
, NULL
);
1047 ret
= http_get_strbuf(url
, &buf
, HTTP_NO_CACHE
);
1052 while (i
< buf
.len
) {
1056 if (i
+ 52 <= buf
.len
&&
1057 !prefixcmp(data
+ i
, " pack-") &&
1058 !prefixcmp(data
+ i
+ 46, ".pack\n")) {
1059 get_sha1_hex(data
+ i
+ 6, sha1
);
1060 fetch_and_setup_pack_index(packs_head
, sha1
,
1066 while (i
< buf
.len
&& data
[i
] != '\n')
1077 void release_http_pack_request(struct http_pack_request
*preq
)
1079 if (preq
->packfile
!= NULL
) {
1080 fclose(preq
->packfile
);
1081 preq
->packfile
= NULL
;
1082 preq
->slot
->local
= NULL
;
1084 if (preq
->range_header
!= NULL
) {
1085 curl_slist_free_all(preq
->range_header
);
1086 preq
->range_header
= NULL
;
1092 int finish_http_pack_request(struct http_pack_request
*preq
)
1094 struct packed_git
**lst
;
1095 struct packed_git
*p
= preq
->target
;
1097 struct child_process ip
;
1098 const char *ip_argv
[8];
1100 close_pack_index(p
);
1102 fclose(preq
->packfile
);
1103 preq
->packfile
= NULL
;
1104 preq
->slot
->local
= NULL
;
1108 lst
= &((*lst
)->next
);
1109 *lst
= (*lst
)->next
;
1111 tmp_idx
= xstrdup(preq
->tmpfile
);
1112 strcpy(tmp_idx
+ strlen(tmp_idx
) - strlen(".pack.temp"),
1115 ip_argv
[0] = "index-pack";
1117 ip_argv
[2] = tmp_idx
;
1118 ip_argv
[3] = preq
->tmpfile
;
1121 memset(&ip
, 0, sizeof(ip
));
1127 if (run_command(&ip
)) {
1128 unlink(preq
->tmpfile
);
1134 unlink(sha1_pack_index_name(p
->sha1
));
1136 if (move_temp_to_file(preq
->tmpfile
, sha1_pack_name(p
->sha1
))
1137 || move_temp_to_file(tmp_idx
, sha1_pack_index_name(p
->sha1
))) {
1142 install_packed_git(p
);
1147 struct http_pack_request
*new_http_pack_request(
1148 struct packed_git
*target
, const char *base_url
)
1151 char range
[RANGE_HEADER_SIZE
];
1152 struct strbuf buf
= STRBUF_INIT
;
1153 struct http_pack_request
*preq
;
1155 preq
= xcalloc(1, sizeof(*preq
));
1156 preq
->target
= target
;
1158 end_url_with_slash(&buf
, base_url
);
1159 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
1160 sha1_to_hex(target
->sha1
));
1161 preq
->url
= strbuf_detach(&buf
, NULL
);
1163 snprintf(preq
->tmpfile
, sizeof(preq
->tmpfile
), "%s.temp",
1164 sha1_pack_name(target
->sha1
));
1165 preq
->packfile
= fopen(preq
->tmpfile
, "a");
1166 if (!preq
->packfile
) {
1167 error("Unable to open local file %s for pack",
1172 preq
->slot
= get_active_slot();
1173 preq
->slot
->local
= preq
->packfile
;
1174 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
1175 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1176 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
1177 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1181 * If there is data present from a previous transfer attempt,
1182 * resume where it left off
1184 prev_posn
= ftell(preq
->packfile
);
1186 if (http_is_verbose
)
1188 "Resuming fetch of pack %s at byte %ld\n",
1189 sha1_to_hex(target
->sha1
), prev_posn
);
1190 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1191 preq
->range_header
= curl_slist_append(NULL
, range
);
1192 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1193 preq
->range_header
);
1204 /* Helpers for fetching objects (loose) */
1205 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
1208 unsigned char expn
[4096];
1209 size_t size
= eltsize
* nmemb
;
1211 struct http_object_request
*freq
=
1212 (struct http_object_request
*)data
;
1214 ssize_t retval
= xwrite(freq
->localfile
,
1215 (char *) ptr
+ posn
, size
- posn
);
1219 } while (posn
< size
);
1221 freq
->stream
.avail_in
= size
;
1222 freq
->stream
.next_in
= (void *)ptr
;
1224 freq
->stream
.next_out
= expn
;
1225 freq
->stream
.avail_out
= sizeof(expn
);
1226 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
1227 git_SHA1_Update(&freq
->c
, expn
,
1228 sizeof(expn
) - freq
->stream
.avail_out
);
1229 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
1234 struct http_object_request
*new_http_object_request(const char *base_url
,
1235 unsigned char *sha1
)
1237 char *hex
= sha1_to_hex(sha1
);
1239 char prevfile
[PATH_MAX
];
1241 char prev_buf
[PREV_BUF_SIZE
];
1242 ssize_t prev_read
= 0;
1244 char range
[RANGE_HEADER_SIZE
];
1245 struct curl_slist
*range_header
= NULL
;
1246 struct http_object_request
*freq
;
1248 freq
= xcalloc(1, sizeof(*freq
));
1249 hashcpy(freq
->sha1
, sha1
);
1250 freq
->localfile
= -1;
1252 filename
= sha1_file_name(sha1
);
1253 snprintf(freq
->tmpfile
, sizeof(freq
->tmpfile
),
1254 "%s.temp", filename
);
1256 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
);
1257 unlink_or_warn(prevfile
);
1258 rename(freq
->tmpfile
, prevfile
);
1259 unlink_or_warn(freq
->tmpfile
);
1261 if (freq
->localfile
!= -1)
1262 error("fd leakage in start: %d", freq
->localfile
);
1263 freq
->localfile
= open(freq
->tmpfile
,
1264 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1266 * This could have failed due to the "lazy directory creation";
1267 * try to mkdir the last path component.
1269 if (freq
->localfile
< 0 && errno
== ENOENT
) {
1270 char *dir
= strrchr(freq
->tmpfile
, '/');
1273 mkdir(freq
->tmpfile
, 0777);
1276 freq
->localfile
= open(freq
->tmpfile
,
1277 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1280 if (freq
->localfile
< 0) {
1281 error("Couldn't create temporary file %s: %s",
1282 freq
->tmpfile
, strerror(errno
));
1286 git_inflate_init(&freq
->stream
);
1288 git_SHA1_Init(&freq
->c
);
1290 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
1293 * If a previous temp file is present, process what was already
1296 prevlocal
= open(prevfile
, O_RDONLY
);
1297 if (prevlocal
!= -1) {
1299 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
1301 if (fwrite_sha1_file(prev_buf
,
1304 freq
) == prev_read
) {
1305 prev_posn
+= prev_read
;
1310 } while (prev_read
> 0);
1313 unlink_or_warn(prevfile
);
1316 * Reset inflate/SHA1 if there was an error reading the previous temp
1317 * file; also rewind to the beginning of the local file.
1319 if (prev_read
== -1) {
1320 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
1321 git_inflate_init(&freq
->stream
);
1322 git_SHA1_Init(&freq
->c
);
1325 lseek(freq
->localfile
, 0, SEEK_SET
);
1326 if (ftruncate(freq
->localfile
, 0) < 0) {
1327 error("Couldn't truncate temporary file %s: %s",
1328 freq
->tmpfile
, strerror(errno
));
1334 freq
->slot
= get_active_slot();
1336 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
1337 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
1338 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
1339 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
1340 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
1343 * If we have successfully processed data from a previous fetch
1344 * attempt, only fetch the data we don't already have.
1347 if (http_is_verbose
)
1349 "Resuming fetch of object %s at byte %ld\n",
1351 sprintf(range
, "Range: bytes=%ld-", prev_posn
);
1352 range_header
= curl_slist_append(range_header
, range
);
1353 curl_easy_setopt(freq
->slot
->curl
,
1354 CURLOPT_HTTPHEADER
, range_header
);
1365 void process_http_object_request(struct http_object_request
*freq
)
1367 if (freq
->slot
== NULL
)
1369 freq
->curl_result
= freq
->slot
->curl_result
;
1370 freq
->http_code
= freq
->slot
->http_code
;
1374 int finish_http_object_request(struct http_object_request
*freq
)
1378 close(freq
->localfile
);
1379 freq
->localfile
= -1;
1381 process_http_object_request(freq
);
1383 if (freq
->http_code
== 416) {
1384 warning("requested range invalid; we may already have all the data.");
1385 } else if (freq
->curl_result
!= CURLE_OK
) {
1386 if (stat(freq
->tmpfile
, &st
) == 0)
1387 if (st
.st_size
== 0)
1388 unlink_or_warn(freq
->tmpfile
);
1392 git_inflate_end(&freq
->stream
);
1393 git_SHA1_Final(freq
->real_sha1
, &freq
->c
);
1394 if (freq
->zret
!= Z_STREAM_END
) {
1395 unlink_or_warn(freq
->tmpfile
);
1398 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
1399 unlink_or_warn(freq
->tmpfile
);
1403 move_temp_to_file(freq
->tmpfile
, sha1_file_name(freq
->sha1
));
1405 return freq
->rename
;
1408 void abort_http_object_request(struct http_object_request
*freq
)
1410 unlink_or_warn(freq
->tmpfile
);
1412 release_http_object_request(freq
);
1415 void release_http_object_request(struct http_object_request
*freq
)
1417 if (freq
->localfile
!= -1) {
1418 close(freq
->localfile
);
1419 freq
->localfile
= -1;
1421 if (freq
->url
!= NULL
) {
1425 if (freq
->slot
!= NULL
) {
1426 freq
->slot
->callback_func
= NULL
;
1427 freq
->slot
->callback_data
= NULL
;
1428 release_active_slot(freq
->slot
);