1 #include "git-compat-util.h"
5 #include "run-command.h"
8 #include "credential.h"
12 #include "transport.h"
14 #if LIBCURL_VERSION_NUM >= 0x070a08
15 long int git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
17 long int git_curl_ipresolve
;
21 size_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
23 #if LIBCURL_VERSION_NUM >= 0x070a06
24 #define LIBCURL_CAN_HANDLE_AUTH_ANY
27 static int min_curl_sessions
= 1;
28 static int curl_session_count
;
30 static int max_requests
= -1;
33 #ifndef NO_CURL_EASY_DUPHANDLE
34 static CURL
*curl_default
;
37 #define PREV_BUF_SIZE 4096
39 char curl_errorstr
[CURL_ERROR_SIZE
];
41 static int curl_ssl_verify
= -1;
42 static int curl_ssl_try
;
43 static const char *ssl_cert
;
44 static const char *ssl_cipherlist
;
45 static const char *ssl_version
;
50 { "sslv2", CURL_SSLVERSION_SSLv2
},
51 { "sslv3", CURL_SSLVERSION_SSLv3
},
52 { "tlsv1", CURL_SSLVERSION_TLSv1
},
53 #if LIBCURL_VERSION_NUM >= 0x072200
54 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0
},
55 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1
},
56 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2
},
59 #if LIBCURL_VERSION_NUM >= 0x070903
60 static const char *ssl_key
;
62 #if LIBCURL_VERSION_NUM >= 0x070908
63 static const char *ssl_capath
;
65 #if LIBCURL_VERSION_NUM >= 0x072c00
66 static const char *ssl_pinnedkey
;
68 static const char *ssl_cainfo
;
69 static long curl_low_speed_limit
= -1;
70 static long curl_low_speed_time
= -1;
71 static int curl_ftp_no_epsv
;
72 static const char *curl_http_proxy
;
73 static const char *curl_no_proxy
;
74 static const char *http_proxy_authmethod
;
78 } proxy_authmethods
[] = {
79 { "basic", CURLAUTH_BASIC
},
80 { "digest", CURLAUTH_DIGEST
},
81 { "negotiate", CURLAUTH_GSSNEGOTIATE
},
82 { "ntlm", CURLAUTH_NTLM
},
83 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
84 { "anyauth", CURLAUTH_ANY
},
87 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
88 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
92 static struct credential proxy_auth
= CREDENTIAL_INIT
;
93 static const char *curl_proxyuserpwd
;
94 static const char *curl_cookie_file
;
95 static int curl_save_cookies
;
96 struct credential http_auth
= CREDENTIAL_INIT
;
97 static int http_proactive_auth
;
98 static const char *user_agent
;
99 static int curl_empty_auth
;
101 enum http_follow_config http_follow_config
= HTTP_FOLLOW_INITIAL
;
103 #if LIBCURL_VERSION_NUM >= 0x071700
104 /* Use CURLOPT_KEYPASSWD as is */
105 #elif LIBCURL_VERSION_NUM >= 0x070903
106 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
108 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
111 static struct credential cert_auth
= CREDENTIAL_INIT
;
112 static int ssl_cert_password_required
;
113 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
114 static unsigned long http_auth_methods
= CURLAUTH_ANY
;
117 static struct curl_slist
*pragma_header
;
118 static struct curl_slist
*no_pragma_header
;
119 static struct curl_slist
*extra_http_headers
;
121 static struct active_request_slot
*active_queue_head
;
123 static char *cached_accept_language
;
125 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
127 size_t size
= eltsize
* nmemb
;
128 struct buffer
*buffer
= buffer_
;
130 if (size
> buffer
->buf
.len
- buffer
->posn
)
131 size
= buffer
->buf
.len
- buffer
->posn
;
132 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
133 buffer
->posn
+= size
;
138 #ifndef NO_CURL_IOCTL
139 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
141 struct buffer
*buffer
= clientp
;
147 case CURLIOCMD_RESTARTREAD
:
152 return CURLIOE_UNKNOWNCMD
;
157 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
159 size_t size
= eltsize
* nmemb
;
160 struct strbuf
*buffer
= buffer_
;
162 strbuf_add(buffer
, ptr
, size
);
166 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
168 return eltsize
* nmemb
;
171 static void closedown_active_slot(struct active_request_slot
*slot
)
177 static void finish_active_slot(struct active_request_slot
*slot
)
179 closedown_active_slot(slot
);
180 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
182 if (slot
->finished
!= NULL
)
183 (*slot
->finished
) = 1;
185 /* Store slot results so they can be read after the slot is reused */
186 if (slot
->results
!= NULL
) {
187 slot
->results
->curl_result
= slot
->curl_result
;
188 slot
->results
->http_code
= slot
->http_code
;
189 #if LIBCURL_VERSION_NUM >= 0x070a08
190 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
191 &slot
->results
->auth_avail
);
193 slot
->results
->auth_avail
= 0;
196 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CONNECTCODE
,
197 &slot
->results
->http_connectcode
);
200 /* Run callback if appropriate */
201 if (slot
->callback_func
!= NULL
)
202 slot
->callback_func(slot
->callback_data
);
205 #ifdef USE_CURL_MULTI
206 static void process_curl_messages(void)
209 struct active_request_slot
*slot
;
210 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
212 while (curl_message
!= NULL
) {
213 if (curl_message
->msg
== CURLMSG_DONE
) {
214 int curl_result
= curl_message
->data
.result
;
215 slot
= active_queue_head
;
216 while (slot
!= NULL
&&
217 slot
->curl
!= curl_message
->easy_handle
)
220 curl_multi_remove_handle(curlm
, slot
->curl
);
221 slot
->curl_result
= curl_result
;
222 finish_active_slot(slot
);
224 fprintf(stderr
, "Received DONE message for unknown request!\n");
227 fprintf(stderr
, "Unknown CURL message received: %d\n",
228 (int)curl_message
->msg
);
230 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
235 static int http_options(const char *var
, const char *value
, void *cb
)
237 if (!strcmp("http.sslverify", var
)) {
238 curl_ssl_verify
= git_config_bool(var
, value
);
241 if (!strcmp("http.sslcipherlist", var
))
242 return git_config_string(&ssl_cipherlist
, var
, value
);
243 if (!strcmp("http.sslversion", var
))
244 return git_config_string(&ssl_version
, var
, value
);
245 if (!strcmp("http.sslcert", var
))
246 return git_config_string(&ssl_cert
, var
, value
);
247 #if LIBCURL_VERSION_NUM >= 0x070903
248 if (!strcmp("http.sslkey", var
))
249 return git_config_string(&ssl_key
, var
, value
);
251 #if LIBCURL_VERSION_NUM >= 0x070908
252 if (!strcmp("http.sslcapath", var
))
253 return git_config_pathname(&ssl_capath
, var
, value
);
255 if (!strcmp("http.sslcainfo", var
))
256 return git_config_pathname(&ssl_cainfo
, var
, value
);
257 if (!strcmp("http.sslcertpasswordprotected", var
)) {
258 ssl_cert_password_required
= git_config_bool(var
, value
);
261 if (!strcmp("http.ssltry", var
)) {
262 curl_ssl_try
= git_config_bool(var
, value
);
265 if (!strcmp("http.minsessions", var
)) {
266 min_curl_sessions
= git_config_int(var
, value
);
267 #ifndef USE_CURL_MULTI
268 if (min_curl_sessions
> 1)
269 min_curl_sessions
= 1;
273 #ifdef USE_CURL_MULTI
274 if (!strcmp("http.maxrequests", var
)) {
275 max_requests
= git_config_int(var
, value
);
279 if (!strcmp("http.lowspeedlimit", var
)) {
280 curl_low_speed_limit
= (long)git_config_int(var
, value
);
283 if (!strcmp("http.lowspeedtime", var
)) {
284 curl_low_speed_time
= (long)git_config_int(var
, value
);
288 if (!strcmp("http.noepsv", var
)) {
289 curl_ftp_no_epsv
= git_config_bool(var
, value
);
292 if (!strcmp("http.proxy", var
))
293 return git_config_string(&curl_http_proxy
, var
, value
);
295 if (!strcmp("http.proxyauthmethod", var
))
296 return git_config_string(&http_proxy_authmethod
, var
, value
);
298 if (!strcmp("http.cookiefile", var
))
299 return git_config_pathname(&curl_cookie_file
, var
, value
);
300 if (!strcmp("http.savecookies", var
)) {
301 curl_save_cookies
= git_config_bool(var
, value
);
305 if (!strcmp("http.postbuffer", var
)) {
306 http_post_buffer
= git_config_int(var
, value
);
307 if (http_post_buffer
< LARGE_PACKET_MAX
)
308 http_post_buffer
= LARGE_PACKET_MAX
;
312 if (!strcmp("http.useragent", var
))
313 return git_config_string(&user_agent
, var
, value
);
315 if (!strcmp("http.emptyauth", var
)) {
316 curl_empty_auth
= git_config_bool(var
, value
);
320 if (!strcmp("http.pinnedpubkey", var
)) {
321 #if LIBCURL_VERSION_NUM >= 0x072c00
322 return git_config_pathname(&ssl_pinnedkey
, var
, value
);
324 warning(_("Public key pinning not supported with cURL < 7.44.0"));
329 if (!strcmp("http.extraheader", var
)) {
331 return config_error_nonbool(var
);
332 } else if (!*value
) {
333 curl_slist_free_all(extra_http_headers
);
334 extra_http_headers
= NULL
;
337 curl_slist_append(extra_http_headers
, value
);
342 if (!strcmp("http.followredirects", var
)) {
343 if (value
&& !strcmp(value
, "initial"))
344 http_follow_config
= HTTP_FOLLOW_INITIAL
;
345 else if (git_config_bool(var
, value
))
346 http_follow_config
= HTTP_FOLLOW_ALWAYS
;
348 http_follow_config
= HTTP_FOLLOW_NONE
;
352 /* Fall back on the default ones */
353 return git_default_config(var
, value
, cb
);
356 static void init_curl_http_auth(CURL
*result
)
358 if (!http_auth
.username
) {
360 curl_easy_setopt(result
, CURLOPT_USERPWD
, ":");
364 credential_fill(&http_auth
);
366 #if LIBCURL_VERSION_NUM >= 0x071301
367 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
368 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
371 static struct strbuf up
= STRBUF_INIT
;
373 * Note that we assume we only ever have a single set of
374 * credentials in a given program run, so we do not have
375 * to worry about updating this buffer, only setting its
379 strbuf_addf(&up
, "%s:%s",
380 http_auth
.username
, http_auth
.password
);
381 curl_easy_setopt(result
, CURLOPT_USERPWD
, up
.buf
);
386 /* *var must be free-able */
387 static void var_override(const char **var
, char *value
)
391 *var
= xstrdup(value
);
395 static void set_proxyauth_name_password(CURL
*result
)
397 #if LIBCURL_VERSION_NUM >= 0x071301
398 curl_easy_setopt(result
, CURLOPT_PROXYUSERNAME
,
399 proxy_auth
.username
);
400 curl_easy_setopt(result
, CURLOPT_PROXYPASSWORD
,
401 proxy_auth
.password
);
403 struct strbuf s
= STRBUF_INIT
;
405 strbuf_addstr_urlencode(&s
, proxy_auth
.username
, 1);
406 strbuf_addch(&s
, ':');
407 strbuf_addstr_urlencode(&s
, proxy_auth
.password
, 1);
408 curl_proxyuserpwd
= strbuf_detach(&s
, NULL
);
409 curl_easy_setopt(result
, CURLOPT_PROXYUSERPWD
, curl_proxyuserpwd
);
413 static void init_curl_proxy_auth(CURL
*result
)
415 if (proxy_auth
.username
) {
416 if (!proxy_auth
.password
)
417 credential_fill(&proxy_auth
);
418 set_proxyauth_name_password(result
);
421 var_override(&http_proxy_authmethod
, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
423 #if LIBCURL_VERSION_NUM >= 0x070a07 /* CURLOPT_PROXYAUTH and CURLAUTH_ANY */
424 if (http_proxy_authmethod
) {
426 for (i
= 0; i
< ARRAY_SIZE(proxy_authmethods
); i
++) {
427 if (!strcmp(http_proxy_authmethod
, proxy_authmethods
[i
].name
)) {
428 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
,
429 proxy_authmethods
[i
].curlauth_param
);
433 if (i
== ARRAY_SIZE(proxy_authmethods
)) {
434 warning("unsupported proxy authentication method %s: using anyauth",
435 http_proxy_authmethod
);
436 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
440 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
444 static int has_cert_password(void)
446 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
448 if (!cert_auth
.password
) {
449 cert_auth
.protocol
= xstrdup("cert");
450 cert_auth
.username
= xstrdup("");
451 cert_auth
.path
= xstrdup(ssl_cert
);
452 credential_fill(&cert_auth
);
457 #if LIBCURL_VERSION_NUM >= 0x071900
458 static void set_curl_keepalive(CURL
*c
)
460 curl_easy_setopt(c
, CURLOPT_TCP_KEEPALIVE
, 1);
463 #elif LIBCURL_VERSION_NUM >= 0x071000
464 static int sockopt_callback(void *client
, curl_socket_t fd
, curlsocktype type
)
468 socklen_t len
= (socklen_t
)sizeof(ka
);
470 if (type
!= CURLSOCKTYPE_IPCXN
)
473 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
475 warning_errno("unable to set SO_KEEPALIVE on socket");
477 return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
480 static void set_curl_keepalive(CURL
*c
)
482 curl_easy_setopt(c
, CURLOPT_SOCKOPTFUNCTION
, sockopt_callback
);
486 static void set_curl_keepalive(CURL
*c
)
488 /* not supported on older curl versions */
492 static CURL
*get_curl_handle(void)
494 CURL
*result
= curl_easy_init();
495 long allowed_protocols
= 0;
498 die("curl_easy_init failed");
500 if (!curl_ssl_verify
) {
501 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
502 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
504 /* Verify authenticity of the peer's certificate */
505 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
506 /* The name in the cert must match whom we tried to connect */
507 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
510 #if LIBCURL_VERSION_NUM >= 0x070907
511 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
513 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
514 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
517 if (http_proactive_auth
)
518 init_curl_http_auth(result
);
520 if (getenv("GIT_SSL_VERSION"))
521 ssl_version
= getenv("GIT_SSL_VERSION");
522 if (ssl_version
&& *ssl_version
) {
524 for (i
= 0; i
< ARRAY_SIZE(sslversions
); i
++) {
525 if (!strcmp(ssl_version
, sslversions
[i
].name
)) {
526 curl_easy_setopt(result
, CURLOPT_SSLVERSION
,
527 sslversions
[i
].ssl_version
);
531 if (i
== ARRAY_SIZE(sslversions
))
532 warning("unsupported ssl version %s: using default",
536 if (getenv("GIT_SSL_CIPHER_LIST"))
537 ssl_cipherlist
= getenv("GIT_SSL_CIPHER_LIST");
538 if (ssl_cipherlist
!= NULL
&& *ssl_cipherlist
)
539 curl_easy_setopt(result
, CURLOPT_SSL_CIPHER_LIST
,
542 if (ssl_cert
!= NULL
)
543 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
544 if (has_cert_password())
545 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
546 #if LIBCURL_VERSION_NUM >= 0x070903
548 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
550 #if LIBCURL_VERSION_NUM >= 0x070908
551 if (ssl_capath
!= NULL
)
552 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
554 #if LIBCURL_VERSION_NUM >= 0x072c00
555 if (ssl_pinnedkey
!= NULL
)
556 curl_easy_setopt(result
, CURLOPT_PINNEDPUBLICKEY
, ssl_pinnedkey
);
558 if (ssl_cainfo
!= NULL
)
559 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
561 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
562 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
563 curl_low_speed_limit
);
564 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
565 curl_low_speed_time
);
568 curl_easy_setopt(result
, CURLOPT_MAXREDIRS
, 20);
569 #if LIBCURL_VERSION_NUM >= 0x071301
570 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
571 #elif LIBCURL_VERSION_NUM >= 0x071101
572 curl_easy_setopt(result
, CURLOPT_POST301
, 1);
574 #if LIBCURL_VERSION_NUM >= 0x071304
575 if (is_transport_allowed("http"))
576 allowed_protocols
|= CURLPROTO_HTTP
;
577 if (is_transport_allowed("https"))
578 allowed_protocols
|= CURLPROTO_HTTPS
;
579 if (is_transport_allowed("ftp"))
580 allowed_protocols
|= CURLPROTO_FTP
;
581 if (is_transport_allowed("ftps"))
582 allowed_protocols
|= CURLPROTO_FTPS
;
583 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS
, allowed_protocols
);
585 if (transport_restrict_protocols())
586 warning("protocol restrictions not applied to curl redirects because\n"
587 "your curl version is too old (>= 7.19.4)");
590 if (getenv("GIT_CURL_VERBOSE"))
591 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1);
593 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
594 user_agent
? user_agent
: git_user_agent());
596 if (curl_ftp_no_epsv
)
597 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
599 #ifdef CURLOPT_USE_SSL
601 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
605 * CURL also examines these variables as a fallback; but we need to query
606 * them here in order to decide whether to prompt for missing password (cf.
607 * init_curl_proxy_auth()).
609 * Unlike many other common environment variables, these are historically
610 * lowercase only. It appears that CURL did not know this and implemented
611 * only uppercase variants, which was later corrected to take both - with
612 * the exception of http_proxy, which is lowercase only also in CURL. As
613 * the lowercase versions are the historical quasi-standard, they take
614 * precedence here, as in CURL.
616 if (!curl_http_proxy
) {
617 if (!strcmp(http_auth
.protocol
, "https")) {
618 var_override(&curl_http_proxy
, getenv("HTTPS_PROXY"));
619 var_override(&curl_http_proxy
, getenv("https_proxy"));
621 var_override(&curl_http_proxy
, getenv("http_proxy"));
623 if (!curl_http_proxy
) {
624 var_override(&curl_http_proxy
, getenv("ALL_PROXY"));
625 var_override(&curl_http_proxy
, getenv("all_proxy"));
629 if (curl_http_proxy
) {
630 curl_easy_setopt(result
, CURLOPT_PROXY
, curl_http_proxy
);
631 #if LIBCURL_VERSION_NUM >= 0x071800
632 if (starts_with(curl_http_proxy
, "socks5h"))
633 curl_easy_setopt(result
,
634 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5_HOSTNAME
);
635 else if (starts_with(curl_http_proxy
, "socks5"))
636 curl_easy_setopt(result
,
637 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
638 else if (starts_with(curl_http_proxy
, "socks4a"))
639 curl_easy_setopt(result
,
640 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4A
);
641 else if (starts_with(curl_http_proxy
, "socks"))
642 curl_easy_setopt(result
,
643 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4
);
645 if (strstr(curl_http_proxy
, "://"))
646 credential_from_url(&proxy_auth
, curl_http_proxy
);
648 struct strbuf url
= STRBUF_INIT
;
649 strbuf_addf(&url
, "http://%s", curl_http_proxy
);
650 credential_from_url(&proxy_auth
, url
.buf
);
651 strbuf_release(&url
);
654 curl_easy_setopt(result
, CURLOPT_PROXY
, proxy_auth
.host
);
655 #if LIBCURL_VERSION_NUM >= 0x071304
656 var_override(&curl_no_proxy
, getenv("NO_PROXY"));
657 var_override(&curl_no_proxy
, getenv("no_proxy"));
658 curl_easy_setopt(result
, CURLOPT_NOPROXY
, curl_no_proxy
);
661 init_curl_proxy_auth(result
);
663 set_curl_keepalive(result
);
668 static void set_from_env(const char **var
, const char *envname
)
670 const char *val
= getenv(envname
);
675 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
677 char *low_speed_limit
;
678 char *low_speed_time
;
679 char *normalized_url
;
680 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
682 config
.section
= "http";
684 config
.collect_fn
= http_options
;
685 config
.cascade_fn
= git_default_config
;
689 normalized_url
= url_normalize(url
, &config
.url
);
691 git_config(urlmatch_config_entry
, &config
);
692 free(normalized_url
);
694 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
695 die("curl_global_init failed");
697 http_proactive_auth
= proactive_auth
;
699 if (remote
&& remote
->http_proxy
)
700 curl_http_proxy
= xstrdup(remote
->http_proxy
);
703 var_override(&http_proxy_authmethod
, remote
->http_proxy_authmethod
);
705 pragma_header
= curl_slist_append(http_copy_default_headers(),
707 no_pragma_header
= curl_slist_append(http_copy_default_headers(),
710 #ifdef USE_CURL_MULTI
712 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
713 if (http_max_requests
!= NULL
)
714 max_requests
= atoi(http_max_requests
);
717 curlm
= curl_multi_init();
719 die("curl_multi_init failed");
722 if (getenv("GIT_SSL_NO_VERIFY"))
725 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
726 #if LIBCURL_VERSION_NUM >= 0x070903
727 set_from_env(&ssl_key
, "GIT_SSL_KEY");
729 #if LIBCURL_VERSION_NUM >= 0x070908
730 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
732 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
734 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
736 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
737 if (low_speed_limit
!= NULL
)
738 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
739 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
740 if (low_speed_time
!= NULL
)
741 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
743 if (curl_ssl_verify
== -1)
746 curl_session_count
= 0;
747 #ifdef USE_CURL_MULTI
748 if (max_requests
< 1)
749 max_requests
= DEFAULT_MAX_REQUESTS
;
752 if (getenv("GIT_CURL_FTP_NO_EPSV"))
753 curl_ftp_no_epsv
= 1;
756 credential_from_url(&http_auth
, url
);
757 if (!ssl_cert_password_required
&&
758 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
759 starts_with(url
, "https://"))
760 ssl_cert_password_required
= 1;
763 #ifndef NO_CURL_EASY_DUPHANDLE
764 curl_default
= get_curl_handle();
768 void http_cleanup(void)
770 struct active_request_slot
*slot
= active_queue_head
;
772 while (slot
!= NULL
) {
773 struct active_request_slot
*next
= slot
->next
;
774 if (slot
->curl
!= NULL
) {
775 #ifdef USE_CURL_MULTI
776 curl_multi_remove_handle(curlm
, slot
->curl
);
778 curl_easy_cleanup(slot
->curl
);
783 active_queue_head
= NULL
;
785 #ifndef NO_CURL_EASY_DUPHANDLE
786 curl_easy_cleanup(curl_default
);
789 #ifdef USE_CURL_MULTI
790 curl_multi_cleanup(curlm
);
792 curl_global_cleanup();
794 curl_slist_free_all(extra_http_headers
);
795 extra_http_headers
= NULL
;
797 curl_slist_free_all(pragma_header
);
798 pragma_header
= NULL
;
800 curl_slist_free_all(no_pragma_header
);
801 no_pragma_header
= NULL
;
803 if (curl_http_proxy
) {
804 free((void *)curl_http_proxy
);
805 curl_http_proxy
= NULL
;
808 if (proxy_auth
.password
) {
809 memset(proxy_auth
.password
, 0, strlen(proxy_auth
.password
));
810 free(proxy_auth
.password
);
811 proxy_auth
.password
= NULL
;
814 free((void *)curl_proxyuserpwd
);
815 curl_proxyuserpwd
= NULL
;
817 free((void *)http_proxy_authmethod
);
818 http_proxy_authmethod
= NULL
;
820 if (cert_auth
.password
!= NULL
) {
821 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
822 free(cert_auth
.password
);
823 cert_auth
.password
= NULL
;
825 ssl_cert_password_required
= 0;
827 free(cached_accept_language
);
828 cached_accept_language
= NULL
;
831 struct active_request_slot
*get_active_slot(void)
833 struct active_request_slot
*slot
= active_queue_head
;
834 struct active_request_slot
*newslot
;
836 #ifdef USE_CURL_MULTI
839 /* Wait for a slot to open up if the queue is full */
840 while (active_requests
>= max_requests
) {
841 curl_multi_perform(curlm
, &num_transfers
);
842 if (num_transfers
< active_requests
)
843 process_curl_messages();
847 while (slot
!= NULL
&& slot
->in_use
)
851 newslot
= xmalloc(sizeof(*newslot
));
852 newslot
->curl
= NULL
;
854 newslot
->next
= NULL
;
856 slot
= active_queue_head
;
858 active_queue_head
= newslot
;
860 while (slot
->next
!= NULL
)
862 slot
->next
= newslot
;
867 if (slot
->curl
== NULL
) {
868 #ifdef NO_CURL_EASY_DUPHANDLE
869 slot
->curl
= get_curl_handle();
871 slot
->curl
= curl_easy_duphandle(curl_default
);
873 curl_session_count
++;
878 slot
->results
= NULL
;
879 slot
->finished
= NULL
;
880 slot
->callback_data
= NULL
;
881 slot
->callback_func
= NULL
;
882 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
883 if (curl_save_cookies
)
884 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
885 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
886 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
887 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
888 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
889 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
890 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
891 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
892 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
893 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
894 curl_easy_setopt(slot
->curl
, CURLOPT_RANGE
, NULL
);
897 * Default following to off unless "ALWAYS" is configured; this gives
898 * callers a sane starting point, and they can tweak for individual
899 * HTTP_FOLLOW_* cases themselves.
901 if (http_follow_config
== HTTP_FOLLOW_ALWAYS
)
902 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
904 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 0);
906 #if LIBCURL_VERSION_NUM >= 0x070a08
907 curl_easy_setopt(slot
->curl
, CURLOPT_IPRESOLVE
, git_curl_ipresolve
);
909 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
910 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPAUTH
, http_auth_methods
);
912 if (http_auth
.password
|| curl_empty_auth
)
913 init_curl_http_auth(slot
->curl
);
918 int start_active_slot(struct active_request_slot
*slot
)
920 #ifdef USE_CURL_MULTI
921 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
924 if (curlm_result
!= CURLM_OK
&&
925 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
932 * We know there must be something to do, since we just added
935 curl_multi_perform(curlm
, &num_transfers
);
940 #ifdef USE_CURL_MULTI
944 struct fill_chain
*next
;
947 static struct fill_chain
*fill_cfg
;
949 void add_fill_function(void *data
, int (*fill
)(void *))
951 struct fill_chain
*new = xmalloc(sizeof(*new));
952 struct fill_chain
**linkp
= &fill_cfg
;
957 linkp
= &(*linkp
)->next
;
961 void fill_active_slots(void)
963 struct active_request_slot
*slot
= active_queue_head
;
965 while (active_requests
< max_requests
) {
966 struct fill_chain
*fill
;
967 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
968 if (fill
->fill(fill
->data
))
975 while (slot
!= NULL
) {
976 if (!slot
->in_use
&& slot
->curl
!= NULL
977 && curl_session_count
> min_curl_sessions
) {
978 curl_easy_cleanup(slot
->curl
);
980 curl_session_count
--;
986 void step_active_slots(void)
989 CURLMcode curlm_result
;
992 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
993 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
994 if (num_transfers
< active_requests
) {
995 process_curl_messages();
1001 void run_active_slot(struct active_request_slot
*slot
)
1003 #ifdef USE_CURL_MULTI
1008 struct timeval select_timeout
;
1011 slot
->finished
= &finished
;
1013 step_active_slots();
1016 #if LIBCURL_VERSION_NUM >= 0x070f04
1018 curl_multi_timeout(curlm
, &curl_timeout
);
1019 if (curl_timeout
== 0) {
1021 } else if (curl_timeout
== -1) {
1022 select_timeout
.tv_sec
= 0;
1023 select_timeout
.tv_usec
= 50000;
1025 select_timeout
.tv_sec
= curl_timeout
/ 1000;
1026 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
1029 select_timeout
.tv_sec
= 0;
1030 select_timeout
.tv_usec
= 50000;
1037 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
1040 * It can happen that curl_multi_timeout returns a pathologically
1041 * long timeout when curl_multi_fdset returns no file descriptors
1042 * to read. See commit message for more details.
1045 (select_timeout
.tv_sec
> 0 ||
1046 select_timeout
.tv_usec
> 50000)) {
1047 select_timeout
.tv_sec
= 0;
1048 select_timeout
.tv_usec
= 50000;
1051 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
1055 while (slot
->in_use
) {
1056 slot
->curl_result
= curl_easy_perform(slot
->curl
);
1057 finish_active_slot(slot
);
1062 static void release_active_slot(struct active_request_slot
*slot
)
1064 closedown_active_slot(slot
);
1065 if (slot
->curl
&& curl_session_count
> min_curl_sessions
) {
1066 #ifdef USE_CURL_MULTI
1067 curl_multi_remove_handle(curlm
, slot
->curl
);
1069 curl_easy_cleanup(slot
->curl
);
1071 curl_session_count
--;
1073 #ifdef USE_CURL_MULTI
1074 fill_active_slots();
1078 void finish_all_active_slots(void)
1080 struct active_request_slot
*slot
= active_queue_head
;
1082 while (slot
!= NULL
)
1084 run_active_slot(slot
);
1085 slot
= active_queue_head
;
1091 /* Helpers for modifying and creating URLs */
1092 static inline int needs_quote(int ch
)
1094 if (((ch
>= 'A') && (ch
<= 'Z'))
1095 || ((ch
>= 'a') && (ch
<= 'z'))
1096 || ((ch
>= '0') && (ch
<= '9'))
1104 static char *quote_ref_url(const char *base
, const char *ref
)
1106 struct strbuf buf
= STRBUF_INIT
;
1110 end_url_with_slash(&buf
, base
);
1112 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
1113 if (needs_quote(ch
))
1114 strbuf_addf(&buf
, "%%%02x", ch
);
1116 strbuf_addch(&buf
, *cp
);
1118 return strbuf_detach(&buf
, NULL
);
1121 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
1123 int only_two_digit_prefix
)
1125 end_url_with_slash(buf
, url
);
1127 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
1128 if (!only_two_digit_prefix
)
1129 strbuf_addstr(buf
, hex
+ 2);
1132 char *get_remote_object_url(const char *url
, const char *hex
,
1133 int only_two_digit_prefix
)
1135 struct strbuf buf
= STRBUF_INIT
;
1136 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
1137 return strbuf_detach(&buf
, NULL
);
1140 static int handle_curl_result(struct slot_results
*results
)
1143 * If we see a failing http code with CURLE_OK, we have turned off
1144 * FAILONERROR (to keep the server's custom error response), and should
1145 * translate the code into failure here.
1147 * Likewise, if we see a redirect (30x code), that means we turned off
1148 * redirect-following, and we should treat the result as an error.
1150 if (results
->curl_result
== CURLE_OK
&&
1151 results
->http_code
>= 300) {
1152 results
->curl_result
= CURLE_HTTP_RETURNED_ERROR
;
1154 * Normally curl will already have put the "reason phrase"
1155 * from the server into curl_errorstr; unfortunately without
1156 * FAILONERROR it is lost, so we can give only the numeric
1159 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
1160 "The requested URL returned error: %ld",
1161 results
->http_code
);
1164 if (results
->curl_result
== CURLE_OK
) {
1165 credential_approve(&http_auth
);
1166 if (proxy_auth
.password
)
1167 credential_approve(&proxy_auth
);
1169 } else if (missing_target(results
))
1170 return HTTP_MISSING_TARGET
;
1171 else if (results
->http_code
== 401) {
1172 if (http_auth
.username
&& http_auth
.password
) {
1173 credential_reject(&http_auth
);
1176 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1177 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
1182 if (results
->http_connectcode
== 407)
1183 credential_reject(&proxy_auth
);
1184 #if LIBCURL_VERSION_NUM >= 0x070c00
1185 if (!curl_errorstr
[0])
1186 strlcpy(curl_errorstr
,
1187 curl_easy_strerror(results
->curl_result
),
1188 sizeof(curl_errorstr
));
1194 int run_one_slot(struct active_request_slot
*slot
,
1195 struct slot_results
*results
)
1197 slot
->results
= results
;
1198 if (!start_active_slot(slot
)) {
1199 snprintf(curl_errorstr
, sizeof(curl_errorstr
),
1200 "failed to start HTTP request");
1201 return HTTP_START_FAILED
;
1204 run_active_slot(slot
);
1205 return handle_curl_result(results
);
1208 struct curl_slist
*http_copy_default_headers(void)
1210 struct curl_slist
*headers
= NULL
, *h
;
1212 for (h
= extra_http_headers
; h
; h
= h
->next
)
1213 headers
= curl_slist_append(headers
, h
->data
);
1218 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
1224 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
1226 strbuf_addstr(buf
, ptr
);
1231 * Check for and extract a content-type parameter. "raw"
1232 * should be positioned at the start of the potential
1233 * parameter, with any whitespace already removed.
1235 * "name" is the name of the parameter. The value is appended
1238 static int extract_param(const char *raw
, const char *name
,
1241 size_t len
= strlen(name
);
1243 if (strncasecmp(raw
, name
, len
))
1251 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
1252 strbuf_addch(out
, *raw
++);
1257 * Extract a normalized version of the content type, with any
1258 * spaces suppressed, all letters lowercased, and no trailing ";"
1261 * Note that we will silently remove even invalid whitespace. For
1262 * example, "text / plain" is specifically forbidden by RFC 2616,
1263 * but "text/plain" is the only reasonable output, and this keeps
1266 * If the "charset" argument is not NULL, store the value of any
1267 * charset parameter there.
1270 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1271 * "text / plain" -> "text/plain"
1273 static void extract_content_type(struct strbuf
*raw
, struct strbuf
*type
,
1274 struct strbuf
*charset
)
1279 strbuf_grow(type
, raw
->len
);
1280 for (p
= raw
->buf
; *p
; p
++) {
1287 strbuf_addch(type
, tolower(*p
));
1293 strbuf_reset(charset
);
1295 while (isspace(*p
) || *p
== ';')
1297 if (!extract_param(p
, "charset", charset
))
1299 while (*p
&& !isspace(*p
))
1303 if (!charset
->len
&& starts_with(type
->buf
, "text/"))
1304 strbuf_addstr(charset
, "ISO-8859-1");
1307 static void write_accept_language(struct strbuf
*buf
)
1310 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1311 * that, q-value will be smaller than 0.001, the minimum q-value the
1312 * HTTP specification allows. See
1313 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1315 const int MAX_DECIMAL_PLACES
= 3;
1316 const int MAX_LANGUAGE_TAGS
= 1000;
1317 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE
= 4000;
1318 char **language_tags
= NULL
;
1320 const char *s
= get_preferred_languages();
1322 struct strbuf tag
= STRBUF_INIT
;
1324 /* Don't add Accept-Language header if no language is preferred. */
1329 * Split the colon-separated string of preferred languages into
1330 * language_tags array.
1333 /* collect language tag */
1334 for (; *s
&& (isalnum(*s
) || *s
== '_'); s
++)
1335 strbuf_addch(&tag
, *s
== '_' ? '-' : *s
);
1337 /* skip .codeset, @modifier and any other unnecessary parts */
1338 while (*s
&& *s
!= ':')
1343 REALLOC_ARRAY(language_tags
, num_langs
);
1344 language_tags
[num_langs
- 1] = strbuf_detach(&tag
, NULL
);
1345 if (num_langs
>= MAX_LANGUAGE_TAGS
- 1) /* -1 for '*' */
1350 /* write Accept-Language header into buf */
1352 int last_buf_len
= 0;
1358 REALLOC_ARRAY(language_tags
, num_langs
+ 1);
1359 language_tags
[num_langs
++] = "*"; /* it's OK; this won't be freed */
1361 /* compute decimal_places */
1362 for (max_q
= 1, decimal_places
= 0;
1363 max_q
< num_langs
&& decimal_places
<= MAX_DECIMAL_PLACES
;
1364 decimal_places
++, max_q
*= 10)
1367 xsnprintf(q_format
, sizeof(q_format
), ";q=0.%%0%dd", decimal_places
);
1369 strbuf_addstr(buf
, "Accept-Language: ");
1371 for (i
= 0; i
< num_langs
; i
++) {
1373 strbuf_addstr(buf
, ", ");
1375 strbuf_addstr(buf
, language_tags
[i
]);
1378 strbuf_addf(buf
, q_format
, max_q
- i
);
1380 if (buf
->len
> MAX_ACCEPT_LANGUAGE_HEADER_SIZE
) {
1381 strbuf_remove(buf
, last_buf_len
, buf
->len
- last_buf_len
);
1385 last_buf_len
= buf
->len
;
1389 /* free language tags -- last one is a static '*' */
1390 for (i
= 0; i
< num_langs
- 1; i
++)
1391 free(language_tags
[i
]);
1392 free(language_tags
);
1396 * Get an Accept-Language header which indicates user's preferred languages.
1400 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1401 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1402 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1403 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1404 * LANGUAGE= LANG=C -> ""
1406 static const char *get_accept_language(void)
1408 if (!cached_accept_language
) {
1409 struct strbuf buf
= STRBUF_INIT
;
1410 write_accept_language(&buf
);
1412 cached_accept_language
= strbuf_detach(&buf
, NULL
);
1415 return cached_accept_language
;
1418 static void http_opt_request_remainder(CURL
*curl
, off_t pos
)
1421 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
"-", (uintmax_t)pos
);
1422 curl_easy_setopt(curl
, CURLOPT_RANGE
, buf
);
1425 /* http_request() targets */
1426 #define HTTP_REQUEST_STRBUF 0
1427 #define HTTP_REQUEST_FILE 1
1429 static int http_request(const char *url
,
1430 void *result
, int target
,
1431 const struct http_get_options
*options
)
1433 struct active_request_slot
*slot
;
1434 struct slot_results results
;
1435 struct curl_slist
*headers
= http_copy_default_headers();
1436 struct strbuf buf
= STRBUF_INIT
;
1437 const char *accept_language
;
1440 slot
= get_active_slot();
1441 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1443 if (result
== NULL
) {
1444 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
1446 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1447 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
1449 if (target
== HTTP_REQUEST_FILE
) {
1450 off_t posn
= ftello(result
);
1451 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1454 http_opt_request_remainder(slot
->curl
, posn
);
1456 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1460 accept_language
= get_accept_language();
1462 if (accept_language
)
1463 headers
= curl_slist_append(headers
, accept_language
);
1465 strbuf_addstr(&buf
, "Pragma:");
1466 if (options
&& options
->no_cache
)
1467 strbuf_addstr(&buf
, " no-cache");
1468 if (options
&& options
->keep_error
)
1469 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1470 if (options
&& options
->initial_request
&&
1471 http_follow_config
== HTTP_FOLLOW_INITIAL
)
1472 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1474 headers
= curl_slist_append(headers
, buf
.buf
);
1476 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1477 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1478 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
1480 ret
= run_one_slot(slot
, &results
);
1482 if (options
&& options
->content_type
) {
1483 struct strbuf raw
= STRBUF_INIT
;
1484 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
, &raw
);
1485 extract_content_type(&raw
, options
->content_type
,
1487 strbuf_release(&raw
);
1490 if (options
&& options
->effective_url
)
1491 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
1492 options
->effective_url
);
1494 curl_slist_free_all(headers
);
1495 strbuf_release(&buf
);
1501 * Update the "base" url to a more appropriate value, as deduced by
1502 * redirects seen when requesting a URL starting with "url".
1504 * The "asked" parameter is a URL that we asked curl to access, and must begin
1507 * The "got" parameter is the URL that curl reported to us as where we ended
1510 * Returns 1 if we updated the base url, 0 otherwise.
1512 * Our basic strategy is to compare "base" and "asked" to find the bits
1513 * specific to our request. We then strip those bits off of "got" to yield the
1514 * new base. So for example, if our base is "http://example.com/foo.git",
1515 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1516 * with "https://other.example.com/foo.git/info/refs". We would want the
1517 * new URL to become "https://other.example.com/foo.git".
1519 * Note that this assumes a sane redirect scheme. It's entirely possible
1520 * in the example above to end up at a URL that does not even end in
1521 * "info/refs". In such a case we die. There's not much we can do, such a
1522 * scheme is unlikely to represent a real git repository, and failing to
1523 * rewrite the base opens options for malicious redirects to do funny things.
1525 static int update_url_from_redirect(struct strbuf
*base
,
1527 const struct strbuf
*got
)
1532 if (!strcmp(asked
, got
->buf
))
1535 if (!skip_prefix(asked
, base
->buf
, &tail
))
1536 die("BUG: update_url_from_redirect: %s is not a superset of %s",
1540 if (!strip_suffix_mem(got
->buf
, &new_len
, tail
))
1541 die(_("unable to update url base from redirection:\n"
1547 strbuf_add(base
, got
->buf
, new_len
);
1552 static int http_request_reauth(const char *url
,
1553 void *result
, int target
,
1554 struct http_get_options
*options
)
1556 int ret
= http_request(url
, result
, target
, options
);
1558 if (options
&& options
->effective_url
&& options
->base_url
) {
1559 if (update_url_from_redirect(options
->base_url
,
1560 url
, options
->effective_url
)) {
1561 credential_from_url(&http_auth
, options
->base_url
->buf
);
1562 url
= options
->effective_url
->buf
;
1566 if (ret
!= HTTP_REAUTH
)
1570 * If we are using KEEP_ERROR, the previous request may have
1571 * put cruft into our output stream; we should clear it out before
1572 * making our next request. We only know how to do this for
1573 * the strbuf case, but that is enough to satisfy current callers.
1575 if (options
&& options
->keep_error
) {
1577 case HTTP_REQUEST_STRBUF
:
1578 strbuf_reset(result
);
1581 die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
1585 credential_fill(&http_auth
);
1587 return http_request(url
, result
, target
, options
);
1590 int http_get_strbuf(const char *url
,
1591 struct strbuf
*result
,
1592 struct http_get_options
*options
)
1594 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
1598 * Downloads a URL and stores the result in the given file.
1600 * If a previous interrupted download is detected (i.e. a previous temporary
1601 * file is still around) the download is resumed.
1603 static int http_get_file(const char *url
, const char *filename
,
1604 struct http_get_options
*options
)
1607 struct strbuf tmpfile
= STRBUF_INIT
;
1610 strbuf_addf(&tmpfile
, "%s.temp", filename
);
1611 result
= fopen(tmpfile
.buf
, "a");
1613 error("Unable to open local file %s", tmpfile
.buf
);
1618 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
1621 if (ret
== HTTP_OK
&& finalize_object_file(tmpfile
.buf
, filename
))
1624 strbuf_release(&tmpfile
);
1628 int http_fetch_ref(const char *base
, struct ref
*ref
)
1630 struct http_get_options options
= {0};
1632 struct strbuf buffer
= STRBUF_INIT
;
1635 options
.no_cache
= 1;
1637 url
= quote_ref_url(base
, ref
->name
);
1638 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
1639 strbuf_rtrim(&buffer
);
1640 if (buffer
.len
== 40)
1641 ret
= get_oid_hex(buffer
.buf
, &ref
->old_oid
);
1642 else if (starts_with(buffer
.buf
, "ref: ")) {
1643 ref
->symref
= xstrdup(buffer
.buf
+ 5);
1648 strbuf_release(&buffer
);
1653 /* Helpers for fetching packs */
1654 static char *fetch_pack_index(unsigned char *sha1
, const char *base_url
)
1657 struct strbuf buf
= STRBUF_INIT
;
1659 if (http_is_verbose
)
1660 fprintf(stderr
, "Getting index for pack %s\n", sha1_to_hex(sha1
));
1662 end_url_with_slash(&buf
, base_url
);
1663 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", sha1_to_hex(sha1
));
1664 url
= strbuf_detach(&buf
, NULL
);
1666 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(sha1
));
1667 tmp
= strbuf_detach(&buf
, NULL
);
1669 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
1670 error("Unable to get pack index %s", url
);
1679 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
1680 unsigned char *sha1
, const char *base_url
)
1682 struct packed_git
*new_pack
;
1683 char *tmp_idx
= NULL
;
1686 if (has_pack_index(sha1
)) {
1687 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
1689 return -1; /* parse_pack_index() already issued error message */
1693 tmp_idx
= fetch_pack_index(sha1
, base_url
);
1697 new_pack
= parse_pack_index(sha1
, tmp_idx
);
1702 return -1; /* parse_pack_index() already issued error message */
1705 ret
= verify_pack_index(new_pack
);
1707 close_pack_index(new_pack
);
1708 ret
= finalize_object_file(tmp_idx
, sha1_pack_index_name(sha1
));
1715 new_pack
->next
= *packs_head
;
1716 *packs_head
= new_pack
;
1720 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
1722 struct http_get_options options
= {0};
1725 struct strbuf buf
= STRBUF_INIT
;
1726 unsigned char sha1
[20];
1728 end_url_with_slash(&buf
, base_url
);
1729 strbuf_addstr(&buf
, "objects/info/packs");
1730 url
= strbuf_detach(&buf
, NULL
);
1732 options
.no_cache
= 1;
1733 ret
= http_get_strbuf(url
, &buf
, &options
);
1738 while (i
< buf
.len
) {
1742 if (i
+ 52 <= buf
.len
&&
1743 starts_with(data
+ i
, " pack-") &&
1744 starts_with(data
+ i
+ 46, ".pack\n")) {
1745 get_sha1_hex(data
+ i
+ 6, sha1
);
1746 fetch_and_setup_pack_index(packs_head
, sha1
,
1752 while (i
< buf
.len
&& data
[i
] != '\n')
1763 void release_http_pack_request(struct http_pack_request
*preq
)
1765 if (preq
->packfile
!= NULL
) {
1766 fclose(preq
->packfile
);
1767 preq
->packfile
= NULL
;
1774 int finish_http_pack_request(struct http_pack_request
*preq
)
1776 struct packed_git
**lst
;
1777 struct packed_git
*p
= preq
->target
;
1780 struct child_process ip
= CHILD_PROCESS_INIT
;
1781 const char *ip_argv
[8];
1783 close_pack_index(p
);
1785 fclose(preq
->packfile
);
1786 preq
->packfile
= NULL
;
1790 lst
= &((*lst
)->next
);
1791 *lst
= (*lst
)->next
;
1793 if (!strip_suffix(preq
->tmpfile
, ".pack.temp", &len
))
1794 die("BUG: pack tmpfile does not end in .pack.temp?");
1795 tmp_idx
= xstrfmt("%.*s.idx.temp", (int)len
, preq
->tmpfile
);
1797 ip_argv
[0] = "index-pack";
1799 ip_argv
[2] = tmp_idx
;
1800 ip_argv
[3] = preq
->tmpfile
;
1808 if (run_command(&ip
)) {
1809 unlink(preq
->tmpfile
);
1815 unlink(sha1_pack_index_name(p
->sha1
));
1817 if (finalize_object_file(preq
->tmpfile
, sha1_pack_name(p
->sha1
))
1818 || finalize_object_file(tmp_idx
, sha1_pack_index_name(p
->sha1
))) {
1823 install_packed_git(p
);
1828 struct http_pack_request
*new_http_pack_request(
1829 struct packed_git
*target
, const char *base_url
)
1831 off_t prev_posn
= 0;
1832 struct strbuf buf
= STRBUF_INIT
;
1833 struct http_pack_request
*preq
;
1835 preq
= xcalloc(1, sizeof(*preq
));
1836 preq
->target
= target
;
1838 end_url_with_slash(&buf
, base_url
);
1839 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
1840 sha1_to_hex(target
->sha1
));
1841 preq
->url
= strbuf_detach(&buf
, NULL
);
1843 snprintf(preq
->tmpfile
, sizeof(preq
->tmpfile
), "%s.temp",
1844 sha1_pack_name(target
->sha1
));
1845 preq
->packfile
= fopen(preq
->tmpfile
, "a");
1846 if (!preq
->packfile
) {
1847 error("Unable to open local file %s for pack",
1852 preq
->slot
= get_active_slot();
1853 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
1854 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
1855 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
1856 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
1860 * If there is data present from a previous transfer attempt,
1861 * resume where it left off
1863 prev_posn
= ftello(preq
->packfile
);
1865 if (http_is_verbose
)
1867 "Resuming fetch of pack %s at byte %"PRIuMAX
"\n",
1868 sha1_to_hex(target
->sha1
), (uintmax_t)prev_posn
);
1869 http_opt_request_remainder(preq
->slot
->curl
, prev_posn
);
1880 /* Helpers for fetching objects (loose) */
1881 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
1884 unsigned char expn
[4096];
1885 size_t size
= eltsize
* nmemb
;
1887 struct http_object_request
*freq
=
1888 (struct http_object_request
*)data
;
1890 ssize_t retval
= xwrite(freq
->localfile
,
1891 (char *) ptr
+ posn
, size
- posn
);
1895 } while (posn
< size
);
1897 freq
->stream
.avail_in
= size
;
1898 freq
->stream
.next_in
= (void *)ptr
;
1900 freq
->stream
.next_out
= expn
;
1901 freq
->stream
.avail_out
= sizeof(expn
);
1902 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
1903 git_SHA1_Update(&freq
->c
, expn
,
1904 sizeof(expn
) - freq
->stream
.avail_out
);
1905 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
1909 struct http_object_request
*new_http_object_request(const char *base_url
,
1910 unsigned char *sha1
)
1912 char *hex
= sha1_to_hex(sha1
);
1913 const char *filename
;
1914 char prevfile
[PATH_MAX
];
1916 char prev_buf
[PREV_BUF_SIZE
];
1917 ssize_t prev_read
= 0;
1918 off_t prev_posn
= 0;
1919 struct http_object_request
*freq
;
1921 freq
= xcalloc(1, sizeof(*freq
));
1922 hashcpy(freq
->sha1
, sha1
);
1923 freq
->localfile
= -1;
1925 filename
= sha1_file_name(sha1
);
1926 snprintf(freq
->tmpfile
, sizeof(freq
->tmpfile
),
1927 "%s.temp", filename
);
1929 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
);
1930 unlink_or_warn(prevfile
);
1931 rename(freq
->tmpfile
, prevfile
);
1932 unlink_or_warn(freq
->tmpfile
);
1934 if (freq
->localfile
!= -1)
1935 error("fd leakage in start: %d", freq
->localfile
);
1936 freq
->localfile
= open(freq
->tmpfile
,
1937 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1939 * This could have failed due to the "lazy directory creation";
1940 * try to mkdir the last path component.
1942 if (freq
->localfile
< 0 && errno
== ENOENT
) {
1943 char *dir
= strrchr(freq
->tmpfile
, '/');
1946 mkdir(freq
->tmpfile
, 0777);
1949 freq
->localfile
= open(freq
->tmpfile
,
1950 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
1953 if (freq
->localfile
< 0) {
1954 error_errno("Couldn't create temporary file %s", freq
->tmpfile
);
1958 git_inflate_init(&freq
->stream
);
1960 git_SHA1_Init(&freq
->c
);
1962 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
1965 * If a previous temp file is present, process what was already
1968 prevlocal
= open(prevfile
, O_RDONLY
);
1969 if (prevlocal
!= -1) {
1971 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
1973 if (fwrite_sha1_file(prev_buf
,
1976 freq
) == prev_read
) {
1977 prev_posn
+= prev_read
;
1982 } while (prev_read
> 0);
1985 unlink_or_warn(prevfile
);
1988 * Reset inflate/SHA1 if there was an error reading the previous temp
1989 * file; also rewind to the beginning of the local file.
1991 if (prev_read
== -1) {
1992 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
1993 git_inflate_init(&freq
->stream
);
1994 git_SHA1_Init(&freq
->c
);
1997 lseek(freq
->localfile
, 0, SEEK_SET
);
1998 if (ftruncate(freq
->localfile
, 0) < 0) {
1999 error_errno("Couldn't truncate temporary file %s",
2006 freq
->slot
= get_active_slot();
2008 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
2009 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
2010 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
2011 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
2012 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
2015 * If we have successfully processed data from a previous fetch
2016 * attempt, only fetch the data we don't already have.
2019 if (http_is_verbose
)
2021 "Resuming fetch of object %s at byte %"PRIuMAX
"\n",
2022 hex
, (uintmax_t)prev_posn
);
2023 http_opt_request_remainder(freq
->slot
->curl
, prev_posn
);
2034 void process_http_object_request(struct http_object_request
*freq
)
2036 if (freq
->slot
== NULL
)
2038 freq
->curl_result
= freq
->slot
->curl_result
;
2039 freq
->http_code
= freq
->slot
->http_code
;
2043 int finish_http_object_request(struct http_object_request
*freq
)
2047 close(freq
->localfile
);
2048 freq
->localfile
= -1;
2050 process_http_object_request(freq
);
2052 if (freq
->http_code
== 416) {
2053 warning("requested range invalid; we may already have all the data.");
2054 } else if (freq
->curl_result
!= CURLE_OK
) {
2055 if (stat(freq
->tmpfile
, &st
) == 0)
2056 if (st
.st_size
== 0)
2057 unlink_or_warn(freq
->tmpfile
);
2061 git_inflate_end(&freq
->stream
);
2062 git_SHA1_Final(freq
->real_sha1
, &freq
->c
);
2063 if (freq
->zret
!= Z_STREAM_END
) {
2064 unlink_or_warn(freq
->tmpfile
);
2067 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
2068 unlink_or_warn(freq
->tmpfile
);
2072 finalize_object_file(freq
->tmpfile
, sha1_file_name(freq
->sha1
));
2074 return freq
->rename
;
2077 void abort_http_object_request(struct http_object_request
*freq
)
2079 unlink_or_warn(freq
->tmpfile
);
2081 release_http_object_request(freq
);
2084 void release_http_object_request(struct http_object_request
*freq
)
2086 if (freq
->localfile
!= -1) {
2087 close(freq
->localfile
);
2088 freq
->localfile
= -1;
2090 if (freq
->url
!= NULL
) {
2094 if (freq
->slot
!= NULL
) {
2095 freq
->slot
->callback_func
= NULL
;
2096 freq
->slot
->callback_data
= NULL
;
2097 release_active_slot(freq
->slot
);