1 #include "git-compat-util.h"
2 #include "git-curl-compat.h"
7 #include "run-command.h"
10 #include "credential.h"
14 #include "transport.h"
17 #include "string-list.h"
18 #include "object-store.h"
20 static struct trace_key trace_curl
= TRACE_KEY_INIT(CURL
);
21 static int trace_curl_data
= 1;
22 static int trace_curl_redact
= 1;
23 long int git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
26 ssize_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
28 static int min_curl_sessions
= 1;
29 static int curl_session_count
;
30 static int max_requests
= -1;
32 static CURL
*curl_default
;
34 #define PREV_BUF_SIZE 4096
36 char curl_errorstr
[CURL_ERROR_SIZE
];
38 static int curl_ssl_verify
= -1;
39 static int curl_ssl_try
;
40 static const char *curl_http_version
= NULL
;
41 static const char *ssl_cert
;
42 static const char *ssl_cipherlist
;
43 static const char *ssl_version
;
48 { "sslv2", CURL_SSLVERSION_SSLv2
},
49 { "sslv3", CURL_SSLVERSION_SSLv3
},
50 { "tlsv1", CURL_SSLVERSION_TLSv1
},
51 #ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_0
52 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0
},
53 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1
},
54 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2
},
56 #ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_3
57 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3
},
60 static const char *ssl_key
;
61 static const char *ssl_capath
;
62 static const char *curl_no_proxy
;
63 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
64 static const char *ssl_pinnedkey
;
66 static const char *ssl_cainfo
;
67 static long curl_low_speed_limit
= -1;
68 static long curl_low_speed_time
= -1;
69 static int curl_ftp_no_epsv
;
70 static const char *curl_http_proxy
;
71 static const char *http_proxy_authmethod
;
73 static const char *http_proxy_ssl_cert
;
74 static const char *http_proxy_ssl_key
;
75 static const char *http_proxy_ssl_ca_info
;
76 static struct credential proxy_cert_auth
= CREDENTIAL_INIT
;
77 static int proxy_ssl_cert_password_required
;
82 } proxy_authmethods
[] = {
83 { "basic", CURLAUTH_BASIC
},
84 { "digest", CURLAUTH_DIGEST
},
85 { "negotiate", CURLAUTH_GSSNEGOTIATE
},
86 { "ntlm", CURLAUTH_NTLM
},
87 { "anyauth", CURLAUTH_ANY
},
89 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
90 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
94 #ifdef CURLGSSAPI_DELEGATION_FLAG
95 static const char *curl_deleg
;
98 long curl_deleg_param
;
99 } curl_deleg_levels
[] = {
100 { "none", CURLGSSAPI_DELEGATION_NONE
},
101 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG
},
102 { "always", CURLGSSAPI_DELEGATION_FLAG
},
106 static struct credential proxy_auth
= CREDENTIAL_INIT
;
107 static const char *curl_proxyuserpwd
;
108 static const char *curl_cookie_file
;
109 static int curl_save_cookies
;
110 struct credential http_auth
= CREDENTIAL_INIT
;
111 static int http_proactive_auth
;
112 static const char *user_agent
;
113 static int curl_empty_auth
= -1;
115 enum http_follow_config http_follow_config
= HTTP_FOLLOW_INITIAL
;
117 static struct credential cert_auth
= CREDENTIAL_INIT
;
118 static int ssl_cert_password_required
;
119 static unsigned long http_auth_methods
= CURLAUTH_ANY
;
120 static int http_auth_methods_restricted
;
121 /* Modes for which empty_auth cannot actually help us. */
122 static unsigned long empty_auth_useless
=
127 static struct curl_slist
*pragma_header
;
128 static struct curl_slist
*no_pragma_header
;
129 static struct string_list extra_http_headers
= STRING_LIST_INIT_DUP
;
131 static struct active_request_slot
*active_queue_head
;
133 static char *cached_accept_language
;
135 static char *http_ssl_backend
;
137 static int http_schannel_check_revoke
= 1;
139 * With the backend being set to `schannel`, setting sslCAinfo would override
140 * the Certificate Store in cURL v7.60.0 and later, which is not what we want
143 static int http_schannel_use_ssl_cainfo
;
145 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
147 size_t size
= eltsize
* nmemb
;
148 struct buffer
*buffer
= buffer_
;
150 if (size
> buffer
->buf
.len
- buffer
->posn
)
151 size
= buffer
->buf
.len
- buffer
->posn
;
152 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
153 buffer
->posn
+= size
;
155 return size
/ eltsize
;
158 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
160 struct buffer
*buffer
= clientp
;
166 case CURLIOCMD_RESTARTREAD
:
171 return CURLIOE_UNKNOWNCMD
;
175 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
177 size_t size
= eltsize
* nmemb
;
178 struct strbuf
*buffer
= buffer_
;
180 strbuf_add(buffer
, ptr
, size
);
184 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
189 static void closedown_active_slot(struct active_request_slot
*slot
)
195 static void finish_active_slot(struct active_request_slot
*slot
)
197 closedown_active_slot(slot
);
198 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
200 if (slot
->finished
!= NULL
)
201 (*slot
->finished
) = 1;
203 /* Store slot results so they can be read after the slot is reused */
204 if (slot
->results
!= NULL
) {
205 slot
->results
->curl_result
= slot
->curl_result
;
206 slot
->results
->http_code
= slot
->http_code
;
207 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
208 &slot
->results
->auth_avail
);
210 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CONNECTCODE
,
211 &slot
->results
->http_connectcode
);
214 /* Run callback if appropriate */
215 if (slot
->callback_func
!= NULL
)
216 slot
->callback_func(slot
->callback_data
);
219 static void xmulti_remove_handle(struct active_request_slot
*slot
)
221 curl_multi_remove_handle(curlm
, slot
->curl
);
224 static void process_curl_messages(void)
227 struct active_request_slot
*slot
;
228 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
230 while (curl_message
!= NULL
) {
231 if (curl_message
->msg
== CURLMSG_DONE
) {
232 int curl_result
= curl_message
->data
.result
;
233 slot
= active_queue_head
;
234 while (slot
!= NULL
&&
235 slot
->curl
!= curl_message
->easy_handle
)
238 xmulti_remove_handle(slot
);
239 slot
->curl_result
= curl_result
;
240 finish_active_slot(slot
);
242 fprintf(stderr
, "Received DONE message for unknown request!\n");
245 fprintf(stderr
, "Unknown CURL message received: %d\n",
246 (int)curl_message
->msg
);
248 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
252 static int http_options(const char *var
, const char *value
, void *cb
)
254 if (!strcmp("http.version", var
)) {
255 return git_config_string(&curl_http_version
, var
, value
);
257 if (!strcmp("http.sslverify", var
)) {
258 curl_ssl_verify
= git_config_bool(var
, value
);
261 if (!strcmp("http.sslcipherlist", var
))
262 return git_config_string(&ssl_cipherlist
, var
, value
);
263 if (!strcmp("http.sslversion", var
))
264 return git_config_string(&ssl_version
, var
, value
);
265 if (!strcmp("http.sslcert", var
))
266 return git_config_pathname(&ssl_cert
, var
, value
);
267 if (!strcmp("http.sslkey", var
))
268 return git_config_pathname(&ssl_key
, var
, value
);
269 if (!strcmp("http.sslcapath", var
))
270 return git_config_pathname(&ssl_capath
, var
, value
);
271 if (!strcmp("http.sslcainfo", var
))
272 return git_config_pathname(&ssl_cainfo
, var
, value
);
273 if (!strcmp("http.sslcertpasswordprotected", var
)) {
274 ssl_cert_password_required
= git_config_bool(var
, value
);
277 if (!strcmp("http.ssltry", var
)) {
278 curl_ssl_try
= git_config_bool(var
, value
);
281 if (!strcmp("http.sslbackend", var
)) {
282 free(http_ssl_backend
);
283 http_ssl_backend
= xstrdup_or_null(value
);
287 if (!strcmp("http.schannelcheckrevoke", var
)) {
288 http_schannel_check_revoke
= git_config_bool(var
, value
);
292 if (!strcmp("http.schannelusesslcainfo", var
)) {
293 http_schannel_use_ssl_cainfo
= git_config_bool(var
, value
);
297 if (!strcmp("http.minsessions", var
)) {
298 min_curl_sessions
= git_config_int(var
, value
);
299 if (min_curl_sessions
> 1)
300 min_curl_sessions
= 1;
303 if (!strcmp("http.maxrequests", var
)) {
304 max_requests
= git_config_int(var
, value
);
307 if (!strcmp("http.lowspeedlimit", var
)) {
308 curl_low_speed_limit
= (long)git_config_int(var
, value
);
311 if (!strcmp("http.lowspeedtime", var
)) {
312 curl_low_speed_time
= (long)git_config_int(var
, value
);
316 if (!strcmp("http.noepsv", var
)) {
317 curl_ftp_no_epsv
= git_config_bool(var
, value
);
320 if (!strcmp("http.proxy", var
))
321 return git_config_string(&curl_http_proxy
, var
, value
);
323 if (!strcmp("http.proxyauthmethod", var
))
324 return git_config_string(&http_proxy_authmethod
, var
, value
);
326 if (!strcmp("http.proxysslcert", var
))
327 return git_config_string(&http_proxy_ssl_cert
, var
, value
);
329 if (!strcmp("http.proxysslkey", var
))
330 return git_config_string(&http_proxy_ssl_key
, var
, value
);
332 if (!strcmp("http.proxysslcainfo", var
))
333 return git_config_string(&http_proxy_ssl_ca_info
, var
, value
);
335 if (!strcmp("http.proxysslcertpasswordprotected", var
)) {
336 proxy_ssl_cert_password_required
= git_config_bool(var
, value
);
340 if (!strcmp("http.cookiefile", var
))
341 return git_config_pathname(&curl_cookie_file
, var
, value
);
342 if (!strcmp("http.savecookies", var
)) {
343 curl_save_cookies
= git_config_bool(var
, value
);
347 if (!strcmp("http.postbuffer", var
)) {
348 http_post_buffer
= git_config_ssize_t(var
, value
);
349 if (http_post_buffer
< 0)
350 warning(_("negative value for http.postbuffer; defaulting to %d"), LARGE_PACKET_MAX
);
351 if (http_post_buffer
< LARGE_PACKET_MAX
)
352 http_post_buffer
= LARGE_PACKET_MAX
;
356 if (!strcmp("http.useragent", var
))
357 return git_config_string(&user_agent
, var
, value
);
359 if (!strcmp("http.emptyauth", var
)) {
360 if (value
&& !strcmp("auto", value
))
361 curl_empty_auth
= -1;
363 curl_empty_auth
= git_config_bool(var
, value
);
367 if (!strcmp("http.delegation", var
)) {
368 #ifdef CURLGSSAPI_DELEGATION_FLAG
369 return git_config_string(&curl_deleg
, var
, value
);
371 warning(_("Delegation control is not supported with cURL < 7.22.0"));
376 if (!strcmp("http.pinnedpubkey", var
)) {
377 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
378 return git_config_pathname(&ssl_pinnedkey
, var
, value
);
380 warning(_("Public key pinning not supported with cURL < 7.39.0"));
385 if (!strcmp("http.extraheader", var
)) {
387 return config_error_nonbool(var
);
388 } else if (!*value
) {
389 string_list_clear(&extra_http_headers
, 0);
391 string_list_append(&extra_http_headers
, value
);
396 if (!strcmp("http.followredirects", var
)) {
397 if (value
&& !strcmp(value
, "initial"))
398 http_follow_config
= HTTP_FOLLOW_INITIAL
;
399 else if (git_config_bool(var
, value
))
400 http_follow_config
= HTTP_FOLLOW_ALWAYS
;
402 http_follow_config
= HTTP_FOLLOW_NONE
;
406 /* Fall back on the default ones */
407 return git_default_config(var
, value
, cb
);
410 static int curl_empty_auth_enabled(void)
412 if (curl_empty_auth
>= 0)
413 return curl_empty_auth
;
416 * In the automatic case, kick in the empty-auth
417 * hack as long as we would potentially try some
418 * method more exotic than "Basic" or "Digest".
420 * But only do this when this is our second or
421 * subsequent request, as by then we know what
422 * methods are available.
424 if (http_auth_methods_restricted
&&
425 (http_auth_methods
& ~empty_auth_useless
))
430 static void init_curl_http_auth(CURL
*result
)
432 if (!http_auth
.username
|| !*http_auth
.username
) {
433 if (curl_empty_auth_enabled())
434 curl_easy_setopt(result
, CURLOPT_USERPWD
, ":");
438 credential_fill(&http_auth
);
440 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
441 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
444 /* *var must be free-able */
445 static void var_override(const char **var
, char *value
)
449 *var
= xstrdup(value
);
453 static void set_proxyauth_name_password(CURL
*result
)
455 curl_easy_setopt(result
, CURLOPT_PROXYUSERNAME
,
456 proxy_auth
.username
);
457 curl_easy_setopt(result
, CURLOPT_PROXYPASSWORD
,
458 proxy_auth
.password
);
461 static void init_curl_proxy_auth(CURL
*result
)
463 if (proxy_auth
.username
) {
464 if (!proxy_auth
.password
)
465 credential_fill(&proxy_auth
);
466 set_proxyauth_name_password(result
);
469 var_override(&http_proxy_authmethod
, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
471 if (http_proxy_authmethod
) {
473 for (i
= 0; i
< ARRAY_SIZE(proxy_authmethods
); i
++) {
474 if (!strcmp(http_proxy_authmethod
, proxy_authmethods
[i
].name
)) {
475 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
,
476 proxy_authmethods
[i
].curlauth_param
);
480 if (i
== ARRAY_SIZE(proxy_authmethods
)) {
481 warning("unsupported proxy authentication method %s: using anyauth",
482 http_proxy_authmethod
);
483 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
487 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
490 static int has_cert_password(void)
492 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
494 if (!cert_auth
.password
) {
495 cert_auth
.protocol
= xstrdup("cert");
496 cert_auth
.host
= xstrdup("");
497 cert_auth
.username
= xstrdup("");
498 cert_auth
.path
= xstrdup(ssl_cert
);
499 credential_fill(&cert_auth
);
504 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
505 static int has_proxy_cert_password(void)
507 if (http_proxy_ssl_cert
== NULL
|| proxy_ssl_cert_password_required
!= 1)
509 if (!proxy_cert_auth
.password
) {
510 proxy_cert_auth
.protocol
= xstrdup("cert");
511 proxy_cert_auth
.host
= xstrdup("");
512 proxy_cert_auth
.username
= xstrdup("");
513 proxy_cert_auth
.path
= xstrdup(http_proxy_ssl_cert
);
514 credential_fill(&proxy_cert_auth
);
520 #ifdef GITCURL_HAVE_CURLOPT_TCP_KEEPALIVE
521 static void set_curl_keepalive(CURL
*c
)
523 curl_easy_setopt(c
, CURLOPT_TCP_KEEPALIVE
, 1);
527 static int sockopt_callback(void *client
, curl_socket_t fd
, curlsocktype type
)
531 socklen_t len
= (socklen_t
)sizeof(ka
);
533 if (type
!= CURLSOCKTYPE_IPCXN
)
536 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
538 warning_errno("unable to set SO_KEEPALIVE on socket");
540 return CURL_SOCKOPT_OK
;
543 static void set_curl_keepalive(CURL
*c
)
545 curl_easy_setopt(c
, CURLOPT_SOCKOPTFUNCTION
, sockopt_callback
);
549 static void redact_sensitive_header(struct strbuf
*header
)
551 const char *sensitive_header
;
553 if (trace_curl_redact
&&
554 (skip_iprefix(header
->buf
, "Authorization:", &sensitive_header
) ||
555 skip_iprefix(header
->buf
, "Proxy-Authorization:", &sensitive_header
))) {
556 /* The first token is the type, which is OK to log */
557 while (isspace(*sensitive_header
))
559 while (*sensitive_header
&& !isspace(*sensitive_header
))
561 /* Everything else is opaque and possibly sensitive */
562 strbuf_setlen(header
, sensitive_header
- header
->buf
);
563 strbuf_addstr(header
, " <redacted>");
564 } else if (trace_curl_redact
&&
565 skip_iprefix(header
->buf
, "Cookie:", &sensitive_header
)) {
566 struct strbuf redacted_header
= STRBUF_INIT
;
569 while (isspace(*sensitive_header
))
572 cookie
= sensitive_header
;
576 char *semicolon
= strstr(cookie
, "; ");
579 equals
= strchrnul(cookie
, '=');
581 /* invalid cookie, just append and continue */
582 strbuf_addstr(&redacted_header
, cookie
);
585 strbuf_add(&redacted_header
, cookie
, equals
- cookie
);
586 strbuf_addstr(&redacted_header
, "=<redacted>");
589 * There are more cookies. (Or, for some
590 * reason, the input string ends in "; ".)
592 strbuf_addstr(&redacted_header
, "; ");
593 cookie
= semicolon
+ strlen("; ");
599 strbuf_setlen(header
, sensitive_header
- header
->buf
);
600 strbuf_addbuf(header
, &redacted_header
);
604 static void curl_dump_header(const char *text
, unsigned char *ptr
, size_t size
, int hide_sensitive_header
)
606 struct strbuf out
= STRBUF_INIT
;
607 struct strbuf
**headers
, **header
;
609 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
610 text
, (long)size
, (long)size
);
611 trace_strbuf(&trace_curl
, &out
);
613 strbuf_add(&out
, ptr
, size
);
614 headers
= strbuf_split_max(&out
, '\n', 0);
616 for (header
= headers
; *header
; header
++) {
617 if (hide_sensitive_header
)
618 redact_sensitive_header(*header
);
619 strbuf_insertstr((*header
), 0, text
);
620 strbuf_insertstr((*header
), strlen(text
), ": ");
621 strbuf_rtrim((*header
));
622 strbuf_addch((*header
), '\n');
623 trace_strbuf(&trace_curl
, (*header
));
625 strbuf_list_free(headers
);
626 strbuf_release(&out
);
629 static void curl_dump_data(const char *text
, unsigned char *ptr
, size_t size
)
632 struct strbuf out
= STRBUF_INIT
;
633 unsigned int width
= 60;
635 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
636 text
, (long)size
, (long)size
);
637 trace_strbuf(&trace_curl
, &out
);
639 for (i
= 0; i
< size
; i
+= width
) {
643 strbuf_addf(&out
, "%s: ", text
);
644 for (w
= 0; (w
< width
) && (i
+ w
< size
); w
++) {
645 unsigned char ch
= ptr
[i
+ w
];
648 (ch
>= 0x20) && (ch
< 0x80)
651 strbuf_addch(&out
, '\n');
652 trace_strbuf(&trace_curl
, &out
);
654 strbuf_release(&out
);
657 static int curl_trace(CURL
*handle
, curl_infotype type
, char *data
, size_t size
, void *userp
)
660 enum { NO_FILTER
= 0, DO_FILTER
= 1 };
664 trace_printf_key(&trace_curl
, "== Info: %s", data
);
666 case CURLINFO_HEADER_OUT
:
667 text
= "=> Send header";
668 curl_dump_header(text
, (unsigned char *)data
, size
, DO_FILTER
);
670 case CURLINFO_DATA_OUT
:
671 if (trace_curl_data
) {
672 text
= "=> Send data";
673 curl_dump_data(text
, (unsigned char *)data
, size
);
676 case CURLINFO_SSL_DATA_OUT
:
677 if (trace_curl_data
) {
678 text
= "=> Send SSL data";
679 curl_dump_data(text
, (unsigned char *)data
, size
);
682 case CURLINFO_HEADER_IN
:
683 text
= "<= Recv header";
684 curl_dump_header(text
, (unsigned char *)data
, size
, NO_FILTER
);
686 case CURLINFO_DATA_IN
:
687 if (trace_curl_data
) {
688 text
= "<= Recv data";
689 curl_dump_data(text
, (unsigned char *)data
, size
);
692 case CURLINFO_SSL_DATA_IN
:
693 if (trace_curl_data
) {
694 text
= "<= Recv SSL data";
695 curl_dump_data(text
, (unsigned char *)data
, size
);
699 default: /* we ignore unknown types by default */
705 void http_trace_curl_no_data(void)
707 trace_override_envvar(&trace_curl
, "1");
711 void setup_curl_trace(CURL
*handle
)
713 if (!trace_want(&trace_curl
))
715 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
716 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, curl_trace
);
717 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, NULL
);
720 static long get_curl_allowed_protocols(int from_user
)
722 long allowed_protocols
= 0;
724 if (is_transport_allowed("http", from_user
))
725 allowed_protocols
|= CURLPROTO_HTTP
;
726 if (is_transport_allowed("https", from_user
))
727 allowed_protocols
|= CURLPROTO_HTTPS
;
728 if (is_transport_allowed("ftp", from_user
))
729 allowed_protocols
|= CURLPROTO_FTP
;
730 if (is_transport_allowed("ftps", from_user
))
731 allowed_protocols
|= CURLPROTO_FTPS
;
733 return allowed_protocols
;
736 #ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
737 static int get_curl_http_version_opt(const char *version_string
, long *opt
)
744 { "HTTP/1.1", CURL_HTTP_VERSION_1_1
},
745 { "HTTP/2", CURL_HTTP_VERSION_2
}
748 for (i
= 0; i
< ARRAY_SIZE(choice
); i
++) {
749 if (!strcmp(version_string
, choice
[i
].name
)) {
750 *opt
= choice
[i
].opt_token
;
755 warning("unknown value given to http.version: '%s'", version_string
);
756 return -1; /* not found */
761 static CURL
*get_curl_handle(void)
763 CURL
*result
= curl_easy_init();
766 die("curl_easy_init failed");
768 if (!curl_ssl_verify
) {
769 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
770 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
772 /* Verify authenticity of the peer's certificate */
773 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
774 /* The name in the cert must match whom we tried to connect */
775 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
778 #ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
779 if (curl_http_version
) {
781 if (!get_curl_http_version_opt(curl_http_version
, &opt
)) {
782 /* Set request use http version */
783 curl_easy_setopt(result
, CURLOPT_HTTP_VERSION
, opt
);
788 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
789 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
791 #ifdef CURLGSSAPI_DELEGATION_FLAG
794 for (i
= 0; i
< ARRAY_SIZE(curl_deleg_levels
); i
++) {
795 if (!strcmp(curl_deleg
, curl_deleg_levels
[i
].name
)) {
796 curl_easy_setopt(result
, CURLOPT_GSSAPI_DELEGATION
,
797 curl_deleg_levels
[i
].curl_deleg_param
);
801 if (i
== ARRAY_SIZE(curl_deleg_levels
))
802 warning("Unknown delegation method '%s': using default",
807 if (http_ssl_backend
&& !strcmp("schannel", http_ssl_backend
) &&
808 !http_schannel_check_revoke
) {
809 #ifdef GIT_CURL_HAVE_CURLSSLOPT_NO_REVOKE
810 curl_easy_setopt(result
, CURLOPT_SSL_OPTIONS
, CURLSSLOPT_NO_REVOKE
);
812 warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
816 if (http_proactive_auth
)
817 init_curl_http_auth(result
);
819 if (getenv("GIT_SSL_VERSION"))
820 ssl_version
= getenv("GIT_SSL_VERSION");
821 if (ssl_version
&& *ssl_version
) {
823 for (i
= 0; i
< ARRAY_SIZE(sslversions
); i
++) {
824 if (!strcmp(ssl_version
, sslversions
[i
].name
)) {
825 curl_easy_setopt(result
, CURLOPT_SSLVERSION
,
826 sslversions
[i
].ssl_version
);
830 if (i
== ARRAY_SIZE(sslversions
))
831 warning("unsupported ssl version %s: using default",
835 if (getenv("GIT_SSL_CIPHER_LIST"))
836 ssl_cipherlist
= getenv("GIT_SSL_CIPHER_LIST");
837 if (ssl_cipherlist
!= NULL
&& *ssl_cipherlist
)
838 curl_easy_setopt(result
, CURLOPT_SSL_CIPHER_LIST
,
841 if (ssl_cert
!= NULL
)
842 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
843 if (has_cert_password())
844 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
846 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
847 if (ssl_capath
!= NULL
)
848 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
849 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
850 if (ssl_pinnedkey
!= NULL
)
851 curl_easy_setopt(result
, CURLOPT_PINNEDPUBLICKEY
, ssl_pinnedkey
);
853 if (http_ssl_backend
&& !strcmp("schannel", http_ssl_backend
) &&
854 !http_schannel_use_ssl_cainfo
) {
855 curl_easy_setopt(result
, CURLOPT_CAINFO
, NULL
);
856 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
857 curl_easy_setopt(result
, CURLOPT_PROXY_CAINFO
, NULL
);
859 } else if (ssl_cainfo
!= NULL
|| http_proxy_ssl_ca_info
!= NULL
) {
860 if (ssl_cainfo
!= NULL
)
861 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
862 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
863 if (http_proxy_ssl_ca_info
!= NULL
)
864 curl_easy_setopt(result
, CURLOPT_PROXY_CAINFO
, http_proxy_ssl_ca_info
);
868 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
869 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
870 curl_low_speed_limit
);
871 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
872 curl_low_speed_time
);
875 curl_easy_setopt(result
, CURLOPT_MAXREDIRS
, 20);
876 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
877 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS
,
878 get_curl_allowed_protocols(0));
879 curl_easy_setopt(result
, CURLOPT_PROTOCOLS
,
880 get_curl_allowed_protocols(-1));
881 if (getenv("GIT_CURL_VERBOSE"))
882 http_trace_curl_no_data();
883 setup_curl_trace(result
);
884 if (getenv("GIT_TRACE_CURL_NO_DATA"))
886 if (!git_env_bool("GIT_TRACE_REDACT", 1))
887 trace_curl_redact
= 0;
889 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
890 user_agent
? user_agent
: git_user_agent());
892 if (curl_ftp_no_epsv
)
893 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
896 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
899 * CURL also examines these variables as a fallback; but we need to query
900 * them here in order to decide whether to prompt for missing password (cf.
901 * init_curl_proxy_auth()).
903 * Unlike many other common environment variables, these are historically
904 * lowercase only. It appears that CURL did not know this and implemented
905 * only uppercase variants, which was later corrected to take both - with
906 * the exception of http_proxy, which is lowercase only also in CURL. As
907 * the lowercase versions are the historical quasi-standard, they take
908 * precedence here, as in CURL.
910 if (!curl_http_proxy
) {
911 if (http_auth
.protocol
&& !strcmp(http_auth
.protocol
, "https")) {
912 var_override(&curl_http_proxy
, getenv("HTTPS_PROXY"));
913 var_override(&curl_http_proxy
, getenv("https_proxy"));
915 var_override(&curl_http_proxy
, getenv("http_proxy"));
917 if (!curl_http_proxy
) {
918 var_override(&curl_http_proxy
, getenv("ALL_PROXY"));
919 var_override(&curl_http_proxy
, getenv("all_proxy"));
923 if (curl_http_proxy
&& curl_http_proxy
[0] == '\0') {
925 * Handle case with the empty http.proxy value here to keep
927 * NB: empty option disables proxying at all.
929 curl_easy_setopt(result
, CURLOPT_PROXY
, "");
930 } else if (curl_http_proxy
) {
931 if (starts_with(curl_http_proxy
, "socks5h"))
932 curl_easy_setopt(result
,
933 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5_HOSTNAME
);
934 else if (starts_with(curl_http_proxy
, "socks5"))
935 curl_easy_setopt(result
,
936 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
937 else if (starts_with(curl_http_proxy
, "socks4a"))
938 curl_easy_setopt(result
,
939 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4A
);
940 else if (starts_with(curl_http_proxy
, "socks"))
941 curl_easy_setopt(result
,
942 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4
);
943 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
944 else if (starts_with(curl_http_proxy
, "https")) {
945 curl_easy_setopt(result
, CURLOPT_PROXYTYPE
, CURLPROXY_HTTPS
);
947 if (http_proxy_ssl_cert
)
948 curl_easy_setopt(result
, CURLOPT_PROXY_SSLCERT
, http_proxy_ssl_cert
);
950 if (http_proxy_ssl_key
)
951 curl_easy_setopt(result
, CURLOPT_PROXY_SSLKEY
, http_proxy_ssl_key
);
953 if (has_proxy_cert_password())
954 curl_easy_setopt(result
, CURLOPT_PROXY_KEYPASSWD
, proxy_cert_auth
.password
);
957 if (strstr(curl_http_proxy
, "://"))
958 credential_from_url(&proxy_auth
, curl_http_proxy
);
960 struct strbuf url
= STRBUF_INIT
;
961 strbuf_addf(&url
, "http://%s", curl_http_proxy
);
962 credential_from_url(&proxy_auth
, url
.buf
);
963 strbuf_release(&url
);
966 if (!proxy_auth
.host
)
967 die("Invalid proxy URL '%s'", curl_http_proxy
);
969 curl_easy_setopt(result
, CURLOPT_PROXY
, proxy_auth
.host
);
970 var_override(&curl_no_proxy
, getenv("NO_PROXY"));
971 var_override(&curl_no_proxy
, getenv("no_proxy"));
972 curl_easy_setopt(result
, CURLOPT_NOPROXY
, curl_no_proxy
);
974 init_curl_proxy_auth(result
);
976 set_curl_keepalive(result
);
981 static void set_from_env(const char **var
, const char *envname
)
983 const char *val
= getenv(envname
);
988 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
990 char *low_speed_limit
;
991 char *low_speed_time
;
992 char *normalized_url
;
993 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
995 config
.section
= "http";
997 config
.collect_fn
= http_options
;
998 config
.cascade_fn
= git_default_config
;
1001 http_is_verbose
= 0;
1002 normalized_url
= url_normalize(url
, &config
.url
);
1004 git_config(urlmatch_config_entry
, &config
);
1005 free(normalized_url
);
1006 string_list_clear(&config
.vars
, 1);
1008 #ifdef GIT_CURL_HAVE_CURLSSLSET_NO_BACKENDS
1009 if (http_ssl_backend
) {
1010 const curl_ssl_backend
**backends
;
1011 struct strbuf buf
= STRBUF_INIT
;
1014 switch (curl_global_sslset(-1, http_ssl_backend
, &backends
)) {
1015 case CURLSSLSET_UNKNOWN_BACKEND
:
1016 strbuf_addf(&buf
, _("Unsupported SSL backend '%s'. "
1017 "Supported SSL backends:"),
1019 for (i
= 0; backends
[i
]; i
++)
1020 strbuf_addf(&buf
, "\n\t%s", backends
[i
]->name
);
1022 case CURLSSLSET_NO_BACKENDS
:
1023 die(_("Could not set SSL backend to '%s': "
1024 "cURL was built without SSL backends"),
1026 case CURLSSLSET_TOO_LATE
:
1027 die(_("Could not set SSL backend to '%s': already set"),
1035 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
1036 die("curl_global_init failed");
1038 http_proactive_auth
= proactive_auth
;
1040 if (remote
&& remote
->http_proxy
)
1041 curl_http_proxy
= xstrdup(remote
->http_proxy
);
1044 var_override(&http_proxy_authmethod
, remote
->http_proxy_authmethod
);
1046 pragma_header
= curl_slist_append(http_copy_default_headers(),
1047 "Pragma: no-cache");
1048 no_pragma_header
= curl_slist_append(http_copy_default_headers(),
1052 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1053 if (http_max_requests
!= NULL
)
1054 max_requests
= atoi(http_max_requests
);
1057 curlm
= curl_multi_init();
1059 die("curl_multi_init failed");
1061 if (getenv("GIT_SSL_NO_VERIFY"))
1062 curl_ssl_verify
= 0;
1064 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
1065 set_from_env(&ssl_key
, "GIT_SSL_KEY");
1066 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
1067 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
1069 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
1071 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1072 if (low_speed_limit
!= NULL
)
1073 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1074 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1075 if (low_speed_time
!= NULL
)
1076 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1078 if (curl_ssl_verify
== -1)
1079 curl_ssl_verify
= 1;
1081 curl_session_count
= 0;
1082 if (max_requests
< 1)
1083 max_requests
= DEFAULT_MAX_REQUESTS
;
1085 set_from_env(&http_proxy_ssl_cert
, "GIT_PROXY_SSL_CERT");
1086 set_from_env(&http_proxy_ssl_key
, "GIT_PROXY_SSL_KEY");
1087 set_from_env(&http_proxy_ssl_ca_info
, "GIT_PROXY_SSL_CAINFO");
1089 if (getenv("GIT_PROXY_SSL_CERT_PASSWORD_PROTECTED"))
1090 proxy_ssl_cert_password_required
= 1;
1092 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1093 curl_ftp_no_epsv
= 1;
1096 credential_from_url(&http_auth
, url
);
1097 if (!ssl_cert_password_required
&&
1098 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
1099 starts_with(url
, "https://"))
1100 ssl_cert_password_required
= 1;
1103 curl_default
= get_curl_handle();
1106 void http_cleanup(void)
1108 struct active_request_slot
*slot
= active_queue_head
;
1110 while (slot
!= NULL
) {
1111 struct active_request_slot
*next
= slot
->next
;
1112 if (slot
->curl
!= NULL
) {
1113 xmulti_remove_handle(slot
);
1114 curl_easy_cleanup(slot
->curl
);
1119 active_queue_head
= NULL
;
1121 curl_easy_cleanup(curl_default
);
1123 curl_multi_cleanup(curlm
);
1124 curl_global_cleanup();
1126 string_list_clear(&extra_http_headers
, 0);
1128 curl_slist_free_all(pragma_header
);
1129 pragma_header
= NULL
;
1131 curl_slist_free_all(no_pragma_header
);
1132 no_pragma_header
= NULL
;
1134 if (curl_http_proxy
) {
1135 free((void *)curl_http_proxy
);
1136 curl_http_proxy
= NULL
;
1139 if (proxy_auth
.password
) {
1140 memset(proxy_auth
.password
, 0, strlen(proxy_auth
.password
));
1141 FREE_AND_NULL(proxy_auth
.password
);
1144 free((void *)curl_proxyuserpwd
);
1145 curl_proxyuserpwd
= NULL
;
1147 free((void *)http_proxy_authmethod
);
1148 http_proxy_authmethod
= NULL
;
1150 if (cert_auth
.password
!= NULL
) {
1151 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
1152 FREE_AND_NULL(cert_auth
.password
);
1154 ssl_cert_password_required
= 0;
1156 if (proxy_cert_auth
.password
!= NULL
) {
1157 memset(proxy_cert_auth
.password
, 0, strlen(proxy_cert_auth
.password
));
1158 FREE_AND_NULL(proxy_cert_auth
.password
);
1160 proxy_ssl_cert_password_required
= 0;
1162 FREE_AND_NULL(cached_accept_language
);
1165 struct active_request_slot
*get_active_slot(void)
1167 struct active_request_slot
*slot
= active_queue_head
;
1168 struct active_request_slot
*newslot
;
1172 /* Wait for a slot to open up if the queue is full */
1173 while (active_requests
>= max_requests
) {
1174 curl_multi_perform(curlm
, &num_transfers
);
1175 if (num_transfers
< active_requests
)
1176 process_curl_messages();
1179 while (slot
!= NULL
&& slot
->in_use
)
1183 newslot
= xmalloc(sizeof(*newslot
));
1184 newslot
->curl
= NULL
;
1185 newslot
->in_use
= 0;
1186 newslot
->next
= NULL
;
1188 slot
= active_queue_head
;
1190 active_queue_head
= newslot
;
1192 while (slot
->next
!= NULL
)
1194 slot
->next
= newslot
;
1199 if (slot
->curl
== NULL
) {
1200 slot
->curl
= curl_easy_duphandle(curl_default
);
1201 curl_session_count
++;
1206 slot
->results
= NULL
;
1207 slot
->finished
= NULL
;
1208 slot
->callback_data
= NULL
;
1209 slot
->callback_func
= NULL
;
1210 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
1211 if (curl_save_cookies
)
1212 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
1213 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
1214 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
1215 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
1216 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
1217 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
1218 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
1219 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
1220 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1221 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
1222 curl_easy_setopt(slot
->curl
, CURLOPT_RANGE
, NULL
);
1225 * Default following to off unless "ALWAYS" is configured; this gives
1226 * callers a sane starting point, and they can tweak for individual
1227 * HTTP_FOLLOW_* cases themselves.
1229 if (http_follow_config
== HTTP_FOLLOW_ALWAYS
)
1230 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1232 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 0);
1234 curl_easy_setopt(slot
->curl
, CURLOPT_IPRESOLVE
, git_curl_ipresolve
);
1235 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPAUTH
, http_auth_methods
);
1236 if (http_auth
.password
|| curl_empty_auth_enabled())
1237 init_curl_http_auth(slot
->curl
);
1242 int start_active_slot(struct active_request_slot
*slot
)
1244 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
1247 if (curlm_result
!= CURLM_OK
&&
1248 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
1249 warning("curl_multi_add_handle failed: %s",
1250 curl_multi_strerror(curlm_result
));
1257 * We know there must be something to do, since we just added
1260 curl_multi_perform(curlm
, &num_transfers
);
1266 int (*fill
)(void *);
1267 struct fill_chain
*next
;
1270 static struct fill_chain
*fill_cfg
;
1272 void add_fill_function(void *data
, int (*fill
)(void *))
1274 struct fill_chain
*new_fill
= xmalloc(sizeof(*new_fill
));
1275 struct fill_chain
**linkp
= &fill_cfg
;
1276 new_fill
->data
= data
;
1277 new_fill
->fill
= fill
;
1278 new_fill
->next
= NULL
;
1280 linkp
= &(*linkp
)->next
;
1284 void fill_active_slots(void)
1286 struct active_request_slot
*slot
= active_queue_head
;
1288 while (active_requests
< max_requests
) {
1289 struct fill_chain
*fill
;
1290 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
1291 if (fill
->fill(fill
->data
))
1298 while (slot
!= NULL
) {
1299 if (!slot
->in_use
&& slot
->curl
!= NULL
1300 && curl_session_count
> min_curl_sessions
) {
1301 curl_easy_cleanup(slot
->curl
);
1303 curl_session_count
--;
1309 void step_active_slots(void)
1312 CURLMcode curlm_result
;
1315 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
1316 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
1317 if (num_transfers
< active_requests
) {
1318 process_curl_messages();
1319 fill_active_slots();
1323 void run_active_slot(struct active_request_slot
*slot
)
1329 struct timeval select_timeout
;
1332 slot
->finished
= &finished
;
1334 step_active_slots();
1338 curl_multi_timeout(curlm
, &curl_timeout
);
1339 if (curl_timeout
== 0) {
1341 } else if (curl_timeout
== -1) {
1342 select_timeout
.tv_sec
= 0;
1343 select_timeout
.tv_usec
= 50000;
1345 select_timeout
.tv_sec
= curl_timeout
/ 1000;
1346 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
1353 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
1356 * It can happen that curl_multi_timeout returns a pathologically
1357 * long timeout when curl_multi_fdset returns no file descriptors
1358 * to read. See commit message for more details.
1361 (select_timeout
.tv_sec
> 0 ||
1362 select_timeout
.tv_usec
> 50000)) {
1363 select_timeout
.tv_sec
= 0;
1364 select_timeout
.tv_usec
= 50000;
1367 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
1372 static void release_active_slot(struct active_request_slot
*slot
)
1374 closedown_active_slot(slot
);
1376 xmulti_remove_handle(slot
);
1377 if (curl_session_count
> min_curl_sessions
) {
1378 curl_easy_cleanup(slot
->curl
);
1380 curl_session_count
--;
1383 fill_active_slots();
1386 void finish_all_active_slots(void)
1388 struct active_request_slot
*slot
= active_queue_head
;
1390 while (slot
!= NULL
)
1392 run_active_slot(slot
);
1393 slot
= active_queue_head
;
1399 /* Helpers for modifying and creating URLs */
1400 static inline int needs_quote(int ch
)
1402 if (((ch
>= 'A') && (ch
<= 'Z'))
1403 || ((ch
>= 'a') && (ch
<= 'z'))
1404 || ((ch
>= '0') && (ch
<= '9'))
1412 static char *quote_ref_url(const char *base
, const char *ref
)
1414 struct strbuf buf
= STRBUF_INIT
;
1418 end_url_with_slash(&buf
, base
);
1420 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
1421 if (needs_quote(ch
))
1422 strbuf_addf(&buf
, "%%%02x", ch
);
1424 strbuf_addch(&buf
, *cp
);
1426 return strbuf_detach(&buf
, NULL
);
1429 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
1431 int only_two_digit_prefix
)
1433 end_url_with_slash(buf
, url
);
1435 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
1436 if (!only_two_digit_prefix
)
1437 strbuf_addstr(buf
, hex
+ 2);
1440 char *get_remote_object_url(const char *url
, const char *hex
,
1441 int only_two_digit_prefix
)
1443 struct strbuf buf
= STRBUF_INIT
;
1444 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
1445 return strbuf_detach(&buf
, NULL
);
1448 void normalize_curl_result(CURLcode
*result
, long http_code
,
1449 char *errorstr
, size_t errorlen
)
1452 * If we see a failing http code with CURLE_OK, we have turned off
1453 * FAILONERROR (to keep the server's custom error response), and should
1454 * translate the code into failure here.
1456 * Likewise, if we see a redirect (30x code), that means we turned off
1457 * redirect-following, and we should treat the result as an error.
1459 if (*result
== CURLE_OK
&& http_code
>= 300) {
1460 *result
= CURLE_HTTP_RETURNED_ERROR
;
1462 * Normally curl will already have put the "reason phrase"
1463 * from the server into curl_errorstr; unfortunately without
1464 * FAILONERROR it is lost, so we can give only the numeric
1467 xsnprintf(errorstr
, errorlen
,
1468 "The requested URL returned error: %ld",
1473 static int handle_curl_result(struct slot_results
*results
)
1475 normalize_curl_result(&results
->curl_result
, results
->http_code
,
1476 curl_errorstr
, sizeof(curl_errorstr
));
1478 if (results
->curl_result
== CURLE_OK
) {
1479 credential_approve(&http_auth
);
1480 credential_approve(&proxy_auth
);
1481 credential_approve(&cert_auth
);
1483 } else if (results
->curl_result
== CURLE_SSL_CERTPROBLEM
) {
1485 * We can't tell from here whether it's a bad path, bad
1486 * certificate, bad password, or something else wrong
1487 * with the certificate. So we reject the credential to
1488 * avoid caching or saving a bad password.
1490 credential_reject(&cert_auth
);
1492 } else if (missing_target(results
))
1493 return HTTP_MISSING_TARGET
;
1494 else if (results
->http_code
== 401) {
1495 if (http_auth
.username
&& http_auth
.password
) {
1496 credential_reject(&http_auth
);
1499 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
1500 if (results
->auth_avail
) {
1501 http_auth_methods
&= results
->auth_avail
;
1502 http_auth_methods_restricted
= 1;
1507 if (results
->http_connectcode
== 407)
1508 credential_reject(&proxy_auth
);
1509 if (!curl_errorstr
[0])
1510 strlcpy(curl_errorstr
,
1511 curl_easy_strerror(results
->curl_result
),
1512 sizeof(curl_errorstr
));
1517 int run_one_slot(struct active_request_slot
*slot
,
1518 struct slot_results
*results
)
1520 slot
->results
= results
;
1521 if (!start_active_slot(slot
)) {
1522 xsnprintf(curl_errorstr
, sizeof(curl_errorstr
),
1523 "failed to start HTTP request");
1524 return HTTP_START_FAILED
;
1527 run_active_slot(slot
);
1528 return handle_curl_result(results
);
1531 struct curl_slist
*http_copy_default_headers(void)
1533 struct curl_slist
*headers
= NULL
;
1534 const struct string_list_item
*item
;
1536 for_each_string_list_item(item
, &extra_http_headers
)
1537 headers
= curl_slist_append(headers
, item
->string
);
1542 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
1548 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
1550 strbuf_addstr(buf
, ptr
);
1555 * Check for and extract a content-type parameter. "raw"
1556 * should be positioned at the start of the potential
1557 * parameter, with any whitespace already removed.
1559 * "name" is the name of the parameter. The value is appended
1562 static int extract_param(const char *raw
, const char *name
,
1565 size_t len
= strlen(name
);
1567 if (strncasecmp(raw
, name
, len
))
1575 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
1576 strbuf_addch(out
, *raw
++);
1581 * Extract a normalized version of the content type, with any
1582 * spaces suppressed, all letters lowercased, and no trailing ";"
1585 * Note that we will silently remove even invalid whitespace. For
1586 * example, "text / plain" is specifically forbidden by RFC 2616,
1587 * but "text/plain" is the only reasonable output, and this keeps
1590 * If the "charset" argument is not NULL, store the value of any
1591 * charset parameter there.
1594 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1595 * "text / plain" -> "text/plain"
1597 static void extract_content_type(struct strbuf
*raw
, struct strbuf
*type
,
1598 struct strbuf
*charset
)
1603 strbuf_grow(type
, raw
->len
);
1604 for (p
= raw
->buf
; *p
; p
++) {
1611 strbuf_addch(type
, tolower(*p
));
1617 strbuf_reset(charset
);
1619 while (isspace(*p
) || *p
== ';')
1621 if (!extract_param(p
, "charset", charset
))
1623 while (*p
&& !isspace(*p
))
1627 if (!charset
->len
&& starts_with(type
->buf
, "text/"))
1628 strbuf_addstr(charset
, "ISO-8859-1");
1631 static void write_accept_language(struct strbuf
*buf
)
1634 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1635 * that, q-value will be smaller than 0.001, the minimum q-value the
1636 * HTTP specification allows. See
1637 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1639 const int MAX_DECIMAL_PLACES
= 3;
1640 const int MAX_LANGUAGE_TAGS
= 1000;
1641 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE
= 4000;
1642 char **language_tags
= NULL
;
1644 const char *s
= get_preferred_languages();
1646 struct strbuf tag
= STRBUF_INIT
;
1648 /* Don't add Accept-Language header if no language is preferred. */
1653 * Split the colon-separated string of preferred languages into
1654 * language_tags array.
1657 /* collect language tag */
1658 for (; *s
&& (isalnum(*s
) || *s
== '_'); s
++)
1659 strbuf_addch(&tag
, *s
== '_' ? '-' : *s
);
1661 /* skip .codeset, @modifier and any other unnecessary parts */
1662 while (*s
&& *s
!= ':')
1667 REALLOC_ARRAY(language_tags
, num_langs
);
1668 language_tags
[num_langs
- 1] = strbuf_detach(&tag
, NULL
);
1669 if (num_langs
>= MAX_LANGUAGE_TAGS
- 1) /* -1 for '*' */
1674 /* write Accept-Language header into buf */
1676 int last_buf_len
= 0;
1682 REALLOC_ARRAY(language_tags
, num_langs
+ 1);
1683 language_tags
[num_langs
++] = "*"; /* it's OK; this won't be freed */
1685 /* compute decimal_places */
1686 for (max_q
= 1, decimal_places
= 0;
1687 max_q
< num_langs
&& decimal_places
<= MAX_DECIMAL_PLACES
;
1688 decimal_places
++, max_q
*= 10)
1691 xsnprintf(q_format
, sizeof(q_format
), ";q=0.%%0%dd", decimal_places
);
1693 strbuf_addstr(buf
, "Accept-Language: ");
1695 for (i
= 0; i
< num_langs
; i
++) {
1697 strbuf_addstr(buf
, ", ");
1699 strbuf_addstr(buf
, language_tags
[i
]);
1702 strbuf_addf(buf
, q_format
, max_q
- i
);
1704 if (buf
->len
> MAX_ACCEPT_LANGUAGE_HEADER_SIZE
) {
1705 strbuf_remove(buf
, last_buf_len
, buf
->len
- last_buf_len
);
1709 last_buf_len
= buf
->len
;
1713 /* free language tags -- last one is a static '*' */
1714 for (i
= 0; i
< num_langs
- 1; i
++)
1715 free(language_tags
[i
]);
1716 free(language_tags
);
1720 * Get an Accept-Language header which indicates user's preferred languages.
1724 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1725 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1726 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1727 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1728 * LANGUAGE= LANG=C -> ""
1730 static const char *get_accept_language(void)
1732 if (!cached_accept_language
) {
1733 struct strbuf buf
= STRBUF_INIT
;
1734 write_accept_language(&buf
);
1736 cached_accept_language
= strbuf_detach(&buf
, NULL
);
1739 return cached_accept_language
;
1742 static void http_opt_request_remainder(CURL
*curl
, off_t pos
)
1745 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
"-", (uintmax_t)pos
);
1746 curl_easy_setopt(curl
, CURLOPT_RANGE
, buf
);
1749 /* http_request() targets */
1750 #define HTTP_REQUEST_STRBUF 0
1751 #define HTTP_REQUEST_FILE 1
1753 static int http_request(const char *url
,
1754 void *result
, int target
,
1755 const struct http_get_options
*options
)
1757 struct active_request_slot
*slot
;
1758 struct slot_results results
;
1759 struct curl_slist
*headers
= http_copy_default_headers();
1760 struct strbuf buf
= STRBUF_INIT
;
1761 const char *accept_language
;
1764 slot
= get_active_slot();
1765 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1767 if (result
== NULL
) {
1768 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
1770 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1771 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, result
);
1773 if (target
== HTTP_REQUEST_FILE
) {
1774 off_t posn
= ftello(result
);
1775 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1778 http_opt_request_remainder(slot
->curl
, posn
);
1780 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1784 accept_language
= get_accept_language();
1786 if (accept_language
)
1787 headers
= curl_slist_append(headers
, accept_language
);
1789 strbuf_addstr(&buf
, "Pragma:");
1790 if (options
&& options
->no_cache
)
1791 strbuf_addstr(&buf
, " no-cache");
1792 if (options
&& options
->initial_request
&&
1793 http_follow_config
== HTTP_FOLLOW_INITIAL
)
1794 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1796 headers
= curl_slist_append(headers
, buf
.buf
);
1798 /* Add additional headers here */
1799 if (options
&& options
->extra_headers
) {
1800 const struct string_list_item
*item
;
1801 for_each_string_list_item(item
, options
->extra_headers
) {
1802 headers
= curl_slist_append(headers
, item
->string
);
1806 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1807 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1808 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
1809 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1811 ret
= run_one_slot(slot
, &results
);
1813 if (options
&& options
->content_type
) {
1814 struct strbuf raw
= STRBUF_INIT
;
1815 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
, &raw
);
1816 extract_content_type(&raw
, options
->content_type
,
1818 strbuf_release(&raw
);
1821 if (options
&& options
->effective_url
)
1822 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
1823 options
->effective_url
);
1825 curl_slist_free_all(headers
);
1826 strbuf_release(&buf
);
1832 * Update the "base" url to a more appropriate value, as deduced by
1833 * redirects seen when requesting a URL starting with "url".
1835 * The "asked" parameter is a URL that we asked curl to access, and must begin
1838 * The "got" parameter is the URL that curl reported to us as where we ended
1841 * Returns 1 if we updated the base url, 0 otherwise.
1843 * Our basic strategy is to compare "base" and "asked" to find the bits
1844 * specific to our request. We then strip those bits off of "got" to yield the
1845 * new base. So for example, if our base is "http://example.com/foo.git",
1846 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1847 * with "https://other.example.com/foo.git/info/refs". We would want the
1848 * new URL to become "https://other.example.com/foo.git".
1850 * Note that this assumes a sane redirect scheme. It's entirely possible
1851 * in the example above to end up at a URL that does not even end in
1852 * "info/refs". In such a case we die. There's not much we can do, such a
1853 * scheme is unlikely to represent a real git repository, and failing to
1854 * rewrite the base opens options for malicious redirects to do funny things.
1856 static int update_url_from_redirect(struct strbuf
*base
,
1858 const struct strbuf
*got
)
1863 if (!strcmp(asked
, got
->buf
))
1866 if (!skip_prefix(asked
, base
->buf
, &tail
))
1867 BUG("update_url_from_redirect: %s is not a superset of %s",
1871 if (!strip_suffix_mem(got
->buf
, &new_len
, tail
))
1872 die(_("unable to update url base from redirection:\n"
1878 strbuf_add(base
, got
->buf
, new_len
);
1883 static int http_request_reauth(const char *url
,
1884 void *result
, int target
,
1885 struct http_get_options
*options
)
1887 int ret
= http_request(url
, result
, target
, options
);
1889 if (ret
!= HTTP_OK
&& ret
!= HTTP_REAUTH
)
1892 if (options
&& options
->effective_url
&& options
->base_url
) {
1893 if (update_url_from_redirect(options
->base_url
,
1894 url
, options
->effective_url
)) {
1895 credential_from_url(&http_auth
, options
->base_url
->buf
);
1896 url
= options
->effective_url
->buf
;
1900 if (ret
!= HTTP_REAUTH
)
1904 * The previous request may have put cruft into our output stream; we
1905 * should clear it out before making our next request.
1908 case HTTP_REQUEST_STRBUF
:
1909 strbuf_reset(result
);
1911 case HTTP_REQUEST_FILE
:
1912 if (fflush(result
)) {
1913 error_errno("unable to flush a file");
1914 return HTTP_START_FAILED
;
1917 if (ftruncate(fileno(result
), 0) < 0) {
1918 error_errno("unable to truncate a file");
1919 return HTTP_START_FAILED
;
1923 BUG("Unknown http_request target");
1926 credential_fill(&http_auth
);
1928 return http_request(url
, result
, target
, options
);
1931 int http_get_strbuf(const char *url
,
1932 struct strbuf
*result
,
1933 struct http_get_options
*options
)
1935 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
1939 * Downloads a URL and stores the result in the given file.
1941 * If a previous interrupted download is detected (i.e. a previous temporary
1942 * file is still around) the download is resumed.
1944 static int http_get_file(const char *url
, const char *filename
,
1945 struct http_get_options
*options
)
1948 struct strbuf tmpfile
= STRBUF_INIT
;
1951 strbuf_addf(&tmpfile
, "%s.temp", filename
);
1952 result
= fopen(tmpfile
.buf
, "a");
1954 error("Unable to open local file %s", tmpfile
.buf
);
1959 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
1962 if (ret
== HTTP_OK
&& finalize_object_file(tmpfile
.buf
, filename
))
1965 strbuf_release(&tmpfile
);
1969 int http_fetch_ref(const char *base
, struct ref
*ref
)
1971 struct http_get_options options
= {0};
1973 struct strbuf buffer
= STRBUF_INIT
;
1976 options
.no_cache
= 1;
1978 url
= quote_ref_url(base
, ref
->name
);
1979 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
1980 strbuf_rtrim(&buffer
);
1981 if (buffer
.len
== the_hash_algo
->hexsz
)
1982 ret
= get_oid_hex(buffer
.buf
, &ref
->old_oid
);
1983 else if (starts_with(buffer
.buf
, "ref: ")) {
1984 ref
->symref
= xstrdup(buffer
.buf
+ 5);
1989 strbuf_release(&buffer
);
1994 /* Helpers for fetching packs */
1995 static char *fetch_pack_index(unsigned char *hash
, const char *base_url
)
1998 struct strbuf buf
= STRBUF_INIT
;
2000 if (http_is_verbose
)
2001 fprintf(stderr
, "Getting index for pack %s\n", hash_to_hex(hash
));
2003 end_url_with_slash(&buf
, base_url
);
2004 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", hash_to_hex(hash
));
2005 url
= strbuf_detach(&buf
, NULL
);
2007 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(hash
));
2008 tmp
= strbuf_detach(&buf
, NULL
);
2010 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
2011 error("Unable to get pack index %s", url
);
2019 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
2020 unsigned char *sha1
, const char *base_url
)
2022 struct packed_git
*new_pack
;
2023 char *tmp_idx
= NULL
;
2026 if (has_pack_index(sha1
)) {
2027 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
2029 return -1; /* parse_pack_index() already issued error message */
2033 tmp_idx
= fetch_pack_index(sha1
, base_url
);
2037 new_pack
= parse_pack_index(sha1
, tmp_idx
);
2042 return -1; /* parse_pack_index() already issued error message */
2045 ret
= verify_pack_index(new_pack
);
2047 close_pack_index(new_pack
);
2048 ret
= finalize_object_file(tmp_idx
, sha1_pack_index_name(sha1
));
2055 new_pack
->next
= *packs_head
;
2056 *packs_head
= new_pack
;
2060 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
2062 struct http_get_options options
= {0};
2066 struct strbuf buf
= STRBUF_INIT
;
2067 struct object_id oid
;
2069 end_url_with_slash(&buf
, base_url
);
2070 strbuf_addstr(&buf
, "objects/info/packs");
2071 url
= strbuf_detach(&buf
, NULL
);
2073 options
.no_cache
= 1;
2074 ret
= http_get_strbuf(url
, &buf
, &options
);
2080 if (skip_prefix(data
, "P pack-", &data
) &&
2081 !parse_oid_hex(data
, &oid
, &data
) &&
2082 skip_prefix(data
, ".pack", &data
) &&
2083 (*data
== '\n' || *data
== '\0')) {
2084 fetch_and_setup_pack_index(packs_head
, oid
.hash
, base_url
);
2086 data
= strchrnul(data
, '\n');
2089 data
++; /* skip past newline */
2097 void release_http_pack_request(struct http_pack_request
*preq
)
2099 if (preq
->packfile
!= NULL
) {
2100 fclose(preq
->packfile
);
2101 preq
->packfile
= NULL
;
2104 strbuf_release(&preq
->tmpfile
);
2109 static const char *default_index_pack_args
[] =
2110 {"index-pack", "--stdin", NULL
};
2112 int finish_http_pack_request(struct http_pack_request
*preq
)
2114 struct child_process ip
= CHILD_PROCESS_INIT
;
2118 fclose(preq
->packfile
);
2119 preq
->packfile
= NULL
;
2121 tmpfile_fd
= xopen(preq
->tmpfile
.buf
, O_RDONLY
);
2125 ip
.argv
= preq
->index_pack_args
? preq
->index_pack_args
2126 : default_index_pack_args
;
2128 if (preq
->preserve_index_pack_stdout
)
2133 if (run_command(&ip
)) {
2140 unlink(preq
->tmpfile
.buf
);
2144 void http_install_packfile(struct packed_git
*p
,
2145 struct packed_git
**list_to_remove_from
)
2147 struct packed_git
**lst
= list_to_remove_from
;
2150 lst
= &((*lst
)->next
);
2151 *lst
= (*lst
)->next
;
2153 install_packed_git(the_repository
, p
);
2156 struct http_pack_request
*new_http_pack_request(
2157 const unsigned char *packed_git_hash
, const char *base_url
) {
2159 struct strbuf buf
= STRBUF_INIT
;
2161 end_url_with_slash(&buf
, base_url
);
2162 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
2163 hash_to_hex(packed_git_hash
));
2164 return new_direct_http_pack_request(packed_git_hash
,
2165 strbuf_detach(&buf
, NULL
));
2168 struct http_pack_request
*new_direct_http_pack_request(
2169 const unsigned char *packed_git_hash
, char *url
)
2171 off_t prev_posn
= 0;
2172 struct http_pack_request
*preq
;
2174 CALLOC_ARRAY(preq
, 1);
2175 strbuf_init(&preq
->tmpfile
, 0);
2179 strbuf_addf(&preq
->tmpfile
, "%s.temp", sha1_pack_name(packed_git_hash
));
2180 preq
->packfile
= fopen(preq
->tmpfile
.buf
, "a");
2181 if (!preq
->packfile
) {
2182 error("Unable to open local file %s for pack",
2187 preq
->slot
= get_active_slot();
2188 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEDATA
, preq
->packfile
);
2189 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
2190 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
2191 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
2195 * If there is data present from a previous transfer attempt,
2196 * resume where it left off
2198 prev_posn
= ftello(preq
->packfile
);
2200 if (http_is_verbose
)
2202 "Resuming fetch of pack %s at byte %"PRIuMAX
"\n",
2203 hash_to_hex(packed_git_hash
),
2204 (uintmax_t)prev_posn
);
2205 http_opt_request_remainder(preq
->slot
->curl
, prev_posn
);
2211 strbuf_release(&preq
->tmpfile
);
2217 /* Helpers for fetching objects (loose) */
2218 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
2221 unsigned char expn
[4096];
2222 size_t size
= eltsize
* nmemb
;
2224 struct http_object_request
*freq
= data
;
2225 struct active_request_slot
*slot
= freq
->slot
;
2228 CURLcode c
= curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
,
2231 BUG("curl_easy_getinfo for HTTP code failed: %s",
2232 curl_easy_strerror(c
));
2233 if (slot
->http_code
>= 300)
2238 ssize_t retval
= xwrite(freq
->localfile
,
2239 (char *) ptr
+ posn
, size
- posn
);
2241 return posn
/ eltsize
;
2243 } while (posn
< size
);
2245 freq
->stream
.avail_in
= size
;
2246 freq
->stream
.next_in
= (void *)ptr
;
2248 freq
->stream
.next_out
= expn
;
2249 freq
->stream
.avail_out
= sizeof(expn
);
2250 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
2251 the_hash_algo
->update_fn(&freq
->c
, expn
,
2252 sizeof(expn
) - freq
->stream
.avail_out
);
2253 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
2257 struct http_object_request
*new_http_object_request(const char *base_url
,
2258 const struct object_id
*oid
)
2260 char *hex
= oid_to_hex(oid
);
2261 struct strbuf filename
= STRBUF_INIT
;
2262 struct strbuf prevfile
= STRBUF_INIT
;
2264 char prev_buf
[PREV_BUF_SIZE
];
2265 ssize_t prev_read
= 0;
2266 off_t prev_posn
= 0;
2267 struct http_object_request
*freq
;
2269 CALLOC_ARRAY(freq
, 1);
2270 strbuf_init(&freq
->tmpfile
, 0);
2271 oidcpy(&freq
->oid
, oid
);
2272 freq
->localfile
= -1;
2274 loose_object_path(the_repository
, &filename
, oid
);
2275 strbuf_addf(&freq
->tmpfile
, "%s.temp", filename
.buf
);
2277 strbuf_addf(&prevfile
, "%s.prev", filename
.buf
);
2278 unlink_or_warn(prevfile
.buf
);
2279 rename(freq
->tmpfile
.buf
, prevfile
.buf
);
2280 unlink_or_warn(freq
->tmpfile
.buf
);
2281 strbuf_release(&filename
);
2283 if (freq
->localfile
!= -1)
2284 error("fd leakage in start: %d", freq
->localfile
);
2285 freq
->localfile
= open(freq
->tmpfile
.buf
,
2286 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2288 * This could have failed due to the "lazy directory creation";
2289 * try to mkdir the last path component.
2291 if (freq
->localfile
< 0 && errno
== ENOENT
) {
2292 char *dir
= strrchr(freq
->tmpfile
.buf
, '/');
2295 mkdir(freq
->tmpfile
.buf
, 0777);
2298 freq
->localfile
= open(freq
->tmpfile
.buf
,
2299 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2302 if (freq
->localfile
< 0) {
2303 error_errno("Couldn't create temporary file %s",
2308 git_inflate_init(&freq
->stream
);
2310 the_hash_algo
->init_fn(&freq
->c
);
2312 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
2315 * If a previous temp file is present, process what was already
2318 prevlocal
= open(prevfile
.buf
, O_RDONLY
);
2319 if (prevlocal
!= -1) {
2321 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
2323 if (fwrite_sha1_file(prev_buf
,
2326 freq
) == prev_read
) {
2327 prev_posn
+= prev_read
;
2332 } while (prev_read
> 0);
2335 unlink_or_warn(prevfile
.buf
);
2336 strbuf_release(&prevfile
);
2339 * Reset inflate/SHA1 if there was an error reading the previous temp
2340 * file; also rewind to the beginning of the local file.
2342 if (prev_read
== -1) {
2343 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
2344 git_inflate_init(&freq
->stream
);
2345 the_hash_algo
->init_fn(&freq
->c
);
2348 lseek(freq
->localfile
, 0, SEEK_SET
);
2349 if (ftruncate(freq
->localfile
, 0) < 0) {
2350 error_errno("Couldn't truncate temporary file %s",
2357 freq
->slot
= get_active_slot();
2359 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEDATA
, freq
);
2360 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FAILONERROR
, 0);
2361 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
2362 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
2363 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
2364 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
2367 * If we have successfully processed data from a previous fetch
2368 * attempt, only fetch the data we don't already have.
2371 if (http_is_verbose
)
2373 "Resuming fetch of object %s at byte %"PRIuMAX
"\n",
2374 hex
, (uintmax_t)prev_posn
);
2375 http_opt_request_remainder(freq
->slot
->curl
, prev_posn
);
2381 strbuf_release(&prevfile
);
2387 void process_http_object_request(struct http_object_request
*freq
)
2389 if (freq
->slot
== NULL
)
2391 freq
->curl_result
= freq
->slot
->curl_result
;
2392 freq
->http_code
= freq
->slot
->http_code
;
2396 int finish_http_object_request(struct http_object_request
*freq
)
2399 struct strbuf filename
= STRBUF_INIT
;
2401 close(freq
->localfile
);
2402 freq
->localfile
= -1;
2404 process_http_object_request(freq
);
2406 if (freq
->http_code
== 416) {
2407 warning("requested range invalid; we may already have all the data.");
2408 } else if (freq
->curl_result
!= CURLE_OK
) {
2409 if (stat(freq
->tmpfile
.buf
, &st
) == 0)
2410 if (st
.st_size
== 0)
2411 unlink_or_warn(freq
->tmpfile
.buf
);
2415 git_inflate_end(&freq
->stream
);
2416 the_hash_algo
->final_oid_fn(&freq
->real_oid
, &freq
->c
);
2417 if (freq
->zret
!= Z_STREAM_END
) {
2418 unlink_or_warn(freq
->tmpfile
.buf
);
2421 if (!oideq(&freq
->oid
, &freq
->real_oid
)) {
2422 unlink_or_warn(freq
->tmpfile
.buf
);
2425 loose_object_path(the_repository
, &filename
, &freq
->oid
);
2426 freq
->rename
= finalize_object_file(freq
->tmpfile
.buf
, filename
.buf
);
2427 strbuf_release(&filename
);
2429 return freq
->rename
;
2432 void abort_http_object_request(struct http_object_request
*freq
)
2434 unlink_or_warn(freq
->tmpfile
.buf
);
2436 release_http_object_request(freq
);
2439 void release_http_object_request(struct http_object_request
*freq
)
2441 if (freq
->localfile
!= -1) {
2442 close(freq
->localfile
);
2443 freq
->localfile
= -1;
2445 FREE_AND_NULL(freq
->url
);
2446 if (freq
->slot
!= NULL
) {
2447 freq
->slot
->callback_func
= NULL
;
2448 freq
->slot
->callback_data
= NULL
;
2449 release_active_slot(freq
->slot
);
2452 strbuf_release(&freq
->tmpfile
);