1 #include "git-compat-util.h"
2 #include "git-curl-compat.h"
8 #include "run-command.h"
11 #include "credential.h"
16 #include "transport.h"
19 #include "string-list.h"
20 #include "object-file.h"
21 #include "object-store.h"
23 static struct trace_key trace_curl
= TRACE_KEY_INIT(CURL
);
24 static int trace_curl_data
= 1;
25 static int trace_curl_redact
= 1;
26 long int git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
29 ssize_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
31 static int min_curl_sessions
= 1;
32 static int curl_session_count
;
33 static int max_requests
= -1;
35 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 *curl_http_version
= NULL
;
44 static const char *ssl_cert
;
45 static const char *ssl_cert_type
;
46 static const char *ssl_cipherlist
;
47 static const char *ssl_version
;
52 { "sslv2", CURL_SSLVERSION_SSLv2
},
53 { "sslv3", CURL_SSLVERSION_SSLv3
},
54 { "tlsv1", CURL_SSLVERSION_TLSv1
},
55 #ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_0
56 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0
},
57 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1
},
58 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2
},
60 #ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_3
61 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3
},
64 static const char *ssl_key
;
65 static const char *ssl_key_type
;
66 static const char *ssl_capath
;
67 static const char *curl_no_proxy
;
68 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
69 static const char *ssl_pinnedkey
;
71 static const char *ssl_cainfo
;
72 static long curl_low_speed_limit
= -1;
73 static long curl_low_speed_time
= -1;
74 static int curl_ftp_no_epsv
;
75 static const char *curl_http_proxy
;
76 static const char *http_proxy_authmethod
;
78 static const char *http_proxy_ssl_cert
;
79 static const char *http_proxy_ssl_key
;
80 static const char *http_proxy_ssl_ca_info
;
81 static struct credential proxy_cert_auth
= CREDENTIAL_INIT
;
82 static int proxy_ssl_cert_password_required
;
87 } proxy_authmethods
[] = {
88 { "basic", CURLAUTH_BASIC
},
89 { "digest", CURLAUTH_DIGEST
},
90 { "negotiate", CURLAUTH_GSSNEGOTIATE
},
91 { "ntlm", CURLAUTH_NTLM
},
92 { "anyauth", CURLAUTH_ANY
},
94 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
95 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
99 #ifdef CURLGSSAPI_DELEGATION_FLAG
100 static const char *curl_deleg
;
103 long curl_deleg_param
;
104 } curl_deleg_levels
[] = {
105 { "none", CURLGSSAPI_DELEGATION_NONE
},
106 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG
},
107 { "always", CURLGSSAPI_DELEGATION_FLAG
},
111 static struct credential proxy_auth
= CREDENTIAL_INIT
;
112 static const char *curl_proxyuserpwd
;
113 static const char *curl_cookie_file
;
114 static int curl_save_cookies
;
115 struct credential http_auth
= CREDENTIAL_INIT
;
116 static int http_proactive_auth
;
117 static const char *user_agent
;
118 static int curl_empty_auth
= -1;
120 enum http_follow_config http_follow_config
= HTTP_FOLLOW_INITIAL
;
122 static struct credential cert_auth
= CREDENTIAL_INIT
;
123 static int ssl_cert_password_required
;
124 static unsigned long http_auth_methods
= CURLAUTH_ANY
;
125 static int http_auth_methods_restricted
;
126 /* Modes for which empty_auth cannot actually help us. */
127 static unsigned long empty_auth_useless
=
132 static struct curl_slist
*pragma_header
;
133 static struct curl_slist
*no_pragma_header
;
134 static struct string_list extra_http_headers
= STRING_LIST_INIT_DUP
;
136 static struct curl_slist
*host_resolutions
;
138 static struct active_request_slot
*active_queue_head
;
140 static char *cached_accept_language
;
142 static char *http_ssl_backend
;
144 static int http_schannel_check_revoke
= 1;
146 * With the backend being set to `schannel`, setting sslCAinfo would override
147 * the Certificate Store in cURL v7.60.0 and later, which is not what we want
150 static int http_schannel_use_ssl_cainfo
;
152 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
154 size_t size
= eltsize
* nmemb
;
155 struct buffer
*buffer
= buffer_
;
157 if (size
> buffer
->buf
.len
- buffer
->posn
)
158 size
= buffer
->buf
.len
- buffer
->posn
;
159 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
160 buffer
->posn
+= size
;
162 return size
/ eltsize
;
165 int seek_buffer(void *clientp
, curl_off_t offset
, int origin
)
167 struct buffer
*buffer
= clientp
;
169 if (origin
!= SEEK_SET
)
170 BUG("seek_buffer only handles SEEK_SET");
171 if (offset
< 0 || offset
>= buffer
->buf
.len
) {
172 error("curl seek would be outside of buffer");
173 return CURL_SEEKFUNC_FAIL
;
176 buffer
->posn
= offset
;
177 return CURL_SEEKFUNC_OK
;
180 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
182 size_t size
= eltsize
* nmemb
;
183 struct strbuf
*buffer
= buffer_
;
185 strbuf_add(buffer
, ptr
, size
);
190 * A folded header continuation line starts with any number of spaces or
191 * horizontal tab characters (SP or HTAB) as per RFC 7230 section 3.2.
192 * It is not a continuation line if the line starts with any other character.
194 static inline int is_hdr_continuation(const char *ptr
, const size_t size
)
196 return size
&& (*ptr
== ' ' || *ptr
== '\t');
199 static size_t fwrite_wwwauth(char *ptr
, size_t eltsize
, size_t nmemb
, void *p
)
201 size_t size
= eltsize
* nmemb
;
202 struct strvec
*values
= &http_auth
.wwwauth_headers
;
203 struct strbuf buf
= STRBUF_INIT
;
208 * Header lines may not come NULL-terminated from libcurl so we must
209 * limit all scans to the maximum length of the header line, or leverage
210 * strbufs for all operations.
212 * In addition, it is possible that header values can be split over
213 * multiple lines as per RFC 7230. 'Line folding' has been deprecated
214 * but older servers may still emit them. A continuation header field
215 * value is identified as starting with a space or horizontal tab.
217 * The formal definition of a header field as given in RFC 7230 is:
219 * header-field = field-name ":" OWS field-value OWS
222 * field-value = *( field-content / obs-fold )
223 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
224 * field-vchar = VCHAR / obs-text
226 * obs-fold = CRLF 1*( SP / HTAB )
227 * ; obsolete line folding
228 * ; see Section 3.2.4
231 /* Start of a new WWW-Authenticate header */
232 if (skip_iprefix_mem(ptr
, size
, "www-authenticate:", &val
, &val_len
)) {
233 strbuf_add(&buf
, val
, val_len
);
236 * Strip the CRLF that should be present at the end of each
237 * field as well as any trailing or leading whitespace from the
242 strvec_push(values
, buf
.buf
);
243 http_auth
.header_is_last_match
= 1;
248 * This line could be a continuation of the previously matched header
249 * field. If this is the case then we should append this value to the
250 * end of the previously consumed value.
252 if (http_auth
.header_is_last_match
&& is_hdr_continuation(ptr
, size
)) {
254 * Trim the CRLF and any leading or trailing from this line.
256 strbuf_add(&buf
, ptr
, size
);
260 * At this point we should always have at least one existing
261 * value, even if it is empty. Do not bother appending the new
262 * value if this continuation header is itself empty.
265 BUG("should have at least one existing header value");
266 } else if (buf
.len
) {
267 char *prev
= xstrdup(values
->v
[values
->nr
- 1]);
269 /* Join two non-empty values with a single space. */
270 const char *const sp
= *prev
? " " : "";
273 strvec_pushf(values
, "%s%s%s", prev
, sp
, buf
.buf
);
280 /* Not a continuation of a previously matched auth header line. */
281 http_auth
.header_is_last_match
= 0;
284 * If this is a HTTP status line and not a header field, this signals
285 * a different HTTP response. libcurl writes all the output of all
286 * response headers of all responses, including redirects.
287 * We only care about the last HTTP request response's headers so clear
288 * the existing array.
290 if (skip_iprefix_mem(ptr
, size
, "http/", &val
, &val_len
))
291 strvec_clear(values
);
294 strbuf_release(&buf
);
298 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
303 static void closedown_active_slot(struct active_request_slot
*slot
)
309 static void finish_active_slot(struct active_request_slot
*slot
)
311 closedown_active_slot(slot
);
312 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
315 (*slot
->finished
) = 1;
317 /* Store slot results so they can be read after the slot is reused */
319 slot
->results
->curl_result
= slot
->curl_result
;
320 slot
->results
->http_code
= slot
->http_code
;
321 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
322 &slot
->results
->auth_avail
);
324 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CONNECTCODE
,
325 &slot
->results
->http_connectcode
);
328 /* Run callback if appropriate */
329 if (slot
->callback_func
)
330 slot
->callback_func(slot
->callback_data
);
333 static void xmulti_remove_handle(struct active_request_slot
*slot
)
335 curl_multi_remove_handle(curlm
, slot
->curl
);
338 static void process_curl_messages(void)
341 struct active_request_slot
*slot
;
342 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
344 while (curl_message
!= NULL
) {
345 if (curl_message
->msg
== CURLMSG_DONE
) {
346 int curl_result
= curl_message
->data
.result
;
347 slot
= active_queue_head
;
348 while (slot
!= NULL
&&
349 slot
->curl
!= curl_message
->easy_handle
)
352 xmulti_remove_handle(slot
);
353 slot
->curl_result
= curl_result
;
354 finish_active_slot(slot
);
356 fprintf(stderr
, "Received DONE message for unknown request!\n");
359 fprintf(stderr
, "Unknown CURL message received: %d\n",
360 (int)curl_message
->msg
);
362 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
366 static int http_options(const char *var
, const char *value
, void *cb
)
368 if (!strcmp("http.version", var
)) {
369 return git_config_string(&curl_http_version
, var
, value
);
371 if (!strcmp("http.sslverify", var
)) {
372 curl_ssl_verify
= git_config_bool(var
, value
);
375 if (!strcmp("http.sslcipherlist", var
))
376 return git_config_string(&ssl_cipherlist
, var
, value
);
377 if (!strcmp("http.sslversion", var
))
378 return git_config_string(&ssl_version
, var
, value
);
379 if (!strcmp("http.sslcert", var
))
380 return git_config_pathname(&ssl_cert
, var
, value
);
381 if (!strcmp("http.sslcerttype", var
))
382 return git_config_string(&ssl_cert_type
, var
, value
);
383 if (!strcmp("http.sslkey", var
))
384 return git_config_pathname(&ssl_key
, var
, value
);
385 if (!strcmp("http.sslkeytype", var
))
386 return git_config_string(&ssl_key_type
, var
, value
);
387 if (!strcmp("http.sslcapath", var
))
388 return git_config_pathname(&ssl_capath
, var
, value
);
389 if (!strcmp("http.sslcainfo", var
))
390 return git_config_pathname(&ssl_cainfo
, var
, value
);
391 if (!strcmp("http.sslcertpasswordprotected", var
)) {
392 ssl_cert_password_required
= git_config_bool(var
, value
);
395 if (!strcmp("http.ssltry", var
)) {
396 curl_ssl_try
= git_config_bool(var
, value
);
399 if (!strcmp("http.sslbackend", var
)) {
400 free(http_ssl_backend
);
401 http_ssl_backend
= xstrdup_or_null(value
);
405 if (!strcmp("http.schannelcheckrevoke", var
)) {
406 http_schannel_check_revoke
= git_config_bool(var
, value
);
410 if (!strcmp("http.schannelusesslcainfo", var
)) {
411 http_schannel_use_ssl_cainfo
= git_config_bool(var
, value
);
415 if (!strcmp("http.minsessions", var
)) {
416 min_curl_sessions
= git_config_int(var
, value
);
417 if (min_curl_sessions
> 1)
418 min_curl_sessions
= 1;
421 if (!strcmp("http.maxrequests", var
)) {
422 max_requests
= git_config_int(var
, value
);
425 if (!strcmp("http.lowspeedlimit", var
)) {
426 curl_low_speed_limit
= (long)git_config_int(var
, value
);
429 if (!strcmp("http.lowspeedtime", var
)) {
430 curl_low_speed_time
= (long)git_config_int(var
, value
);
434 if (!strcmp("http.noepsv", var
)) {
435 curl_ftp_no_epsv
= git_config_bool(var
, value
);
438 if (!strcmp("http.proxy", var
))
439 return git_config_string(&curl_http_proxy
, var
, value
);
441 if (!strcmp("http.proxyauthmethod", var
))
442 return git_config_string(&http_proxy_authmethod
, var
, value
);
444 if (!strcmp("http.proxysslcert", var
))
445 return git_config_string(&http_proxy_ssl_cert
, var
, value
);
447 if (!strcmp("http.proxysslkey", var
))
448 return git_config_string(&http_proxy_ssl_key
, var
, value
);
450 if (!strcmp("http.proxysslcainfo", var
))
451 return git_config_string(&http_proxy_ssl_ca_info
, var
, value
);
453 if (!strcmp("http.proxysslcertpasswordprotected", var
)) {
454 proxy_ssl_cert_password_required
= git_config_bool(var
, value
);
458 if (!strcmp("http.cookiefile", var
))
459 return git_config_pathname(&curl_cookie_file
, var
, value
);
460 if (!strcmp("http.savecookies", var
)) {
461 curl_save_cookies
= git_config_bool(var
, value
);
465 if (!strcmp("http.postbuffer", var
)) {
466 http_post_buffer
= git_config_ssize_t(var
, value
);
467 if (http_post_buffer
< 0)
468 warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX
);
469 if (http_post_buffer
< LARGE_PACKET_MAX
)
470 http_post_buffer
= LARGE_PACKET_MAX
;
474 if (!strcmp("http.useragent", var
))
475 return git_config_string(&user_agent
, var
, value
);
477 if (!strcmp("http.emptyauth", var
)) {
478 if (value
&& !strcmp("auto", value
))
479 curl_empty_auth
= -1;
481 curl_empty_auth
= git_config_bool(var
, value
);
485 if (!strcmp("http.delegation", var
)) {
486 #ifdef CURLGSSAPI_DELEGATION_FLAG
487 return git_config_string(&curl_deleg
, var
, value
);
489 warning(_("Delegation control is not supported with cURL < 7.22.0"));
494 if (!strcmp("http.pinnedpubkey", var
)) {
495 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
496 return git_config_pathname(&ssl_pinnedkey
, var
, value
);
498 warning(_("Public key pinning not supported with cURL < 7.39.0"));
503 if (!strcmp("http.extraheader", var
)) {
505 return config_error_nonbool(var
);
506 } else if (!*value
) {
507 string_list_clear(&extra_http_headers
, 0);
509 string_list_append(&extra_http_headers
, value
);
514 if (!strcmp("http.curloptresolve", var
)) {
516 return config_error_nonbool(var
);
517 } else if (!*value
) {
518 curl_slist_free_all(host_resolutions
);
519 host_resolutions
= NULL
;
521 host_resolutions
= curl_slist_append(host_resolutions
, value
);
526 if (!strcmp("http.followredirects", var
)) {
527 if (value
&& !strcmp(value
, "initial"))
528 http_follow_config
= HTTP_FOLLOW_INITIAL
;
529 else if (git_config_bool(var
, value
))
530 http_follow_config
= HTTP_FOLLOW_ALWAYS
;
532 http_follow_config
= HTTP_FOLLOW_NONE
;
536 /* Fall back on the default ones */
537 return git_default_config(var
, value
, cb
);
540 static int curl_empty_auth_enabled(void)
542 if (curl_empty_auth
>= 0)
543 return curl_empty_auth
;
546 * In the automatic case, kick in the empty-auth
547 * hack as long as we would potentially try some
548 * method more exotic than "Basic" or "Digest".
550 * But only do this when this is our second or
551 * subsequent request, as by then we know what
552 * methods are available.
554 if (http_auth_methods_restricted
&&
555 (http_auth_methods
& ~empty_auth_useless
))
560 static void init_curl_http_auth(CURL
*result
)
562 if (!http_auth
.username
|| !*http_auth
.username
) {
563 if (curl_empty_auth_enabled())
564 curl_easy_setopt(result
, CURLOPT_USERPWD
, ":");
568 credential_fill(&http_auth
);
570 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
571 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
574 /* *var must be free-able */
575 static void var_override(const char **var
, char *value
)
579 *var
= xstrdup(value
);
583 static void set_proxyauth_name_password(CURL
*result
)
585 curl_easy_setopt(result
, CURLOPT_PROXYUSERNAME
,
586 proxy_auth
.username
);
587 curl_easy_setopt(result
, CURLOPT_PROXYPASSWORD
,
588 proxy_auth
.password
);
591 static void init_curl_proxy_auth(CURL
*result
)
593 if (proxy_auth
.username
) {
594 if (!proxy_auth
.password
)
595 credential_fill(&proxy_auth
);
596 set_proxyauth_name_password(result
);
599 var_override(&http_proxy_authmethod
, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
601 if (http_proxy_authmethod
) {
603 for (i
= 0; i
< ARRAY_SIZE(proxy_authmethods
); i
++) {
604 if (!strcmp(http_proxy_authmethod
, proxy_authmethods
[i
].name
)) {
605 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
,
606 proxy_authmethods
[i
].curlauth_param
);
610 if (i
== ARRAY_SIZE(proxy_authmethods
)) {
611 warning("unsupported proxy authentication method %s: using anyauth",
612 http_proxy_authmethod
);
613 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
617 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
620 static int has_cert_password(void)
622 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
624 if (!cert_auth
.password
) {
625 cert_auth
.protocol
= xstrdup("cert");
626 cert_auth
.host
= xstrdup("");
627 cert_auth
.username
= xstrdup("");
628 cert_auth
.path
= xstrdup(ssl_cert
);
629 credential_fill(&cert_auth
);
634 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
635 static int has_proxy_cert_password(void)
637 if (http_proxy_ssl_cert
== NULL
|| proxy_ssl_cert_password_required
!= 1)
639 if (!proxy_cert_auth
.password
) {
640 proxy_cert_auth
.protocol
= xstrdup("cert");
641 proxy_cert_auth
.host
= xstrdup("");
642 proxy_cert_auth
.username
= xstrdup("");
643 proxy_cert_auth
.path
= xstrdup(http_proxy_ssl_cert
);
644 credential_fill(&proxy_cert_auth
);
650 #ifdef GITCURL_HAVE_CURLOPT_TCP_KEEPALIVE
651 static void set_curl_keepalive(CURL
*c
)
653 curl_easy_setopt(c
, CURLOPT_TCP_KEEPALIVE
, 1);
657 static int sockopt_callback(void *client
, curl_socket_t fd
, curlsocktype type
)
661 socklen_t len
= (socklen_t
)sizeof(ka
);
663 if (type
!= CURLSOCKTYPE_IPCXN
)
666 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
668 warning_errno("unable to set SO_KEEPALIVE on socket");
670 return CURL_SOCKOPT_OK
;
673 static void set_curl_keepalive(CURL
*c
)
675 curl_easy_setopt(c
, CURLOPT_SOCKOPTFUNCTION
, sockopt_callback
);
679 /* Return 1 if redactions have been made, 0 otherwise. */
680 static int redact_sensitive_header(struct strbuf
*header
, size_t offset
)
683 const char *sensitive_header
;
685 if (trace_curl_redact
&&
686 (skip_iprefix(header
->buf
+ offset
, "Authorization:", &sensitive_header
) ||
687 skip_iprefix(header
->buf
+ offset
, "Proxy-Authorization:", &sensitive_header
))) {
688 /* The first token is the type, which is OK to log */
689 while (isspace(*sensitive_header
))
691 while (*sensitive_header
&& !isspace(*sensitive_header
))
693 /* Everything else is opaque and possibly sensitive */
694 strbuf_setlen(header
, sensitive_header
- header
->buf
);
695 strbuf_addstr(header
, " <redacted>");
697 } else if (trace_curl_redact
&&
698 skip_iprefix(header
->buf
+ offset
, "Cookie:", &sensitive_header
)) {
699 struct strbuf redacted_header
= STRBUF_INIT
;
702 while (isspace(*sensitive_header
))
705 cookie
= sensitive_header
;
709 char *semicolon
= strstr(cookie
, "; ");
712 equals
= strchrnul(cookie
, '=');
714 /* invalid cookie, just append and continue */
715 strbuf_addstr(&redacted_header
, cookie
);
718 strbuf_add(&redacted_header
, cookie
, equals
- cookie
);
719 strbuf_addstr(&redacted_header
, "=<redacted>");
722 * There are more cookies. (Or, for some
723 * reason, the input string ends in "; ".)
725 strbuf_addstr(&redacted_header
, "; ");
726 cookie
= semicolon
+ strlen("; ");
732 strbuf_setlen(header
, sensitive_header
- header
->buf
);
733 strbuf_addbuf(header
, &redacted_header
);
739 /* Redact headers in info */
740 static void redact_sensitive_info_header(struct strbuf
*header
)
742 const char *sensitive_header
;
745 * curl's h2h3 prints headers in info, e.g.:
746 * h2h3 [<header-name>: <header-val>]
748 if (trace_curl_redact
&&
749 (skip_iprefix(header
->buf
, "h2h3 [", &sensitive_header
) ||
750 skip_iprefix(header
->buf
, "h2 [", &sensitive_header
))) {
751 if (redact_sensitive_header(header
, sensitive_header
- header
->buf
)) {
752 /* redaction ate our closing bracket */
753 strbuf_addch(header
, ']');
758 static void curl_dump_header(const char *text
, unsigned char *ptr
, size_t size
, int hide_sensitive_header
)
760 struct strbuf out
= STRBUF_INIT
;
761 struct strbuf
**headers
, **header
;
763 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
764 text
, (long)size
, (long)size
);
765 trace_strbuf(&trace_curl
, &out
);
767 strbuf_add(&out
, ptr
, size
);
768 headers
= strbuf_split_max(&out
, '\n', 0);
770 for (header
= headers
; *header
; header
++) {
771 if (hide_sensitive_header
)
772 redact_sensitive_header(*header
, 0);
773 strbuf_insertstr((*header
), 0, text
);
774 strbuf_insertstr((*header
), strlen(text
), ": ");
775 strbuf_rtrim((*header
));
776 strbuf_addch((*header
), '\n');
777 trace_strbuf(&trace_curl
, (*header
));
779 strbuf_list_free(headers
);
780 strbuf_release(&out
);
783 static void curl_dump_data(const char *text
, unsigned char *ptr
, size_t size
)
786 struct strbuf out
= STRBUF_INIT
;
787 unsigned int width
= 60;
789 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
790 text
, (long)size
, (long)size
);
791 trace_strbuf(&trace_curl
, &out
);
793 for (i
= 0; i
< size
; i
+= width
) {
797 strbuf_addf(&out
, "%s: ", text
);
798 for (w
= 0; (w
< width
) && (i
+ w
< size
); w
++) {
799 unsigned char ch
= ptr
[i
+ w
];
802 (ch
>= 0x20) && (ch
< 0x80)
805 strbuf_addch(&out
, '\n');
806 trace_strbuf(&trace_curl
, &out
);
808 strbuf_release(&out
);
811 static void curl_dump_info(char *data
, size_t size
)
813 struct strbuf buf
= STRBUF_INIT
;
815 strbuf_add(&buf
, data
, size
);
817 redact_sensitive_info_header(&buf
);
818 trace_printf_key(&trace_curl
, "== Info: %s", buf
.buf
);
820 strbuf_release(&buf
);
823 static int curl_trace(CURL
*handle
, curl_infotype type
, char *data
, size_t size
, void *userp
)
826 enum { NO_FILTER
= 0, DO_FILTER
= 1 };
830 curl_dump_info(data
, size
);
832 case CURLINFO_HEADER_OUT
:
833 text
= "=> Send header";
834 curl_dump_header(text
, (unsigned char *)data
, size
, DO_FILTER
);
836 case CURLINFO_DATA_OUT
:
837 if (trace_curl_data
) {
838 text
= "=> Send data";
839 curl_dump_data(text
, (unsigned char *)data
, size
);
842 case CURLINFO_SSL_DATA_OUT
:
843 if (trace_curl_data
) {
844 text
= "=> Send SSL data";
845 curl_dump_data(text
, (unsigned char *)data
, size
);
848 case CURLINFO_HEADER_IN
:
849 text
= "<= Recv header";
850 curl_dump_header(text
, (unsigned char *)data
, size
, NO_FILTER
);
852 case CURLINFO_DATA_IN
:
853 if (trace_curl_data
) {
854 text
= "<= Recv data";
855 curl_dump_data(text
, (unsigned char *)data
, size
);
858 case CURLINFO_SSL_DATA_IN
:
859 if (trace_curl_data
) {
860 text
= "<= Recv SSL data";
861 curl_dump_data(text
, (unsigned char *)data
, size
);
865 default: /* we ignore unknown types by default */
871 void http_trace_curl_no_data(void)
873 trace_override_envvar(&trace_curl
, "1");
877 void setup_curl_trace(CURL
*handle
)
879 if (!trace_want(&trace_curl
))
881 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
882 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, curl_trace
);
883 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, NULL
);
886 static void proto_list_append(struct strbuf
*list
, const char *proto
)
891 strbuf_addch(list
, ',');
892 strbuf_addstr(list
, proto
);
895 static long get_curl_allowed_protocols(int from_user
, struct strbuf
*list
)
899 if (is_transport_allowed("http", from_user
)) {
900 bits
|= CURLPROTO_HTTP
;
901 proto_list_append(list
, "http");
903 if (is_transport_allowed("https", from_user
)) {
904 bits
|= CURLPROTO_HTTPS
;
905 proto_list_append(list
, "https");
907 if (is_transport_allowed("ftp", from_user
)) {
908 bits
|= CURLPROTO_FTP
;
909 proto_list_append(list
, "ftp");
911 if (is_transport_allowed("ftps", from_user
)) {
912 bits
|= CURLPROTO_FTPS
;
913 proto_list_append(list
, "ftps");
919 #ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
920 static int get_curl_http_version_opt(const char *version_string
, long *opt
)
927 { "HTTP/1.1", CURL_HTTP_VERSION_1_1
},
928 { "HTTP/2", CURL_HTTP_VERSION_2
}
931 for (i
= 0; i
< ARRAY_SIZE(choice
); i
++) {
932 if (!strcmp(version_string
, choice
[i
].name
)) {
933 *opt
= choice
[i
].opt_token
;
938 warning("unknown value given to http.version: '%s'", version_string
);
939 return -1; /* not found */
944 static CURL
*get_curl_handle(void)
946 CURL
*result
= curl_easy_init();
949 die("curl_easy_init failed");
951 if (!curl_ssl_verify
) {
952 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
953 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
955 /* Verify authenticity of the peer's certificate */
956 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
957 /* The name in the cert must match whom we tried to connect */
958 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
961 #ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
962 if (curl_http_version
) {
964 if (!get_curl_http_version_opt(curl_http_version
, &opt
)) {
965 /* Set request use http version */
966 curl_easy_setopt(result
, CURLOPT_HTTP_VERSION
, opt
);
971 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
972 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
974 #ifdef CURLGSSAPI_DELEGATION_FLAG
977 for (i
= 0; i
< ARRAY_SIZE(curl_deleg_levels
); i
++) {
978 if (!strcmp(curl_deleg
, curl_deleg_levels
[i
].name
)) {
979 curl_easy_setopt(result
, CURLOPT_GSSAPI_DELEGATION
,
980 curl_deleg_levels
[i
].curl_deleg_param
);
984 if (i
== ARRAY_SIZE(curl_deleg_levels
))
985 warning("Unknown delegation method '%s': using default",
990 if (http_ssl_backend
&& !strcmp("schannel", http_ssl_backend
) &&
991 !http_schannel_check_revoke
) {
992 #ifdef GIT_CURL_HAVE_CURLSSLOPT_NO_REVOKE
993 curl_easy_setopt(result
, CURLOPT_SSL_OPTIONS
, CURLSSLOPT_NO_REVOKE
);
995 warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
999 if (http_proactive_auth
)
1000 init_curl_http_auth(result
);
1002 if (getenv("GIT_SSL_VERSION"))
1003 ssl_version
= getenv("GIT_SSL_VERSION");
1004 if (ssl_version
&& *ssl_version
) {
1006 for (i
= 0; i
< ARRAY_SIZE(sslversions
); i
++) {
1007 if (!strcmp(ssl_version
, sslversions
[i
].name
)) {
1008 curl_easy_setopt(result
, CURLOPT_SSLVERSION
,
1009 sslversions
[i
].ssl_version
);
1013 if (i
== ARRAY_SIZE(sslversions
))
1014 warning("unsupported ssl version %s: using default",
1018 if (getenv("GIT_SSL_CIPHER_LIST"))
1019 ssl_cipherlist
= getenv("GIT_SSL_CIPHER_LIST");
1020 if (ssl_cipherlist
!= NULL
&& *ssl_cipherlist
)
1021 curl_easy_setopt(result
, CURLOPT_SSL_CIPHER_LIST
,
1025 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
1027 curl_easy_setopt(result
, CURLOPT_SSLCERTTYPE
, ssl_cert_type
);
1028 if (has_cert_password())
1029 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
1031 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
1033 curl_easy_setopt(result
, CURLOPT_SSLKEYTYPE
, ssl_key_type
);
1035 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
1036 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
1038 curl_easy_setopt(result
, CURLOPT_PINNEDPUBLICKEY
, ssl_pinnedkey
);
1040 if (http_ssl_backend
&& !strcmp("schannel", http_ssl_backend
) &&
1041 !http_schannel_use_ssl_cainfo
) {
1042 curl_easy_setopt(result
, CURLOPT_CAINFO
, NULL
);
1043 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
1044 curl_easy_setopt(result
, CURLOPT_PROXY_CAINFO
, NULL
);
1046 } else if (ssl_cainfo
!= NULL
|| http_proxy_ssl_ca_info
!= NULL
) {
1048 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
1049 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
1050 if (http_proxy_ssl_ca_info
)
1051 curl_easy_setopt(result
, CURLOPT_PROXY_CAINFO
, http_proxy_ssl_ca_info
);
1055 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
1056 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
1057 curl_low_speed_limit
);
1058 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
1059 curl_low_speed_time
);
1062 curl_easy_setopt(result
, CURLOPT_MAXREDIRS
, 20);
1063 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
1065 #ifdef GIT_CURL_HAVE_CURLOPT_PROTOCOLS_STR
1067 struct strbuf buf
= STRBUF_INIT
;
1069 get_curl_allowed_protocols(0, &buf
);
1070 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS_STR
, buf
.buf
);
1073 get_curl_allowed_protocols(-1, &buf
);
1074 curl_easy_setopt(result
, CURLOPT_PROTOCOLS_STR
, buf
.buf
);
1075 strbuf_release(&buf
);
1078 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS
,
1079 get_curl_allowed_protocols(0, NULL
));
1080 curl_easy_setopt(result
, CURLOPT_PROTOCOLS
,
1081 get_curl_allowed_protocols(-1, NULL
));
1084 if (getenv("GIT_CURL_VERBOSE"))
1085 http_trace_curl_no_data();
1086 setup_curl_trace(result
);
1087 if (getenv("GIT_TRACE_CURL_NO_DATA"))
1088 trace_curl_data
= 0;
1089 if (!git_env_bool("GIT_TRACE_REDACT", 1))
1090 trace_curl_redact
= 0;
1092 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
1093 user_agent
? user_agent
: git_user_agent());
1095 if (curl_ftp_no_epsv
)
1096 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
1099 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
1102 * CURL also examines these variables as a fallback; but we need to query
1103 * them here in order to decide whether to prompt for missing password (cf.
1104 * init_curl_proxy_auth()).
1106 * Unlike many other common environment variables, these are historically
1107 * lowercase only. It appears that CURL did not know this and implemented
1108 * only uppercase variants, which was later corrected to take both - with
1109 * the exception of http_proxy, which is lowercase only also in CURL. As
1110 * the lowercase versions are the historical quasi-standard, they take
1111 * precedence here, as in CURL.
1113 if (!curl_http_proxy
) {
1114 if (http_auth
.protocol
&& !strcmp(http_auth
.protocol
, "https")) {
1115 var_override(&curl_http_proxy
, getenv("HTTPS_PROXY"));
1116 var_override(&curl_http_proxy
, getenv("https_proxy"));
1118 var_override(&curl_http_proxy
, getenv("http_proxy"));
1120 if (!curl_http_proxy
) {
1121 var_override(&curl_http_proxy
, getenv("ALL_PROXY"));
1122 var_override(&curl_http_proxy
, getenv("all_proxy"));
1126 if (curl_http_proxy
&& curl_http_proxy
[0] == '\0') {
1128 * Handle case with the empty http.proxy value here to keep
1129 * common code clean.
1130 * NB: empty option disables proxying at all.
1132 curl_easy_setopt(result
, CURLOPT_PROXY
, "");
1133 } else if (curl_http_proxy
) {
1134 if (starts_with(curl_http_proxy
, "socks5h"))
1135 curl_easy_setopt(result
,
1136 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5_HOSTNAME
);
1137 else if (starts_with(curl_http_proxy
, "socks5"))
1138 curl_easy_setopt(result
,
1139 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
1140 else if (starts_with(curl_http_proxy
, "socks4a"))
1141 curl_easy_setopt(result
,
1142 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4A
);
1143 else if (starts_with(curl_http_proxy
, "socks"))
1144 curl_easy_setopt(result
,
1145 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4
);
1146 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
1147 else if (starts_with(curl_http_proxy
, "https")) {
1148 curl_easy_setopt(result
, CURLOPT_PROXYTYPE
, CURLPROXY_HTTPS
);
1150 if (http_proxy_ssl_cert
)
1151 curl_easy_setopt(result
, CURLOPT_PROXY_SSLCERT
, http_proxy_ssl_cert
);
1153 if (http_proxy_ssl_key
)
1154 curl_easy_setopt(result
, CURLOPT_PROXY_SSLKEY
, http_proxy_ssl_key
);
1156 if (has_proxy_cert_password())
1157 curl_easy_setopt(result
, CURLOPT_PROXY_KEYPASSWD
, proxy_cert_auth
.password
);
1160 if (strstr(curl_http_proxy
, "://"))
1161 credential_from_url(&proxy_auth
, curl_http_proxy
);
1163 struct strbuf url
= STRBUF_INIT
;
1164 strbuf_addf(&url
, "http://%s", curl_http_proxy
);
1165 credential_from_url(&proxy_auth
, url
.buf
);
1166 strbuf_release(&url
);
1169 if (!proxy_auth
.host
)
1170 die("Invalid proxy URL '%s'", curl_http_proxy
);
1172 curl_easy_setopt(result
, CURLOPT_PROXY
, proxy_auth
.host
);
1173 var_override(&curl_no_proxy
, getenv("NO_PROXY"));
1174 var_override(&curl_no_proxy
, getenv("no_proxy"));
1175 curl_easy_setopt(result
, CURLOPT_NOPROXY
, curl_no_proxy
);
1177 init_curl_proxy_auth(result
);
1179 set_curl_keepalive(result
);
1184 static void set_from_env(const char **var
, const char *envname
)
1186 const char *val
= getenv(envname
);
1191 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
1193 char *low_speed_limit
;
1194 char *low_speed_time
;
1195 char *normalized_url
;
1196 struct urlmatch_config config
= URLMATCH_CONFIG_INIT
;
1198 config
.section
= "http";
1200 config
.collect_fn
= http_options
;
1201 config
.cascade_fn
= git_default_config
;
1204 http_is_verbose
= 0;
1205 normalized_url
= url_normalize(url
, &config
.url
);
1207 git_config(urlmatch_config_entry
, &config
);
1208 free(normalized_url
);
1209 string_list_clear(&config
.vars
, 1);
1211 #ifdef GIT_CURL_HAVE_CURLSSLSET_NO_BACKENDS
1212 if (http_ssl_backend
) {
1213 const curl_ssl_backend
**backends
;
1214 struct strbuf buf
= STRBUF_INIT
;
1217 switch (curl_global_sslset(-1, http_ssl_backend
, &backends
)) {
1218 case CURLSSLSET_UNKNOWN_BACKEND
:
1219 strbuf_addf(&buf
, _("Unsupported SSL backend '%s'. "
1220 "Supported SSL backends:"),
1222 for (i
= 0; backends
[i
]; i
++)
1223 strbuf_addf(&buf
, "\n\t%s", backends
[i
]->name
);
1225 case CURLSSLSET_NO_BACKENDS
:
1226 die(_("Could not set SSL backend to '%s': "
1227 "cURL was built without SSL backends"),
1229 case CURLSSLSET_TOO_LATE
:
1230 die(_("Could not set SSL backend to '%s': already set"),
1238 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
1239 die("curl_global_init failed");
1241 http_proactive_auth
= proactive_auth
;
1243 if (remote
&& remote
->http_proxy
)
1244 curl_http_proxy
= xstrdup(remote
->http_proxy
);
1247 var_override(&http_proxy_authmethod
, remote
->http_proxy_authmethod
);
1249 pragma_header
= curl_slist_append(http_copy_default_headers(),
1250 "Pragma: no-cache");
1251 no_pragma_header
= curl_slist_append(http_copy_default_headers(),
1255 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1256 if (http_max_requests
)
1257 max_requests
= atoi(http_max_requests
);
1260 curlm
= curl_multi_init();
1262 die("curl_multi_init failed");
1264 if (getenv("GIT_SSL_NO_VERIFY"))
1265 curl_ssl_verify
= 0;
1267 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
1268 set_from_env(&ssl_cert_type
, "GIT_SSL_CERT_TYPE");
1269 set_from_env(&ssl_key
, "GIT_SSL_KEY");
1270 set_from_env(&ssl_key_type
, "GIT_SSL_KEY_TYPE");
1271 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
1272 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
1274 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
1276 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1277 if (low_speed_limit
)
1278 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1279 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1281 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1283 if (curl_ssl_verify
== -1)
1284 curl_ssl_verify
= 1;
1286 curl_session_count
= 0;
1287 if (max_requests
< 1)
1288 max_requests
= DEFAULT_MAX_REQUESTS
;
1290 set_from_env(&http_proxy_ssl_cert
, "GIT_PROXY_SSL_CERT");
1291 set_from_env(&http_proxy_ssl_key
, "GIT_PROXY_SSL_KEY");
1292 set_from_env(&http_proxy_ssl_ca_info
, "GIT_PROXY_SSL_CAINFO");
1294 if (getenv("GIT_PROXY_SSL_CERT_PASSWORD_PROTECTED"))
1295 proxy_ssl_cert_password_required
= 1;
1297 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1298 curl_ftp_no_epsv
= 1;
1301 credential_from_url(&http_auth
, url
);
1302 if (!ssl_cert_password_required
&&
1303 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
1304 starts_with(url
, "https://"))
1305 ssl_cert_password_required
= 1;
1308 curl_default
= get_curl_handle();
1311 void http_cleanup(void)
1313 struct active_request_slot
*slot
= active_queue_head
;
1315 while (slot
!= NULL
) {
1316 struct active_request_slot
*next
= slot
->next
;
1318 xmulti_remove_handle(slot
);
1319 curl_easy_cleanup(slot
->curl
);
1324 active_queue_head
= NULL
;
1326 curl_easy_cleanup(curl_default
);
1328 curl_multi_cleanup(curlm
);
1329 curl_global_cleanup();
1331 string_list_clear(&extra_http_headers
, 0);
1333 curl_slist_free_all(pragma_header
);
1334 pragma_header
= NULL
;
1336 curl_slist_free_all(no_pragma_header
);
1337 no_pragma_header
= NULL
;
1339 curl_slist_free_all(host_resolutions
);
1340 host_resolutions
= NULL
;
1342 if (curl_http_proxy
) {
1343 free((void *)curl_http_proxy
);
1344 curl_http_proxy
= NULL
;
1347 if (proxy_auth
.password
) {
1348 memset(proxy_auth
.password
, 0, strlen(proxy_auth
.password
));
1349 FREE_AND_NULL(proxy_auth
.password
);
1352 free((void *)curl_proxyuserpwd
);
1353 curl_proxyuserpwd
= NULL
;
1355 free((void *)http_proxy_authmethod
);
1356 http_proxy_authmethod
= NULL
;
1358 if (cert_auth
.password
) {
1359 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
1360 FREE_AND_NULL(cert_auth
.password
);
1362 ssl_cert_password_required
= 0;
1364 if (proxy_cert_auth
.password
) {
1365 memset(proxy_cert_auth
.password
, 0, strlen(proxy_cert_auth
.password
));
1366 FREE_AND_NULL(proxy_cert_auth
.password
);
1368 proxy_ssl_cert_password_required
= 0;
1370 FREE_AND_NULL(cached_accept_language
);
1373 struct active_request_slot
*get_active_slot(void)
1375 struct active_request_slot
*slot
= active_queue_head
;
1376 struct active_request_slot
*newslot
;
1380 /* Wait for a slot to open up if the queue is full */
1381 while (active_requests
>= max_requests
) {
1382 curl_multi_perform(curlm
, &num_transfers
);
1383 if (num_transfers
< active_requests
)
1384 process_curl_messages();
1387 while (slot
!= NULL
&& slot
->in_use
)
1391 newslot
= xmalloc(sizeof(*newslot
));
1392 newslot
->curl
= NULL
;
1393 newslot
->in_use
= 0;
1394 newslot
->next
= NULL
;
1396 slot
= active_queue_head
;
1398 active_queue_head
= newslot
;
1400 while (slot
->next
!= NULL
)
1402 slot
->next
= newslot
;
1408 slot
->curl
= curl_easy_duphandle(curl_default
);
1409 curl_session_count
++;
1414 slot
->results
= NULL
;
1415 slot
->finished
= NULL
;
1416 slot
->callback_data
= NULL
;
1417 slot
->callback_func
= NULL
;
1418 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
1419 if (curl_save_cookies
)
1420 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
1421 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
1422 curl_easy_setopt(slot
->curl
, CURLOPT_RESOLVE
, host_resolutions
);
1423 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
1424 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
1425 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
1426 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
1427 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
1428 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
1429 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1430 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
1431 curl_easy_setopt(slot
->curl
, CURLOPT_RANGE
, NULL
);
1434 * Default following to off unless "ALWAYS" is configured; this gives
1435 * callers a sane starting point, and they can tweak for individual
1436 * HTTP_FOLLOW_* cases themselves.
1438 if (http_follow_config
== HTTP_FOLLOW_ALWAYS
)
1439 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1441 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 0);
1443 curl_easy_setopt(slot
->curl
, CURLOPT_IPRESOLVE
, git_curl_ipresolve
);
1444 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPAUTH
, http_auth_methods
);
1445 if (http_auth
.password
|| curl_empty_auth_enabled())
1446 init_curl_http_auth(slot
->curl
);
1451 int start_active_slot(struct active_request_slot
*slot
)
1453 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
1456 if (curlm_result
!= CURLM_OK
&&
1457 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
1458 warning("curl_multi_add_handle failed: %s",
1459 curl_multi_strerror(curlm_result
));
1466 * We know there must be something to do, since we just added
1469 curl_multi_perform(curlm
, &num_transfers
);
1475 int (*fill
)(void *);
1476 struct fill_chain
*next
;
1479 static struct fill_chain
*fill_cfg
;
1481 void add_fill_function(void *data
, int (*fill
)(void *))
1483 struct fill_chain
*new_fill
= xmalloc(sizeof(*new_fill
));
1484 struct fill_chain
**linkp
= &fill_cfg
;
1485 new_fill
->data
= data
;
1486 new_fill
->fill
= fill
;
1487 new_fill
->next
= NULL
;
1489 linkp
= &(*linkp
)->next
;
1493 void fill_active_slots(void)
1495 struct active_request_slot
*slot
= active_queue_head
;
1497 while (active_requests
< max_requests
) {
1498 struct fill_chain
*fill
;
1499 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
1500 if (fill
->fill(fill
->data
))
1507 while (slot
!= NULL
) {
1508 if (!slot
->in_use
&& slot
->curl
!= NULL
1509 && curl_session_count
> min_curl_sessions
) {
1510 curl_easy_cleanup(slot
->curl
);
1512 curl_session_count
--;
1518 void step_active_slots(void)
1521 CURLMcode curlm_result
;
1524 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
1525 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
1526 if (num_transfers
< active_requests
) {
1527 process_curl_messages();
1528 fill_active_slots();
1532 void run_active_slot(struct active_request_slot
*slot
)
1538 struct timeval select_timeout
;
1541 slot
->finished
= &finished
;
1543 step_active_slots();
1547 curl_multi_timeout(curlm
, &curl_timeout
);
1548 if (curl_timeout
== 0) {
1550 } else if (curl_timeout
== -1) {
1551 select_timeout
.tv_sec
= 0;
1552 select_timeout
.tv_usec
= 50000;
1554 select_timeout
.tv_sec
= curl_timeout
/ 1000;
1555 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
1562 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
1565 * It can happen that curl_multi_timeout returns a pathologically
1566 * long timeout when curl_multi_fdset returns no file descriptors
1567 * to read. See commit message for more details.
1570 (select_timeout
.tv_sec
> 0 ||
1571 select_timeout
.tv_usec
> 50000)) {
1572 select_timeout
.tv_sec
= 0;
1573 select_timeout
.tv_usec
= 50000;
1576 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
1581 * The value of slot->finished we set before the loop was used
1582 * to set our "finished" variable when our request completed.
1584 * 1. The slot may not have been reused for another requst
1585 * yet, in which case it still has &finished.
1587 * 2. The slot may already be in-use to serve another request,
1588 * which can further be divided into two cases:
1590 * (a) If call run_active_slot() hasn't been called for that
1591 * other request, slot->finished would have been cleared
1592 * by get_active_slot() and has NULL.
1594 * (b) If the request did call run_active_slot(), then the
1595 * call would have updated slot->finished at the beginning
1596 * of this function, and with the clearing of the member
1597 * below, we would find that slot->finished is now NULL.
1599 * In all cases, slot->finished has no useful information to
1600 * anybody at this point. Some compilers warn us for
1601 * attempting to smuggle a pointer that is about to become
1602 * invalid, i.e. &finished. We clear it here to assure them.
1604 slot
->finished
= NULL
;
1607 static void release_active_slot(struct active_request_slot
*slot
)
1609 closedown_active_slot(slot
);
1611 xmulti_remove_handle(slot
);
1612 if (curl_session_count
> min_curl_sessions
) {
1613 curl_easy_cleanup(slot
->curl
);
1615 curl_session_count
--;
1618 fill_active_slots();
1621 void finish_all_active_slots(void)
1623 struct active_request_slot
*slot
= active_queue_head
;
1625 while (slot
!= NULL
)
1627 run_active_slot(slot
);
1628 slot
= active_queue_head
;
1634 /* Helpers for modifying and creating URLs */
1635 static inline int needs_quote(int ch
)
1637 if (((ch
>= 'A') && (ch
<= 'Z'))
1638 || ((ch
>= 'a') && (ch
<= 'z'))
1639 || ((ch
>= '0') && (ch
<= '9'))
1647 static char *quote_ref_url(const char *base
, const char *ref
)
1649 struct strbuf buf
= STRBUF_INIT
;
1653 end_url_with_slash(&buf
, base
);
1655 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
1656 if (needs_quote(ch
))
1657 strbuf_addf(&buf
, "%%%02x", ch
);
1659 strbuf_addch(&buf
, *cp
);
1661 return strbuf_detach(&buf
, NULL
);
1664 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
1666 int only_two_digit_prefix
)
1668 end_url_with_slash(buf
, url
);
1670 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
1671 if (!only_two_digit_prefix
)
1672 strbuf_addstr(buf
, hex
+ 2);
1675 char *get_remote_object_url(const char *url
, const char *hex
,
1676 int only_two_digit_prefix
)
1678 struct strbuf buf
= STRBUF_INIT
;
1679 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
1680 return strbuf_detach(&buf
, NULL
);
1683 void normalize_curl_result(CURLcode
*result
, long http_code
,
1684 char *errorstr
, size_t errorlen
)
1687 * If we see a failing http code with CURLE_OK, we have turned off
1688 * FAILONERROR (to keep the server's custom error response), and should
1689 * translate the code into failure here.
1691 * Likewise, if we see a redirect (30x code), that means we turned off
1692 * redirect-following, and we should treat the result as an error.
1694 if (*result
== CURLE_OK
&& http_code
>= 300) {
1695 *result
= CURLE_HTTP_RETURNED_ERROR
;
1697 * Normally curl will already have put the "reason phrase"
1698 * from the server into curl_errorstr; unfortunately without
1699 * FAILONERROR it is lost, so we can give only the numeric
1702 xsnprintf(errorstr
, errorlen
,
1703 "The requested URL returned error: %ld",
1708 static int handle_curl_result(struct slot_results
*results
)
1710 normalize_curl_result(&results
->curl_result
, results
->http_code
,
1711 curl_errorstr
, sizeof(curl_errorstr
));
1713 if (results
->curl_result
== CURLE_OK
) {
1714 credential_approve(&http_auth
);
1715 credential_approve(&proxy_auth
);
1716 credential_approve(&cert_auth
);
1718 } else if (results
->curl_result
== CURLE_SSL_CERTPROBLEM
) {
1720 * We can't tell from here whether it's a bad path, bad
1721 * certificate, bad password, or something else wrong
1722 * with the certificate. So we reject the credential to
1723 * avoid caching or saving a bad password.
1725 credential_reject(&cert_auth
);
1727 #ifdef GIT_CURL_HAVE_CURLE_SSL_PINNEDPUBKEYNOTMATCH
1728 } else if (results
->curl_result
== CURLE_SSL_PINNEDPUBKEYNOTMATCH
) {
1729 return HTTP_NOMATCHPUBLICKEY
;
1731 } else if (missing_target(results
))
1732 return HTTP_MISSING_TARGET
;
1733 else if (results
->http_code
== 401) {
1734 if (http_auth
.username
&& http_auth
.password
) {
1735 credential_reject(&http_auth
);
1738 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
1739 if (results
->auth_avail
) {
1740 http_auth_methods
&= results
->auth_avail
;
1741 http_auth_methods_restricted
= 1;
1746 if (results
->http_connectcode
== 407)
1747 credential_reject(&proxy_auth
);
1748 if (!curl_errorstr
[0])
1749 strlcpy(curl_errorstr
,
1750 curl_easy_strerror(results
->curl_result
),
1751 sizeof(curl_errorstr
));
1756 int run_one_slot(struct active_request_slot
*slot
,
1757 struct slot_results
*results
)
1759 slot
->results
= results
;
1760 if (!start_active_slot(slot
)) {
1761 xsnprintf(curl_errorstr
, sizeof(curl_errorstr
),
1762 "failed to start HTTP request");
1763 return HTTP_START_FAILED
;
1766 run_active_slot(slot
);
1767 return handle_curl_result(results
);
1770 struct curl_slist
*http_copy_default_headers(void)
1772 struct curl_slist
*headers
= NULL
;
1773 const struct string_list_item
*item
;
1775 for_each_string_list_item(item
, &extra_http_headers
)
1776 headers
= curl_slist_append(headers
, item
->string
);
1781 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
1787 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
1789 strbuf_addstr(buf
, ptr
);
1794 * Check for and extract a content-type parameter. "raw"
1795 * should be positioned at the start of the potential
1796 * parameter, with any whitespace already removed.
1798 * "name" is the name of the parameter. The value is appended
1801 static int extract_param(const char *raw
, const char *name
,
1804 size_t len
= strlen(name
);
1806 if (strncasecmp(raw
, name
, len
))
1814 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
1815 strbuf_addch(out
, *raw
++);
1820 * Extract a normalized version of the content type, with any
1821 * spaces suppressed, all letters lowercased, and no trailing ";"
1824 * Note that we will silently remove even invalid whitespace. For
1825 * example, "text / plain" is specifically forbidden by RFC 2616,
1826 * but "text/plain" is the only reasonable output, and this keeps
1829 * If the "charset" argument is not NULL, store the value of any
1830 * charset parameter there.
1833 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1834 * "text / plain" -> "text/plain"
1836 static void extract_content_type(struct strbuf
*raw
, struct strbuf
*type
,
1837 struct strbuf
*charset
)
1842 strbuf_grow(type
, raw
->len
);
1843 for (p
= raw
->buf
; *p
; p
++) {
1850 strbuf_addch(type
, tolower(*p
));
1856 strbuf_reset(charset
);
1858 while (isspace(*p
) || *p
== ';')
1860 if (!extract_param(p
, "charset", charset
))
1862 while (*p
&& !isspace(*p
))
1866 if (!charset
->len
&& starts_with(type
->buf
, "text/"))
1867 strbuf_addstr(charset
, "ISO-8859-1");
1870 static void write_accept_language(struct strbuf
*buf
)
1873 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1874 * that, q-value will be smaller than 0.001, the minimum q-value the
1875 * HTTP specification allows. See
1876 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1878 const int MAX_DECIMAL_PLACES
= 3;
1879 const int MAX_LANGUAGE_TAGS
= 1000;
1880 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE
= 4000;
1881 char **language_tags
= NULL
;
1883 const char *s
= get_preferred_languages();
1885 struct strbuf tag
= STRBUF_INIT
;
1887 /* Don't add Accept-Language header if no language is preferred. */
1892 * Split the colon-separated string of preferred languages into
1893 * language_tags array.
1896 /* collect language tag */
1897 for (; *s
&& (isalnum(*s
) || *s
== '_'); s
++)
1898 strbuf_addch(&tag
, *s
== '_' ? '-' : *s
);
1900 /* skip .codeset, @modifier and any other unnecessary parts */
1901 while (*s
&& *s
!= ':')
1906 REALLOC_ARRAY(language_tags
, num_langs
);
1907 language_tags
[num_langs
- 1] = strbuf_detach(&tag
, NULL
);
1908 if (num_langs
>= MAX_LANGUAGE_TAGS
- 1) /* -1 for '*' */
1913 /* write Accept-Language header into buf */
1915 int last_buf_len
= 0;
1921 REALLOC_ARRAY(language_tags
, num_langs
+ 1);
1922 language_tags
[num_langs
++] = "*"; /* it's OK; this won't be freed */
1924 /* compute decimal_places */
1925 for (max_q
= 1, decimal_places
= 0;
1926 max_q
< num_langs
&& decimal_places
<= MAX_DECIMAL_PLACES
;
1927 decimal_places
++, max_q
*= 10)
1930 xsnprintf(q_format
, sizeof(q_format
), ";q=0.%%0%dd", decimal_places
);
1932 strbuf_addstr(buf
, "Accept-Language: ");
1934 for (i
= 0; i
< num_langs
; i
++) {
1936 strbuf_addstr(buf
, ", ");
1938 strbuf_addstr(buf
, language_tags
[i
]);
1941 strbuf_addf(buf
, q_format
, max_q
- i
);
1943 if (buf
->len
> MAX_ACCEPT_LANGUAGE_HEADER_SIZE
) {
1944 strbuf_remove(buf
, last_buf_len
, buf
->len
- last_buf_len
);
1948 last_buf_len
= buf
->len
;
1952 /* free language tags -- last one is a static '*' */
1953 for (i
= 0; i
< num_langs
- 1; i
++)
1954 free(language_tags
[i
]);
1955 free(language_tags
);
1959 * Get an Accept-Language header which indicates user's preferred languages.
1963 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1964 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1965 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1966 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1967 * LANGUAGE= LANG=C -> ""
1969 const char *http_get_accept_language_header(void)
1971 if (!cached_accept_language
) {
1972 struct strbuf buf
= STRBUF_INIT
;
1973 write_accept_language(&buf
);
1975 cached_accept_language
= strbuf_detach(&buf
, NULL
);
1978 return cached_accept_language
;
1981 static void http_opt_request_remainder(CURL
*curl
, off_t pos
)
1984 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
"-", (uintmax_t)pos
);
1985 curl_easy_setopt(curl
, CURLOPT_RANGE
, buf
);
1988 /* http_request() targets */
1989 #define HTTP_REQUEST_STRBUF 0
1990 #define HTTP_REQUEST_FILE 1
1992 static int http_request(const char *url
,
1993 void *result
, int target
,
1994 const struct http_get_options
*options
)
1996 struct active_request_slot
*slot
;
1997 struct slot_results results
;
1998 struct curl_slist
*headers
= http_copy_default_headers();
1999 struct strbuf buf
= STRBUF_INIT
;
2000 const char *accept_language
;
2003 slot
= get_active_slot();
2004 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
2007 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
2009 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
2010 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, result
);
2012 if (target
== HTTP_REQUEST_FILE
) {
2013 off_t posn
= ftello(result
);
2014 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
2017 http_opt_request_remainder(slot
->curl
, posn
);
2019 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
2023 curl_easy_setopt(slot
->curl
, CURLOPT_HEADERFUNCTION
, fwrite_wwwauth
);
2025 accept_language
= http_get_accept_language_header();
2027 if (accept_language
)
2028 headers
= curl_slist_append(headers
, accept_language
);
2030 strbuf_addstr(&buf
, "Pragma:");
2031 if (options
&& options
->no_cache
)
2032 strbuf_addstr(&buf
, " no-cache");
2033 if (options
&& options
->initial_request
&&
2034 http_follow_config
== HTTP_FOLLOW_INITIAL
)
2035 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
2037 headers
= curl_slist_append(headers
, buf
.buf
);
2039 /* Add additional headers here */
2040 if (options
&& options
->extra_headers
) {
2041 const struct string_list_item
*item
;
2042 for_each_string_list_item(item
, options
->extra_headers
) {
2043 headers
= curl_slist_append(headers
, item
->string
);
2047 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
2048 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
2049 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
2050 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
2052 ret
= run_one_slot(slot
, &results
);
2054 if (options
&& options
->content_type
) {
2055 struct strbuf raw
= STRBUF_INIT
;
2056 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
, &raw
);
2057 extract_content_type(&raw
, options
->content_type
,
2059 strbuf_release(&raw
);
2062 if (options
&& options
->effective_url
)
2063 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
2064 options
->effective_url
);
2066 curl_slist_free_all(headers
);
2067 strbuf_release(&buf
);
2073 * Update the "base" url to a more appropriate value, as deduced by
2074 * redirects seen when requesting a URL starting with "url".
2076 * The "asked" parameter is a URL that we asked curl to access, and must begin
2079 * The "got" parameter is the URL that curl reported to us as where we ended
2082 * Returns 1 if we updated the base url, 0 otherwise.
2084 * Our basic strategy is to compare "base" and "asked" to find the bits
2085 * specific to our request. We then strip those bits off of "got" to yield the
2086 * new base. So for example, if our base is "http://example.com/foo.git",
2087 * and we ask for "http://example.com/foo.git/info/refs", we might end up
2088 * with "https://other.example.com/foo.git/info/refs". We would want the
2089 * new URL to become "https://other.example.com/foo.git".
2091 * Note that this assumes a sane redirect scheme. It's entirely possible
2092 * in the example above to end up at a URL that does not even end in
2093 * "info/refs". In such a case we die. There's not much we can do, such a
2094 * scheme is unlikely to represent a real git repository, and failing to
2095 * rewrite the base opens options for malicious redirects to do funny things.
2097 static int update_url_from_redirect(struct strbuf
*base
,
2099 const struct strbuf
*got
)
2104 if (!strcmp(asked
, got
->buf
))
2107 if (!skip_prefix(asked
, base
->buf
, &tail
))
2108 BUG("update_url_from_redirect: %s is not a superset of %s",
2112 if (!strip_suffix_mem(got
->buf
, &new_len
, tail
))
2113 die(_("unable to update url base from redirection:\n"
2119 strbuf_add(base
, got
->buf
, new_len
);
2124 static int http_request_reauth(const char *url
,
2125 void *result
, int target
,
2126 struct http_get_options
*options
)
2128 int ret
= http_request(url
, result
, target
, options
);
2130 if (ret
!= HTTP_OK
&& ret
!= HTTP_REAUTH
)
2133 if (options
&& options
->effective_url
&& options
->base_url
) {
2134 if (update_url_from_redirect(options
->base_url
,
2135 url
, options
->effective_url
)) {
2136 credential_from_url(&http_auth
, options
->base_url
->buf
);
2137 url
= options
->effective_url
->buf
;
2141 if (ret
!= HTTP_REAUTH
)
2145 * The previous request may have put cruft into our output stream; we
2146 * should clear it out before making our next request.
2149 case HTTP_REQUEST_STRBUF
:
2150 strbuf_reset(result
);
2152 case HTTP_REQUEST_FILE
:
2153 if (fflush(result
)) {
2154 error_errno("unable to flush a file");
2155 return HTTP_START_FAILED
;
2158 if (ftruncate(fileno(result
), 0) < 0) {
2159 error_errno("unable to truncate a file");
2160 return HTTP_START_FAILED
;
2164 BUG("Unknown http_request target");
2167 credential_fill(&http_auth
);
2169 return http_request(url
, result
, target
, options
);
2172 int http_get_strbuf(const char *url
,
2173 struct strbuf
*result
,
2174 struct http_get_options
*options
)
2176 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
2180 * Downloads a URL and stores the result in the given file.
2182 * If a previous interrupted download is detected (i.e. a previous temporary
2183 * file is still around) the download is resumed.
2185 int http_get_file(const char *url
, const char *filename
,
2186 struct http_get_options
*options
)
2189 struct strbuf tmpfile
= STRBUF_INIT
;
2192 strbuf_addf(&tmpfile
, "%s.temp", filename
);
2193 result
= fopen(tmpfile
.buf
, "a");
2195 error("Unable to open local file %s", tmpfile
.buf
);
2200 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
2203 if (ret
== HTTP_OK
&& finalize_object_file(tmpfile
.buf
, filename
))
2206 strbuf_release(&tmpfile
);
2210 int http_fetch_ref(const char *base
, struct ref
*ref
)
2212 struct http_get_options options
= {0};
2214 struct strbuf buffer
= STRBUF_INIT
;
2217 options
.no_cache
= 1;
2219 url
= quote_ref_url(base
, ref
->name
);
2220 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
2221 strbuf_rtrim(&buffer
);
2222 if (buffer
.len
== the_hash_algo
->hexsz
)
2223 ret
= get_oid_hex(buffer
.buf
, &ref
->old_oid
);
2224 else if (starts_with(buffer
.buf
, "ref: ")) {
2225 ref
->symref
= xstrdup(buffer
.buf
+ 5);
2230 strbuf_release(&buffer
);
2235 /* Helpers for fetching packs */
2236 static char *fetch_pack_index(unsigned char *hash
, const char *base_url
)
2239 struct strbuf buf
= STRBUF_INIT
;
2241 if (http_is_verbose
)
2242 fprintf(stderr
, "Getting index for pack %s\n", hash_to_hex(hash
));
2244 end_url_with_slash(&buf
, base_url
);
2245 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", hash_to_hex(hash
));
2246 url
= strbuf_detach(&buf
, NULL
);
2248 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(hash
));
2249 tmp
= strbuf_detach(&buf
, NULL
);
2251 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
2252 error("Unable to get pack index %s", url
);
2260 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
2261 unsigned char *sha1
, const char *base_url
)
2263 struct packed_git
*new_pack
;
2264 char *tmp_idx
= NULL
;
2267 if (has_pack_index(sha1
)) {
2268 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
2270 return -1; /* parse_pack_index() already issued error message */
2274 tmp_idx
= fetch_pack_index(sha1
, base_url
);
2278 new_pack
= parse_pack_index(sha1
, tmp_idx
);
2283 return -1; /* parse_pack_index() already issued error message */
2286 ret
= verify_pack_index(new_pack
);
2288 close_pack_index(new_pack
);
2289 ret
= finalize_object_file(tmp_idx
, sha1_pack_index_name(sha1
));
2296 new_pack
->next
= *packs_head
;
2297 *packs_head
= new_pack
;
2301 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
2303 struct http_get_options options
= {0};
2307 struct strbuf buf
= STRBUF_INIT
;
2308 struct object_id oid
;
2310 end_url_with_slash(&buf
, base_url
);
2311 strbuf_addstr(&buf
, "objects/info/packs");
2312 url
= strbuf_detach(&buf
, NULL
);
2314 options
.no_cache
= 1;
2315 ret
= http_get_strbuf(url
, &buf
, &options
);
2321 if (skip_prefix(data
, "P pack-", &data
) &&
2322 !parse_oid_hex(data
, &oid
, &data
) &&
2323 skip_prefix(data
, ".pack", &data
) &&
2324 (*data
== '\n' || *data
== '\0')) {
2325 fetch_and_setup_pack_index(packs_head
, oid
.hash
, base_url
);
2327 data
= strchrnul(data
, '\n');
2330 data
++; /* skip past newline */
2338 void release_http_pack_request(struct http_pack_request
*preq
)
2340 if (preq
->packfile
) {
2341 fclose(preq
->packfile
);
2342 preq
->packfile
= NULL
;
2345 strbuf_release(&preq
->tmpfile
);
2350 static const char *default_index_pack_args
[] =
2351 {"index-pack", "--stdin", NULL
};
2353 int finish_http_pack_request(struct http_pack_request
*preq
)
2355 struct child_process ip
= CHILD_PROCESS_INIT
;
2359 fclose(preq
->packfile
);
2360 preq
->packfile
= NULL
;
2362 tmpfile_fd
= xopen(preq
->tmpfile
.buf
, O_RDONLY
);
2366 strvec_pushv(&ip
.args
, preq
->index_pack_args
?
2367 preq
->index_pack_args
:
2368 default_index_pack_args
);
2370 if (preq
->preserve_index_pack_stdout
)
2375 if (run_command(&ip
)) {
2382 unlink(preq
->tmpfile
.buf
);
2386 void http_install_packfile(struct packed_git
*p
,
2387 struct packed_git
**list_to_remove_from
)
2389 struct packed_git
**lst
= list_to_remove_from
;
2392 lst
= &((*lst
)->next
);
2393 *lst
= (*lst
)->next
;
2395 install_packed_git(the_repository
, p
);
2398 struct http_pack_request
*new_http_pack_request(
2399 const unsigned char *packed_git_hash
, const char *base_url
) {
2401 struct strbuf buf
= STRBUF_INIT
;
2403 end_url_with_slash(&buf
, base_url
);
2404 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
2405 hash_to_hex(packed_git_hash
));
2406 return new_direct_http_pack_request(packed_git_hash
,
2407 strbuf_detach(&buf
, NULL
));
2410 struct http_pack_request
*new_direct_http_pack_request(
2411 const unsigned char *packed_git_hash
, char *url
)
2413 off_t prev_posn
= 0;
2414 struct http_pack_request
*preq
;
2416 CALLOC_ARRAY(preq
, 1);
2417 strbuf_init(&preq
->tmpfile
, 0);
2421 strbuf_addf(&preq
->tmpfile
, "%s.temp", sha1_pack_name(packed_git_hash
));
2422 preq
->packfile
= fopen(preq
->tmpfile
.buf
, "a");
2423 if (!preq
->packfile
) {
2424 error("Unable to open local file %s for pack",
2429 preq
->slot
= get_active_slot();
2430 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEDATA
, preq
->packfile
);
2431 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
2432 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
2433 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
2437 * If there is data present from a previous transfer attempt,
2438 * resume where it left off
2440 prev_posn
= ftello(preq
->packfile
);
2442 if (http_is_verbose
)
2444 "Resuming fetch of pack %s at byte %"PRIuMAX
"\n",
2445 hash_to_hex(packed_git_hash
),
2446 (uintmax_t)prev_posn
);
2447 http_opt_request_remainder(preq
->slot
->curl
, prev_posn
);
2453 strbuf_release(&preq
->tmpfile
);
2459 /* Helpers for fetching objects (loose) */
2460 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
2463 unsigned char expn
[4096];
2464 size_t size
= eltsize
* nmemb
;
2466 struct http_object_request
*freq
= data
;
2467 struct active_request_slot
*slot
= freq
->slot
;
2470 CURLcode c
= curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
,
2473 BUG("curl_easy_getinfo for HTTP code failed: %s",
2474 curl_easy_strerror(c
));
2475 if (slot
->http_code
>= 300)
2480 ssize_t retval
= xwrite(freq
->localfile
,
2481 (char *) ptr
+ posn
, size
- posn
);
2483 return posn
/ eltsize
;
2485 } while (posn
< size
);
2487 freq
->stream
.avail_in
= size
;
2488 freq
->stream
.next_in
= (void *)ptr
;
2490 freq
->stream
.next_out
= expn
;
2491 freq
->stream
.avail_out
= sizeof(expn
);
2492 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
2493 the_hash_algo
->update_fn(&freq
->c
, expn
,
2494 sizeof(expn
) - freq
->stream
.avail_out
);
2495 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
2499 struct http_object_request
*new_http_object_request(const char *base_url
,
2500 const struct object_id
*oid
)
2502 char *hex
= oid_to_hex(oid
);
2503 struct strbuf filename
= STRBUF_INIT
;
2504 struct strbuf prevfile
= STRBUF_INIT
;
2506 char prev_buf
[PREV_BUF_SIZE
];
2507 ssize_t prev_read
= 0;
2508 off_t prev_posn
= 0;
2509 struct http_object_request
*freq
;
2511 CALLOC_ARRAY(freq
, 1);
2512 strbuf_init(&freq
->tmpfile
, 0);
2513 oidcpy(&freq
->oid
, oid
);
2514 freq
->localfile
= -1;
2516 loose_object_path(the_repository
, &filename
, oid
);
2517 strbuf_addf(&freq
->tmpfile
, "%s.temp", filename
.buf
);
2519 strbuf_addf(&prevfile
, "%s.prev", filename
.buf
);
2520 unlink_or_warn(prevfile
.buf
);
2521 rename(freq
->tmpfile
.buf
, prevfile
.buf
);
2522 unlink_or_warn(freq
->tmpfile
.buf
);
2523 strbuf_release(&filename
);
2525 if (freq
->localfile
!= -1)
2526 error("fd leakage in start: %d", freq
->localfile
);
2527 freq
->localfile
= open(freq
->tmpfile
.buf
,
2528 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2530 * This could have failed due to the "lazy directory creation";
2531 * try to mkdir the last path component.
2533 if (freq
->localfile
< 0 && errno
== ENOENT
) {
2534 char *dir
= strrchr(freq
->tmpfile
.buf
, '/');
2537 mkdir(freq
->tmpfile
.buf
, 0777);
2540 freq
->localfile
= open(freq
->tmpfile
.buf
,
2541 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2544 if (freq
->localfile
< 0) {
2545 error_errno("Couldn't create temporary file %s",
2550 git_inflate_init(&freq
->stream
);
2552 the_hash_algo
->init_fn(&freq
->c
);
2554 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
2557 * If a previous temp file is present, process what was already
2560 prevlocal
= open(prevfile
.buf
, O_RDONLY
);
2561 if (prevlocal
!= -1) {
2563 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
2565 if (fwrite_sha1_file(prev_buf
,
2568 freq
) == prev_read
) {
2569 prev_posn
+= prev_read
;
2574 } while (prev_read
> 0);
2577 unlink_or_warn(prevfile
.buf
);
2578 strbuf_release(&prevfile
);
2581 * Reset inflate/SHA1 if there was an error reading the previous temp
2582 * file; also rewind to the beginning of the local file.
2584 if (prev_read
== -1) {
2585 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
2586 git_inflate_init(&freq
->stream
);
2587 the_hash_algo
->init_fn(&freq
->c
);
2590 lseek(freq
->localfile
, 0, SEEK_SET
);
2591 if (ftruncate(freq
->localfile
, 0) < 0) {
2592 error_errno("Couldn't truncate temporary file %s",
2599 freq
->slot
= get_active_slot();
2601 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEDATA
, freq
);
2602 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FAILONERROR
, 0);
2603 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
2604 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
2605 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
2606 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
2609 * If we have successfully processed data from a previous fetch
2610 * attempt, only fetch the data we don't already have.
2613 if (http_is_verbose
)
2615 "Resuming fetch of object %s at byte %"PRIuMAX
"\n",
2616 hex
, (uintmax_t)prev_posn
);
2617 http_opt_request_remainder(freq
->slot
->curl
, prev_posn
);
2623 strbuf_release(&prevfile
);
2629 void process_http_object_request(struct http_object_request
*freq
)
2633 freq
->curl_result
= freq
->slot
->curl_result
;
2634 freq
->http_code
= freq
->slot
->http_code
;
2638 int finish_http_object_request(struct http_object_request
*freq
)
2641 struct strbuf filename
= STRBUF_INIT
;
2643 close(freq
->localfile
);
2644 freq
->localfile
= -1;
2646 process_http_object_request(freq
);
2648 if (freq
->http_code
== 416) {
2649 warning("requested range invalid; we may already have all the data.");
2650 } else if (freq
->curl_result
!= CURLE_OK
) {
2651 if (stat(freq
->tmpfile
.buf
, &st
) == 0)
2652 if (st
.st_size
== 0)
2653 unlink_or_warn(freq
->tmpfile
.buf
);
2657 git_inflate_end(&freq
->stream
);
2658 the_hash_algo
->final_oid_fn(&freq
->real_oid
, &freq
->c
);
2659 if (freq
->zret
!= Z_STREAM_END
) {
2660 unlink_or_warn(freq
->tmpfile
.buf
);
2663 if (!oideq(&freq
->oid
, &freq
->real_oid
)) {
2664 unlink_or_warn(freq
->tmpfile
.buf
);
2667 loose_object_path(the_repository
, &filename
, &freq
->oid
);
2668 freq
->rename
= finalize_object_file(freq
->tmpfile
.buf
, filename
.buf
);
2669 strbuf_release(&filename
);
2671 return freq
->rename
;
2674 void abort_http_object_request(struct http_object_request
*freq
)
2676 unlink_or_warn(freq
->tmpfile
.buf
);
2678 release_http_object_request(freq
);
2681 void release_http_object_request(struct http_object_request
*freq
)
2683 if (freq
->localfile
!= -1) {
2684 close(freq
->localfile
);
2685 freq
->localfile
= -1;
2687 FREE_AND_NULL(freq
->url
);
2689 freq
->slot
->callback_func
= NULL
;
2690 freq
->slot
->callback_data
= NULL
;
2691 release_active_slot(freq
->slot
);
2694 strbuf_release(&freq
->tmpfile
);