1 #include "git-compat-util.h"
6 #include "run-command.h"
9 #include "credential.h"
13 #include "transport.h"
16 #include "string-list.h"
17 #include "object-store.h"
19 static struct trace_key trace_curl
= TRACE_KEY_INIT(CURL
);
20 static int trace_curl_data
= 1;
21 static int trace_curl_redact
= 1;
22 #if LIBCURL_VERSION_NUM >= 0x070a08
23 long int git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
25 long int git_curl_ipresolve
;
29 ssize_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
31 #if LIBCURL_VERSION_NUM >= 0x070a06
32 #define LIBCURL_CAN_HANDLE_AUTH_ANY
35 static int min_curl_sessions
= 1;
36 static int curl_session_count
;
38 static int max_requests
= -1;
41 #ifndef NO_CURL_EASY_DUPHANDLE
42 static CURL
*curl_default
;
45 #define PREV_BUF_SIZE 4096
47 char curl_errorstr
[CURL_ERROR_SIZE
];
49 static int curl_ssl_verify
= -1;
50 static int curl_ssl_try
;
51 static const char *curl_http_version
= NULL
;
52 static const char *ssl_cert
;
53 static const char *ssl_cipherlist
;
54 static const char *ssl_version
;
59 { "sslv2", CURL_SSLVERSION_SSLv2
},
60 { "sslv3", CURL_SSLVERSION_SSLv3
},
61 { "tlsv1", CURL_SSLVERSION_TLSv1
},
62 #if LIBCURL_VERSION_NUM >= 0x072200
63 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0
},
64 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1
},
65 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2
},
67 #if LIBCURL_VERSION_NUM >= 0x073400
68 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3
},
71 #if LIBCURL_VERSION_NUM >= 0x070903
72 static const char *ssl_key
;
74 #if LIBCURL_VERSION_NUM >= 0x070908
75 static const char *ssl_capath
;
77 #if LIBCURL_VERSION_NUM >= 0x071304
78 static const char *curl_no_proxy
;
80 #if LIBCURL_VERSION_NUM >= 0x072c00
81 static const char *ssl_pinnedkey
;
83 static const char *ssl_cainfo
;
84 static long curl_low_speed_limit
= -1;
85 static long curl_low_speed_time
= -1;
86 static int curl_ftp_no_epsv
;
87 static const char *curl_http_proxy
;
88 static const char *http_proxy_authmethod
;
90 static const char *http_proxy_ssl_cert
;
91 static const char *http_proxy_ssl_key
;
92 static const char *http_proxy_ssl_ca_info
;
93 static struct credential proxy_cert_auth
= CREDENTIAL_INIT
;
94 static int proxy_ssl_cert_password_required
;
99 } proxy_authmethods
[] = {
100 { "basic", CURLAUTH_BASIC
},
101 { "digest", CURLAUTH_DIGEST
},
102 { "negotiate", CURLAUTH_GSSNEGOTIATE
},
103 { "ntlm", CURLAUTH_NTLM
},
104 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
105 { "anyauth", CURLAUTH_ANY
},
108 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
109 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
113 #ifdef CURLGSSAPI_DELEGATION_FLAG
114 static const char *curl_deleg
;
117 long curl_deleg_param
;
118 } curl_deleg_levels
[] = {
119 { "none", CURLGSSAPI_DELEGATION_NONE
},
120 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG
},
121 { "always", CURLGSSAPI_DELEGATION_FLAG
},
125 static struct credential proxy_auth
= CREDENTIAL_INIT
;
126 static const char *curl_proxyuserpwd
;
127 static const char *curl_cookie_file
;
128 static int curl_save_cookies
;
129 struct credential http_auth
= CREDENTIAL_INIT
;
130 static int http_proactive_auth
;
131 static const char *user_agent
;
132 static int curl_empty_auth
= -1;
134 enum http_follow_config http_follow_config
= HTTP_FOLLOW_INITIAL
;
136 #if LIBCURL_VERSION_NUM >= 0x071700
137 /* Use CURLOPT_KEYPASSWD as is */
138 #elif LIBCURL_VERSION_NUM >= 0x070903
139 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
141 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
144 static struct credential cert_auth
= CREDENTIAL_INIT
;
145 static int ssl_cert_password_required
;
146 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
147 static unsigned long http_auth_methods
= CURLAUTH_ANY
;
148 static int http_auth_methods_restricted
;
149 /* Modes for which empty_auth cannot actually help us. */
150 static unsigned long empty_auth_useless
=
152 #ifdef CURLAUTH_DIGEST_IE
158 static struct curl_slist
*pragma_header
;
159 static struct curl_slist
*no_pragma_header
;
160 static struct string_list extra_http_headers
= STRING_LIST_INIT_DUP
;
162 static struct active_request_slot
*active_queue_head
;
164 static char *cached_accept_language
;
166 static char *http_ssl_backend
;
168 static int http_schannel_check_revoke
= 1;
170 * With the backend being set to `schannel`, setting sslCAinfo would override
171 * the Certificate Store in cURL v7.60.0 and later, which is not what we want
174 static int http_schannel_use_ssl_cainfo
;
176 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
178 size_t size
= eltsize
* nmemb
;
179 struct buffer
*buffer
= buffer_
;
181 if (size
> buffer
->buf
.len
- buffer
->posn
)
182 size
= buffer
->buf
.len
- buffer
->posn
;
183 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
184 buffer
->posn
+= size
;
186 return size
/ eltsize
;
190 int seek_buffer(void *clientp
, curl_off_t offset
, int origin
)
192 struct buffer
*buffer
= clientp
;
194 if (origin
!= SEEK_SET
)
195 BUG("seek_buffer only handles SEEK_SET");
196 if (offset
< 0 || offset
>= buffer
->buf
.len
) {
197 error("curl seek would be outside of buffer");
198 return CURL_SEEKFUNC_FAIL
;
201 buffer
->posn
= offset
;
202 return CURL_SEEKFUNC_OK
;
206 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
208 size_t size
= eltsize
* nmemb
;
209 struct strbuf
*buffer
= buffer_
;
211 strbuf_add(buffer
, ptr
, size
);
215 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
220 static void closedown_active_slot(struct active_request_slot
*slot
)
226 static void finish_active_slot(struct active_request_slot
*slot
)
228 closedown_active_slot(slot
);
229 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
231 if (slot
->finished
!= NULL
)
232 (*slot
->finished
) = 1;
234 /* Store slot results so they can be read after the slot is reused */
235 if (slot
->results
!= NULL
) {
236 slot
->results
->curl_result
= slot
->curl_result
;
237 slot
->results
->http_code
= slot
->http_code
;
238 #if LIBCURL_VERSION_NUM >= 0x070a08
239 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
240 &slot
->results
->auth_avail
);
242 slot
->results
->auth_avail
= 0;
245 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CONNECTCODE
,
246 &slot
->results
->http_connectcode
);
249 /* Run callback if appropriate */
250 if (slot
->callback_func
!= NULL
)
251 slot
->callback_func(slot
->callback_data
);
254 static void xmulti_remove_handle(struct active_request_slot
*slot
)
256 #ifdef USE_CURL_MULTI
257 curl_multi_remove_handle(curlm
, slot
->curl
);
261 #ifdef USE_CURL_MULTI
262 static void process_curl_messages(void)
265 struct active_request_slot
*slot
;
266 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
268 while (curl_message
!= NULL
) {
269 if (curl_message
->msg
== CURLMSG_DONE
) {
270 int curl_result
= curl_message
->data
.result
;
271 slot
= active_queue_head
;
272 while (slot
!= NULL
&&
273 slot
->curl
!= curl_message
->easy_handle
)
276 xmulti_remove_handle(slot
);
277 slot
->curl_result
= curl_result
;
278 finish_active_slot(slot
);
280 fprintf(stderr
, "Received DONE message for unknown request!\n");
283 fprintf(stderr
, "Unknown CURL message received: %d\n",
284 (int)curl_message
->msg
);
286 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
291 static int http_options(const char *var
, const char *value
, void *cb
)
293 if (!strcmp("http.version", var
)) {
294 return git_config_string(&curl_http_version
, var
, value
);
296 if (!strcmp("http.sslverify", var
)) {
297 curl_ssl_verify
= git_config_bool(var
, value
);
300 if (!strcmp("http.sslcipherlist", var
))
301 return git_config_string(&ssl_cipherlist
, var
, value
);
302 if (!strcmp("http.sslversion", var
))
303 return git_config_string(&ssl_version
, var
, value
);
304 if (!strcmp("http.sslcert", var
))
305 return git_config_pathname(&ssl_cert
, var
, value
);
306 #if LIBCURL_VERSION_NUM >= 0x070903
307 if (!strcmp("http.sslkey", var
))
308 return git_config_pathname(&ssl_key
, var
, value
);
310 #if LIBCURL_VERSION_NUM >= 0x070908
311 if (!strcmp("http.sslcapath", var
))
312 return git_config_pathname(&ssl_capath
, var
, value
);
314 if (!strcmp("http.sslcainfo", var
))
315 return git_config_pathname(&ssl_cainfo
, var
, value
);
316 if (!strcmp("http.sslcertpasswordprotected", var
)) {
317 ssl_cert_password_required
= git_config_bool(var
, value
);
320 if (!strcmp("http.ssltry", var
)) {
321 curl_ssl_try
= git_config_bool(var
, value
);
324 if (!strcmp("http.sslbackend", var
)) {
325 free(http_ssl_backend
);
326 http_ssl_backend
= xstrdup_or_null(value
);
330 if (!strcmp("http.schannelcheckrevoke", var
)) {
331 http_schannel_check_revoke
= git_config_bool(var
, value
);
335 if (!strcmp("http.schannelusesslcainfo", var
)) {
336 http_schannel_use_ssl_cainfo
= git_config_bool(var
, value
);
340 if (!strcmp("http.minsessions", var
)) {
341 min_curl_sessions
= git_config_int(var
, value
);
342 #ifndef USE_CURL_MULTI
343 if (min_curl_sessions
> 1)
344 min_curl_sessions
= 1;
348 #ifdef USE_CURL_MULTI
349 if (!strcmp("http.maxrequests", var
)) {
350 max_requests
= git_config_int(var
, value
);
354 if (!strcmp("http.lowspeedlimit", var
)) {
355 curl_low_speed_limit
= (long)git_config_int(var
, value
);
358 if (!strcmp("http.lowspeedtime", var
)) {
359 curl_low_speed_time
= (long)git_config_int(var
, value
);
363 if (!strcmp("http.noepsv", var
)) {
364 curl_ftp_no_epsv
= git_config_bool(var
, value
);
367 if (!strcmp("http.proxy", var
))
368 return git_config_string(&curl_http_proxy
, var
, value
);
370 if (!strcmp("http.proxyauthmethod", var
))
371 return git_config_string(&http_proxy_authmethod
, var
, value
);
373 if (!strcmp("http.proxysslcert", var
))
374 return git_config_string(&http_proxy_ssl_cert
, var
, value
);
376 if (!strcmp("http.proxysslkey", var
))
377 return git_config_string(&http_proxy_ssl_key
, var
, value
);
379 if (!strcmp("http.proxysslcainfo", var
))
380 return git_config_string(&http_proxy_ssl_ca_info
, var
, value
);
382 if (!strcmp("http.proxysslcertpasswordprotected", var
)) {
383 proxy_ssl_cert_password_required
= git_config_bool(var
, value
);
387 if (!strcmp("http.cookiefile", var
))
388 return git_config_pathname(&curl_cookie_file
, var
, value
);
389 if (!strcmp("http.savecookies", var
)) {
390 curl_save_cookies
= git_config_bool(var
, value
);
394 if (!strcmp("http.postbuffer", var
)) {
395 http_post_buffer
= git_config_ssize_t(var
, value
);
396 if (http_post_buffer
< 0)
397 warning(_("negative value for http.postbuffer; defaulting to %d"), LARGE_PACKET_MAX
);
398 if (http_post_buffer
< LARGE_PACKET_MAX
)
399 http_post_buffer
= LARGE_PACKET_MAX
;
403 if (!strcmp("http.useragent", var
))
404 return git_config_string(&user_agent
, var
, value
);
406 if (!strcmp("http.emptyauth", var
)) {
407 if (value
&& !strcmp("auto", value
))
408 curl_empty_auth
= -1;
410 curl_empty_auth
= git_config_bool(var
, value
);
414 if (!strcmp("http.delegation", var
)) {
415 #ifdef CURLGSSAPI_DELEGATION_FLAG
416 return git_config_string(&curl_deleg
, var
, value
);
418 warning(_("Delegation control is not supported with cURL < 7.22.0"));
423 if (!strcmp("http.pinnedpubkey", var
)) {
424 #if LIBCURL_VERSION_NUM >= 0x072c00
425 return git_config_pathname(&ssl_pinnedkey
, var
, value
);
427 warning(_("Public key pinning not supported with cURL < 7.44.0"));
432 if (!strcmp("http.extraheader", var
)) {
434 return config_error_nonbool(var
);
435 } else if (!*value
) {
436 string_list_clear(&extra_http_headers
, 0);
438 string_list_append(&extra_http_headers
, value
);
443 if (!strcmp("http.followredirects", var
)) {
444 if (value
&& !strcmp(value
, "initial"))
445 http_follow_config
= HTTP_FOLLOW_INITIAL
;
446 else if (git_config_bool(var
, value
))
447 http_follow_config
= HTTP_FOLLOW_ALWAYS
;
449 http_follow_config
= HTTP_FOLLOW_NONE
;
453 /* Fall back on the default ones */
454 return git_default_config(var
, value
, cb
);
457 static int curl_empty_auth_enabled(void)
459 if (curl_empty_auth
>= 0)
460 return curl_empty_auth
;
462 #ifndef LIBCURL_CAN_HANDLE_AUTH_ANY
464 * Our libcurl is too old to do AUTH_ANY in the first place;
465 * just default to turning the feature off.
469 * In the automatic case, kick in the empty-auth
470 * hack as long as we would potentially try some
471 * method more exotic than "Basic" or "Digest".
473 * But only do this when this is our second or
474 * subsequent request, as by then we know what
475 * methods are available.
477 if (http_auth_methods_restricted
&&
478 (http_auth_methods
& ~empty_auth_useless
))
484 static void init_curl_http_auth(CURL
*result
)
486 if (!http_auth
.username
|| !*http_auth
.username
) {
487 if (curl_empty_auth_enabled())
488 curl_easy_setopt(result
, CURLOPT_USERPWD
, ":");
492 credential_fill(&http_auth
);
494 #if LIBCURL_VERSION_NUM >= 0x071301
495 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
496 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
499 static struct strbuf up
= STRBUF_INIT
;
501 * Note that we assume we only ever have a single set of
502 * credentials in a given program run, so we do not have
503 * to worry about updating this buffer, only setting its
507 strbuf_addf(&up
, "%s:%s",
508 http_auth
.username
, http_auth
.password
);
509 curl_easy_setopt(result
, CURLOPT_USERPWD
, up
.buf
);
514 /* *var must be free-able */
515 static void var_override(const char **var
, char *value
)
519 *var
= xstrdup(value
);
523 static void set_proxyauth_name_password(CURL
*result
)
525 #if LIBCURL_VERSION_NUM >= 0x071301
526 curl_easy_setopt(result
, CURLOPT_PROXYUSERNAME
,
527 proxy_auth
.username
);
528 curl_easy_setopt(result
, CURLOPT_PROXYPASSWORD
,
529 proxy_auth
.password
);
531 struct strbuf s
= STRBUF_INIT
;
533 strbuf_addstr_urlencode(&s
, proxy_auth
.username
,
534 is_rfc3986_unreserved
);
535 strbuf_addch(&s
, ':');
536 strbuf_addstr_urlencode(&s
, proxy_auth
.password
,
537 is_rfc3986_unreserved
);
538 curl_proxyuserpwd
= strbuf_detach(&s
, NULL
);
539 curl_easy_setopt(result
, CURLOPT_PROXYUSERPWD
, curl_proxyuserpwd
);
543 static void init_curl_proxy_auth(CURL
*result
)
545 if (proxy_auth
.username
) {
546 if (!proxy_auth
.password
)
547 credential_fill(&proxy_auth
);
548 set_proxyauth_name_password(result
);
551 var_override(&http_proxy_authmethod
, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
553 #if LIBCURL_VERSION_NUM >= 0x070a07 /* CURLOPT_PROXYAUTH and CURLAUTH_ANY */
554 if (http_proxy_authmethod
) {
556 for (i
= 0; i
< ARRAY_SIZE(proxy_authmethods
); i
++) {
557 if (!strcmp(http_proxy_authmethod
, proxy_authmethods
[i
].name
)) {
558 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
,
559 proxy_authmethods
[i
].curlauth_param
);
563 if (i
== ARRAY_SIZE(proxy_authmethods
)) {
564 warning("unsupported proxy authentication method %s: using anyauth",
565 http_proxy_authmethod
);
566 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
570 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
574 static int has_cert_password(void)
576 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
578 if (!cert_auth
.password
) {
579 cert_auth
.protocol
= xstrdup("cert");
580 cert_auth
.host
= xstrdup("");
581 cert_auth
.username
= xstrdup("");
582 cert_auth
.path
= xstrdup(ssl_cert
);
583 credential_fill(&cert_auth
);
588 #if LIBCURL_VERSION_NUM >= 0x073400
589 static int has_proxy_cert_password(void)
591 if (http_proxy_ssl_cert
== NULL
|| proxy_ssl_cert_password_required
!= 1)
593 if (!proxy_cert_auth
.password
) {
594 proxy_cert_auth
.protocol
= xstrdup("cert");
595 proxy_cert_auth
.host
= xstrdup("");
596 proxy_cert_auth
.username
= xstrdup("");
597 proxy_cert_auth
.path
= xstrdup(http_proxy_ssl_cert
);
598 credential_fill(&proxy_cert_auth
);
604 #if LIBCURL_VERSION_NUM >= 0x071900
605 static void set_curl_keepalive(CURL
*c
)
607 curl_easy_setopt(c
, CURLOPT_TCP_KEEPALIVE
, 1);
610 #elif LIBCURL_VERSION_NUM >= 0x071000
611 static int sockopt_callback(void *client
, curl_socket_t fd
, curlsocktype type
)
615 socklen_t len
= (socklen_t
)sizeof(ka
);
617 if (type
!= CURLSOCKTYPE_IPCXN
)
620 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
622 warning_errno("unable to set SO_KEEPALIVE on socket");
624 return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
627 static void set_curl_keepalive(CURL
*c
)
629 curl_easy_setopt(c
, CURLOPT_SOCKOPTFUNCTION
, sockopt_callback
);
633 static void set_curl_keepalive(CURL
*c
)
635 /* not supported on older curl versions */
639 static void redact_sensitive_header(struct strbuf
*header
)
641 const char *sensitive_header
;
643 if (trace_curl_redact
&&
644 (skip_prefix(header
->buf
, "Authorization:", &sensitive_header
) ||
645 skip_prefix(header
->buf
, "Proxy-Authorization:", &sensitive_header
))) {
646 /* The first token is the type, which is OK to log */
647 while (isspace(*sensitive_header
))
649 while (*sensitive_header
&& !isspace(*sensitive_header
))
651 /* Everything else is opaque and possibly sensitive */
652 strbuf_setlen(header
, sensitive_header
- header
->buf
);
653 strbuf_addstr(header
, " <redacted>");
654 } else if (trace_curl_redact
&&
655 skip_prefix(header
->buf
, "Cookie:", &sensitive_header
)) {
656 struct strbuf redacted_header
= STRBUF_INIT
;
659 while (isspace(*sensitive_header
))
662 cookie
= sensitive_header
;
666 char *semicolon
= strstr(cookie
, "; ");
669 equals
= strchrnul(cookie
, '=');
671 /* invalid cookie, just append and continue */
672 strbuf_addstr(&redacted_header
, cookie
);
675 strbuf_add(&redacted_header
, cookie
, equals
- cookie
);
676 strbuf_addstr(&redacted_header
, "=<redacted>");
679 * There are more cookies. (Or, for some
680 * reason, the input string ends in "; ".)
682 strbuf_addstr(&redacted_header
, "; ");
683 cookie
= semicolon
+ strlen("; ");
689 strbuf_setlen(header
, sensitive_header
- header
->buf
);
690 strbuf_addbuf(header
, &redacted_header
);
694 static void curl_dump_header(const char *text
, unsigned char *ptr
, size_t size
, int hide_sensitive_header
)
696 struct strbuf out
= STRBUF_INIT
;
697 struct strbuf
**headers
, **header
;
699 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
700 text
, (long)size
, (long)size
);
701 trace_strbuf(&trace_curl
, &out
);
703 strbuf_add(&out
, ptr
, size
);
704 headers
= strbuf_split_max(&out
, '\n', 0);
706 for (header
= headers
; *header
; header
++) {
707 if (hide_sensitive_header
)
708 redact_sensitive_header(*header
);
709 strbuf_insertstr((*header
), 0, text
);
710 strbuf_insertstr((*header
), strlen(text
), ": ");
711 strbuf_rtrim((*header
));
712 strbuf_addch((*header
), '\n');
713 trace_strbuf(&trace_curl
, (*header
));
715 strbuf_list_free(headers
);
716 strbuf_release(&out
);
719 static void curl_dump_data(const char *text
, unsigned char *ptr
, size_t size
)
722 struct strbuf out
= STRBUF_INIT
;
723 unsigned int width
= 60;
725 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
726 text
, (long)size
, (long)size
);
727 trace_strbuf(&trace_curl
, &out
);
729 for (i
= 0; i
< size
; i
+= width
) {
733 strbuf_addf(&out
, "%s: ", text
);
734 for (w
= 0; (w
< width
) && (i
+ w
< size
); w
++) {
735 unsigned char ch
= ptr
[i
+ w
];
738 (ch
>= 0x20) && (ch
< 0x80)
741 strbuf_addch(&out
, '\n');
742 trace_strbuf(&trace_curl
, &out
);
744 strbuf_release(&out
);
747 static int curl_trace(CURL
*handle
, curl_infotype type
, char *data
, size_t size
, void *userp
)
750 enum { NO_FILTER
= 0, DO_FILTER
= 1 };
754 trace_printf_key(&trace_curl
, "== Info: %s", data
);
756 case CURLINFO_HEADER_OUT
:
757 text
= "=> Send header";
758 curl_dump_header(text
, (unsigned char *)data
, size
, DO_FILTER
);
760 case CURLINFO_DATA_OUT
:
761 if (trace_curl_data
) {
762 text
= "=> Send data";
763 curl_dump_data(text
, (unsigned char *)data
, size
);
766 case CURLINFO_SSL_DATA_OUT
:
767 if (trace_curl_data
) {
768 text
= "=> Send SSL data";
769 curl_dump_data(text
, (unsigned char *)data
, size
);
772 case CURLINFO_HEADER_IN
:
773 text
= "<= Recv header";
774 curl_dump_header(text
, (unsigned char *)data
, size
, NO_FILTER
);
776 case CURLINFO_DATA_IN
:
777 if (trace_curl_data
) {
778 text
= "<= Recv data";
779 curl_dump_data(text
, (unsigned char *)data
, size
);
782 case CURLINFO_SSL_DATA_IN
:
783 if (trace_curl_data
) {
784 text
= "<= Recv SSL data";
785 curl_dump_data(text
, (unsigned char *)data
, size
);
789 default: /* we ignore unknown types by default */
795 void http_trace_curl_no_data(void)
797 trace_override_envvar(&trace_curl
, "1");
801 void setup_curl_trace(CURL
*handle
)
803 if (!trace_want(&trace_curl
))
805 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
806 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, curl_trace
);
807 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, NULL
);
810 #ifdef CURLPROTO_HTTP
811 static long get_curl_allowed_protocols(int from_user
)
813 long allowed_protocols
= 0;
815 if (is_transport_allowed("http", from_user
))
816 allowed_protocols
|= CURLPROTO_HTTP
;
817 if (is_transport_allowed("https", from_user
))
818 allowed_protocols
|= CURLPROTO_HTTPS
;
819 if (is_transport_allowed("ftp", from_user
))
820 allowed_protocols
|= CURLPROTO_FTP
;
821 if (is_transport_allowed("ftps", from_user
))
822 allowed_protocols
|= CURLPROTO_FTPS
;
824 return allowed_protocols
;
828 #if LIBCURL_VERSION_NUM >=0x072f00
829 static int get_curl_http_version_opt(const char *version_string
, long *opt
)
836 { "HTTP/1.1", CURL_HTTP_VERSION_1_1
},
837 { "HTTP/2", CURL_HTTP_VERSION_2
}
840 for (i
= 0; i
< ARRAY_SIZE(choice
); i
++) {
841 if (!strcmp(version_string
, choice
[i
].name
)) {
842 *opt
= choice
[i
].opt_token
;
847 warning("unknown value given to http.version: '%s'", version_string
);
848 return -1; /* not found */
853 static CURL
*get_curl_handle(void)
855 CURL
*result
= curl_easy_init();
858 die("curl_easy_init failed");
860 if (!curl_ssl_verify
) {
861 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
862 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
864 /* Verify authenticity of the peer's certificate */
865 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
866 /* The name in the cert must match whom we tried to connect */
867 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
870 #if LIBCURL_VERSION_NUM >= 0x072f00 // 7.47.0
871 if (curl_http_version
) {
873 if (!get_curl_http_version_opt(curl_http_version
, &opt
)) {
874 /* Set request use http version */
875 curl_easy_setopt(result
, CURLOPT_HTTP_VERSION
, opt
);
880 #if LIBCURL_VERSION_NUM >= 0x070907
881 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
883 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
884 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
887 #ifdef CURLGSSAPI_DELEGATION_FLAG
890 for (i
= 0; i
< ARRAY_SIZE(curl_deleg_levels
); i
++) {
891 if (!strcmp(curl_deleg
, curl_deleg_levels
[i
].name
)) {
892 curl_easy_setopt(result
, CURLOPT_GSSAPI_DELEGATION
,
893 curl_deleg_levels
[i
].curl_deleg_param
);
897 if (i
== ARRAY_SIZE(curl_deleg_levels
))
898 warning("Unknown delegation method '%s': using default",
903 if (http_ssl_backend
&& !strcmp("schannel", http_ssl_backend
) &&
904 !http_schannel_check_revoke
) {
905 #if LIBCURL_VERSION_NUM >= 0x072c00
906 curl_easy_setopt(result
, CURLOPT_SSL_OPTIONS
, CURLSSLOPT_NO_REVOKE
);
908 warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
912 if (http_proactive_auth
)
913 init_curl_http_auth(result
);
915 if (getenv("GIT_SSL_VERSION"))
916 ssl_version
= getenv("GIT_SSL_VERSION");
917 if (ssl_version
&& *ssl_version
) {
919 for (i
= 0; i
< ARRAY_SIZE(sslversions
); i
++) {
920 if (!strcmp(ssl_version
, sslversions
[i
].name
)) {
921 curl_easy_setopt(result
, CURLOPT_SSLVERSION
,
922 sslversions
[i
].ssl_version
);
926 if (i
== ARRAY_SIZE(sslversions
))
927 warning("unsupported ssl version %s: using default",
931 if (getenv("GIT_SSL_CIPHER_LIST"))
932 ssl_cipherlist
= getenv("GIT_SSL_CIPHER_LIST");
933 if (ssl_cipherlist
!= NULL
&& *ssl_cipherlist
)
934 curl_easy_setopt(result
, CURLOPT_SSL_CIPHER_LIST
,
937 if (ssl_cert
!= NULL
)
938 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
939 if (has_cert_password())
940 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
941 #if LIBCURL_VERSION_NUM >= 0x070903
943 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
945 #if LIBCURL_VERSION_NUM >= 0x070908
946 if (ssl_capath
!= NULL
)
947 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
949 #if LIBCURL_VERSION_NUM >= 0x072c00
950 if (ssl_pinnedkey
!= NULL
)
951 curl_easy_setopt(result
, CURLOPT_PINNEDPUBLICKEY
, ssl_pinnedkey
);
953 if (http_ssl_backend
&& !strcmp("schannel", http_ssl_backend
) &&
954 !http_schannel_use_ssl_cainfo
) {
955 curl_easy_setopt(result
, CURLOPT_CAINFO
, NULL
);
956 #if LIBCURL_VERSION_NUM >= 0x073400
957 curl_easy_setopt(result
, CURLOPT_PROXY_CAINFO
, NULL
);
959 } else if (ssl_cainfo
!= NULL
|| http_proxy_ssl_ca_info
!= NULL
) {
960 if (ssl_cainfo
!= NULL
)
961 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
962 #if LIBCURL_VERSION_NUM >= 0x073400
963 if (http_proxy_ssl_ca_info
!= NULL
)
964 curl_easy_setopt(result
, CURLOPT_PROXY_CAINFO
, http_proxy_ssl_ca_info
);
968 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
969 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
970 curl_low_speed_limit
);
971 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
972 curl_low_speed_time
);
975 curl_easy_setopt(result
, CURLOPT_MAXREDIRS
, 20);
976 #if LIBCURL_VERSION_NUM >= 0x071301
977 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
978 #elif LIBCURL_VERSION_NUM >= 0x071101
979 curl_easy_setopt(result
, CURLOPT_POST301
, 1);
981 #ifdef CURLPROTO_HTTP
982 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS
,
983 get_curl_allowed_protocols(0));
984 curl_easy_setopt(result
, CURLOPT_PROTOCOLS
,
985 get_curl_allowed_protocols(-1));
987 warning(_("Protocol restrictions not supported with cURL < 7.19.4"));
989 if (getenv("GIT_CURL_VERBOSE"))
990 http_trace_curl_no_data();
991 setup_curl_trace(result
);
992 if (getenv("GIT_TRACE_CURL_NO_DATA"))
994 if (!git_env_bool("GIT_TRACE_REDACT", 1))
995 trace_curl_redact
= 0;
997 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
998 user_agent
? user_agent
: git_user_agent());
1000 if (curl_ftp_no_epsv
)
1001 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
1003 #ifdef CURLOPT_USE_SSL
1005 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
1009 * CURL also examines these variables as a fallback; but we need to query
1010 * them here in order to decide whether to prompt for missing password (cf.
1011 * init_curl_proxy_auth()).
1013 * Unlike many other common environment variables, these are historically
1014 * lowercase only. It appears that CURL did not know this and implemented
1015 * only uppercase variants, which was later corrected to take both - with
1016 * the exception of http_proxy, which is lowercase only also in CURL. As
1017 * the lowercase versions are the historical quasi-standard, they take
1018 * precedence here, as in CURL.
1020 if (!curl_http_proxy
) {
1021 if (http_auth
.protocol
&& !strcmp(http_auth
.protocol
, "https")) {
1022 var_override(&curl_http_proxy
, getenv("HTTPS_PROXY"));
1023 var_override(&curl_http_proxy
, getenv("https_proxy"));
1025 var_override(&curl_http_proxy
, getenv("http_proxy"));
1027 if (!curl_http_proxy
) {
1028 var_override(&curl_http_proxy
, getenv("ALL_PROXY"));
1029 var_override(&curl_http_proxy
, getenv("all_proxy"));
1033 if (curl_http_proxy
&& curl_http_proxy
[0] == '\0') {
1035 * Handle case with the empty http.proxy value here to keep
1036 * common code clean.
1037 * NB: empty option disables proxying at all.
1039 curl_easy_setopt(result
, CURLOPT_PROXY
, "");
1040 } else if (curl_http_proxy
) {
1041 #if LIBCURL_VERSION_NUM >= 0x071800
1042 if (starts_with(curl_http_proxy
, "socks5h"))
1043 curl_easy_setopt(result
,
1044 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5_HOSTNAME
);
1045 else if (starts_with(curl_http_proxy
, "socks5"))
1046 curl_easy_setopt(result
,
1047 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
1048 else if (starts_with(curl_http_proxy
, "socks4a"))
1049 curl_easy_setopt(result
,
1050 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4A
);
1051 else if (starts_with(curl_http_proxy
, "socks"))
1052 curl_easy_setopt(result
,
1053 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4
);
1055 #if LIBCURL_VERSION_NUM >= 0x073400
1056 else if (starts_with(curl_http_proxy
, "https")) {
1057 curl_easy_setopt(result
, CURLOPT_PROXYTYPE
, CURLPROXY_HTTPS
);
1059 if (http_proxy_ssl_cert
)
1060 curl_easy_setopt(result
, CURLOPT_PROXY_SSLCERT
, http_proxy_ssl_cert
);
1062 if (http_proxy_ssl_key
)
1063 curl_easy_setopt(result
, CURLOPT_PROXY_SSLKEY
, http_proxy_ssl_key
);
1065 if (has_proxy_cert_password())
1066 curl_easy_setopt(result
, CURLOPT_PROXY_KEYPASSWD
, proxy_cert_auth
.password
);
1069 if (strstr(curl_http_proxy
, "://"))
1070 credential_from_url(&proxy_auth
, curl_http_proxy
);
1072 struct strbuf url
= STRBUF_INIT
;
1073 strbuf_addf(&url
, "http://%s", curl_http_proxy
);
1074 credential_from_url(&proxy_auth
, url
.buf
);
1075 strbuf_release(&url
);
1078 if (!proxy_auth
.host
)
1079 die("Invalid proxy URL '%s'", curl_http_proxy
);
1081 curl_easy_setopt(result
, CURLOPT_PROXY
, proxy_auth
.host
);
1082 #if LIBCURL_VERSION_NUM >= 0x071304
1083 var_override(&curl_no_proxy
, getenv("NO_PROXY"));
1084 var_override(&curl_no_proxy
, getenv("no_proxy"));
1085 curl_easy_setopt(result
, CURLOPT_NOPROXY
, curl_no_proxy
);
1088 init_curl_proxy_auth(result
);
1090 set_curl_keepalive(result
);
1095 static void set_from_env(const char **var
, const char *envname
)
1097 const char *val
= getenv(envname
);
1102 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
1104 char *low_speed_limit
;
1105 char *low_speed_time
;
1106 char *normalized_url
;
1107 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
1109 config
.section
= "http";
1111 config
.collect_fn
= http_options
;
1112 config
.cascade_fn
= git_default_config
;
1115 http_is_verbose
= 0;
1116 normalized_url
= url_normalize(url
, &config
.url
);
1118 git_config(urlmatch_config_entry
, &config
);
1119 free(normalized_url
);
1120 string_list_clear(&config
.vars
, 1);
1122 #if LIBCURL_VERSION_NUM >= 0x073800
1123 if (http_ssl_backend
) {
1124 const curl_ssl_backend
**backends
;
1125 struct strbuf buf
= STRBUF_INIT
;
1128 switch (curl_global_sslset(-1, http_ssl_backend
, &backends
)) {
1129 case CURLSSLSET_UNKNOWN_BACKEND
:
1130 strbuf_addf(&buf
, _("Unsupported SSL backend '%s'. "
1131 "Supported SSL backends:"),
1133 for (i
= 0; backends
[i
]; i
++)
1134 strbuf_addf(&buf
, "\n\t%s", backends
[i
]->name
);
1136 case CURLSSLSET_NO_BACKENDS
:
1137 die(_("Could not set SSL backend to '%s': "
1138 "cURL was built without SSL backends"),
1140 case CURLSSLSET_TOO_LATE
:
1141 die(_("Could not set SSL backend to '%s': already set"),
1149 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
1150 die("curl_global_init failed");
1152 http_proactive_auth
= proactive_auth
;
1154 if (remote
&& remote
->http_proxy
)
1155 curl_http_proxy
= xstrdup(remote
->http_proxy
);
1158 var_override(&http_proxy_authmethod
, remote
->http_proxy_authmethod
);
1160 pragma_header
= curl_slist_append(http_copy_default_headers(),
1161 "Pragma: no-cache");
1162 no_pragma_header
= curl_slist_append(http_copy_default_headers(),
1165 #ifdef USE_CURL_MULTI
1167 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1168 if (http_max_requests
!= NULL
)
1169 max_requests
= atoi(http_max_requests
);
1172 curlm
= curl_multi_init();
1174 die("curl_multi_init failed");
1177 if (getenv("GIT_SSL_NO_VERIFY"))
1178 curl_ssl_verify
= 0;
1180 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
1181 #if LIBCURL_VERSION_NUM >= 0x070903
1182 set_from_env(&ssl_key
, "GIT_SSL_KEY");
1184 #if LIBCURL_VERSION_NUM >= 0x070908
1185 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
1187 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
1189 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
1191 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1192 if (low_speed_limit
!= NULL
)
1193 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1194 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1195 if (low_speed_time
!= NULL
)
1196 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1198 if (curl_ssl_verify
== -1)
1199 curl_ssl_verify
= 1;
1201 curl_session_count
= 0;
1202 #ifdef USE_CURL_MULTI
1203 if (max_requests
< 1)
1204 max_requests
= DEFAULT_MAX_REQUESTS
;
1207 set_from_env(&http_proxy_ssl_cert
, "GIT_PROXY_SSL_CERT");
1208 set_from_env(&http_proxy_ssl_key
, "GIT_PROXY_SSL_KEY");
1209 set_from_env(&http_proxy_ssl_ca_info
, "GIT_PROXY_SSL_CAINFO");
1211 if (getenv("GIT_PROXY_SSL_CERT_PASSWORD_PROTECTED"))
1212 proxy_ssl_cert_password_required
= 1;
1214 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1215 curl_ftp_no_epsv
= 1;
1218 credential_from_url(&http_auth
, url
);
1219 if (!ssl_cert_password_required
&&
1220 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
1221 starts_with(url
, "https://"))
1222 ssl_cert_password_required
= 1;
1225 #ifndef NO_CURL_EASY_DUPHANDLE
1226 curl_default
= get_curl_handle();
1230 void http_cleanup(void)
1232 struct active_request_slot
*slot
= active_queue_head
;
1234 while (slot
!= NULL
) {
1235 struct active_request_slot
*next
= slot
->next
;
1236 if (slot
->curl
!= NULL
) {
1237 xmulti_remove_handle(slot
);
1238 curl_easy_cleanup(slot
->curl
);
1243 active_queue_head
= NULL
;
1245 #ifndef NO_CURL_EASY_DUPHANDLE
1246 curl_easy_cleanup(curl_default
);
1249 #ifdef USE_CURL_MULTI
1250 curl_multi_cleanup(curlm
);
1252 curl_global_cleanup();
1254 string_list_clear(&extra_http_headers
, 0);
1256 curl_slist_free_all(pragma_header
);
1257 pragma_header
= NULL
;
1259 curl_slist_free_all(no_pragma_header
);
1260 no_pragma_header
= NULL
;
1262 if (curl_http_proxy
) {
1263 free((void *)curl_http_proxy
);
1264 curl_http_proxy
= NULL
;
1267 if (proxy_auth
.password
) {
1268 memset(proxy_auth
.password
, 0, strlen(proxy_auth
.password
));
1269 FREE_AND_NULL(proxy_auth
.password
);
1272 free((void *)curl_proxyuserpwd
);
1273 curl_proxyuserpwd
= NULL
;
1275 free((void *)http_proxy_authmethod
);
1276 http_proxy_authmethod
= NULL
;
1278 if (cert_auth
.password
!= NULL
) {
1279 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
1280 FREE_AND_NULL(cert_auth
.password
);
1282 ssl_cert_password_required
= 0;
1284 if (proxy_cert_auth
.password
!= NULL
) {
1285 memset(proxy_cert_auth
.password
, 0, strlen(proxy_cert_auth
.password
));
1286 FREE_AND_NULL(proxy_cert_auth
.password
);
1288 proxy_ssl_cert_password_required
= 0;
1290 FREE_AND_NULL(cached_accept_language
);
1293 struct active_request_slot
*get_active_slot(void)
1295 struct active_request_slot
*slot
= active_queue_head
;
1296 struct active_request_slot
*newslot
;
1298 #ifdef USE_CURL_MULTI
1301 /* Wait for a slot to open up if the queue is full */
1302 while (active_requests
>= max_requests
) {
1303 curl_multi_perform(curlm
, &num_transfers
);
1304 if (num_transfers
< active_requests
)
1305 process_curl_messages();
1309 while (slot
!= NULL
&& slot
->in_use
)
1313 newslot
= xmalloc(sizeof(*newslot
));
1314 newslot
->curl
= NULL
;
1315 newslot
->in_use
= 0;
1316 newslot
->next
= NULL
;
1318 slot
= active_queue_head
;
1320 active_queue_head
= newslot
;
1322 while (slot
->next
!= NULL
)
1324 slot
->next
= newslot
;
1329 if (slot
->curl
== NULL
) {
1330 #ifdef NO_CURL_EASY_DUPHANDLE
1331 slot
->curl
= get_curl_handle();
1333 slot
->curl
= curl_easy_duphandle(curl_default
);
1335 curl_session_count
++;
1340 slot
->results
= NULL
;
1341 slot
->finished
= NULL
;
1342 slot
->callback_data
= NULL
;
1343 slot
->callback_func
= NULL
;
1344 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
1345 if (curl_save_cookies
)
1346 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
1347 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
1348 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
1349 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
1350 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
1351 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
1352 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
1353 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
1354 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1355 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
1356 curl_easy_setopt(slot
->curl
, CURLOPT_RANGE
, NULL
);
1359 * Default following to off unless "ALWAYS" is configured; this gives
1360 * callers a sane starting point, and they can tweak for individual
1361 * HTTP_FOLLOW_* cases themselves.
1363 if (http_follow_config
== HTTP_FOLLOW_ALWAYS
)
1364 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1366 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 0);
1368 #if LIBCURL_VERSION_NUM >= 0x070a08
1369 curl_easy_setopt(slot
->curl
, CURLOPT_IPRESOLVE
, git_curl_ipresolve
);
1371 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1372 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPAUTH
, http_auth_methods
);
1374 if (http_auth
.password
|| curl_empty_auth_enabled())
1375 init_curl_http_auth(slot
->curl
);
1380 int start_active_slot(struct active_request_slot
*slot
)
1382 #ifdef USE_CURL_MULTI
1383 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
1386 if (curlm_result
!= CURLM_OK
&&
1387 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
1388 warning("curl_multi_add_handle failed: %s",
1389 curl_multi_strerror(curlm_result
));
1396 * We know there must be something to do, since we just added
1399 curl_multi_perform(curlm
, &num_transfers
);
1404 #ifdef USE_CURL_MULTI
1407 int (*fill
)(void *);
1408 struct fill_chain
*next
;
1411 static struct fill_chain
*fill_cfg
;
1413 void add_fill_function(void *data
, int (*fill
)(void *))
1415 struct fill_chain
*new_fill
= xmalloc(sizeof(*new_fill
));
1416 struct fill_chain
**linkp
= &fill_cfg
;
1417 new_fill
->data
= data
;
1418 new_fill
->fill
= fill
;
1419 new_fill
->next
= NULL
;
1421 linkp
= &(*linkp
)->next
;
1425 void fill_active_slots(void)
1427 struct active_request_slot
*slot
= active_queue_head
;
1429 while (active_requests
< max_requests
) {
1430 struct fill_chain
*fill
;
1431 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
1432 if (fill
->fill(fill
->data
))
1439 while (slot
!= NULL
) {
1440 if (!slot
->in_use
&& slot
->curl
!= NULL
1441 && curl_session_count
> min_curl_sessions
) {
1442 curl_easy_cleanup(slot
->curl
);
1444 curl_session_count
--;
1450 void step_active_slots(void)
1453 CURLMcode curlm_result
;
1456 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
1457 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
1458 if (num_transfers
< active_requests
) {
1459 process_curl_messages();
1460 fill_active_slots();
1465 void run_active_slot(struct active_request_slot
*slot
)
1467 #ifdef USE_CURL_MULTI
1472 struct timeval select_timeout
;
1475 slot
->finished
= &finished
;
1477 step_active_slots();
1480 #if LIBCURL_VERSION_NUM >= 0x070f04
1482 curl_multi_timeout(curlm
, &curl_timeout
);
1483 if (curl_timeout
== 0) {
1485 } else if (curl_timeout
== -1) {
1486 select_timeout
.tv_sec
= 0;
1487 select_timeout
.tv_usec
= 50000;
1489 select_timeout
.tv_sec
= curl_timeout
/ 1000;
1490 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
1493 select_timeout
.tv_sec
= 0;
1494 select_timeout
.tv_usec
= 50000;
1501 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
1504 * It can happen that curl_multi_timeout returns a pathologically
1505 * long timeout when curl_multi_fdset returns no file descriptors
1506 * to read. See commit message for more details.
1509 (select_timeout
.tv_sec
> 0 ||
1510 select_timeout
.tv_usec
> 50000)) {
1511 select_timeout
.tv_sec
= 0;
1512 select_timeout
.tv_usec
= 50000;
1515 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
1519 while (slot
->in_use
) {
1520 slot
->curl_result
= curl_easy_perform(slot
->curl
);
1521 finish_active_slot(slot
);
1526 static void release_active_slot(struct active_request_slot
*slot
)
1528 closedown_active_slot(slot
);
1530 xmulti_remove_handle(slot
);
1531 if (curl_session_count
> min_curl_sessions
) {
1532 curl_easy_cleanup(slot
->curl
);
1534 curl_session_count
--;
1537 #ifdef USE_CURL_MULTI
1538 fill_active_slots();
1542 void finish_all_active_slots(void)
1544 struct active_request_slot
*slot
= active_queue_head
;
1546 while (slot
!= NULL
)
1548 run_active_slot(slot
);
1549 slot
= active_queue_head
;
1555 /* Helpers for modifying and creating URLs */
1556 static inline int needs_quote(int ch
)
1558 if (((ch
>= 'A') && (ch
<= 'Z'))
1559 || ((ch
>= 'a') && (ch
<= 'z'))
1560 || ((ch
>= '0') && (ch
<= '9'))
1568 static char *quote_ref_url(const char *base
, const char *ref
)
1570 struct strbuf buf
= STRBUF_INIT
;
1574 end_url_with_slash(&buf
, base
);
1576 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
1577 if (needs_quote(ch
))
1578 strbuf_addf(&buf
, "%%%02x", ch
);
1580 strbuf_addch(&buf
, *cp
);
1582 return strbuf_detach(&buf
, NULL
);
1585 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
1587 int only_two_digit_prefix
)
1589 end_url_with_slash(buf
, url
);
1591 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
1592 if (!only_two_digit_prefix
)
1593 strbuf_addstr(buf
, hex
+ 2);
1596 char *get_remote_object_url(const char *url
, const char *hex
,
1597 int only_two_digit_prefix
)
1599 struct strbuf buf
= STRBUF_INIT
;
1600 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
1601 return strbuf_detach(&buf
, NULL
);
1604 void normalize_curl_result(CURLcode
*result
, long http_code
,
1605 char *errorstr
, size_t errorlen
)
1608 * If we see a failing http code with CURLE_OK, we have turned off
1609 * FAILONERROR (to keep the server's custom error response), and should
1610 * translate the code into failure here.
1612 * Likewise, if we see a redirect (30x code), that means we turned off
1613 * redirect-following, and we should treat the result as an error.
1615 if (*result
== CURLE_OK
&& http_code
>= 300) {
1616 *result
= CURLE_HTTP_RETURNED_ERROR
;
1618 * Normally curl will already have put the "reason phrase"
1619 * from the server into curl_errorstr; unfortunately without
1620 * FAILONERROR it is lost, so we can give only the numeric
1623 xsnprintf(errorstr
, errorlen
,
1624 "The requested URL returned error: %ld",
1629 static int handle_curl_result(struct slot_results
*results
)
1631 normalize_curl_result(&results
->curl_result
, results
->http_code
,
1632 curl_errorstr
, sizeof(curl_errorstr
));
1634 if (results
->curl_result
== CURLE_OK
) {
1635 credential_approve(&http_auth
);
1636 if (proxy_auth
.password
)
1637 credential_approve(&proxy_auth
);
1639 } else if (missing_target(results
))
1640 return HTTP_MISSING_TARGET
;
1641 else if (results
->http_code
== 401) {
1642 if (http_auth
.username
&& http_auth
.password
) {
1643 credential_reject(&http_auth
);
1646 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1647 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
1648 if (results
->auth_avail
) {
1649 http_auth_methods
&= results
->auth_avail
;
1650 http_auth_methods_restricted
= 1;
1656 if (results
->http_connectcode
== 407)
1657 credential_reject(&proxy_auth
);
1658 #if LIBCURL_VERSION_NUM >= 0x070c00
1659 if (!curl_errorstr
[0])
1660 strlcpy(curl_errorstr
,
1661 curl_easy_strerror(results
->curl_result
),
1662 sizeof(curl_errorstr
));
1668 int run_one_slot(struct active_request_slot
*slot
,
1669 struct slot_results
*results
)
1671 slot
->results
= results
;
1672 if (!start_active_slot(slot
)) {
1673 xsnprintf(curl_errorstr
, sizeof(curl_errorstr
),
1674 "failed to start HTTP request");
1675 return HTTP_START_FAILED
;
1678 run_active_slot(slot
);
1679 return handle_curl_result(results
);
1682 struct curl_slist
*http_copy_default_headers(void)
1684 struct curl_slist
*headers
= NULL
;
1685 const struct string_list_item
*item
;
1687 for_each_string_list_item(item
, &extra_http_headers
)
1688 headers
= curl_slist_append(headers
, item
->string
);
1693 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
1699 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
1701 strbuf_addstr(buf
, ptr
);
1706 * Check for and extract a content-type parameter. "raw"
1707 * should be positioned at the start of the potential
1708 * parameter, with any whitespace already removed.
1710 * "name" is the name of the parameter. The value is appended
1713 static int extract_param(const char *raw
, const char *name
,
1716 size_t len
= strlen(name
);
1718 if (strncasecmp(raw
, name
, len
))
1726 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
1727 strbuf_addch(out
, *raw
++);
1732 * Extract a normalized version of the content type, with any
1733 * spaces suppressed, all letters lowercased, and no trailing ";"
1736 * Note that we will silently remove even invalid whitespace. For
1737 * example, "text / plain" is specifically forbidden by RFC 2616,
1738 * but "text/plain" is the only reasonable output, and this keeps
1741 * If the "charset" argument is not NULL, store the value of any
1742 * charset parameter there.
1745 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1746 * "text / plain" -> "text/plain"
1748 static void extract_content_type(struct strbuf
*raw
, struct strbuf
*type
,
1749 struct strbuf
*charset
)
1754 strbuf_grow(type
, raw
->len
);
1755 for (p
= raw
->buf
; *p
; p
++) {
1762 strbuf_addch(type
, tolower(*p
));
1768 strbuf_reset(charset
);
1770 while (isspace(*p
) || *p
== ';')
1772 if (!extract_param(p
, "charset", charset
))
1774 while (*p
&& !isspace(*p
))
1778 if (!charset
->len
&& starts_with(type
->buf
, "text/"))
1779 strbuf_addstr(charset
, "ISO-8859-1");
1782 static void write_accept_language(struct strbuf
*buf
)
1785 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1786 * that, q-value will be smaller than 0.001, the minimum q-value the
1787 * HTTP specification allows. See
1788 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1790 const int MAX_DECIMAL_PLACES
= 3;
1791 const int MAX_LANGUAGE_TAGS
= 1000;
1792 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE
= 4000;
1793 char **language_tags
= NULL
;
1795 const char *s
= get_preferred_languages();
1797 struct strbuf tag
= STRBUF_INIT
;
1799 /* Don't add Accept-Language header if no language is preferred. */
1804 * Split the colon-separated string of preferred languages into
1805 * language_tags array.
1808 /* collect language tag */
1809 for (; *s
&& (isalnum(*s
) || *s
== '_'); s
++)
1810 strbuf_addch(&tag
, *s
== '_' ? '-' : *s
);
1812 /* skip .codeset, @modifier and any other unnecessary parts */
1813 while (*s
&& *s
!= ':')
1818 REALLOC_ARRAY(language_tags
, num_langs
);
1819 language_tags
[num_langs
- 1] = strbuf_detach(&tag
, NULL
);
1820 if (num_langs
>= MAX_LANGUAGE_TAGS
- 1) /* -1 for '*' */
1825 /* write Accept-Language header into buf */
1827 int last_buf_len
= 0;
1833 REALLOC_ARRAY(language_tags
, num_langs
+ 1);
1834 language_tags
[num_langs
++] = "*"; /* it's OK; this won't be freed */
1836 /* compute decimal_places */
1837 for (max_q
= 1, decimal_places
= 0;
1838 max_q
< num_langs
&& decimal_places
<= MAX_DECIMAL_PLACES
;
1839 decimal_places
++, max_q
*= 10)
1842 xsnprintf(q_format
, sizeof(q_format
), ";q=0.%%0%dd", decimal_places
);
1844 strbuf_addstr(buf
, "Accept-Language: ");
1846 for (i
= 0; i
< num_langs
; i
++) {
1848 strbuf_addstr(buf
, ", ");
1850 strbuf_addstr(buf
, language_tags
[i
]);
1853 strbuf_addf(buf
, q_format
, max_q
- i
);
1855 if (buf
->len
> MAX_ACCEPT_LANGUAGE_HEADER_SIZE
) {
1856 strbuf_remove(buf
, last_buf_len
, buf
->len
- last_buf_len
);
1860 last_buf_len
= buf
->len
;
1864 /* free language tags -- last one is a static '*' */
1865 for (i
= 0; i
< num_langs
- 1; i
++)
1866 free(language_tags
[i
]);
1867 free(language_tags
);
1871 * Get an Accept-Language header which indicates user's preferred languages.
1875 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1876 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1877 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1878 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1879 * LANGUAGE= LANG=C -> ""
1881 static const char *get_accept_language(void)
1883 if (!cached_accept_language
) {
1884 struct strbuf buf
= STRBUF_INIT
;
1885 write_accept_language(&buf
);
1887 cached_accept_language
= strbuf_detach(&buf
, NULL
);
1890 return cached_accept_language
;
1893 static void http_opt_request_remainder(CURL
*curl
, off_t pos
)
1896 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
"-", (uintmax_t)pos
);
1897 curl_easy_setopt(curl
, CURLOPT_RANGE
, buf
);
1900 /* http_request() targets */
1901 #define HTTP_REQUEST_STRBUF 0
1902 #define HTTP_REQUEST_FILE 1
1904 static int http_request(const char *url
,
1905 void *result
, int target
,
1906 const struct http_get_options
*options
)
1908 struct active_request_slot
*slot
;
1909 struct slot_results results
;
1910 struct curl_slist
*headers
= http_copy_default_headers();
1911 struct strbuf buf
= STRBUF_INIT
;
1912 const char *accept_language
;
1915 slot
= get_active_slot();
1916 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1918 if (result
== NULL
) {
1919 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
1921 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1922 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
1924 if (target
== HTTP_REQUEST_FILE
) {
1925 off_t posn
= ftello(result
);
1926 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1929 http_opt_request_remainder(slot
->curl
, posn
);
1931 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1935 accept_language
= get_accept_language();
1937 if (accept_language
)
1938 headers
= curl_slist_append(headers
, accept_language
);
1940 strbuf_addstr(&buf
, "Pragma:");
1941 if (options
&& options
->no_cache
)
1942 strbuf_addstr(&buf
, " no-cache");
1943 if (options
&& options
->initial_request
&&
1944 http_follow_config
== HTTP_FOLLOW_INITIAL
)
1945 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1947 headers
= curl_slist_append(headers
, buf
.buf
);
1949 /* Add additional headers here */
1950 if (options
&& options
->extra_headers
) {
1951 const struct string_list_item
*item
;
1952 for_each_string_list_item(item
, options
->extra_headers
) {
1953 headers
= curl_slist_append(headers
, item
->string
);
1957 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1958 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1959 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
1960 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1962 ret
= run_one_slot(slot
, &results
);
1964 if (options
&& options
->content_type
) {
1965 struct strbuf raw
= STRBUF_INIT
;
1966 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
, &raw
);
1967 extract_content_type(&raw
, options
->content_type
,
1969 strbuf_release(&raw
);
1972 if (options
&& options
->effective_url
)
1973 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
1974 options
->effective_url
);
1976 curl_slist_free_all(headers
);
1977 strbuf_release(&buf
);
1983 * Update the "base" url to a more appropriate value, as deduced by
1984 * redirects seen when requesting a URL starting with "url".
1986 * The "asked" parameter is a URL that we asked curl to access, and must begin
1989 * The "got" parameter is the URL that curl reported to us as where we ended
1992 * Returns 1 if we updated the base url, 0 otherwise.
1994 * Our basic strategy is to compare "base" and "asked" to find the bits
1995 * specific to our request. We then strip those bits off of "got" to yield the
1996 * new base. So for example, if our base is "http://example.com/foo.git",
1997 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1998 * with "https://other.example.com/foo.git/info/refs". We would want the
1999 * new URL to become "https://other.example.com/foo.git".
2001 * Note that this assumes a sane redirect scheme. It's entirely possible
2002 * in the example above to end up at a URL that does not even end in
2003 * "info/refs". In such a case we die. There's not much we can do, such a
2004 * scheme is unlikely to represent a real git repository, and failing to
2005 * rewrite the base opens options for malicious redirects to do funny things.
2007 static int update_url_from_redirect(struct strbuf
*base
,
2009 const struct strbuf
*got
)
2014 if (!strcmp(asked
, got
->buf
))
2017 if (!skip_prefix(asked
, base
->buf
, &tail
))
2018 BUG("update_url_from_redirect: %s is not a superset of %s",
2022 if (!strip_suffix_mem(got
->buf
, &new_len
, tail
))
2023 die(_("unable to update url base from redirection:\n"
2029 strbuf_add(base
, got
->buf
, new_len
);
2034 static int http_request_reauth(const char *url
,
2035 void *result
, int target
,
2036 struct http_get_options
*options
)
2038 int ret
= http_request(url
, result
, target
, options
);
2040 if (ret
!= HTTP_OK
&& ret
!= HTTP_REAUTH
)
2043 if (options
&& options
->effective_url
&& options
->base_url
) {
2044 if (update_url_from_redirect(options
->base_url
,
2045 url
, options
->effective_url
)) {
2046 credential_from_url(&http_auth
, options
->base_url
->buf
);
2047 url
= options
->effective_url
->buf
;
2051 if (ret
!= HTTP_REAUTH
)
2055 * The previous request may have put cruft into our output stream; we
2056 * should clear it out before making our next request.
2059 case HTTP_REQUEST_STRBUF
:
2060 strbuf_reset(result
);
2062 case HTTP_REQUEST_FILE
:
2063 if (fflush(result
)) {
2064 error_errno("unable to flush a file");
2065 return HTTP_START_FAILED
;
2068 if (ftruncate(fileno(result
), 0) < 0) {
2069 error_errno("unable to truncate a file");
2070 return HTTP_START_FAILED
;
2074 BUG("Unknown http_request target");
2077 credential_fill(&http_auth
);
2079 return http_request(url
, result
, target
, options
);
2082 int http_get_strbuf(const char *url
,
2083 struct strbuf
*result
,
2084 struct http_get_options
*options
)
2086 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
2090 * Downloads a URL and stores the result in the given file.
2092 * If a previous interrupted download is detected (i.e. a previous temporary
2093 * file is still around) the download is resumed.
2095 static int http_get_file(const char *url
, const char *filename
,
2096 struct http_get_options
*options
)
2099 struct strbuf tmpfile
= STRBUF_INIT
;
2102 strbuf_addf(&tmpfile
, "%s.temp", filename
);
2103 result
= fopen(tmpfile
.buf
, "a");
2105 error("Unable to open local file %s", tmpfile
.buf
);
2110 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
2113 if (ret
== HTTP_OK
&& finalize_object_file(tmpfile
.buf
, filename
))
2116 strbuf_release(&tmpfile
);
2120 int http_fetch_ref(const char *base
, struct ref
*ref
)
2122 struct http_get_options options
= {0};
2124 struct strbuf buffer
= STRBUF_INIT
;
2127 options
.no_cache
= 1;
2129 url
= quote_ref_url(base
, ref
->name
);
2130 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
2131 strbuf_rtrim(&buffer
);
2132 if (buffer
.len
== the_hash_algo
->hexsz
)
2133 ret
= get_oid_hex(buffer
.buf
, &ref
->old_oid
);
2134 else if (starts_with(buffer
.buf
, "ref: ")) {
2135 ref
->symref
= xstrdup(buffer
.buf
+ 5);
2140 strbuf_release(&buffer
);
2145 /* Helpers for fetching packs */
2146 static char *fetch_pack_index(unsigned char *hash
, const char *base_url
)
2149 struct strbuf buf
= STRBUF_INIT
;
2151 if (http_is_verbose
)
2152 fprintf(stderr
, "Getting index for pack %s\n", hash_to_hex(hash
));
2154 end_url_with_slash(&buf
, base_url
);
2155 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", hash_to_hex(hash
));
2156 url
= strbuf_detach(&buf
, NULL
);
2158 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(hash
));
2159 tmp
= strbuf_detach(&buf
, NULL
);
2161 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
2162 error("Unable to get pack index %s", url
);
2170 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
2171 unsigned char *sha1
, const char *base_url
)
2173 struct packed_git
*new_pack
;
2174 char *tmp_idx
= NULL
;
2177 if (has_pack_index(sha1
)) {
2178 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
2180 return -1; /* parse_pack_index() already issued error message */
2184 tmp_idx
= fetch_pack_index(sha1
, base_url
);
2188 new_pack
= parse_pack_index(sha1
, tmp_idx
);
2193 return -1; /* parse_pack_index() already issued error message */
2196 ret
= verify_pack_index(new_pack
);
2198 close_pack_index(new_pack
);
2199 ret
= finalize_object_file(tmp_idx
, sha1_pack_index_name(sha1
));
2206 new_pack
->next
= *packs_head
;
2207 *packs_head
= new_pack
;
2211 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
2213 struct http_get_options options
= {0};
2217 struct strbuf buf
= STRBUF_INIT
;
2218 struct object_id oid
;
2220 end_url_with_slash(&buf
, base_url
);
2221 strbuf_addstr(&buf
, "objects/info/packs");
2222 url
= strbuf_detach(&buf
, NULL
);
2224 options
.no_cache
= 1;
2225 ret
= http_get_strbuf(url
, &buf
, &options
);
2231 if (skip_prefix(data
, "P pack-", &data
) &&
2232 !parse_oid_hex(data
, &oid
, &data
) &&
2233 skip_prefix(data
, ".pack", &data
) &&
2234 (*data
== '\n' || *data
== '\0')) {
2235 fetch_and_setup_pack_index(packs_head
, oid
.hash
, base_url
);
2237 data
= strchrnul(data
, '\n');
2240 data
++; /* skip past newline */
2248 void release_http_pack_request(struct http_pack_request
*preq
)
2250 if (preq
->packfile
!= NULL
) {
2251 fclose(preq
->packfile
);
2252 preq
->packfile
= NULL
;
2255 strbuf_release(&preq
->tmpfile
);
2260 int finish_http_pack_request(struct http_pack_request
*preq
)
2262 struct child_process ip
= CHILD_PROCESS_INIT
;
2266 fclose(preq
->packfile
);
2267 preq
->packfile
= NULL
;
2269 tmpfile_fd
= xopen(preq
->tmpfile
.buf
, O_RDONLY
);
2271 strvec_push(&ip
.args
, "index-pack");
2272 strvec_push(&ip
.args
, "--stdin");
2275 if (preq
->generate_keep
) {
2276 strvec_pushf(&ip
.args
, "--keep=git %"PRIuMAX
,
2277 (uintmax_t)getpid());
2283 if (run_command(&ip
)) {
2290 unlink(preq
->tmpfile
.buf
);
2294 void http_install_packfile(struct packed_git
*p
,
2295 struct packed_git
**list_to_remove_from
)
2297 struct packed_git
**lst
= list_to_remove_from
;
2300 lst
= &((*lst
)->next
);
2301 *lst
= (*lst
)->next
;
2303 install_packed_git(the_repository
, p
);
2306 struct http_pack_request
*new_http_pack_request(
2307 const unsigned char *packed_git_hash
, const char *base_url
) {
2309 struct strbuf buf
= STRBUF_INIT
;
2311 end_url_with_slash(&buf
, base_url
);
2312 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
2313 hash_to_hex(packed_git_hash
));
2314 return new_direct_http_pack_request(packed_git_hash
,
2315 strbuf_detach(&buf
, NULL
));
2318 struct http_pack_request
*new_direct_http_pack_request(
2319 const unsigned char *packed_git_hash
, char *url
)
2321 off_t prev_posn
= 0;
2322 struct http_pack_request
*preq
;
2324 preq
= xcalloc(1, sizeof(*preq
));
2325 strbuf_init(&preq
->tmpfile
, 0);
2329 strbuf_addf(&preq
->tmpfile
, "%s.temp", sha1_pack_name(packed_git_hash
));
2330 preq
->packfile
= fopen(preq
->tmpfile
.buf
, "a");
2331 if (!preq
->packfile
) {
2332 error("Unable to open local file %s for pack",
2337 preq
->slot
= get_active_slot();
2338 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
2339 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
2340 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
2341 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
2345 * If there is data present from a previous transfer attempt,
2346 * resume where it left off
2348 prev_posn
= ftello(preq
->packfile
);
2350 if (http_is_verbose
)
2352 "Resuming fetch of pack %s at byte %"PRIuMAX
"\n",
2353 hash_to_hex(packed_git_hash
),
2354 (uintmax_t)prev_posn
);
2355 http_opt_request_remainder(preq
->slot
->curl
, prev_posn
);
2361 strbuf_release(&preq
->tmpfile
);
2367 /* Helpers for fetching objects (loose) */
2368 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
2371 unsigned char expn
[4096];
2372 size_t size
= eltsize
* nmemb
;
2374 struct http_object_request
*freq
= data
;
2375 struct active_request_slot
*slot
= freq
->slot
;
2378 CURLcode c
= curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
,
2381 BUG("curl_easy_getinfo for HTTP code failed: %s",
2382 curl_easy_strerror(c
));
2383 if (slot
->http_code
>= 300)
2388 ssize_t retval
= xwrite(freq
->localfile
,
2389 (char *) ptr
+ posn
, size
- posn
);
2391 return posn
/ eltsize
;
2393 } while (posn
< size
);
2395 freq
->stream
.avail_in
= size
;
2396 freq
->stream
.next_in
= (void *)ptr
;
2398 freq
->stream
.next_out
= expn
;
2399 freq
->stream
.avail_out
= sizeof(expn
);
2400 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
2401 the_hash_algo
->update_fn(&freq
->c
, expn
,
2402 sizeof(expn
) - freq
->stream
.avail_out
);
2403 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
2407 struct http_object_request
*new_http_object_request(const char *base_url
,
2408 const struct object_id
*oid
)
2410 char *hex
= oid_to_hex(oid
);
2411 struct strbuf filename
= STRBUF_INIT
;
2412 struct strbuf prevfile
= STRBUF_INIT
;
2414 char prev_buf
[PREV_BUF_SIZE
];
2415 ssize_t prev_read
= 0;
2416 off_t prev_posn
= 0;
2417 struct http_object_request
*freq
;
2419 freq
= xcalloc(1, sizeof(*freq
));
2420 strbuf_init(&freq
->tmpfile
, 0);
2421 oidcpy(&freq
->oid
, oid
);
2422 freq
->localfile
= -1;
2424 loose_object_path(the_repository
, &filename
, oid
);
2425 strbuf_addf(&freq
->tmpfile
, "%s.temp", filename
.buf
);
2427 strbuf_addf(&prevfile
, "%s.prev", filename
.buf
);
2428 unlink_or_warn(prevfile
.buf
);
2429 rename(freq
->tmpfile
.buf
, prevfile
.buf
);
2430 unlink_or_warn(freq
->tmpfile
.buf
);
2431 strbuf_release(&filename
);
2433 if (freq
->localfile
!= -1)
2434 error("fd leakage in start: %d", freq
->localfile
);
2435 freq
->localfile
= open(freq
->tmpfile
.buf
,
2436 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2438 * This could have failed due to the "lazy directory creation";
2439 * try to mkdir the last path component.
2441 if (freq
->localfile
< 0 && errno
== ENOENT
) {
2442 char *dir
= strrchr(freq
->tmpfile
.buf
, '/');
2445 mkdir(freq
->tmpfile
.buf
, 0777);
2448 freq
->localfile
= open(freq
->tmpfile
.buf
,
2449 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2452 if (freq
->localfile
< 0) {
2453 error_errno("Couldn't create temporary file %s",
2458 git_inflate_init(&freq
->stream
);
2460 the_hash_algo
->init_fn(&freq
->c
);
2462 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
2465 * If a previous temp file is present, process what was already
2468 prevlocal
= open(prevfile
.buf
, O_RDONLY
);
2469 if (prevlocal
!= -1) {
2471 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
2473 if (fwrite_sha1_file(prev_buf
,
2476 freq
) == prev_read
) {
2477 prev_posn
+= prev_read
;
2482 } while (prev_read
> 0);
2485 unlink_or_warn(prevfile
.buf
);
2486 strbuf_release(&prevfile
);
2489 * Reset inflate/SHA1 if there was an error reading the previous temp
2490 * file; also rewind to the beginning of the local file.
2492 if (prev_read
== -1) {
2493 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
2494 git_inflate_init(&freq
->stream
);
2495 the_hash_algo
->init_fn(&freq
->c
);
2498 lseek(freq
->localfile
, 0, SEEK_SET
);
2499 if (ftruncate(freq
->localfile
, 0) < 0) {
2500 error_errno("Couldn't truncate temporary file %s",
2507 freq
->slot
= get_active_slot();
2509 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
2510 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FAILONERROR
, 0);
2511 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
2512 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
2513 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
2514 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
2517 * If we have successfully processed data from a previous fetch
2518 * attempt, only fetch the data we don't already have.
2521 if (http_is_verbose
)
2523 "Resuming fetch of object %s at byte %"PRIuMAX
"\n",
2524 hex
, (uintmax_t)prev_posn
);
2525 http_opt_request_remainder(freq
->slot
->curl
, prev_posn
);
2531 strbuf_release(&prevfile
);
2537 void process_http_object_request(struct http_object_request
*freq
)
2539 if (freq
->slot
== NULL
)
2541 freq
->curl_result
= freq
->slot
->curl_result
;
2542 freq
->http_code
= freq
->slot
->http_code
;
2546 int finish_http_object_request(struct http_object_request
*freq
)
2549 struct strbuf filename
= STRBUF_INIT
;
2551 close(freq
->localfile
);
2552 freq
->localfile
= -1;
2554 process_http_object_request(freq
);
2556 if (freq
->http_code
== 416) {
2557 warning("requested range invalid; we may already have all the data.");
2558 } else if (freq
->curl_result
!= CURLE_OK
) {
2559 if (stat(freq
->tmpfile
.buf
, &st
) == 0)
2560 if (st
.st_size
== 0)
2561 unlink_or_warn(freq
->tmpfile
.buf
);
2565 git_inflate_end(&freq
->stream
);
2566 the_hash_algo
->final_fn(freq
->real_oid
.hash
, &freq
->c
);
2567 if (freq
->zret
!= Z_STREAM_END
) {
2568 unlink_or_warn(freq
->tmpfile
.buf
);
2571 if (!oideq(&freq
->oid
, &freq
->real_oid
)) {
2572 unlink_or_warn(freq
->tmpfile
.buf
);
2575 loose_object_path(the_repository
, &filename
, &freq
->oid
);
2576 freq
->rename
= finalize_object_file(freq
->tmpfile
.buf
, filename
.buf
);
2577 strbuf_release(&filename
);
2579 return freq
->rename
;
2582 void abort_http_object_request(struct http_object_request
*freq
)
2584 unlink_or_warn(freq
->tmpfile
.buf
);
2586 release_http_object_request(freq
);
2589 void release_http_object_request(struct http_object_request
*freq
)
2591 if (freq
->localfile
!= -1) {
2592 close(freq
->localfile
);
2593 freq
->localfile
= -1;
2595 FREE_AND_NULL(freq
->url
);
2596 if (freq
->slot
!= NULL
) {
2597 freq
->slot
->callback_func
= NULL
;
2598 freq
->slot
->callback_data
= NULL
;
2599 release_active_slot(freq
->slot
);
2602 strbuf_release(&freq
->tmpfile
);