1 #include "git-compat-util.h"
2 #include "git-curl-compat.h"
7 #include "run-command.h"
10 #include "credential.h"
14 #include "transport.h"
17 #include "string-list.h"
18 #include "object-store.h"
20 static struct trace_key trace_curl
= TRACE_KEY_INIT(CURL
);
21 static int trace_curl_data
= 1;
22 static int trace_curl_redact
= 1;
23 long int git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
26 ssize_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
28 static int min_curl_sessions
= 1;
29 static int curl_session_count
;
30 static int max_requests
= -1;
32 static CURL
*curl_default
;
34 #define PREV_BUF_SIZE 4096
36 char curl_errorstr
[CURL_ERROR_SIZE
];
38 static int curl_ssl_verify
= -1;
39 static int curl_ssl_try
;
40 static const char *curl_http_version
= NULL
;
41 static const char *ssl_cert
;
42 static const char *ssl_cipherlist
;
43 static const char *ssl_version
;
48 { "sslv2", CURL_SSLVERSION_SSLv2
},
49 { "sslv3", CURL_SSLVERSION_SSLv3
},
50 { "tlsv1", CURL_SSLVERSION_TLSv1
},
51 #ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_0
52 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0
},
53 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1
},
54 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2
},
56 #ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_3
57 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3
},
60 static const char *ssl_key
;
61 static const char *ssl_capath
;
62 static const char *curl_no_proxy
;
63 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
64 static const char *ssl_pinnedkey
;
66 static const char *ssl_cainfo
;
67 static long curl_low_speed_limit
= -1;
68 static long curl_low_speed_time
= -1;
69 static int curl_ftp_no_epsv
;
70 static const char *curl_http_proxy
;
71 static const char *http_proxy_authmethod
;
73 static const char *http_proxy_ssl_cert
;
74 static const char *http_proxy_ssl_key
;
75 static const char *http_proxy_ssl_ca_info
;
76 static struct credential proxy_cert_auth
= CREDENTIAL_INIT
;
77 static int proxy_ssl_cert_password_required
;
82 } proxy_authmethods
[] = {
83 { "basic", CURLAUTH_BASIC
},
84 { "digest", CURLAUTH_DIGEST
},
85 { "negotiate", CURLAUTH_GSSNEGOTIATE
},
86 { "ntlm", CURLAUTH_NTLM
},
87 { "anyauth", CURLAUTH_ANY
},
89 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
90 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
94 #ifdef CURLGSSAPI_DELEGATION_FLAG
95 static const char *curl_deleg
;
98 long curl_deleg_param
;
99 } curl_deleg_levels
[] = {
100 { "none", CURLGSSAPI_DELEGATION_NONE
},
101 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG
},
102 { "always", CURLGSSAPI_DELEGATION_FLAG
},
106 static struct credential proxy_auth
= CREDENTIAL_INIT
;
107 static const char *curl_proxyuserpwd
;
108 static const char *curl_cookie_file
;
109 static int curl_save_cookies
;
110 struct credential http_auth
= CREDENTIAL_INIT
;
111 static int http_proactive_auth
;
112 static const char *user_agent
;
113 static int curl_empty_auth
= -1;
115 enum http_follow_config http_follow_config
= HTTP_FOLLOW_INITIAL
;
117 static struct credential cert_auth
= CREDENTIAL_INIT
;
118 static int ssl_cert_password_required
;
119 static unsigned long http_auth_methods
= CURLAUTH_ANY
;
120 static int http_auth_methods_restricted
;
121 /* Modes for which empty_auth cannot actually help us. */
122 static unsigned long empty_auth_useless
=
127 static struct curl_slist
*pragma_header
;
128 static struct curl_slist
*no_pragma_header
;
129 static struct string_list extra_http_headers
= STRING_LIST_INIT_DUP
;
131 static struct curl_slist
*host_resolutions
;
133 static struct active_request_slot
*active_queue_head
;
135 static char *cached_accept_language
;
137 static char *http_ssl_backend
;
139 static int http_schannel_check_revoke
= 1;
141 * With the backend being set to `schannel`, setting sslCAinfo would override
142 * the Certificate Store in cURL v7.60.0 and later, which is not what we want
145 static int http_schannel_use_ssl_cainfo
;
147 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
149 size_t size
= eltsize
* nmemb
;
150 struct buffer
*buffer
= buffer_
;
152 if (size
> buffer
->buf
.len
- buffer
->posn
)
153 size
= buffer
->buf
.len
- buffer
->posn
;
154 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
155 buffer
->posn
+= size
;
157 return size
/ eltsize
;
160 int seek_buffer(void *clientp
, curl_off_t offset
, int origin
)
162 struct buffer
*buffer
= clientp
;
164 if (origin
!= SEEK_SET
)
165 BUG("seek_buffer only handles SEEK_SET");
166 if (offset
< 0 || offset
>= buffer
->buf
.len
) {
167 error("curl seek would be outside of buffer");
168 return CURL_SEEKFUNC_FAIL
;
171 buffer
->posn
= offset
;
172 return CURL_SEEKFUNC_OK
;
175 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
177 size_t size
= eltsize
* nmemb
;
178 struct strbuf
*buffer
= buffer_
;
180 strbuf_add(buffer
, ptr
, size
);
184 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
189 static void closedown_active_slot(struct active_request_slot
*slot
)
195 static void finish_active_slot(struct active_request_slot
*slot
)
197 closedown_active_slot(slot
);
198 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
201 (*slot
->finished
) = 1;
203 /* Store slot results so they can be read after the slot is reused */
205 slot
->results
->curl_result
= slot
->curl_result
;
206 slot
->results
->http_code
= slot
->http_code
;
207 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
208 &slot
->results
->auth_avail
);
210 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CONNECTCODE
,
211 &slot
->results
->http_connectcode
);
214 /* Run callback if appropriate */
215 if (slot
->callback_func
)
216 slot
->callback_func(slot
->callback_data
);
219 static void xmulti_remove_handle(struct active_request_slot
*slot
)
221 curl_multi_remove_handle(curlm
, slot
->curl
);
224 static void process_curl_messages(void)
227 struct active_request_slot
*slot
;
228 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
230 while (curl_message
!= NULL
) {
231 if (curl_message
->msg
== CURLMSG_DONE
) {
232 int curl_result
= curl_message
->data
.result
;
233 slot
= active_queue_head
;
234 while (slot
!= NULL
&&
235 slot
->curl
!= curl_message
->easy_handle
)
238 xmulti_remove_handle(slot
);
239 slot
->curl_result
= curl_result
;
240 finish_active_slot(slot
);
242 fprintf(stderr
, "Received DONE message for unknown request!\n");
245 fprintf(stderr
, "Unknown CURL message received: %d\n",
246 (int)curl_message
->msg
);
248 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
252 static int http_options(const char *var
, const char *value
, void *cb
)
254 if (!strcmp("http.version", var
)) {
255 return git_config_string(&curl_http_version
, var
, value
);
257 if (!strcmp("http.sslverify", var
)) {
258 curl_ssl_verify
= git_config_bool(var
, value
);
261 if (!strcmp("http.sslcipherlist", var
))
262 return git_config_string(&ssl_cipherlist
, var
, value
);
263 if (!strcmp("http.sslversion", var
))
264 return git_config_string(&ssl_version
, var
, value
);
265 if (!strcmp("http.sslcert", var
))
266 return git_config_pathname(&ssl_cert
, var
, value
);
267 if (!strcmp("http.sslkey", var
))
268 return git_config_pathname(&ssl_key
, var
, value
);
269 if (!strcmp("http.sslcapath", var
))
270 return git_config_pathname(&ssl_capath
, var
, value
);
271 if (!strcmp("http.sslcainfo", var
))
272 return git_config_pathname(&ssl_cainfo
, var
, value
);
273 if (!strcmp("http.sslcertpasswordprotected", var
)) {
274 ssl_cert_password_required
= git_config_bool(var
, value
);
277 if (!strcmp("http.ssltry", var
)) {
278 curl_ssl_try
= git_config_bool(var
, value
);
281 if (!strcmp("http.sslbackend", var
)) {
282 free(http_ssl_backend
);
283 http_ssl_backend
= xstrdup_or_null(value
);
287 if (!strcmp("http.schannelcheckrevoke", var
)) {
288 http_schannel_check_revoke
= git_config_bool(var
, value
);
292 if (!strcmp("http.schannelusesslcainfo", var
)) {
293 http_schannel_use_ssl_cainfo
= git_config_bool(var
, value
);
297 if (!strcmp("http.minsessions", var
)) {
298 min_curl_sessions
= git_config_int(var
, value
);
299 if (min_curl_sessions
> 1)
300 min_curl_sessions
= 1;
303 if (!strcmp("http.maxrequests", var
)) {
304 max_requests
= git_config_int(var
, value
);
307 if (!strcmp("http.lowspeedlimit", var
)) {
308 curl_low_speed_limit
= (long)git_config_int(var
, value
);
311 if (!strcmp("http.lowspeedtime", var
)) {
312 curl_low_speed_time
= (long)git_config_int(var
, value
);
316 if (!strcmp("http.noepsv", var
)) {
317 curl_ftp_no_epsv
= git_config_bool(var
, value
);
320 if (!strcmp("http.proxy", var
))
321 return git_config_string(&curl_http_proxy
, var
, value
);
323 if (!strcmp("http.proxyauthmethod", var
))
324 return git_config_string(&http_proxy_authmethod
, var
, value
);
326 if (!strcmp("http.proxysslcert", var
))
327 return git_config_string(&http_proxy_ssl_cert
, var
, value
);
329 if (!strcmp("http.proxysslkey", var
))
330 return git_config_string(&http_proxy_ssl_key
, var
, value
);
332 if (!strcmp("http.proxysslcainfo", var
))
333 return git_config_string(&http_proxy_ssl_ca_info
, var
, value
);
335 if (!strcmp("http.proxysslcertpasswordprotected", var
)) {
336 proxy_ssl_cert_password_required
= git_config_bool(var
, value
);
340 if (!strcmp("http.cookiefile", var
))
341 return git_config_pathname(&curl_cookie_file
, var
, value
);
342 if (!strcmp("http.savecookies", var
)) {
343 curl_save_cookies
= git_config_bool(var
, value
);
347 if (!strcmp("http.postbuffer", var
)) {
348 http_post_buffer
= git_config_ssize_t(var
, value
);
349 if (http_post_buffer
< 0)
350 warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX
);
351 if (http_post_buffer
< LARGE_PACKET_MAX
)
352 http_post_buffer
= LARGE_PACKET_MAX
;
356 if (!strcmp("http.useragent", var
))
357 return git_config_string(&user_agent
, var
, value
);
359 if (!strcmp("http.emptyauth", var
)) {
360 if (value
&& !strcmp("auto", value
))
361 curl_empty_auth
= -1;
363 curl_empty_auth
= git_config_bool(var
, value
);
367 if (!strcmp("http.delegation", var
)) {
368 #ifdef CURLGSSAPI_DELEGATION_FLAG
369 return git_config_string(&curl_deleg
, var
, value
);
371 warning(_("Delegation control is not supported with cURL < 7.22.0"));
376 if (!strcmp("http.pinnedpubkey", var
)) {
377 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
378 return git_config_pathname(&ssl_pinnedkey
, var
, value
);
380 warning(_("Public key pinning not supported with cURL < 7.39.0"));
385 if (!strcmp("http.extraheader", var
)) {
387 return config_error_nonbool(var
);
388 } else if (!*value
) {
389 string_list_clear(&extra_http_headers
, 0);
391 string_list_append(&extra_http_headers
, value
);
396 if (!strcmp("http.curloptresolve", var
)) {
398 return config_error_nonbool(var
);
399 } else if (!*value
) {
400 curl_slist_free_all(host_resolutions
);
401 host_resolutions
= NULL
;
403 host_resolutions
= curl_slist_append(host_resolutions
, value
);
408 if (!strcmp("http.followredirects", var
)) {
409 if (value
&& !strcmp(value
, "initial"))
410 http_follow_config
= HTTP_FOLLOW_INITIAL
;
411 else if (git_config_bool(var
, value
))
412 http_follow_config
= HTTP_FOLLOW_ALWAYS
;
414 http_follow_config
= HTTP_FOLLOW_NONE
;
418 /* Fall back on the default ones */
419 return git_default_config(var
, value
, cb
);
422 static int curl_empty_auth_enabled(void)
424 if (curl_empty_auth
>= 0)
425 return curl_empty_auth
;
428 * In the automatic case, kick in the empty-auth
429 * hack as long as we would potentially try some
430 * method more exotic than "Basic" or "Digest".
432 * But only do this when this is our second or
433 * subsequent request, as by then we know what
434 * methods are available.
436 if (http_auth_methods_restricted
&&
437 (http_auth_methods
& ~empty_auth_useless
))
442 static void init_curl_http_auth(CURL
*result
)
444 if (!http_auth
.username
|| !*http_auth
.username
) {
445 if (curl_empty_auth_enabled())
446 curl_easy_setopt(result
, CURLOPT_USERPWD
, ":");
450 credential_fill(&http_auth
);
452 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
453 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
456 /* *var must be free-able */
457 static void var_override(const char **var
, char *value
)
461 *var
= xstrdup(value
);
465 static void set_proxyauth_name_password(CURL
*result
)
467 curl_easy_setopt(result
, CURLOPT_PROXYUSERNAME
,
468 proxy_auth
.username
);
469 curl_easy_setopt(result
, CURLOPT_PROXYPASSWORD
,
470 proxy_auth
.password
);
473 static void init_curl_proxy_auth(CURL
*result
)
475 if (proxy_auth
.username
) {
476 if (!proxy_auth
.password
)
477 credential_fill(&proxy_auth
);
478 set_proxyauth_name_password(result
);
481 var_override(&http_proxy_authmethod
, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
483 if (http_proxy_authmethod
) {
485 for (i
= 0; i
< ARRAY_SIZE(proxy_authmethods
); i
++) {
486 if (!strcmp(http_proxy_authmethod
, proxy_authmethods
[i
].name
)) {
487 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
,
488 proxy_authmethods
[i
].curlauth_param
);
492 if (i
== ARRAY_SIZE(proxy_authmethods
)) {
493 warning("unsupported proxy authentication method %s: using anyauth",
494 http_proxy_authmethod
);
495 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
499 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
502 static int has_cert_password(void)
504 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
506 if (!cert_auth
.password
) {
507 cert_auth
.protocol
= xstrdup("cert");
508 cert_auth
.host
= xstrdup("");
509 cert_auth
.username
= xstrdup("");
510 cert_auth
.path
= xstrdup(ssl_cert
);
511 credential_fill(&cert_auth
);
516 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
517 static int has_proxy_cert_password(void)
519 if (http_proxy_ssl_cert
== NULL
|| proxy_ssl_cert_password_required
!= 1)
521 if (!proxy_cert_auth
.password
) {
522 proxy_cert_auth
.protocol
= xstrdup("cert");
523 proxy_cert_auth
.host
= xstrdup("");
524 proxy_cert_auth
.username
= xstrdup("");
525 proxy_cert_auth
.path
= xstrdup(http_proxy_ssl_cert
);
526 credential_fill(&proxy_cert_auth
);
532 #ifdef GITCURL_HAVE_CURLOPT_TCP_KEEPALIVE
533 static void set_curl_keepalive(CURL
*c
)
535 curl_easy_setopt(c
, CURLOPT_TCP_KEEPALIVE
, 1);
539 static int sockopt_callback(void *client
, curl_socket_t fd
, curlsocktype type
)
543 socklen_t len
= (socklen_t
)sizeof(ka
);
545 if (type
!= CURLSOCKTYPE_IPCXN
)
548 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
550 warning_errno("unable to set SO_KEEPALIVE on socket");
552 return CURL_SOCKOPT_OK
;
555 static void set_curl_keepalive(CURL
*c
)
557 curl_easy_setopt(c
, CURLOPT_SOCKOPTFUNCTION
, sockopt_callback
);
561 /* Return 1 if redactions have been made, 0 otherwise. */
562 static int redact_sensitive_header(struct strbuf
*header
, size_t offset
)
565 const char *sensitive_header
;
567 if (trace_curl_redact
&&
568 (skip_iprefix(header
->buf
+ offset
, "Authorization:", &sensitive_header
) ||
569 skip_iprefix(header
->buf
+ offset
, "Proxy-Authorization:", &sensitive_header
))) {
570 /* The first token is the type, which is OK to log */
571 while (isspace(*sensitive_header
))
573 while (*sensitive_header
&& !isspace(*sensitive_header
))
575 /* Everything else is opaque and possibly sensitive */
576 strbuf_setlen(header
, sensitive_header
- header
->buf
);
577 strbuf_addstr(header
, " <redacted>");
579 } else if (trace_curl_redact
&&
580 skip_iprefix(header
->buf
+ offset
, "Cookie:", &sensitive_header
)) {
581 struct strbuf redacted_header
= STRBUF_INIT
;
584 while (isspace(*sensitive_header
))
587 cookie
= sensitive_header
;
591 char *semicolon
= strstr(cookie
, "; ");
594 equals
= strchrnul(cookie
, '=');
596 /* invalid cookie, just append and continue */
597 strbuf_addstr(&redacted_header
, cookie
);
600 strbuf_add(&redacted_header
, cookie
, equals
- cookie
);
601 strbuf_addstr(&redacted_header
, "=<redacted>");
604 * There are more cookies. (Or, for some
605 * reason, the input string ends in "; ".)
607 strbuf_addstr(&redacted_header
, "; ");
608 cookie
= semicolon
+ strlen("; ");
614 strbuf_setlen(header
, sensitive_header
- header
->buf
);
615 strbuf_addbuf(header
, &redacted_header
);
621 static int match_curl_h2_trace(const char *line
, const char **out
)
626 * curl prior to 8.1.0 gives us:
628 * h2h3 [<header-name>: <header-val>]
630 * Starting in 8.1.0, the first token became just "h2".
632 if (skip_iprefix(line
, "h2h3 [", out
) ||
633 skip_iprefix(line
, "h2 [", out
))
638 * [HTTP/2] [<stream-id>] [<header-name>: <header-val>]
639 * where <stream-id> is numeric.
641 if (skip_iprefix(line
, "[HTTP/2] [", &p
)) {
644 if (skip_prefix(p
, "] [", out
))
651 /* Redact headers in info */
652 static void redact_sensitive_info_header(struct strbuf
*header
)
654 const char *sensitive_header
;
656 if (trace_curl_redact
&&
657 match_curl_h2_trace(header
->buf
, &sensitive_header
)) {
658 if (redact_sensitive_header(header
, sensitive_header
- header
->buf
)) {
659 /* redaction ate our closing bracket */
660 strbuf_addch(header
, ']');
665 static void curl_dump_header(const char *text
, unsigned char *ptr
, size_t size
, int hide_sensitive_header
)
667 struct strbuf out
= STRBUF_INIT
;
668 struct strbuf
**headers
, **header
;
670 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
671 text
, (long)size
, (long)size
);
672 trace_strbuf(&trace_curl
, &out
);
674 strbuf_add(&out
, ptr
, size
);
675 headers
= strbuf_split_max(&out
, '\n', 0);
677 for (header
= headers
; *header
; header
++) {
678 if (hide_sensitive_header
)
679 redact_sensitive_header(*header
, 0);
680 strbuf_insertstr((*header
), 0, text
);
681 strbuf_insertstr((*header
), strlen(text
), ": ");
682 strbuf_rtrim((*header
));
683 strbuf_addch((*header
), '\n');
684 trace_strbuf(&trace_curl
, (*header
));
686 strbuf_list_free(headers
);
687 strbuf_release(&out
);
690 static void curl_dump_data(const char *text
, unsigned char *ptr
, size_t size
)
693 struct strbuf out
= STRBUF_INIT
;
694 unsigned int width
= 60;
696 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
697 text
, (long)size
, (long)size
);
698 trace_strbuf(&trace_curl
, &out
);
700 for (i
= 0; i
< size
; i
+= width
) {
704 strbuf_addf(&out
, "%s: ", text
);
705 for (w
= 0; (w
< width
) && (i
+ w
< size
); w
++) {
706 unsigned char ch
= ptr
[i
+ w
];
709 (ch
>= 0x20) && (ch
< 0x80)
712 strbuf_addch(&out
, '\n');
713 trace_strbuf(&trace_curl
, &out
);
715 strbuf_release(&out
);
718 static void curl_dump_info(char *data
, size_t size
)
720 struct strbuf buf
= STRBUF_INIT
;
722 strbuf_add(&buf
, data
, size
);
724 redact_sensitive_info_header(&buf
);
725 trace_printf_key(&trace_curl
, "== Info: %s", buf
.buf
);
727 strbuf_release(&buf
);
730 static int curl_trace(CURL
*handle
, curl_infotype type
, char *data
, size_t size
, void *userp
)
733 enum { NO_FILTER
= 0, DO_FILTER
= 1 };
737 curl_dump_info(data
, size
);
739 case CURLINFO_HEADER_OUT
:
740 text
= "=> Send header";
741 curl_dump_header(text
, (unsigned char *)data
, size
, DO_FILTER
);
743 case CURLINFO_DATA_OUT
:
744 if (trace_curl_data
) {
745 text
= "=> Send data";
746 curl_dump_data(text
, (unsigned char *)data
, size
);
749 case CURLINFO_SSL_DATA_OUT
:
750 if (trace_curl_data
) {
751 text
= "=> Send SSL data";
752 curl_dump_data(text
, (unsigned char *)data
, size
);
755 case CURLINFO_HEADER_IN
:
756 text
= "<= Recv header";
757 curl_dump_header(text
, (unsigned char *)data
, size
, NO_FILTER
);
759 case CURLINFO_DATA_IN
:
760 if (trace_curl_data
) {
761 text
= "<= Recv data";
762 curl_dump_data(text
, (unsigned char *)data
, size
);
765 case CURLINFO_SSL_DATA_IN
:
766 if (trace_curl_data
) {
767 text
= "<= Recv SSL data";
768 curl_dump_data(text
, (unsigned char *)data
, size
);
772 default: /* we ignore unknown types by default */
778 void http_trace_curl_no_data(void)
780 trace_override_envvar(&trace_curl
, "1");
784 void setup_curl_trace(CURL
*handle
)
786 if (!trace_want(&trace_curl
))
788 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
789 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, curl_trace
);
790 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, NULL
);
793 static void proto_list_append(struct strbuf
*list
, const char *proto
)
798 strbuf_addch(list
, ',');
799 strbuf_addstr(list
, proto
);
802 static long get_curl_allowed_protocols(int from_user
, struct strbuf
*list
)
806 if (is_transport_allowed("http", from_user
)) {
807 bits
|= CURLPROTO_HTTP
;
808 proto_list_append(list
, "http");
810 if (is_transport_allowed("https", from_user
)) {
811 bits
|= CURLPROTO_HTTPS
;
812 proto_list_append(list
, "https");
814 if (is_transport_allowed("ftp", from_user
)) {
815 bits
|= CURLPROTO_FTP
;
816 proto_list_append(list
, "ftp");
818 if (is_transport_allowed("ftps", from_user
)) {
819 bits
|= CURLPROTO_FTPS
;
820 proto_list_append(list
, "ftps");
826 #ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
827 static int get_curl_http_version_opt(const char *version_string
, long *opt
)
834 { "HTTP/1.1", CURL_HTTP_VERSION_1_1
},
835 { "HTTP/2", CURL_HTTP_VERSION_2
}
838 for (i
= 0; i
< ARRAY_SIZE(choice
); i
++) {
839 if (!strcmp(version_string
, choice
[i
].name
)) {
840 *opt
= choice
[i
].opt_token
;
845 warning("unknown value given to http.version: '%s'", version_string
);
846 return -1; /* not found */
851 static CURL
*get_curl_handle(void)
853 CURL
*result
= curl_easy_init();
856 die("curl_easy_init failed");
858 if (!curl_ssl_verify
) {
859 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
860 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
862 /* Verify authenticity of the peer's certificate */
863 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
864 /* The name in the cert must match whom we tried to connect */
865 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
868 #ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
869 if (curl_http_version
) {
871 if (!get_curl_http_version_opt(curl_http_version
, &opt
)) {
872 /* Set request use http version */
873 curl_easy_setopt(result
, CURLOPT_HTTP_VERSION
, opt
);
878 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
879 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
881 #ifdef CURLGSSAPI_DELEGATION_FLAG
884 for (i
= 0; i
< ARRAY_SIZE(curl_deleg_levels
); i
++) {
885 if (!strcmp(curl_deleg
, curl_deleg_levels
[i
].name
)) {
886 curl_easy_setopt(result
, CURLOPT_GSSAPI_DELEGATION
,
887 curl_deleg_levels
[i
].curl_deleg_param
);
891 if (i
== ARRAY_SIZE(curl_deleg_levels
))
892 warning("Unknown delegation method '%s': using default",
897 if (http_ssl_backend
&& !strcmp("schannel", http_ssl_backend
) &&
898 !http_schannel_check_revoke
) {
899 #ifdef GIT_CURL_HAVE_CURLSSLOPT_NO_REVOKE
900 curl_easy_setopt(result
, CURLOPT_SSL_OPTIONS
, CURLSSLOPT_NO_REVOKE
);
902 warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
906 if (http_proactive_auth
)
907 init_curl_http_auth(result
);
909 if (getenv("GIT_SSL_VERSION"))
910 ssl_version
= getenv("GIT_SSL_VERSION");
911 if (ssl_version
&& *ssl_version
) {
913 for (i
= 0; i
< ARRAY_SIZE(sslversions
); i
++) {
914 if (!strcmp(ssl_version
, sslversions
[i
].name
)) {
915 curl_easy_setopt(result
, CURLOPT_SSLVERSION
,
916 sslversions
[i
].ssl_version
);
920 if (i
== ARRAY_SIZE(sslversions
))
921 warning("unsupported ssl version %s: using default",
925 if (getenv("GIT_SSL_CIPHER_LIST"))
926 ssl_cipherlist
= getenv("GIT_SSL_CIPHER_LIST");
927 if (ssl_cipherlist
!= NULL
&& *ssl_cipherlist
)
928 curl_easy_setopt(result
, CURLOPT_SSL_CIPHER_LIST
,
932 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
933 if (has_cert_password())
934 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
936 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
938 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
939 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
941 curl_easy_setopt(result
, CURLOPT_PINNEDPUBLICKEY
, ssl_pinnedkey
);
943 if (http_ssl_backend
&& !strcmp("schannel", http_ssl_backend
) &&
944 !http_schannel_use_ssl_cainfo
) {
945 curl_easy_setopt(result
, CURLOPT_CAINFO
, NULL
);
946 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
947 curl_easy_setopt(result
, CURLOPT_PROXY_CAINFO
, NULL
);
949 } else if (ssl_cainfo
!= NULL
|| http_proxy_ssl_ca_info
!= NULL
) {
951 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
952 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
953 if (http_proxy_ssl_ca_info
)
954 curl_easy_setopt(result
, CURLOPT_PROXY_CAINFO
, http_proxy_ssl_ca_info
);
958 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
959 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
960 curl_low_speed_limit
);
961 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
962 curl_low_speed_time
);
965 curl_easy_setopt(result
, CURLOPT_MAXREDIRS
, 20);
966 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
968 #ifdef GIT_CURL_HAVE_CURLOPT_PROTOCOLS_STR
970 struct strbuf buf
= STRBUF_INIT
;
972 get_curl_allowed_protocols(0, &buf
);
973 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS_STR
, buf
.buf
);
976 get_curl_allowed_protocols(-1, &buf
);
977 curl_easy_setopt(result
, CURLOPT_PROTOCOLS_STR
, buf
.buf
);
978 strbuf_release(&buf
);
981 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS
,
982 get_curl_allowed_protocols(0, NULL
));
983 curl_easy_setopt(result
, CURLOPT_PROTOCOLS
,
984 get_curl_allowed_protocols(-1, NULL
));
987 if (getenv("GIT_CURL_VERBOSE"))
988 http_trace_curl_no_data();
989 setup_curl_trace(result
);
990 if (getenv("GIT_TRACE_CURL_NO_DATA"))
992 if (!git_env_bool("GIT_TRACE_REDACT", 1))
993 trace_curl_redact
= 0;
995 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
996 user_agent
? user_agent
: git_user_agent());
998 if (curl_ftp_no_epsv
)
999 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
1002 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
1005 * CURL also examines these variables as a fallback; but we need to query
1006 * them here in order to decide whether to prompt for missing password (cf.
1007 * init_curl_proxy_auth()).
1009 * Unlike many other common environment variables, these are historically
1010 * lowercase only. It appears that CURL did not know this and implemented
1011 * only uppercase variants, which was later corrected to take both - with
1012 * the exception of http_proxy, which is lowercase only also in CURL. As
1013 * the lowercase versions are the historical quasi-standard, they take
1014 * precedence here, as in CURL.
1016 if (!curl_http_proxy
) {
1017 if (http_auth
.protocol
&& !strcmp(http_auth
.protocol
, "https")) {
1018 var_override(&curl_http_proxy
, getenv("HTTPS_PROXY"));
1019 var_override(&curl_http_proxy
, getenv("https_proxy"));
1021 var_override(&curl_http_proxy
, getenv("http_proxy"));
1023 if (!curl_http_proxy
) {
1024 var_override(&curl_http_proxy
, getenv("ALL_PROXY"));
1025 var_override(&curl_http_proxy
, getenv("all_proxy"));
1029 if (curl_http_proxy
&& curl_http_proxy
[0] == '\0') {
1031 * Handle case with the empty http.proxy value here to keep
1032 * common code clean.
1033 * NB: empty option disables proxying at all.
1035 curl_easy_setopt(result
, CURLOPT_PROXY
, "");
1036 } else if (curl_http_proxy
) {
1037 if (starts_with(curl_http_proxy
, "socks5h"))
1038 curl_easy_setopt(result
,
1039 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5_HOSTNAME
);
1040 else if (starts_with(curl_http_proxy
, "socks5"))
1041 curl_easy_setopt(result
,
1042 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
1043 else if (starts_with(curl_http_proxy
, "socks4a"))
1044 curl_easy_setopt(result
,
1045 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4A
);
1046 else if (starts_with(curl_http_proxy
, "socks"))
1047 curl_easy_setopt(result
,
1048 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4
);
1049 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
1050 else if (starts_with(curl_http_proxy
, "https")) {
1051 curl_easy_setopt(result
, CURLOPT_PROXYTYPE
, CURLPROXY_HTTPS
);
1053 if (http_proxy_ssl_cert
)
1054 curl_easy_setopt(result
, CURLOPT_PROXY_SSLCERT
, http_proxy_ssl_cert
);
1056 if (http_proxy_ssl_key
)
1057 curl_easy_setopt(result
, CURLOPT_PROXY_SSLKEY
, http_proxy_ssl_key
);
1059 if (has_proxy_cert_password())
1060 curl_easy_setopt(result
, CURLOPT_PROXY_KEYPASSWD
, proxy_cert_auth
.password
);
1063 if (strstr(curl_http_proxy
, "://"))
1064 credential_from_url(&proxy_auth
, curl_http_proxy
);
1066 struct strbuf url
= STRBUF_INIT
;
1067 strbuf_addf(&url
, "http://%s", curl_http_proxy
);
1068 credential_from_url(&proxy_auth
, url
.buf
);
1069 strbuf_release(&url
);
1072 if (!proxy_auth
.host
)
1073 die("Invalid proxy URL '%s'", curl_http_proxy
);
1075 curl_easy_setopt(result
, CURLOPT_PROXY
, proxy_auth
.host
);
1076 var_override(&curl_no_proxy
, getenv("NO_PROXY"));
1077 var_override(&curl_no_proxy
, getenv("no_proxy"));
1078 curl_easy_setopt(result
, CURLOPT_NOPROXY
, curl_no_proxy
);
1080 init_curl_proxy_auth(result
);
1082 set_curl_keepalive(result
);
1087 static void set_from_env(const char **var
, const char *envname
)
1089 const char *val
= getenv(envname
);
1094 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
1096 char *low_speed_limit
;
1097 char *low_speed_time
;
1098 char *normalized_url
;
1099 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
1101 config
.section
= "http";
1103 config
.collect_fn
= http_options
;
1104 config
.cascade_fn
= git_default_config
;
1107 http_is_verbose
= 0;
1108 normalized_url
= url_normalize(url
, &config
.url
);
1110 git_config(urlmatch_config_entry
, &config
);
1111 free(normalized_url
);
1112 string_list_clear(&config
.vars
, 1);
1114 #ifdef GIT_CURL_HAVE_CURLSSLSET_NO_BACKENDS
1115 if (http_ssl_backend
) {
1116 const curl_ssl_backend
**backends
;
1117 struct strbuf buf
= STRBUF_INIT
;
1120 switch (curl_global_sslset(-1, http_ssl_backend
, &backends
)) {
1121 case CURLSSLSET_UNKNOWN_BACKEND
:
1122 strbuf_addf(&buf
, _("Unsupported SSL backend '%s'. "
1123 "Supported SSL backends:"),
1125 for (i
= 0; backends
[i
]; i
++)
1126 strbuf_addf(&buf
, "\n\t%s", backends
[i
]->name
);
1128 case CURLSSLSET_NO_BACKENDS
:
1129 die(_("Could not set SSL backend to '%s': "
1130 "cURL was built without SSL backends"),
1132 case CURLSSLSET_TOO_LATE
:
1133 die(_("Could not set SSL backend to '%s': already set"),
1141 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
1142 die("curl_global_init failed");
1144 http_proactive_auth
= proactive_auth
;
1146 if (remote
&& remote
->http_proxy
)
1147 curl_http_proxy
= xstrdup(remote
->http_proxy
);
1150 var_override(&http_proxy_authmethod
, remote
->http_proxy_authmethod
);
1152 pragma_header
= curl_slist_append(http_copy_default_headers(),
1153 "Pragma: no-cache");
1154 no_pragma_header
= curl_slist_append(http_copy_default_headers(),
1158 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1159 if (http_max_requests
)
1160 max_requests
= atoi(http_max_requests
);
1163 curlm
= curl_multi_init();
1165 die("curl_multi_init failed");
1167 if (getenv("GIT_SSL_NO_VERIFY"))
1168 curl_ssl_verify
= 0;
1170 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
1171 set_from_env(&ssl_key
, "GIT_SSL_KEY");
1172 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
1173 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
1175 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
1177 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1178 if (low_speed_limit
)
1179 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1180 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1182 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1184 if (curl_ssl_verify
== -1)
1185 curl_ssl_verify
= 1;
1187 curl_session_count
= 0;
1188 if (max_requests
< 1)
1189 max_requests
= DEFAULT_MAX_REQUESTS
;
1191 set_from_env(&http_proxy_ssl_cert
, "GIT_PROXY_SSL_CERT");
1192 set_from_env(&http_proxy_ssl_key
, "GIT_PROXY_SSL_KEY");
1193 set_from_env(&http_proxy_ssl_ca_info
, "GIT_PROXY_SSL_CAINFO");
1195 if (getenv("GIT_PROXY_SSL_CERT_PASSWORD_PROTECTED"))
1196 proxy_ssl_cert_password_required
= 1;
1198 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1199 curl_ftp_no_epsv
= 1;
1202 credential_from_url(&http_auth
, url
);
1203 if (!ssl_cert_password_required
&&
1204 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
1205 starts_with(url
, "https://"))
1206 ssl_cert_password_required
= 1;
1209 curl_default
= get_curl_handle();
1212 void http_cleanup(void)
1214 struct active_request_slot
*slot
= active_queue_head
;
1216 while (slot
!= NULL
) {
1217 struct active_request_slot
*next
= slot
->next
;
1219 xmulti_remove_handle(slot
);
1220 curl_easy_cleanup(slot
->curl
);
1225 active_queue_head
= NULL
;
1227 curl_easy_cleanup(curl_default
);
1229 curl_multi_cleanup(curlm
);
1230 curl_global_cleanup();
1232 string_list_clear(&extra_http_headers
, 0);
1234 curl_slist_free_all(pragma_header
);
1235 pragma_header
= NULL
;
1237 curl_slist_free_all(no_pragma_header
);
1238 no_pragma_header
= NULL
;
1240 curl_slist_free_all(host_resolutions
);
1241 host_resolutions
= NULL
;
1243 if (curl_http_proxy
) {
1244 free((void *)curl_http_proxy
);
1245 curl_http_proxy
= NULL
;
1248 if (proxy_auth
.password
) {
1249 memset(proxy_auth
.password
, 0, strlen(proxy_auth
.password
));
1250 FREE_AND_NULL(proxy_auth
.password
);
1253 free((void *)curl_proxyuserpwd
);
1254 curl_proxyuserpwd
= NULL
;
1256 free((void *)http_proxy_authmethod
);
1257 http_proxy_authmethod
= NULL
;
1259 if (cert_auth
.password
) {
1260 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
1261 FREE_AND_NULL(cert_auth
.password
);
1263 ssl_cert_password_required
= 0;
1265 if (proxy_cert_auth
.password
) {
1266 memset(proxy_cert_auth
.password
, 0, strlen(proxy_cert_auth
.password
));
1267 FREE_AND_NULL(proxy_cert_auth
.password
);
1269 proxy_ssl_cert_password_required
= 0;
1271 FREE_AND_NULL(cached_accept_language
);
1274 struct active_request_slot
*get_active_slot(void)
1276 struct active_request_slot
*slot
= active_queue_head
;
1277 struct active_request_slot
*newslot
;
1281 /* Wait for a slot to open up if the queue is full */
1282 while (active_requests
>= max_requests
) {
1283 curl_multi_perform(curlm
, &num_transfers
);
1284 if (num_transfers
< active_requests
)
1285 process_curl_messages();
1288 while (slot
!= NULL
&& slot
->in_use
)
1292 newslot
= xmalloc(sizeof(*newslot
));
1293 newslot
->curl
= NULL
;
1294 newslot
->in_use
= 0;
1295 newslot
->next
= NULL
;
1297 slot
= active_queue_head
;
1299 active_queue_head
= newslot
;
1301 while (slot
->next
!= NULL
)
1303 slot
->next
= newslot
;
1309 slot
->curl
= curl_easy_duphandle(curl_default
);
1310 curl_session_count
++;
1315 slot
->results
= NULL
;
1316 slot
->finished
= NULL
;
1317 slot
->callback_data
= NULL
;
1318 slot
->callback_func
= NULL
;
1319 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
1320 if (curl_save_cookies
)
1321 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
1322 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
1323 curl_easy_setopt(slot
->curl
, CURLOPT_RESOLVE
, host_resolutions
);
1324 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
1325 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
1326 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
1327 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
1328 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
1329 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDSIZE
, -1L);
1330 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
1331 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1332 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
1333 curl_easy_setopt(slot
->curl
, CURLOPT_RANGE
, NULL
);
1336 * Default following to off unless "ALWAYS" is configured; this gives
1337 * callers a sane starting point, and they can tweak for individual
1338 * HTTP_FOLLOW_* cases themselves.
1340 if (http_follow_config
== HTTP_FOLLOW_ALWAYS
)
1341 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1343 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 0);
1345 curl_easy_setopt(slot
->curl
, CURLOPT_IPRESOLVE
, git_curl_ipresolve
);
1346 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPAUTH
, http_auth_methods
);
1347 if (http_auth
.password
|| curl_empty_auth_enabled())
1348 init_curl_http_auth(slot
->curl
);
1353 int start_active_slot(struct active_request_slot
*slot
)
1355 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
1358 if (curlm_result
!= CURLM_OK
&&
1359 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
1360 warning("curl_multi_add_handle failed: %s",
1361 curl_multi_strerror(curlm_result
));
1368 * We know there must be something to do, since we just added
1371 curl_multi_perform(curlm
, &num_transfers
);
1377 int (*fill
)(void *);
1378 struct fill_chain
*next
;
1381 static struct fill_chain
*fill_cfg
;
1383 void add_fill_function(void *data
, int (*fill
)(void *))
1385 struct fill_chain
*new_fill
= xmalloc(sizeof(*new_fill
));
1386 struct fill_chain
**linkp
= &fill_cfg
;
1387 new_fill
->data
= data
;
1388 new_fill
->fill
= fill
;
1389 new_fill
->next
= NULL
;
1391 linkp
= &(*linkp
)->next
;
1395 void fill_active_slots(void)
1397 struct active_request_slot
*slot
= active_queue_head
;
1399 while (active_requests
< max_requests
) {
1400 struct fill_chain
*fill
;
1401 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
1402 if (fill
->fill(fill
->data
))
1409 while (slot
!= NULL
) {
1410 if (!slot
->in_use
&& slot
->curl
!= NULL
1411 && curl_session_count
> min_curl_sessions
) {
1412 curl_easy_cleanup(slot
->curl
);
1414 curl_session_count
--;
1420 void step_active_slots(void)
1423 CURLMcode curlm_result
;
1426 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
1427 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
1428 if (num_transfers
< active_requests
) {
1429 process_curl_messages();
1430 fill_active_slots();
1434 void run_active_slot(struct active_request_slot
*slot
)
1440 struct timeval select_timeout
;
1443 slot
->finished
= &finished
;
1445 step_active_slots();
1449 curl_multi_timeout(curlm
, &curl_timeout
);
1450 if (curl_timeout
== 0) {
1452 } else if (curl_timeout
== -1) {
1453 select_timeout
.tv_sec
= 0;
1454 select_timeout
.tv_usec
= 50000;
1456 select_timeout
.tv_sec
= curl_timeout
/ 1000;
1457 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
1464 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
1467 * It can happen that curl_multi_timeout returns a pathologically
1468 * long timeout when curl_multi_fdset returns no file descriptors
1469 * to read. See commit message for more details.
1472 (select_timeout
.tv_sec
> 0 ||
1473 select_timeout
.tv_usec
> 50000)) {
1474 select_timeout
.tv_sec
= 0;
1475 select_timeout
.tv_usec
= 50000;
1478 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
1483 * The value of slot->finished we set before the loop was used
1484 * to set our "finished" variable when our request completed.
1486 * 1. The slot may not have been reused for another requst
1487 * yet, in which case it still has &finished.
1489 * 2. The slot may already be in-use to serve another request,
1490 * which can further be divided into two cases:
1492 * (a) If call run_active_slot() hasn't been called for that
1493 * other request, slot->finished would have been cleared
1494 * by get_active_slot() and has NULL.
1496 * (b) If the request did call run_active_slot(), then the
1497 * call would have updated slot->finished at the beginning
1498 * of this function, and with the clearing of the member
1499 * below, we would find that slot->finished is now NULL.
1501 * In all cases, slot->finished has no useful information to
1502 * anybody at this point. Some compilers warn us for
1503 * attempting to smuggle a pointer that is about to become
1504 * invalid, i.e. &finished. We clear it here to assure them.
1506 slot
->finished
= NULL
;
1509 static void release_active_slot(struct active_request_slot
*slot
)
1511 closedown_active_slot(slot
);
1513 xmulti_remove_handle(slot
);
1514 if (curl_session_count
> min_curl_sessions
) {
1515 curl_easy_cleanup(slot
->curl
);
1517 curl_session_count
--;
1520 fill_active_slots();
1523 void finish_all_active_slots(void)
1525 struct active_request_slot
*slot
= active_queue_head
;
1527 while (slot
!= NULL
)
1529 run_active_slot(slot
);
1530 slot
= active_queue_head
;
1536 /* Helpers for modifying and creating URLs */
1537 static inline int needs_quote(int ch
)
1539 if (((ch
>= 'A') && (ch
<= 'Z'))
1540 || ((ch
>= 'a') && (ch
<= 'z'))
1541 || ((ch
>= '0') && (ch
<= '9'))
1549 static char *quote_ref_url(const char *base
, const char *ref
)
1551 struct strbuf buf
= STRBUF_INIT
;
1555 end_url_with_slash(&buf
, base
);
1557 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
1558 if (needs_quote(ch
))
1559 strbuf_addf(&buf
, "%%%02x", ch
);
1561 strbuf_addch(&buf
, *cp
);
1563 return strbuf_detach(&buf
, NULL
);
1566 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
1568 int only_two_digit_prefix
)
1570 end_url_with_slash(buf
, url
);
1572 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
1573 if (!only_two_digit_prefix
)
1574 strbuf_addstr(buf
, hex
+ 2);
1577 char *get_remote_object_url(const char *url
, const char *hex
,
1578 int only_two_digit_prefix
)
1580 struct strbuf buf
= STRBUF_INIT
;
1581 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
1582 return strbuf_detach(&buf
, NULL
);
1585 void normalize_curl_result(CURLcode
*result
, long http_code
,
1586 char *errorstr
, size_t errorlen
)
1589 * If we see a failing http code with CURLE_OK, we have turned off
1590 * FAILONERROR (to keep the server's custom error response), and should
1591 * translate the code into failure here.
1593 * Likewise, if we see a redirect (30x code), that means we turned off
1594 * redirect-following, and we should treat the result as an error.
1596 if (*result
== CURLE_OK
&& http_code
>= 300) {
1597 *result
= CURLE_HTTP_RETURNED_ERROR
;
1599 * Normally curl will already have put the "reason phrase"
1600 * from the server into curl_errorstr; unfortunately without
1601 * FAILONERROR it is lost, so we can give only the numeric
1604 xsnprintf(errorstr
, errorlen
,
1605 "The requested URL returned error: %ld",
1610 static int handle_curl_result(struct slot_results
*results
)
1612 normalize_curl_result(&results
->curl_result
, results
->http_code
,
1613 curl_errorstr
, sizeof(curl_errorstr
));
1615 if (results
->curl_result
== CURLE_OK
) {
1616 credential_approve(&http_auth
);
1617 credential_approve(&proxy_auth
);
1618 credential_approve(&cert_auth
);
1620 } else if (results
->curl_result
== CURLE_SSL_CERTPROBLEM
) {
1622 * We can't tell from here whether it's a bad path, bad
1623 * certificate, bad password, or something else wrong
1624 * with the certificate. So we reject the credential to
1625 * avoid caching or saving a bad password.
1627 credential_reject(&cert_auth
);
1629 #ifdef GIT_CURL_HAVE_CURLE_SSL_PINNEDPUBKEYNOTMATCH
1630 } else if (results
->curl_result
== CURLE_SSL_PINNEDPUBKEYNOTMATCH
) {
1631 return HTTP_NOMATCHPUBLICKEY
;
1633 } else if (missing_target(results
))
1634 return HTTP_MISSING_TARGET
;
1635 else if (results
->http_code
== 401) {
1636 if (http_auth
.username
&& http_auth
.password
) {
1637 credential_reject(&http_auth
);
1640 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
1641 if (results
->auth_avail
) {
1642 http_auth_methods
&= results
->auth_avail
;
1643 http_auth_methods_restricted
= 1;
1648 if (results
->http_connectcode
== 407)
1649 credential_reject(&proxy_auth
);
1650 if (!curl_errorstr
[0])
1651 strlcpy(curl_errorstr
,
1652 curl_easy_strerror(results
->curl_result
),
1653 sizeof(curl_errorstr
));
1658 int run_one_slot(struct active_request_slot
*slot
,
1659 struct slot_results
*results
)
1661 slot
->results
= results
;
1662 if (!start_active_slot(slot
)) {
1663 xsnprintf(curl_errorstr
, sizeof(curl_errorstr
),
1664 "failed to start HTTP request");
1665 return HTTP_START_FAILED
;
1668 run_active_slot(slot
);
1669 return handle_curl_result(results
);
1672 struct curl_slist
*http_copy_default_headers(void)
1674 struct curl_slist
*headers
= NULL
;
1675 const struct string_list_item
*item
;
1677 for_each_string_list_item(item
, &extra_http_headers
)
1678 headers
= curl_slist_append(headers
, item
->string
);
1683 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
1689 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
1691 strbuf_addstr(buf
, ptr
);
1696 * Check for and extract a content-type parameter. "raw"
1697 * should be positioned at the start of the potential
1698 * parameter, with any whitespace already removed.
1700 * "name" is the name of the parameter. The value is appended
1703 static int extract_param(const char *raw
, const char *name
,
1706 size_t len
= strlen(name
);
1708 if (strncasecmp(raw
, name
, len
))
1716 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
1717 strbuf_addch(out
, *raw
++);
1722 * Extract a normalized version of the content type, with any
1723 * spaces suppressed, all letters lowercased, and no trailing ";"
1726 * Note that we will silently remove even invalid whitespace. For
1727 * example, "text / plain" is specifically forbidden by RFC 2616,
1728 * but "text/plain" is the only reasonable output, and this keeps
1731 * If the "charset" argument is not NULL, store the value of any
1732 * charset parameter there.
1735 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1736 * "text / plain" -> "text/plain"
1738 static void extract_content_type(struct strbuf
*raw
, struct strbuf
*type
,
1739 struct strbuf
*charset
)
1744 strbuf_grow(type
, raw
->len
);
1745 for (p
= raw
->buf
; *p
; p
++) {
1752 strbuf_addch(type
, tolower(*p
));
1758 strbuf_reset(charset
);
1760 while (isspace(*p
) || *p
== ';')
1762 if (!extract_param(p
, "charset", charset
))
1764 while (*p
&& !isspace(*p
))
1768 if (!charset
->len
&& starts_with(type
->buf
, "text/"))
1769 strbuf_addstr(charset
, "ISO-8859-1");
1772 static void write_accept_language(struct strbuf
*buf
)
1775 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1776 * that, q-value will be smaller than 0.001, the minimum q-value the
1777 * HTTP specification allows. See
1778 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1780 const int MAX_DECIMAL_PLACES
= 3;
1781 const int MAX_LANGUAGE_TAGS
= 1000;
1782 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE
= 4000;
1783 char **language_tags
= NULL
;
1785 const char *s
= get_preferred_languages();
1787 struct strbuf tag
= STRBUF_INIT
;
1789 /* Don't add Accept-Language header if no language is preferred. */
1794 * Split the colon-separated string of preferred languages into
1795 * language_tags array.
1798 /* collect language tag */
1799 for (; *s
&& (isalnum(*s
) || *s
== '_'); s
++)
1800 strbuf_addch(&tag
, *s
== '_' ? '-' : *s
);
1802 /* skip .codeset, @modifier and any other unnecessary parts */
1803 while (*s
&& *s
!= ':')
1808 REALLOC_ARRAY(language_tags
, num_langs
);
1809 language_tags
[num_langs
- 1] = strbuf_detach(&tag
, NULL
);
1810 if (num_langs
>= MAX_LANGUAGE_TAGS
- 1) /* -1 for '*' */
1815 /* write Accept-Language header into buf */
1817 int last_buf_len
= 0;
1823 REALLOC_ARRAY(language_tags
, num_langs
+ 1);
1824 language_tags
[num_langs
++] = "*"; /* it's OK; this won't be freed */
1826 /* compute decimal_places */
1827 for (max_q
= 1, decimal_places
= 0;
1828 max_q
< num_langs
&& decimal_places
<= MAX_DECIMAL_PLACES
;
1829 decimal_places
++, max_q
*= 10)
1832 xsnprintf(q_format
, sizeof(q_format
), ";q=0.%%0%dd", decimal_places
);
1834 strbuf_addstr(buf
, "Accept-Language: ");
1836 for (i
= 0; i
< num_langs
; i
++) {
1838 strbuf_addstr(buf
, ", ");
1840 strbuf_addstr(buf
, language_tags
[i
]);
1843 strbuf_addf(buf
, q_format
, max_q
- i
);
1845 if (buf
->len
> MAX_ACCEPT_LANGUAGE_HEADER_SIZE
) {
1846 strbuf_remove(buf
, last_buf_len
, buf
->len
- last_buf_len
);
1850 last_buf_len
= buf
->len
;
1854 /* free language tags -- last one is a static '*' */
1855 for (i
= 0; i
< num_langs
- 1; i
++)
1856 free(language_tags
[i
]);
1857 free(language_tags
);
1861 * Get an Accept-Language header which indicates user's preferred languages.
1865 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1866 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1867 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1868 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1869 * LANGUAGE= LANG=C -> ""
1871 const char *http_get_accept_language_header(void)
1873 if (!cached_accept_language
) {
1874 struct strbuf buf
= STRBUF_INIT
;
1875 write_accept_language(&buf
);
1877 cached_accept_language
= strbuf_detach(&buf
, NULL
);
1880 return cached_accept_language
;
1883 static void http_opt_request_remainder(CURL
*curl
, off_t pos
)
1886 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
"-", (uintmax_t)pos
);
1887 curl_easy_setopt(curl
, CURLOPT_RANGE
, buf
);
1890 /* http_request() targets */
1891 #define HTTP_REQUEST_STRBUF 0
1892 #define HTTP_REQUEST_FILE 1
1894 static int http_request(const char *url
,
1895 void *result
, int target
,
1896 const struct http_get_options
*options
)
1898 struct active_request_slot
*slot
;
1899 struct slot_results results
;
1900 struct curl_slist
*headers
= http_copy_default_headers();
1901 struct strbuf buf
= STRBUF_INIT
;
1902 const char *accept_language
;
1905 slot
= get_active_slot();
1906 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1909 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
1911 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1912 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, result
);
1914 if (target
== HTTP_REQUEST_FILE
) {
1915 off_t posn
= ftello(result
);
1916 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1919 http_opt_request_remainder(slot
->curl
, posn
);
1921 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1925 accept_language
= http_get_accept_language_header();
1927 if (accept_language
)
1928 headers
= curl_slist_append(headers
, accept_language
);
1930 strbuf_addstr(&buf
, "Pragma:");
1931 if (options
&& options
->no_cache
)
1932 strbuf_addstr(&buf
, " no-cache");
1933 if (options
&& options
->initial_request
&&
1934 http_follow_config
== HTTP_FOLLOW_INITIAL
)
1935 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1937 headers
= curl_slist_append(headers
, buf
.buf
);
1939 /* Add additional headers here */
1940 if (options
&& options
->extra_headers
) {
1941 const struct string_list_item
*item
;
1942 for_each_string_list_item(item
, options
->extra_headers
) {
1943 headers
= curl_slist_append(headers
, item
->string
);
1947 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1948 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1949 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
1950 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1952 ret
= run_one_slot(slot
, &results
);
1954 if (options
&& options
->content_type
) {
1955 struct strbuf raw
= STRBUF_INIT
;
1956 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
, &raw
);
1957 extract_content_type(&raw
, options
->content_type
,
1959 strbuf_release(&raw
);
1962 if (options
&& options
->effective_url
)
1963 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
1964 options
->effective_url
);
1966 curl_slist_free_all(headers
);
1967 strbuf_release(&buf
);
1973 * Update the "base" url to a more appropriate value, as deduced by
1974 * redirects seen when requesting a URL starting with "url".
1976 * The "asked" parameter is a URL that we asked curl to access, and must begin
1979 * The "got" parameter is the URL that curl reported to us as where we ended
1982 * Returns 1 if we updated the base url, 0 otherwise.
1984 * Our basic strategy is to compare "base" and "asked" to find the bits
1985 * specific to our request. We then strip those bits off of "got" to yield the
1986 * new base. So for example, if our base is "http://example.com/foo.git",
1987 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1988 * with "https://other.example.com/foo.git/info/refs". We would want the
1989 * new URL to become "https://other.example.com/foo.git".
1991 * Note that this assumes a sane redirect scheme. It's entirely possible
1992 * in the example above to end up at a URL that does not even end in
1993 * "info/refs". In such a case we die. There's not much we can do, such a
1994 * scheme is unlikely to represent a real git repository, and failing to
1995 * rewrite the base opens options for malicious redirects to do funny things.
1997 static int update_url_from_redirect(struct strbuf
*base
,
1999 const struct strbuf
*got
)
2004 if (!strcmp(asked
, got
->buf
))
2007 if (!skip_prefix(asked
, base
->buf
, &tail
))
2008 BUG("update_url_from_redirect: %s is not a superset of %s",
2012 if (!strip_suffix_mem(got
->buf
, &new_len
, tail
))
2013 die(_("unable to update url base from redirection:\n"
2019 strbuf_add(base
, got
->buf
, new_len
);
2024 static int http_request_reauth(const char *url
,
2025 void *result
, int target
,
2026 struct http_get_options
*options
)
2028 int ret
= http_request(url
, result
, target
, options
);
2030 if (ret
!= HTTP_OK
&& ret
!= HTTP_REAUTH
)
2033 if (options
&& options
->effective_url
&& options
->base_url
) {
2034 if (update_url_from_redirect(options
->base_url
,
2035 url
, options
->effective_url
)) {
2036 credential_from_url(&http_auth
, options
->base_url
->buf
);
2037 url
= options
->effective_url
->buf
;
2041 if (ret
!= HTTP_REAUTH
)
2045 * The previous request may have put cruft into our output stream; we
2046 * should clear it out before making our next request.
2049 case HTTP_REQUEST_STRBUF
:
2050 strbuf_reset(result
);
2052 case HTTP_REQUEST_FILE
:
2053 if (fflush(result
)) {
2054 error_errno("unable to flush a file");
2055 return HTTP_START_FAILED
;
2058 if (ftruncate(fileno(result
), 0) < 0) {
2059 error_errno("unable to truncate a file");
2060 return HTTP_START_FAILED
;
2064 BUG("Unknown http_request target");
2067 credential_fill(&http_auth
);
2069 return http_request(url
, result
, target
, options
);
2072 int http_get_strbuf(const char *url
,
2073 struct strbuf
*result
,
2074 struct http_get_options
*options
)
2076 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
2080 * Downloads a URL and stores the result in the given file.
2082 * If a previous interrupted download is detected (i.e. a previous temporary
2083 * file is still around) the download is resumed.
2085 int http_get_file(const char *url
, const char *filename
,
2086 struct http_get_options
*options
)
2089 struct strbuf tmpfile
= STRBUF_INIT
;
2092 strbuf_addf(&tmpfile
, "%s.temp", filename
);
2093 result
= fopen(tmpfile
.buf
, "a");
2095 error("Unable to open local file %s", tmpfile
.buf
);
2100 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
2103 if (ret
== HTTP_OK
&& finalize_object_file(tmpfile
.buf
, filename
))
2106 strbuf_release(&tmpfile
);
2110 int http_fetch_ref(const char *base
, struct ref
*ref
)
2112 struct http_get_options options
= {0};
2114 struct strbuf buffer
= STRBUF_INIT
;
2117 options
.no_cache
= 1;
2119 url
= quote_ref_url(base
, ref
->name
);
2120 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
2121 strbuf_rtrim(&buffer
);
2122 if (buffer
.len
== the_hash_algo
->hexsz
)
2123 ret
= get_oid_hex(buffer
.buf
, &ref
->old_oid
);
2124 else if (starts_with(buffer
.buf
, "ref: ")) {
2125 ref
->symref
= xstrdup(buffer
.buf
+ 5);
2130 strbuf_release(&buffer
);
2135 /* Helpers for fetching packs */
2136 static char *fetch_pack_index(unsigned char *hash
, const char *base_url
)
2139 struct strbuf buf
= STRBUF_INIT
;
2141 if (http_is_verbose
)
2142 fprintf(stderr
, "Getting index for pack %s\n", hash_to_hex(hash
));
2144 end_url_with_slash(&buf
, base_url
);
2145 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", hash_to_hex(hash
));
2146 url
= strbuf_detach(&buf
, NULL
);
2148 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(hash
));
2149 tmp
= strbuf_detach(&buf
, NULL
);
2151 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
2152 error("Unable to get pack index %s", url
);
2160 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
2161 unsigned char *sha1
, const char *base_url
)
2163 struct packed_git
*new_pack
;
2164 char *tmp_idx
= NULL
;
2167 if (has_pack_index(sha1
)) {
2168 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
2170 return -1; /* parse_pack_index() already issued error message */
2174 tmp_idx
= fetch_pack_index(sha1
, base_url
);
2178 new_pack
= parse_pack_index(sha1
, tmp_idx
);
2183 return -1; /* parse_pack_index() already issued error message */
2186 ret
= verify_pack_index(new_pack
);
2188 close_pack_index(new_pack
);
2189 ret
= finalize_object_file(tmp_idx
, sha1_pack_index_name(sha1
));
2196 new_pack
->next
= *packs_head
;
2197 *packs_head
= new_pack
;
2201 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
2203 struct http_get_options options
= {0};
2207 struct strbuf buf
= STRBUF_INIT
;
2208 struct object_id oid
;
2210 end_url_with_slash(&buf
, base_url
);
2211 strbuf_addstr(&buf
, "objects/info/packs");
2212 url
= strbuf_detach(&buf
, NULL
);
2214 options
.no_cache
= 1;
2215 ret
= http_get_strbuf(url
, &buf
, &options
);
2221 if (skip_prefix(data
, "P pack-", &data
) &&
2222 !parse_oid_hex(data
, &oid
, &data
) &&
2223 skip_prefix(data
, ".pack", &data
) &&
2224 (*data
== '\n' || *data
== '\0')) {
2225 fetch_and_setup_pack_index(packs_head
, oid
.hash
, base_url
);
2227 data
= strchrnul(data
, '\n');
2230 data
++; /* skip past newline */
2238 void release_http_pack_request(struct http_pack_request
*preq
)
2240 if (preq
->packfile
) {
2241 fclose(preq
->packfile
);
2242 preq
->packfile
= NULL
;
2245 strbuf_release(&preq
->tmpfile
);
2250 static const char *default_index_pack_args
[] =
2251 {"index-pack", "--stdin", NULL
};
2253 int finish_http_pack_request(struct http_pack_request
*preq
)
2255 struct child_process ip
= CHILD_PROCESS_INIT
;
2259 fclose(preq
->packfile
);
2260 preq
->packfile
= NULL
;
2262 tmpfile_fd
= xopen(preq
->tmpfile
.buf
, O_RDONLY
);
2266 strvec_pushv(&ip
.args
, preq
->index_pack_args
?
2267 preq
->index_pack_args
:
2268 default_index_pack_args
);
2270 if (preq
->preserve_index_pack_stdout
)
2275 if (run_command(&ip
)) {
2282 unlink(preq
->tmpfile
.buf
);
2286 void http_install_packfile(struct packed_git
*p
,
2287 struct packed_git
**list_to_remove_from
)
2289 struct packed_git
**lst
= list_to_remove_from
;
2292 lst
= &((*lst
)->next
);
2293 *lst
= (*lst
)->next
;
2295 install_packed_git(the_repository
, p
);
2298 struct http_pack_request
*new_http_pack_request(
2299 const unsigned char *packed_git_hash
, const char *base_url
) {
2301 struct strbuf buf
= STRBUF_INIT
;
2303 end_url_with_slash(&buf
, base_url
);
2304 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
2305 hash_to_hex(packed_git_hash
));
2306 return new_direct_http_pack_request(packed_git_hash
,
2307 strbuf_detach(&buf
, NULL
));
2310 struct http_pack_request
*new_direct_http_pack_request(
2311 const unsigned char *packed_git_hash
, char *url
)
2313 off_t prev_posn
= 0;
2314 struct http_pack_request
*preq
;
2316 CALLOC_ARRAY(preq
, 1);
2317 strbuf_init(&preq
->tmpfile
, 0);
2321 strbuf_addf(&preq
->tmpfile
, "%s.temp", sha1_pack_name(packed_git_hash
));
2322 preq
->packfile
= fopen(preq
->tmpfile
.buf
, "a");
2323 if (!preq
->packfile
) {
2324 error("Unable to open local file %s for pack",
2329 preq
->slot
= get_active_slot();
2330 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEDATA
, preq
->packfile
);
2331 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
2332 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
2333 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
2337 * If there is data present from a previous transfer attempt,
2338 * resume where it left off
2340 prev_posn
= ftello(preq
->packfile
);
2342 if (http_is_verbose
)
2344 "Resuming fetch of pack %s at byte %"PRIuMAX
"\n",
2345 hash_to_hex(packed_git_hash
),
2346 (uintmax_t)prev_posn
);
2347 http_opt_request_remainder(preq
->slot
->curl
, prev_posn
);
2353 strbuf_release(&preq
->tmpfile
);
2359 /* Helpers for fetching objects (loose) */
2360 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
2363 unsigned char expn
[4096];
2364 size_t size
= eltsize
* nmemb
;
2366 struct http_object_request
*freq
= data
;
2367 struct active_request_slot
*slot
= freq
->slot
;
2370 CURLcode c
= curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
,
2373 BUG("curl_easy_getinfo for HTTP code failed: %s",
2374 curl_easy_strerror(c
));
2375 if (slot
->http_code
>= 300)
2380 ssize_t retval
= xwrite(freq
->localfile
,
2381 (char *) ptr
+ posn
, size
- posn
);
2383 return posn
/ eltsize
;
2385 } while (posn
< size
);
2387 freq
->stream
.avail_in
= size
;
2388 freq
->stream
.next_in
= (void *)ptr
;
2390 freq
->stream
.next_out
= expn
;
2391 freq
->stream
.avail_out
= sizeof(expn
);
2392 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
2393 the_hash_algo
->update_fn(&freq
->c
, expn
,
2394 sizeof(expn
) - freq
->stream
.avail_out
);
2395 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
2399 struct http_object_request
*new_http_object_request(const char *base_url
,
2400 const struct object_id
*oid
)
2402 char *hex
= oid_to_hex(oid
);
2403 struct strbuf filename
= STRBUF_INIT
;
2404 struct strbuf prevfile
= STRBUF_INIT
;
2406 char prev_buf
[PREV_BUF_SIZE
];
2407 ssize_t prev_read
= 0;
2408 off_t prev_posn
= 0;
2409 struct http_object_request
*freq
;
2411 CALLOC_ARRAY(freq
, 1);
2412 strbuf_init(&freq
->tmpfile
, 0);
2413 oidcpy(&freq
->oid
, oid
);
2414 freq
->localfile
= -1;
2416 loose_object_path(the_repository
, &filename
, oid
);
2417 strbuf_addf(&freq
->tmpfile
, "%s.temp", filename
.buf
);
2419 strbuf_addf(&prevfile
, "%s.prev", filename
.buf
);
2420 unlink_or_warn(prevfile
.buf
);
2421 rename(freq
->tmpfile
.buf
, prevfile
.buf
);
2422 unlink_or_warn(freq
->tmpfile
.buf
);
2423 strbuf_release(&filename
);
2425 if (freq
->localfile
!= -1)
2426 error("fd leakage in start: %d", freq
->localfile
);
2427 freq
->localfile
= open(freq
->tmpfile
.buf
,
2428 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2430 * This could have failed due to the "lazy directory creation";
2431 * try to mkdir the last path component.
2433 if (freq
->localfile
< 0 && errno
== ENOENT
) {
2434 char *dir
= strrchr(freq
->tmpfile
.buf
, '/');
2437 mkdir(freq
->tmpfile
.buf
, 0777);
2440 freq
->localfile
= open(freq
->tmpfile
.buf
,
2441 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2444 if (freq
->localfile
< 0) {
2445 error_errno("Couldn't create temporary file %s",
2450 git_inflate_init(&freq
->stream
);
2452 the_hash_algo
->init_fn(&freq
->c
);
2454 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
2457 * If a previous temp file is present, process what was already
2460 prevlocal
= open(prevfile
.buf
, O_RDONLY
);
2461 if (prevlocal
!= -1) {
2463 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
2465 if (fwrite_sha1_file(prev_buf
,
2468 freq
) == prev_read
) {
2469 prev_posn
+= prev_read
;
2474 } while (prev_read
> 0);
2477 unlink_or_warn(prevfile
.buf
);
2478 strbuf_release(&prevfile
);
2481 * Reset inflate/SHA1 if there was an error reading the previous temp
2482 * file; also rewind to the beginning of the local file.
2484 if (prev_read
== -1) {
2485 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
2486 git_inflate_init(&freq
->stream
);
2487 the_hash_algo
->init_fn(&freq
->c
);
2490 lseek(freq
->localfile
, 0, SEEK_SET
);
2491 if (ftruncate(freq
->localfile
, 0) < 0) {
2492 error_errno("Couldn't truncate temporary file %s",
2499 freq
->slot
= get_active_slot();
2501 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEDATA
, freq
);
2502 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FAILONERROR
, 0);
2503 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
2504 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
2505 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
2506 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
2509 * If we have successfully processed data from a previous fetch
2510 * attempt, only fetch the data we don't already have.
2513 if (http_is_verbose
)
2515 "Resuming fetch of object %s at byte %"PRIuMAX
"\n",
2516 hex
, (uintmax_t)prev_posn
);
2517 http_opt_request_remainder(freq
->slot
->curl
, prev_posn
);
2523 strbuf_release(&prevfile
);
2529 void process_http_object_request(struct http_object_request
*freq
)
2533 freq
->curl_result
= freq
->slot
->curl_result
;
2534 freq
->http_code
= freq
->slot
->http_code
;
2538 int finish_http_object_request(struct http_object_request
*freq
)
2541 struct strbuf filename
= STRBUF_INIT
;
2543 close(freq
->localfile
);
2544 freq
->localfile
= -1;
2546 process_http_object_request(freq
);
2548 if (freq
->http_code
== 416) {
2549 warning("requested range invalid; we may already have all the data.");
2550 } else if (freq
->curl_result
!= CURLE_OK
) {
2551 if (stat(freq
->tmpfile
.buf
, &st
) == 0)
2552 if (st
.st_size
== 0)
2553 unlink_or_warn(freq
->tmpfile
.buf
);
2557 git_inflate_end(&freq
->stream
);
2558 the_hash_algo
->final_oid_fn(&freq
->real_oid
, &freq
->c
);
2559 if (freq
->zret
!= Z_STREAM_END
) {
2560 unlink_or_warn(freq
->tmpfile
.buf
);
2563 if (!oideq(&freq
->oid
, &freq
->real_oid
)) {
2564 unlink_or_warn(freq
->tmpfile
.buf
);
2567 loose_object_path(the_repository
, &filename
, &freq
->oid
);
2568 freq
->rename
= finalize_object_file(freq
->tmpfile
.buf
, filename
.buf
);
2569 strbuf_release(&filename
);
2571 return freq
->rename
;
2574 void abort_http_object_request(struct http_object_request
*freq
)
2576 unlink_or_warn(freq
->tmpfile
.buf
);
2578 release_http_object_request(freq
);
2581 void release_http_object_request(struct http_object_request
*freq
)
2583 if (freq
->localfile
!= -1) {
2584 close(freq
->localfile
);
2585 freq
->localfile
= -1;
2587 FREE_AND_NULL(freq
->url
);
2589 freq
->slot
->callback_func
= NULL
;
2590 freq
->slot
->callback_data
= NULL
;
2591 release_active_slot(freq
->slot
);
2594 strbuf_release(&freq
->tmpfile
);