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 long int git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
25 ssize_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
27 static int min_curl_sessions
= 1;
28 static int curl_session_count
;
29 static int max_requests
= -1;
31 static CURL
*curl_default
;
33 #define PREV_BUF_SIZE 4096
35 char curl_errorstr
[CURL_ERROR_SIZE
];
37 static int curl_ssl_verify
= -1;
38 static int curl_ssl_try
;
39 static const char *curl_http_version
= NULL
;
40 static const char *ssl_cert
;
41 static const char *ssl_cipherlist
;
42 static const char *ssl_version
;
47 { "sslv2", CURL_SSLVERSION_SSLv2
},
48 { "sslv3", CURL_SSLVERSION_SSLv3
},
49 { "tlsv1", CURL_SSLVERSION_TLSv1
},
50 #if LIBCURL_VERSION_NUM >= 0x072200
51 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0
},
52 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1
},
53 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2
},
55 #if LIBCURL_VERSION_NUM >= 0x073400
56 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3
},
59 static const char *ssl_key
;
60 static const char *ssl_capath
;
61 static const char *curl_no_proxy
;
62 #if LIBCURL_VERSION_NUM >= 0x072700
63 static const char *ssl_pinnedkey
;
65 static const char *ssl_cainfo
;
66 static long curl_low_speed_limit
= -1;
67 static long curl_low_speed_time
= -1;
68 static int curl_ftp_no_epsv
;
69 static const char *curl_http_proxy
;
70 static const char *http_proxy_authmethod
;
72 static const char *http_proxy_ssl_cert
;
73 static const char *http_proxy_ssl_key
;
74 static const char *http_proxy_ssl_ca_info
;
75 static struct credential proxy_cert_auth
= CREDENTIAL_INIT
;
76 static int proxy_ssl_cert_password_required
;
81 } proxy_authmethods
[] = {
82 { "basic", CURLAUTH_BASIC
},
83 { "digest", CURLAUTH_DIGEST
},
84 { "negotiate", CURLAUTH_GSSNEGOTIATE
},
85 { "ntlm", CURLAUTH_NTLM
},
86 { "anyauth", CURLAUTH_ANY
},
88 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
89 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
93 #ifdef CURLGSSAPI_DELEGATION_FLAG
94 static const char *curl_deleg
;
97 long curl_deleg_param
;
98 } curl_deleg_levels
[] = {
99 { "none", CURLGSSAPI_DELEGATION_NONE
},
100 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG
},
101 { "always", CURLGSSAPI_DELEGATION_FLAG
},
105 static struct credential proxy_auth
= CREDENTIAL_INIT
;
106 static const char *curl_proxyuserpwd
;
107 static const char *curl_cookie_file
;
108 static int curl_save_cookies
;
109 struct credential http_auth
= CREDENTIAL_INIT
;
110 static int http_proactive_auth
;
111 static const char *user_agent
;
112 static int curl_empty_auth
= -1;
114 enum http_follow_config http_follow_config
= HTTP_FOLLOW_INITIAL
;
116 static struct credential cert_auth
= CREDENTIAL_INIT
;
117 static int ssl_cert_password_required
;
118 static unsigned long http_auth_methods
= CURLAUTH_ANY
;
119 static int http_auth_methods_restricted
;
120 /* Modes for which empty_auth cannot actually help us. */
121 static unsigned long empty_auth_useless
=
126 static struct curl_slist
*pragma_header
;
127 static struct curl_slist
*no_pragma_header
;
128 static struct string_list extra_http_headers
= STRING_LIST_INIT_DUP
;
130 static struct active_request_slot
*active_queue_head
;
132 static char *cached_accept_language
;
134 static char *http_ssl_backend
;
136 static int http_schannel_check_revoke
= 1;
138 * With the backend being set to `schannel`, setting sslCAinfo would override
139 * the Certificate Store in cURL v7.60.0 and later, which is not what we want
142 static int http_schannel_use_ssl_cainfo
;
144 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
146 size_t size
= eltsize
* nmemb
;
147 struct buffer
*buffer
= buffer_
;
149 if (size
> buffer
->buf
.len
- buffer
->posn
)
150 size
= buffer
->buf
.len
- buffer
->posn
;
151 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
152 buffer
->posn
+= size
;
154 return size
/ eltsize
;
157 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
159 struct buffer
*buffer
= clientp
;
165 case CURLIOCMD_RESTARTREAD
:
170 return CURLIOE_UNKNOWNCMD
;
174 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
176 size_t size
= eltsize
* nmemb
;
177 struct strbuf
*buffer
= buffer_
;
179 strbuf_add(buffer
, ptr
, size
);
183 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
188 static void closedown_active_slot(struct active_request_slot
*slot
)
194 static void finish_active_slot(struct active_request_slot
*slot
)
196 closedown_active_slot(slot
);
197 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
199 if (slot
->finished
!= NULL
)
200 (*slot
->finished
) = 1;
202 /* Store slot results so they can be read after the slot is reused */
203 if (slot
->results
!= NULL
) {
204 slot
->results
->curl_result
= slot
->curl_result
;
205 slot
->results
->http_code
= slot
->http_code
;
206 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
207 &slot
->results
->auth_avail
);
209 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CONNECTCODE
,
210 &slot
->results
->http_connectcode
);
213 /* Run callback if appropriate */
214 if (slot
->callback_func
!= NULL
)
215 slot
->callback_func(slot
->callback_data
);
218 static void xmulti_remove_handle(struct active_request_slot
*slot
)
220 curl_multi_remove_handle(curlm
, slot
->curl
);
223 static void process_curl_messages(void)
226 struct active_request_slot
*slot
;
227 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
229 while (curl_message
!= NULL
) {
230 if (curl_message
->msg
== CURLMSG_DONE
) {
231 int curl_result
= curl_message
->data
.result
;
232 slot
= active_queue_head
;
233 while (slot
!= NULL
&&
234 slot
->curl
!= curl_message
->easy_handle
)
237 xmulti_remove_handle(slot
);
238 slot
->curl_result
= curl_result
;
239 finish_active_slot(slot
);
241 fprintf(stderr
, "Received DONE message for unknown request!\n");
244 fprintf(stderr
, "Unknown CURL message received: %d\n",
245 (int)curl_message
->msg
);
247 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
251 static int http_options(const char *var
, const char *value
, void *cb
)
253 if (!strcmp("http.version", var
)) {
254 return git_config_string(&curl_http_version
, var
, value
);
256 if (!strcmp("http.sslverify", var
)) {
257 curl_ssl_verify
= git_config_bool(var
, value
);
260 if (!strcmp("http.sslcipherlist", var
))
261 return git_config_string(&ssl_cipherlist
, var
, value
);
262 if (!strcmp("http.sslversion", var
))
263 return git_config_string(&ssl_version
, var
, value
);
264 if (!strcmp("http.sslcert", var
))
265 return git_config_pathname(&ssl_cert
, var
, value
);
266 if (!strcmp("http.sslkey", var
))
267 return git_config_pathname(&ssl_key
, var
, value
);
268 if (!strcmp("http.sslcapath", var
))
269 return git_config_pathname(&ssl_capath
, var
, value
);
270 if (!strcmp("http.sslcainfo", var
))
271 return git_config_pathname(&ssl_cainfo
, var
, value
);
272 if (!strcmp("http.sslcertpasswordprotected", var
)) {
273 ssl_cert_password_required
= git_config_bool(var
, value
);
276 if (!strcmp("http.ssltry", var
)) {
277 curl_ssl_try
= git_config_bool(var
, value
);
280 if (!strcmp("http.sslbackend", var
)) {
281 free(http_ssl_backend
);
282 http_ssl_backend
= xstrdup_or_null(value
);
286 if (!strcmp("http.schannelcheckrevoke", var
)) {
287 http_schannel_check_revoke
= git_config_bool(var
, value
);
291 if (!strcmp("http.schannelusesslcainfo", var
)) {
292 http_schannel_use_ssl_cainfo
= git_config_bool(var
, value
);
296 if (!strcmp("http.minsessions", var
)) {
297 min_curl_sessions
= git_config_int(var
, value
);
298 if (min_curl_sessions
> 1)
299 min_curl_sessions
= 1;
302 if (!strcmp("http.maxrequests", var
)) {
303 max_requests
= git_config_int(var
, value
);
306 if (!strcmp("http.lowspeedlimit", var
)) {
307 curl_low_speed_limit
= (long)git_config_int(var
, value
);
310 if (!strcmp("http.lowspeedtime", var
)) {
311 curl_low_speed_time
= (long)git_config_int(var
, value
);
315 if (!strcmp("http.noepsv", var
)) {
316 curl_ftp_no_epsv
= git_config_bool(var
, value
);
319 if (!strcmp("http.proxy", var
))
320 return git_config_string(&curl_http_proxy
, var
, value
);
322 if (!strcmp("http.proxyauthmethod", var
))
323 return git_config_string(&http_proxy_authmethod
, var
, value
);
325 if (!strcmp("http.proxysslcert", var
))
326 return git_config_string(&http_proxy_ssl_cert
, var
, value
);
328 if (!strcmp("http.proxysslkey", var
))
329 return git_config_string(&http_proxy_ssl_key
, var
, value
);
331 if (!strcmp("http.proxysslcainfo", var
))
332 return git_config_string(&http_proxy_ssl_ca_info
, var
, value
);
334 if (!strcmp("http.proxysslcertpasswordprotected", var
)) {
335 proxy_ssl_cert_password_required
= git_config_bool(var
, value
);
339 if (!strcmp("http.cookiefile", var
))
340 return git_config_pathname(&curl_cookie_file
, var
, value
);
341 if (!strcmp("http.savecookies", var
)) {
342 curl_save_cookies
= git_config_bool(var
, value
);
346 if (!strcmp("http.postbuffer", var
)) {
347 http_post_buffer
= git_config_ssize_t(var
, value
);
348 if (http_post_buffer
< 0)
349 warning(_("negative value for http.postbuffer; defaulting to %d"), LARGE_PACKET_MAX
);
350 if (http_post_buffer
< LARGE_PACKET_MAX
)
351 http_post_buffer
= LARGE_PACKET_MAX
;
355 if (!strcmp("http.useragent", var
))
356 return git_config_string(&user_agent
, var
, value
);
358 if (!strcmp("http.emptyauth", var
)) {
359 if (value
&& !strcmp("auto", value
))
360 curl_empty_auth
= -1;
362 curl_empty_auth
= git_config_bool(var
, value
);
366 if (!strcmp("http.delegation", var
)) {
367 #ifdef CURLGSSAPI_DELEGATION_FLAG
368 return git_config_string(&curl_deleg
, var
, value
);
370 warning(_("Delegation control is not supported with cURL < 7.22.0"));
375 if (!strcmp("http.pinnedpubkey", var
)) {
376 #if LIBCURL_VERSION_NUM >= 0x072700
377 return git_config_pathname(&ssl_pinnedkey
, var
, value
);
379 warning(_("Public key pinning not supported with cURL < 7.39.0"));
384 if (!strcmp("http.extraheader", var
)) {
386 return config_error_nonbool(var
);
387 } else if (!*value
) {
388 string_list_clear(&extra_http_headers
, 0);
390 string_list_append(&extra_http_headers
, value
);
395 if (!strcmp("http.followredirects", var
)) {
396 if (value
&& !strcmp(value
, "initial"))
397 http_follow_config
= HTTP_FOLLOW_INITIAL
;
398 else if (git_config_bool(var
, value
))
399 http_follow_config
= HTTP_FOLLOW_ALWAYS
;
401 http_follow_config
= HTTP_FOLLOW_NONE
;
405 /* Fall back on the default ones */
406 return git_default_config(var
, value
, cb
);
409 static int curl_empty_auth_enabled(void)
411 if (curl_empty_auth
>= 0)
412 return curl_empty_auth
;
415 * In the automatic case, kick in the empty-auth
416 * hack as long as we would potentially try some
417 * method more exotic than "Basic" or "Digest".
419 * But only do this when this is our second or
420 * subsequent request, as by then we know what
421 * methods are available.
423 if (http_auth_methods_restricted
&&
424 (http_auth_methods
& ~empty_auth_useless
))
429 static void init_curl_http_auth(CURL
*result
)
431 if (!http_auth
.username
|| !*http_auth
.username
) {
432 if (curl_empty_auth_enabled())
433 curl_easy_setopt(result
, CURLOPT_USERPWD
, ":");
437 credential_fill(&http_auth
);
439 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
440 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
443 /* *var must be free-able */
444 static void var_override(const char **var
, char *value
)
448 *var
= xstrdup(value
);
452 static void set_proxyauth_name_password(CURL
*result
)
454 curl_easy_setopt(result
, CURLOPT_PROXYUSERNAME
,
455 proxy_auth
.username
);
456 curl_easy_setopt(result
, CURLOPT_PROXYPASSWORD
,
457 proxy_auth
.password
);
460 static void init_curl_proxy_auth(CURL
*result
)
462 if (proxy_auth
.username
) {
463 if (!proxy_auth
.password
)
464 credential_fill(&proxy_auth
);
465 set_proxyauth_name_password(result
);
468 var_override(&http_proxy_authmethod
, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
470 if (http_proxy_authmethod
) {
472 for (i
= 0; i
< ARRAY_SIZE(proxy_authmethods
); i
++) {
473 if (!strcmp(http_proxy_authmethod
, proxy_authmethods
[i
].name
)) {
474 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
,
475 proxy_authmethods
[i
].curlauth_param
);
479 if (i
== ARRAY_SIZE(proxy_authmethods
)) {
480 warning("unsupported proxy authentication method %s: using anyauth",
481 http_proxy_authmethod
);
482 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
486 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
489 static int has_cert_password(void)
491 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
493 if (!cert_auth
.password
) {
494 cert_auth
.protocol
= xstrdup("cert");
495 cert_auth
.host
= xstrdup("");
496 cert_auth
.username
= xstrdup("");
497 cert_auth
.path
= xstrdup(ssl_cert
);
498 credential_fill(&cert_auth
);
503 #if LIBCURL_VERSION_NUM >= 0x073400
504 static int has_proxy_cert_password(void)
506 if (http_proxy_ssl_cert
== NULL
|| proxy_ssl_cert_password_required
!= 1)
508 if (!proxy_cert_auth
.password
) {
509 proxy_cert_auth
.protocol
= xstrdup("cert");
510 proxy_cert_auth
.host
= xstrdup("");
511 proxy_cert_auth
.username
= xstrdup("");
512 proxy_cert_auth
.path
= xstrdup(http_proxy_ssl_cert
);
513 credential_fill(&proxy_cert_auth
);
519 #if LIBCURL_VERSION_NUM >= 0x071900
520 static void set_curl_keepalive(CURL
*c
)
522 curl_easy_setopt(c
, CURLOPT_TCP_KEEPALIVE
, 1);
526 static int sockopt_callback(void *client
, curl_socket_t fd
, curlsocktype type
)
530 socklen_t len
= (socklen_t
)sizeof(ka
);
532 if (type
!= CURLSOCKTYPE_IPCXN
)
535 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
537 warning_errno("unable to set SO_KEEPALIVE on socket");
539 return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
542 static void set_curl_keepalive(CURL
*c
)
544 curl_easy_setopt(c
, CURLOPT_SOCKOPTFUNCTION
, sockopt_callback
);
548 static void redact_sensitive_header(struct strbuf
*header
)
550 const char *sensitive_header
;
552 if (trace_curl_redact
&&
553 (skip_prefix(header
->buf
, "Authorization:", &sensitive_header
) ||
554 skip_prefix(header
->buf
, "Proxy-Authorization:", &sensitive_header
))) {
555 /* The first token is the type, which is OK to log */
556 while (isspace(*sensitive_header
))
558 while (*sensitive_header
&& !isspace(*sensitive_header
))
560 /* Everything else is opaque and possibly sensitive */
561 strbuf_setlen(header
, sensitive_header
- header
->buf
);
562 strbuf_addstr(header
, " <redacted>");
563 } else if (trace_curl_redact
&&
564 skip_prefix(header
->buf
, "Cookie:", &sensitive_header
)) {
565 struct strbuf redacted_header
= STRBUF_INIT
;
568 while (isspace(*sensitive_header
))
571 cookie
= sensitive_header
;
575 char *semicolon
= strstr(cookie
, "; ");
578 equals
= strchrnul(cookie
, '=');
580 /* invalid cookie, just append and continue */
581 strbuf_addstr(&redacted_header
, cookie
);
584 strbuf_add(&redacted_header
, cookie
, equals
- cookie
);
585 strbuf_addstr(&redacted_header
, "=<redacted>");
588 * There are more cookies. (Or, for some
589 * reason, the input string ends in "; ".)
591 strbuf_addstr(&redacted_header
, "; ");
592 cookie
= semicolon
+ strlen("; ");
598 strbuf_setlen(header
, sensitive_header
- header
->buf
);
599 strbuf_addbuf(header
, &redacted_header
);
603 static void curl_dump_header(const char *text
, unsigned char *ptr
, size_t size
, int hide_sensitive_header
)
605 struct strbuf out
= STRBUF_INIT
;
606 struct strbuf
**headers
, **header
;
608 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
609 text
, (long)size
, (long)size
);
610 trace_strbuf(&trace_curl
, &out
);
612 strbuf_add(&out
, ptr
, size
);
613 headers
= strbuf_split_max(&out
, '\n', 0);
615 for (header
= headers
; *header
; header
++) {
616 if (hide_sensitive_header
)
617 redact_sensitive_header(*header
);
618 strbuf_insertstr((*header
), 0, text
);
619 strbuf_insertstr((*header
), strlen(text
), ": ");
620 strbuf_rtrim((*header
));
621 strbuf_addch((*header
), '\n');
622 trace_strbuf(&trace_curl
, (*header
));
624 strbuf_list_free(headers
);
625 strbuf_release(&out
);
628 static void curl_dump_data(const char *text
, unsigned char *ptr
, size_t size
)
631 struct strbuf out
= STRBUF_INIT
;
632 unsigned int width
= 60;
634 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
635 text
, (long)size
, (long)size
);
636 trace_strbuf(&trace_curl
, &out
);
638 for (i
= 0; i
< size
; i
+= width
) {
642 strbuf_addf(&out
, "%s: ", text
);
643 for (w
= 0; (w
< width
) && (i
+ w
< size
); w
++) {
644 unsigned char ch
= ptr
[i
+ w
];
647 (ch
>= 0x20) && (ch
< 0x80)
650 strbuf_addch(&out
, '\n');
651 trace_strbuf(&trace_curl
, &out
);
653 strbuf_release(&out
);
656 static int curl_trace(CURL
*handle
, curl_infotype type
, char *data
, size_t size
, void *userp
)
659 enum { NO_FILTER
= 0, DO_FILTER
= 1 };
663 trace_printf_key(&trace_curl
, "== Info: %s", data
);
665 case CURLINFO_HEADER_OUT
:
666 text
= "=> Send header";
667 curl_dump_header(text
, (unsigned char *)data
, size
, DO_FILTER
);
669 case CURLINFO_DATA_OUT
:
670 if (trace_curl_data
) {
671 text
= "=> Send data";
672 curl_dump_data(text
, (unsigned char *)data
, size
);
675 case CURLINFO_SSL_DATA_OUT
:
676 if (trace_curl_data
) {
677 text
= "=> Send SSL data";
678 curl_dump_data(text
, (unsigned char *)data
, size
);
681 case CURLINFO_HEADER_IN
:
682 text
= "<= Recv header";
683 curl_dump_header(text
, (unsigned char *)data
, size
, NO_FILTER
);
685 case CURLINFO_DATA_IN
:
686 if (trace_curl_data
) {
687 text
= "<= Recv data";
688 curl_dump_data(text
, (unsigned char *)data
, size
);
691 case CURLINFO_SSL_DATA_IN
:
692 if (trace_curl_data
) {
693 text
= "<= Recv SSL data";
694 curl_dump_data(text
, (unsigned char *)data
, size
);
698 default: /* we ignore unknown types by default */
704 void http_trace_curl_no_data(void)
706 trace_override_envvar(&trace_curl
, "1");
710 void setup_curl_trace(CURL
*handle
)
712 if (!trace_want(&trace_curl
))
714 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
715 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, curl_trace
);
716 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, NULL
);
719 static long get_curl_allowed_protocols(int from_user
)
721 long allowed_protocols
= 0;
723 if (is_transport_allowed("http", from_user
))
724 allowed_protocols
|= CURLPROTO_HTTP
;
725 if (is_transport_allowed("https", from_user
))
726 allowed_protocols
|= CURLPROTO_HTTPS
;
727 if (is_transport_allowed("ftp", from_user
))
728 allowed_protocols
|= CURLPROTO_FTP
;
729 if (is_transport_allowed("ftps", from_user
))
730 allowed_protocols
|= CURLPROTO_FTPS
;
732 return allowed_protocols
;
735 #if LIBCURL_VERSION_NUM >=0x072b00
736 static int get_curl_http_version_opt(const char *version_string
, long *opt
)
743 { "HTTP/1.1", CURL_HTTP_VERSION_1_1
},
744 { "HTTP/2", CURL_HTTP_VERSION_2
}
747 for (i
= 0; i
< ARRAY_SIZE(choice
); i
++) {
748 if (!strcmp(version_string
, choice
[i
].name
)) {
749 *opt
= choice
[i
].opt_token
;
754 warning("unknown value given to http.version: '%s'", version_string
);
755 return -1; /* not found */
760 static CURL
*get_curl_handle(void)
762 CURL
*result
= curl_easy_init();
765 die("curl_easy_init failed");
767 if (!curl_ssl_verify
) {
768 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
769 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
771 /* Verify authenticity of the peer's certificate */
772 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
773 /* The name in the cert must match whom we tried to connect */
774 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
777 #if LIBCURL_VERSION_NUM >= 0x072b00
778 if (curl_http_version
) {
780 if (!get_curl_http_version_opt(curl_http_version
, &opt
)) {
781 /* Set request use http version */
782 curl_easy_setopt(result
, CURLOPT_HTTP_VERSION
, opt
);
787 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
788 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
790 #ifdef CURLGSSAPI_DELEGATION_FLAG
793 for (i
= 0; i
< ARRAY_SIZE(curl_deleg_levels
); i
++) {
794 if (!strcmp(curl_deleg
, curl_deleg_levels
[i
].name
)) {
795 curl_easy_setopt(result
, CURLOPT_GSSAPI_DELEGATION
,
796 curl_deleg_levels
[i
].curl_deleg_param
);
800 if (i
== ARRAY_SIZE(curl_deleg_levels
))
801 warning("Unknown delegation method '%s': using default",
806 if (http_ssl_backend
&& !strcmp("schannel", http_ssl_backend
) &&
807 !http_schannel_check_revoke
) {
808 #if LIBCURL_VERSION_NUM >= 0x072c00
809 curl_easy_setopt(result
, CURLOPT_SSL_OPTIONS
, CURLSSLOPT_NO_REVOKE
);
811 warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
815 if (http_proactive_auth
)
816 init_curl_http_auth(result
);
818 if (getenv("GIT_SSL_VERSION"))
819 ssl_version
= getenv("GIT_SSL_VERSION");
820 if (ssl_version
&& *ssl_version
) {
822 for (i
= 0; i
< ARRAY_SIZE(sslversions
); i
++) {
823 if (!strcmp(ssl_version
, sslversions
[i
].name
)) {
824 curl_easy_setopt(result
, CURLOPT_SSLVERSION
,
825 sslversions
[i
].ssl_version
);
829 if (i
== ARRAY_SIZE(sslversions
))
830 warning("unsupported ssl version %s: using default",
834 if (getenv("GIT_SSL_CIPHER_LIST"))
835 ssl_cipherlist
= getenv("GIT_SSL_CIPHER_LIST");
836 if (ssl_cipherlist
!= NULL
&& *ssl_cipherlist
)
837 curl_easy_setopt(result
, CURLOPT_SSL_CIPHER_LIST
,
840 if (ssl_cert
!= NULL
)
841 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
842 if (has_cert_password())
843 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
845 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
846 if (ssl_capath
!= NULL
)
847 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
848 #if LIBCURL_VERSION_NUM >= 0x072700
849 if (ssl_pinnedkey
!= NULL
)
850 curl_easy_setopt(result
, CURLOPT_PINNEDPUBLICKEY
, ssl_pinnedkey
);
852 if (http_ssl_backend
&& !strcmp("schannel", http_ssl_backend
) &&
853 !http_schannel_use_ssl_cainfo
) {
854 curl_easy_setopt(result
, CURLOPT_CAINFO
, NULL
);
855 #if LIBCURL_VERSION_NUM >= 0x073400
856 curl_easy_setopt(result
, CURLOPT_PROXY_CAINFO
, NULL
);
858 } else if (ssl_cainfo
!= NULL
|| http_proxy_ssl_ca_info
!= NULL
) {
859 if (ssl_cainfo
!= NULL
)
860 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
861 #if LIBCURL_VERSION_NUM >= 0x073400
862 if (http_proxy_ssl_ca_info
!= NULL
)
863 curl_easy_setopt(result
, CURLOPT_PROXY_CAINFO
, http_proxy_ssl_ca_info
);
867 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
868 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
869 curl_low_speed_limit
);
870 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
871 curl_low_speed_time
);
874 curl_easy_setopt(result
, CURLOPT_MAXREDIRS
, 20);
875 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
876 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS
,
877 get_curl_allowed_protocols(0));
878 curl_easy_setopt(result
, CURLOPT_PROTOCOLS
,
879 get_curl_allowed_protocols(-1));
880 if (getenv("GIT_CURL_VERBOSE"))
881 http_trace_curl_no_data();
882 setup_curl_trace(result
);
883 if (getenv("GIT_TRACE_CURL_NO_DATA"))
885 if (!git_env_bool("GIT_TRACE_REDACT", 1))
886 trace_curl_redact
= 0;
888 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
889 user_agent
? user_agent
: git_user_agent());
891 if (curl_ftp_no_epsv
)
892 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
895 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
898 * CURL also examines these variables as a fallback; but we need to query
899 * them here in order to decide whether to prompt for missing password (cf.
900 * init_curl_proxy_auth()).
902 * Unlike many other common environment variables, these are historically
903 * lowercase only. It appears that CURL did not know this and implemented
904 * only uppercase variants, which was later corrected to take both - with
905 * the exception of http_proxy, which is lowercase only also in CURL. As
906 * the lowercase versions are the historical quasi-standard, they take
907 * precedence here, as in CURL.
909 if (!curl_http_proxy
) {
910 if (http_auth
.protocol
&& !strcmp(http_auth
.protocol
, "https")) {
911 var_override(&curl_http_proxy
, getenv("HTTPS_PROXY"));
912 var_override(&curl_http_proxy
, getenv("https_proxy"));
914 var_override(&curl_http_proxy
, getenv("http_proxy"));
916 if (!curl_http_proxy
) {
917 var_override(&curl_http_proxy
, getenv("ALL_PROXY"));
918 var_override(&curl_http_proxy
, getenv("all_proxy"));
922 if (curl_http_proxy
&& curl_http_proxy
[0] == '\0') {
924 * Handle case with the empty http.proxy value here to keep
926 * NB: empty option disables proxying at all.
928 curl_easy_setopt(result
, CURLOPT_PROXY
, "");
929 } else if (curl_http_proxy
) {
930 if (starts_with(curl_http_proxy
, "socks5h"))
931 curl_easy_setopt(result
,
932 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5_HOSTNAME
);
933 else if (starts_with(curl_http_proxy
, "socks5"))
934 curl_easy_setopt(result
,
935 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
936 else if (starts_with(curl_http_proxy
, "socks4a"))
937 curl_easy_setopt(result
,
938 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4A
);
939 else if (starts_with(curl_http_proxy
, "socks"))
940 curl_easy_setopt(result
,
941 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4
);
942 #if LIBCURL_VERSION_NUM >= 0x073400
943 else if (starts_with(curl_http_proxy
, "https")) {
944 curl_easy_setopt(result
, CURLOPT_PROXYTYPE
, CURLPROXY_HTTPS
);
946 if (http_proxy_ssl_cert
)
947 curl_easy_setopt(result
, CURLOPT_PROXY_SSLCERT
, http_proxy_ssl_cert
);
949 if (http_proxy_ssl_key
)
950 curl_easy_setopt(result
, CURLOPT_PROXY_SSLKEY
, http_proxy_ssl_key
);
952 if (has_proxy_cert_password())
953 curl_easy_setopt(result
, CURLOPT_PROXY_KEYPASSWD
, proxy_cert_auth
.password
);
956 if (strstr(curl_http_proxy
, "://"))
957 credential_from_url(&proxy_auth
, curl_http_proxy
);
959 struct strbuf url
= STRBUF_INIT
;
960 strbuf_addf(&url
, "http://%s", curl_http_proxy
);
961 credential_from_url(&proxy_auth
, url
.buf
);
962 strbuf_release(&url
);
965 if (!proxy_auth
.host
)
966 die("Invalid proxy URL '%s'", curl_http_proxy
);
968 curl_easy_setopt(result
, CURLOPT_PROXY
, proxy_auth
.host
);
969 var_override(&curl_no_proxy
, getenv("NO_PROXY"));
970 var_override(&curl_no_proxy
, getenv("no_proxy"));
971 curl_easy_setopt(result
, CURLOPT_NOPROXY
, curl_no_proxy
);
973 init_curl_proxy_auth(result
);
975 set_curl_keepalive(result
);
980 static void set_from_env(const char **var
, const char *envname
)
982 const char *val
= getenv(envname
);
987 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
989 char *low_speed_limit
;
990 char *low_speed_time
;
991 char *normalized_url
;
992 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
994 config
.section
= "http";
996 config
.collect_fn
= http_options
;
997 config
.cascade_fn
= git_default_config
;
1000 http_is_verbose
= 0;
1001 normalized_url
= url_normalize(url
, &config
.url
);
1003 git_config(urlmatch_config_entry
, &config
);
1004 free(normalized_url
);
1005 string_list_clear(&config
.vars
, 1);
1007 #if LIBCURL_VERSION_NUM >= 0x073800
1008 if (http_ssl_backend
) {
1009 const curl_ssl_backend
**backends
;
1010 struct strbuf buf
= STRBUF_INIT
;
1013 switch (curl_global_sslset(-1, http_ssl_backend
, &backends
)) {
1014 case CURLSSLSET_UNKNOWN_BACKEND
:
1015 strbuf_addf(&buf
, _("Unsupported SSL backend '%s'. "
1016 "Supported SSL backends:"),
1018 for (i
= 0; backends
[i
]; i
++)
1019 strbuf_addf(&buf
, "\n\t%s", backends
[i
]->name
);
1021 case CURLSSLSET_NO_BACKENDS
:
1022 die(_("Could not set SSL backend to '%s': "
1023 "cURL was built without SSL backends"),
1025 case CURLSSLSET_TOO_LATE
:
1026 die(_("Could not set SSL backend to '%s': already set"),
1034 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
1035 die("curl_global_init failed");
1037 http_proactive_auth
= proactive_auth
;
1039 if (remote
&& remote
->http_proxy
)
1040 curl_http_proxy
= xstrdup(remote
->http_proxy
);
1043 var_override(&http_proxy_authmethod
, remote
->http_proxy_authmethod
);
1045 pragma_header
= curl_slist_append(http_copy_default_headers(),
1046 "Pragma: no-cache");
1047 no_pragma_header
= curl_slist_append(http_copy_default_headers(),
1051 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1052 if (http_max_requests
!= NULL
)
1053 max_requests
= atoi(http_max_requests
);
1056 curlm
= curl_multi_init();
1058 die("curl_multi_init failed");
1060 if (getenv("GIT_SSL_NO_VERIFY"))
1061 curl_ssl_verify
= 0;
1063 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
1064 set_from_env(&ssl_key
, "GIT_SSL_KEY");
1065 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
1066 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
1068 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
1070 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1071 if (low_speed_limit
!= NULL
)
1072 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1073 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1074 if (low_speed_time
!= NULL
)
1075 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1077 if (curl_ssl_verify
== -1)
1078 curl_ssl_verify
= 1;
1080 curl_session_count
= 0;
1081 if (max_requests
< 1)
1082 max_requests
= DEFAULT_MAX_REQUESTS
;
1084 set_from_env(&http_proxy_ssl_cert
, "GIT_PROXY_SSL_CERT");
1085 set_from_env(&http_proxy_ssl_key
, "GIT_PROXY_SSL_KEY");
1086 set_from_env(&http_proxy_ssl_ca_info
, "GIT_PROXY_SSL_CAINFO");
1088 if (getenv("GIT_PROXY_SSL_CERT_PASSWORD_PROTECTED"))
1089 proxy_ssl_cert_password_required
= 1;
1091 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1092 curl_ftp_no_epsv
= 1;
1095 credential_from_url(&http_auth
, url
);
1096 if (!ssl_cert_password_required
&&
1097 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
1098 starts_with(url
, "https://"))
1099 ssl_cert_password_required
= 1;
1102 curl_default
= get_curl_handle();
1105 void http_cleanup(void)
1107 struct active_request_slot
*slot
= active_queue_head
;
1109 while (slot
!= NULL
) {
1110 struct active_request_slot
*next
= slot
->next
;
1111 if (slot
->curl
!= NULL
) {
1112 xmulti_remove_handle(slot
);
1113 curl_easy_cleanup(slot
->curl
);
1118 active_queue_head
= NULL
;
1120 curl_easy_cleanup(curl_default
);
1122 curl_multi_cleanup(curlm
);
1123 curl_global_cleanup();
1125 string_list_clear(&extra_http_headers
, 0);
1127 curl_slist_free_all(pragma_header
);
1128 pragma_header
= NULL
;
1130 curl_slist_free_all(no_pragma_header
);
1131 no_pragma_header
= NULL
;
1133 if (curl_http_proxy
) {
1134 free((void *)curl_http_proxy
);
1135 curl_http_proxy
= NULL
;
1138 if (proxy_auth
.password
) {
1139 memset(proxy_auth
.password
, 0, strlen(proxy_auth
.password
));
1140 FREE_AND_NULL(proxy_auth
.password
);
1143 free((void *)curl_proxyuserpwd
);
1144 curl_proxyuserpwd
= NULL
;
1146 free((void *)http_proxy_authmethod
);
1147 http_proxy_authmethod
= NULL
;
1149 if (cert_auth
.password
!= NULL
) {
1150 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
1151 FREE_AND_NULL(cert_auth
.password
);
1153 ssl_cert_password_required
= 0;
1155 if (proxy_cert_auth
.password
!= NULL
) {
1156 memset(proxy_cert_auth
.password
, 0, strlen(proxy_cert_auth
.password
));
1157 FREE_AND_NULL(proxy_cert_auth
.password
);
1159 proxy_ssl_cert_password_required
= 0;
1161 FREE_AND_NULL(cached_accept_language
);
1164 struct active_request_slot
*get_active_slot(void)
1166 struct active_request_slot
*slot
= active_queue_head
;
1167 struct active_request_slot
*newslot
;
1171 /* Wait for a slot to open up if the queue is full */
1172 while (active_requests
>= max_requests
) {
1173 curl_multi_perform(curlm
, &num_transfers
);
1174 if (num_transfers
< active_requests
)
1175 process_curl_messages();
1178 while (slot
!= NULL
&& slot
->in_use
)
1182 newslot
= xmalloc(sizeof(*newslot
));
1183 newslot
->curl
= NULL
;
1184 newslot
->in_use
= 0;
1185 newslot
->next
= NULL
;
1187 slot
= active_queue_head
;
1189 active_queue_head
= newslot
;
1191 while (slot
->next
!= NULL
)
1193 slot
->next
= newslot
;
1198 if (slot
->curl
== NULL
) {
1199 slot
->curl
= curl_easy_duphandle(curl_default
);
1200 curl_session_count
++;
1205 slot
->results
= NULL
;
1206 slot
->finished
= NULL
;
1207 slot
->callback_data
= NULL
;
1208 slot
->callback_func
= NULL
;
1209 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
1210 if (curl_save_cookies
)
1211 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
1212 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
1213 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
1214 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
1215 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
1216 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
1217 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
1218 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
1219 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1220 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
1221 curl_easy_setopt(slot
->curl
, CURLOPT_RANGE
, NULL
);
1224 * Default following to off unless "ALWAYS" is configured; this gives
1225 * callers a sane starting point, and they can tweak for individual
1226 * HTTP_FOLLOW_* cases themselves.
1228 if (http_follow_config
== HTTP_FOLLOW_ALWAYS
)
1229 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1231 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 0);
1233 curl_easy_setopt(slot
->curl
, CURLOPT_IPRESOLVE
, git_curl_ipresolve
);
1234 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPAUTH
, http_auth_methods
);
1235 if (http_auth
.password
|| curl_empty_auth_enabled())
1236 init_curl_http_auth(slot
->curl
);
1241 int start_active_slot(struct active_request_slot
*slot
)
1243 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
1246 if (curlm_result
!= CURLM_OK
&&
1247 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
1248 warning("curl_multi_add_handle failed: %s",
1249 curl_multi_strerror(curlm_result
));
1256 * We know there must be something to do, since we just added
1259 curl_multi_perform(curlm
, &num_transfers
);
1265 int (*fill
)(void *);
1266 struct fill_chain
*next
;
1269 static struct fill_chain
*fill_cfg
;
1271 void add_fill_function(void *data
, int (*fill
)(void *))
1273 struct fill_chain
*new_fill
= xmalloc(sizeof(*new_fill
));
1274 struct fill_chain
**linkp
= &fill_cfg
;
1275 new_fill
->data
= data
;
1276 new_fill
->fill
= fill
;
1277 new_fill
->next
= NULL
;
1279 linkp
= &(*linkp
)->next
;
1283 void fill_active_slots(void)
1285 struct active_request_slot
*slot
= active_queue_head
;
1287 while (active_requests
< max_requests
) {
1288 struct fill_chain
*fill
;
1289 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
1290 if (fill
->fill(fill
->data
))
1297 while (slot
!= NULL
) {
1298 if (!slot
->in_use
&& slot
->curl
!= NULL
1299 && curl_session_count
> min_curl_sessions
) {
1300 curl_easy_cleanup(slot
->curl
);
1302 curl_session_count
--;
1308 void step_active_slots(void)
1311 CURLMcode curlm_result
;
1314 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
1315 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
1316 if (num_transfers
< active_requests
) {
1317 process_curl_messages();
1318 fill_active_slots();
1322 void run_active_slot(struct active_request_slot
*slot
)
1328 struct timeval select_timeout
;
1331 slot
->finished
= &finished
;
1333 step_active_slots();
1337 curl_multi_timeout(curlm
, &curl_timeout
);
1338 if (curl_timeout
== 0) {
1340 } else if (curl_timeout
== -1) {
1341 select_timeout
.tv_sec
= 0;
1342 select_timeout
.tv_usec
= 50000;
1344 select_timeout
.tv_sec
= curl_timeout
/ 1000;
1345 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
1352 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
1355 * It can happen that curl_multi_timeout returns a pathologically
1356 * long timeout when curl_multi_fdset returns no file descriptors
1357 * to read. See commit message for more details.
1360 (select_timeout
.tv_sec
> 0 ||
1361 select_timeout
.tv_usec
> 50000)) {
1362 select_timeout
.tv_sec
= 0;
1363 select_timeout
.tv_usec
= 50000;
1366 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
1371 static void release_active_slot(struct active_request_slot
*slot
)
1373 closedown_active_slot(slot
);
1375 xmulti_remove_handle(slot
);
1376 if (curl_session_count
> min_curl_sessions
) {
1377 curl_easy_cleanup(slot
->curl
);
1379 curl_session_count
--;
1382 fill_active_slots();
1385 void finish_all_active_slots(void)
1387 struct active_request_slot
*slot
= active_queue_head
;
1389 while (slot
!= NULL
)
1391 run_active_slot(slot
);
1392 slot
= active_queue_head
;
1398 /* Helpers for modifying and creating URLs */
1399 static inline int needs_quote(int ch
)
1401 if (((ch
>= 'A') && (ch
<= 'Z'))
1402 || ((ch
>= 'a') && (ch
<= 'z'))
1403 || ((ch
>= '0') && (ch
<= '9'))
1411 static char *quote_ref_url(const char *base
, const char *ref
)
1413 struct strbuf buf
= STRBUF_INIT
;
1417 end_url_with_slash(&buf
, base
);
1419 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
1420 if (needs_quote(ch
))
1421 strbuf_addf(&buf
, "%%%02x", ch
);
1423 strbuf_addch(&buf
, *cp
);
1425 return strbuf_detach(&buf
, NULL
);
1428 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
1430 int only_two_digit_prefix
)
1432 end_url_with_slash(buf
, url
);
1434 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
1435 if (!only_two_digit_prefix
)
1436 strbuf_addstr(buf
, hex
+ 2);
1439 char *get_remote_object_url(const char *url
, const char *hex
,
1440 int only_two_digit_prefix
)
1442 struct strbuf buf
= STRBUF_INIT
;
1443 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
1444 return strbuf_detach(&buf
, NULL
);
1447 void normalize_curl_result(CURLcode
*result
, long http_code
,
1448 char *errorstr
, size_t errorlen
)
1451 * If we see a failing http code with CURLE_OK, we have turned off
1452 * FAILONERROR (to keep the server's custom error response), and should
1453 * translate the code into failure here.
1455 * Likewise, if we see a redirect (30x code), that means we turned off
1456 * redirect-following, and we should treat the result as an error.
1458 if (*result
== CURLE_OK
&& http_code
>= 300) {
1459 *result
= CURLE_HTTP_RETURNED_ERROR
;
1461 * Normally curl will already have put the "reason phrase"
1462 * from the server into curl_errorstr; unfortunately without
1463 * FAILONERROR it is lost, so we can give only the numeric
1466 xsnprintf(errorstr
, errorlen
,
1467 "The requested URL returned error: %ld",
1472 static int handle_curl_result(struct slot_results
*results
)
1474 normalize_curl_result(&results
->curl_result
, results
->http_code
,
1475 curl_errorstr
, sizeof(curl_errorstr
));
1477 if (results
->curl_result
== CURLE_OK
) {
1478 credential_approve(&http_auth
);
1479 credential_approve(&proxy_auth
);
1480 credential_approve(&cert_auth
);
1482 } else if (results
->curl_result
== CURLE_SSL_CERTPROBLEM
) {
1484 * We can't tell from here whether it's a bad path, bad
1485 * certificate, bad password, or something else wrong
1486 * with the certificate. So we reject the credential to
1487 * avoid caching or saving a bad password.
1489 credential_reject(&cert_auth
);
1491 } else if (missing_target(results
))
1492 return HTTP_MISSING_TARGET
;
1493 else if (results
->http_code
== 401) {
1494 if (http_auth
.username
&& http_auth
.password
) {
1495 credential_reject(&http_auth
);
1498 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
1499 if (results
->auth_avail
) {
1500 http_auth_methods
&= results
->auth_avail
;
1501 http_auth_methods_restricted
= 1;
1506 if (results
->http_connectcode
== 407)
1507 credential_reject(&proxy_auth
);
1508 if (!curl_errorstr
[0])
1509 strlcpy(curl_errorstr
,
1510 curl_easy_strerror(results
->curl_result
),
1511 sizeof(curl_errorstr
));
1516 int run_one_slot(struct active_request_slot
*slot
,
1517 struct slot_results
*results
)
1519 slot
->results
= results
;
1520 if (!start_active_slot(slot
)) {
1521 xsnprintf(curl_errorstr
, sizeof(curl_errorstr
),
1522 "failed to start HTTP request");
1523 return HTTP_START_FAILED
;
1526 run_active_slot(slot
);
1527 return handle_curl_result(results
);
1530 struct curl_slist
*http_copy_default_headers(void)
1532 struct curl_slist
*headers
= NULL
;
1533 const struct string_list_item
*item
;
1535 for_each_string_list_item(item
, &extra_http_headers
)
1536 headers
= curl_slist_append(headers
, item
->string
);
1541 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
1547 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
1549 strbuf_addstr(buf
, ptr
);
1554 * Check for and extract a content-type parameter. "raw"
1555 * should be positioned at the start of the potential
1556 * parameter, with any whitespace already removed.
1558 * "name" is the name of the parameter. The value is appended
1561 static int extract_param(const char *raw
, const char *name
,
1564 size_t len
= strlen(name
);
1566 if (strncasecmp(raw
, name
, len
))
1574 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
1575 strbuf_addch(out
, *raw
++);
1580 * Extract a normalized version of the content type, with any
1581 * spaces suppressed, all letters lowercased, and no trailing ";"
1584 * Note that we will silently remove even invalid whitespace. For
1585 * example, "text / plain" is specifically forbidden by RFC 2616,
1586 * but "text/plain" is the only reasonable output, and this keeps
1589 * If the "charset" argument is not NULL, store the value of any
1590 * charset parameter there.
1593 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1594 * "text / plain" -> "text/plain"
1596 static void extract_content_type(struct strbuf
*raw
, struct strbuf
*type
,
1597 struct strbuf
*charset
)
1602 strbuf_grow(type
, raw
->len
);
1603 for (p
= raw
->buf
; *p
; p
++) {
1610 strbuf_addch(type
, tolower(*p
));
1616 strbuf_reset(charset
);
1618 while (isspace(*p
) || *p
== ';')
1620 if (!extract_param(p
, "charset", charset
))
1622 while (*p
&& !isspace(*p
))
1626 if (!charset
->len
&& starts_with(type
->buf
, "text/"))
1627 strbuf_addstr(charset
, "ISO-8859-1");
1630 static void write_accept_language(struct strbuf
*buf
)
1633 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1634 * that, q-value will be smaller than 0.001, the minimum q-value the
1635 * HTTP specification allows. See
1636 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1638 const int MAX_DECIMAL_PLACES
= 3;
1639 const int MAX_LANGUAGE_TAGS
= 1000;
1640 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE
= 4000;
1641 char **language_tags
= NULL
;
1643 const char *s
= get_preferred_languages();
1645 struct strbuf tag
= STRBUF_INIT
;
1647 /* Don't add Accept-Language header if no language is preferred. */
1652 * Split the colon-separated string of preferred languages into
1653 * language_tags array.
1656 /* collect language tag */
1657 for (; *s
&& (isalnum(*s
) || *s
== '_'); s
++)
1658 strbuf_addch(&tag
, *s
== '_' ? '-' : *s
);
1660 /* skip .codeset, @modifier and any other unnecessary parts */
1661 while (*s
&& *s
!= ':')
1666 REALLOC_ARRAY(language_tags
, num_langs
);
1667 language_tags
[num_langs
- 1] = strbuf_detach(&tag
, NULL
);
1668 if (num_langs
>= MAX_LANGUAGE_TAGS
- 1) /* -1 for '*' */
1673 /* write Accept-Language header into buf */
1675 int last_buf_len
= 0;
1681 REALLOC_ARRAY(language_tags
, num_langs
+ 1);
1682 language_tags
[num_langs
++] = "*"; /* it's OK; this won't be freed */
1684 /* compute decimal_places */
1685 for (max_q
= 1, decimal_places
= 0;
1686 max_q
< num_langs
&& decimal_places
<= MAX_DECIMAL_PLACES
;
1687 decimal_places
++, max_q
*= 10)
1690 xsnprintf(q_format
, sizeof(q_format
), ";q=0.%%0%dd", decimal_places
);
1692 strbuf_addstr(buf
, "Accept-Language: ");
1694 for (i
= 0; i
< num_langs
; i
++) {
1696 strbuf_addstr(buf
, ", ");
1698 strbuf_addstr(buf
, language_tags
[i
]);
1701 strbuf_addf(buf
, q_format
, max_q
- i
);
1703 if (buf
->len
> MAX_ACCEPT_LANGUAGE_HEADER_SIZE
) {
1704 strbuf_remove(buf
, last_buf_len
, buf
->len
- last_buf_len
);
1708 last_buf_len
= buf
->len
;
1712 /* free language tags -- last one is a static '*' */
1713 for (i
= 0; i
< num_langs
- 1; i
++)
1714 free(language_tags
[i
]);
1715 free(language_tags
);
1719 * Get an Accept-Language header which indicates user's preferred languages.
1723 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1724 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1725 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1726 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1727 * LANGUAGE= LANG=C -> ""
1729 static const char *get_accept_language(void)
1731 if (!cached_accept_language
) {
1732 struct strbuf buf
= STRBUF_INIT
;
1733 write_accept_language(&buf
);
1735 cached_accept_language
= strbuf_detach(&buf
, NULL
);
1738 return cached_accept_language
;
1741 static void http_opt_request_remainder(CURL
*curl
, off_t pos
)
1744 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
"-", (uintmax_t)pos
);
1745 curl_easy_setopt(curl
, CURLOPT_RANGE
, buf
);
1748 /* http_request() targets */
1749 #define HTTP_REQUEST_STRBUF 0
1750 #define HTTP_REQUEST_FILE 1
1752 static int http_request(const char *url
,
1753 void *result
, int target
,
1754 const struct http_get_options
*options
)
1756 struct active_request_slot
*slot
;
1757 struct slot_results results
;
1758 struct curl_slist
*headers
= http_copy_default_headers();
1759 struct strbuf buf
= STRBUF_INIT
;
1760 const char *accept_language
;
1763 slot
= get_active_slot();
1764 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1766 if (result
== NULL
) {
1767 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
1769 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1770 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEDATA
, result
);
1772 if (target
== HTTP_REQUEST_FILE
) {
1773 off_t posn
= ftello(result
);
1774 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1777 http_opt_request_remainder(slot
->curl
, posn
);
1779 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1783 accept_language
= get_accept_language();
1785 if (accept_language
)
1786 headers
= curl_slist_append(headers
, accept_language
);
1788 strbuf_addstr(&buf
, "Pragma:");
1789 if (options
&& options
->no_cache
)
1790 strbuf_addstr(&buf
, " no-cache");
1791 if (options
&& options
->initial_request
&&
1792 http_follow_config
== HTTP_FOLLOW_INITIAL
)
1793 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1795 headers
= curl_slist_append(headers
, buf
.buf
);
1797 /* Add additional headers here */
1798 if (options
&& options
->extra_headers
) {
1799 const struct string_list_item
*item
;
1800 for_each_string_list_item(item
, options
->extra_headers
) {
1801 headers
= curl_slist_append(headers
, item
->string
);
1805 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1806 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1807 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "");
1808 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1810 ret
= run_one_slot(slot
, &results
);
1812 if (options
&& options
->content_type
) {
1813 struct strbuf raw
= STRBUF_INIT
;
1814 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
, &raw
);
1815 extract_content_type(&raw
, options
->content_type
,
1817 strbuf_release(&raw
);
1820 if (options
&& options
->effective_url
)
1821 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
1822 options
->effective_url
);
1824 curl_slist_free_all(headers
);
1825 strbuf_release(&buf
);
1831 * Update the "base" url to a more appropriate value, as deduced by
1832 * redirects seen when requesting a URL starting with "url".
1834 * The "asked" parameter is a URL that we asked curl to access, and must begin
1837 * The "got" parameter is the URL that curl reported to us as where we ended
1840 * Returns 1 if we updated the base url, 0 otherwise.
1842 * Our basic strategy is to compare "base" and "asked" to find the bits
1843 * specific to our request. We then strip those bits off of "got" to yield the
1844 * new base. So for example, if our base is "http://example.com/foo.git",
1845 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1846 * with "https://other.example.com/foo.git/info/refs". We would want the
1847 * new URL to become "https://other.example.com/foo.git".
1849 * Note that this assumes a sane redirect scheme. It's entirely possible
1850 * in the example above to end up at a URL that does not even end in
1851 * "info/refs". In such a case we die. There's not much we can do, such a
1852 * scheme is unlikely to represent a real git repository, and failing to
1853 * rewrite the base opens options for malicious redirects to do funny things.
1855 static int update_url_from_redirect(struct strbuf
*base
,
1857 const struct strbuf
*got
)
1862 if (!strcmp(asked
, got
->buf
))
1865 if (!skip_prefix(asked
, base
->buf
, &tail
))
1866 BUG("update_url_from_redirect: %s is not a superset of %s",
1870 if (!strip_suffix_mem(got
->buf
, &new_len
, tail
))
1871 die(_("unable to update url base from redirection:\n"
1877 strbuf_add(base
, got
->buf
, new_len
);
1882 static int http_request_reauth(const char *url
,
1883 void *result
, int target
,
1884 struct http_get_options
*options
)
1886 int ret
= http_request(url
, result
, target
, options
);
1888 if (ret
!= HTTP_OK
&& ret
!= HTTP_REAUTH
)
1891 if (options
&& options
->effective_url
&& options
->base_url
) {
1892 if (update_url_from_redirect(options
->base_url
,
1893 url
, options
->effective_url
)) {
1894 credential_from_url(&http_auth
, options
->base_url
->buf
);
1895 url
= options
->effective_url
->buf
;
1899 if (ret
!= HTTP_REAUTH
)
1903 * The previous request may have put cruft into our output stream; we
1904 * should clear it out before making our next request.
1907 case HTTP_REQUEST_STRBUF
:
1908 strbuf_reset(result
);
1910 case HTTP_REQUEST_FILE
:
1911 if (fflush(result
)) {
1912 error_errno("unable to flush a file");
1913 return HTTP_START_FAILED
;
1916 if (ftruncate(fileno(result
), 0) < 0) {
1917 error_errno("unable to truncate a file");
1918 return HTTP_START_FAILED
;
1922 BUG("Unknown http_request target");
1925 credential_fill(&http_auth
);
1927 return http_request(url
, result
, target
, options
);
1930 int http_get_strbuf(const char *url
,
1931 struct strbuf
*result
,
1932 struct http_get_options
*options
)
1934 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
1938 * Downloads a URL and stores the result in the given file.
1940 * If a previous interrupted download is detected (i.e. a previous temporary
1941 * file is still around) the download is resumed.
1943 static int http_get_file(const char *url
, const char *filename
,
1944 struct http_get_options
*options
)
1947 struct strbuf tmpfile
= STRBUF_INIT
;
1950 strbuf_addf(&tmpfile
, "%s.temp", filename
);
1951 result
= fopen(tmpfile
.buf
, "a");
1953 error("Unable to open local file %s", tmpfile
.buf
);
1958 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
1961 if (ret
== HTTP_OK
&& finalize_object_file(tmpfile
.buf
, filename
))
1964 strbuf_release(&tmpfile
);
1968 int http_fetch_ref(const char *base
, struct ref
*ref
)
1970 struct http_get_options options
= {0};
1972 struct strbuf buffer
= STRBUF_INIT
;
1975 options
.no_cache
= 1;
1977 url
= quote_ref_url(base
, ref
->name
);
1978 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
1979 strbuf_rtrim(&buffer
);
1980 if (buffer
.len
== the_hash_algo
->hexsz
)
1981 ret
= get_oid_hex(buffer
.buf
, &ref
->old_oid
);
1982 else if (starts_with(buffer
.buf
, "ref: ")) {
1983 ref
->symref
= xstrdup(buffer
.buf
+ 5);
1988 strbuf_release(&buffer
);
1993 /* Helpers for fetching packs */
1994 static char *fetch_pack_index(unsigned char *hash
, const char *base_url
)
1997 struct strbuf buf
= STRBUF_INIT
;
1999 if (http_is_verbose
)
2000 fprintf(stderr
, "Getting index for pack %s\n", hash_to_hex(hash
));
2002 end_url_with_slash(&buf
, base_url
);
2003 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", hash_to_hex(hash
));
2004 url
= strbuf_detach(&buf
, NULL
);
2006 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(hash
));
2007 tmp
= strbuf_detach(&buf
, NULL
);
2009 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
2010 error("Unable to get pack index %s", url
);
2018 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
2019 unsigned char *sha1
, const char *base_url
)
2021 struct packed_git
*new_pack
;
2022 char *tmp_idx
= NULL
;
2025 if (has_pack_index(sha1
)) {
2026 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
2028 return -1; /* parse_pack_index() already issued error message */
2032 tmp_idx
= fetch_pack_index(sha1
, base_url
);
2036 new_pack
= parse_pack_index(sha1
, tmp_idx
);
2041 return -1; /* parse_pack_index() already issued error message */
2044 ret
= verify_pack_index(new_pack
);
2046 close_pack_index(new_pack
);
2047 ret
= finalize_object_file(tmp_idx
, sha1_pack_index_name(sha1
));
2054 new_pack
->next
= *packs_head
;
2055 *packs_head
= new_pack
;
2059 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
2061 struct http_get_options options
= {0};
2065 struct strbuf buf
= STRBUF_INIT
;
2066 struct object_id oid
;
2068 end_url_with_slash(&buf
, base_url
);
2069 strbuf_addstr(&buf
, "objects/info/packs");
2070 url
= strbuf_detach(&buf
, NULL
);
2072 options
.no_cache
= 1;
2073 ret
= http_get_strbuf(url
, &buf
, &options
);
2079 if (skip_prefix(data
, "P pack-", &data
) &&
2080 !parse_oid_hex(data
, &oid
, &data
) &&
2081 skip_prefix(data
, ".pack", &data
) &&
2082 (*data
== '\n' || *data
== '\0')) {
2083 fetch_and_setup_pack_index(packs_head
, oid
.hash
, base_url
);
2085 data
= strchrnul(data
, '\n');
2088 data
++; /* skip past newline */
2096 void release_http_pack_request(struct http_pack_request
*preq
)
2098 if (preq
->packfile
!= NULL
) {
2099 fclose(preq
->packfile
);
2100 preq
->packfile
= NULL
;
2103 strbuf_release(&preq
->tmpfile
);
2108 static const char *default_index_pack_args
[] =
2109 {"index-pack", "--stdin", NULL
};
2111 int finish_http_pack_request(struct http_pack_request
*preq
)
2113 struct child_process ip
= CHILD_PROCESS_INIT
;
2117 fclose(preq
->packfile
);
2118 preq
->packfile
= NULL
;
2120 tmpfile_fd
= xopen(preq
->tmpfile
.buf
, O_RDONLY
);
2124 ip
.argv
= preq
->index_pack_args
? preq
->index_pack_args
2125 : default_index_pack_args
;
2127 if (preq
->preserve_index_pack_stdout
)
2132 if (run_command(&ip
)) {
2139 unlink(preq
->tmpfile
.buf
);
2143 void http_install_packfile(struct packed_git
*p
,
2144 struct packed_git
**list_to_remove_from
)
2146 struct packed_git
**lst
= list_to_remove_from
;
2149 lst
= &((*lst
)->next
);
2150 *lst
= (*lst
)->next
;
2152 install_packed_git(the_repository
, p
);
2155 struct http_pack_request
*new_http_pack_request(
2156 const unsigned char *packed_git_hash
, const char *base_url
) {
2158 struct strbuf buf
= STRBUF_INIT
;
2160 end_url_with_slash(&buf
, base_url
);
2161 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
2162 hash_to_hex(packed_git_hash
));
2163 return new_direct_http_pack_request(packed_git_hash
,
2164 strbuf_detach(&buf
, NULL
));
2167 struct http_pack_request
*new_direct_http_pack_request(
2168 const unsigned char *packed_git_hash
, char *url
)
2170 off_t prev_posn
= 0;
2171 struct http_pack_request
*preq
;
2173 CALLOC_ARRAY(preq
, 1);
2174 strbuf_init(&preq
->tmpfile
, 0);
2178 strbuf_addf(&preq
->tmpfile
, "%s.temp", sha1_pack_name(packed_git_hash
));
2179 preq
->packfile
= fopen(preq
->tmpfile
.buf
, "a");
2180 if (!preq
->packfile
) {
2181 error("Unable to open local file %s for pack",
2186 preq
->slot
= get_active_slot();
2187 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEDATA
, preq
->packfile
);
2188 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
2189 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
2190 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
2194 * If there is data present from a previous transfer attempt,
2195 * resume where it left off
2197 prev_posn
= ftello(preq
->packfile
);
2199 if (http_is_verbose
)
2201 "Resuming fetch of pack %s at byte %"PRIuMAX
"\n",
2202 hash_to_hex(packed_git_hash
),
2203 (uintmax_t)prev_posn
);
2204 http_opt_request_remainder(preq
->slot
->curl
, prev_posn
);
2210 strbuf_release(&preq
->tmpfile
);
2216 /* Helpers for fetching objects (loose) */
2217 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
2220 unsigned char expn
[4096];
2221 size_t size
= eltsize
* nmemb
;
2223 struct http_object_request
*freq
= data
;
2224 struct active_request_slot
*slot
= freq
->slot
;
2227 CURLcode c
= curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
,
2230 BUG("curl_easy_getinfo for HTTP code failed: %s",
2231 curl_easy_strerror(c
));
2232 if (slot
->http_code
>= 300)
2237 ssize_t retval
= xwrite(freq
->localfile
,
2238 (char *) ptr
+ posn
, size
- posn
);
2240 return posn
/ eltsize
;
2242 } while (posn
< size
);
2244 freq
->stream
.avail_in
= size
;
2245 freq
->stream
.next_in
= (void *)ptr
;
2247 freq
->stream
.next_out
= expn
;
2248 freq
->stream
.avail_out
= sizeof(expn
);
2249 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
2250 the_hash_algo
->update_fn(&freq
->c
, expn
,
2251 sizeof(expn
) - freq
->stream
.avail_out
);
2252 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
2256 struct http_object_request
*new_http_object_request(const char *base_url
,
2257 const struct object_id
*oid
)
2259 char *hex
= oid_to_hex(oid
);
2260 struct strbuf filename
= STRBUF_INIT
;
2261 struct strbuf prevfile
= STRBUF_INIT
;
2263 char prev_buf
[PREV_BUF_SIZE
];
2264 ssize_t prev_read
= 0;
2265 off_t prev_posn
= 0;
2266 struct http_object_request
*freq
;
2268 CALLOC_ARRAY(freq
, 1);
2269 strbuf_init(&freq
->tmpfile
, 0);
2270 oidcpy(&freq
->oid
, oid
);
2271 freq
->localfile
= -1;
2273 loose_object_path(the_repository
, &filename
, oid
);
2274 strbuf_addf(&freq
->tmpfile
, "%s.temp", filename
.buf
);
2276 strbuf_addf(&prevfile
, "%s.prev", filename
.buf
);
2277 unlink_or_warn(prevfile
.buf
);
2278 rename(freq
->tmpfile
.buf
, prevfile
.buf
);
2279 unlink_or_warn(freq
->tmpfile
.buf
);
2280 strbuf_release(&filename
);
2282 if (freq
->localfile
!= -1)
2283 error("fd leakage in start: %d", freq
->localfile
);
2284 freq
->localfile
= open(freq
->tmpfile
.buf
,
2285 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2287 * This could have failed due to the "lazy directory creation";
2288 * try to mkdir the last path component.
2290 if (freq
->localfile
< 0 && errno
== ENOENT
) {
2291 char *dir
= strrchr(freq
->tmpfile
.buf
, '/');
2294 mkdir(freq
->tmpfile
.buf
, 0777);
2297 freq
->localfile
= open(freq
->tmpfile
.buf
,
2298 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2301 if (freq
->localfile
< 0) {
2302 error_errno("Couldn't create temporary file %s",
2307 git_inflate_init(&freq
->stream
);
2309 the_hash_algo
->init_fn(&freq
->c
);
2311 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
2314 * If a previous temp file is present, process what was already
2317 prevlocal
= open(prevfile
.buf
, O_RDONLY
);
2318 if (prevlocal
!= -1) {
2320 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
2322 if (fwrite_sha1_file(prev_buf
,
2325 freq
) == prev_read
) {
2326 prev_posn
+= prev_read
;
2331 } while (prev_read
> 0);
2334 unlink_or_warn(prevfile
.buf
);
2335 strbuf_release(&prevfile
);
2338 * Reset inflate/SHA1 if there was an error reading the previous temp
2339 * file; also rewind to the beginning of the local file.
2341 if (prev_read
== -1) {
2342 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
2343 git_inflate_init(&freq
->stream
);
2344 the_hash_algo
->init_fn(&freq
->c
);
2347 lseek(freq
->localfile
, 0, SEEK_SET
);
2348 if (ftruncate(freq
->localfile
, 0) < 0) {
2349 error_errno("Couldn't truncate temporary file %s",
2356 freq
->slot
= get_active_slot();
2358 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEDATA
, freq
);
2359 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FAILONERROR
, 0);
2360 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
2361 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
2362 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
2363 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
2366 * If we have successfully processed data from a previous fetch
2367 * attempt, only fetch the data we don't already have.
2370 if (http_is_verbose
)
2372 "Resuming fetch of object %s at byte %"PRIuMAX
"\n",
2373 hex
, (uintmax_t)prev_posn
);
2374 http_opt_request_remainder(freq
->slot
->curl
, prev_posn
);
2380 strbuf_release(&prevfile
);
2386 void process_http_object_request(struct http_object_request
*freq
)
2388 if (freq
->slot
== NULL
)
2390 freq
->curl_result
= freq
->slot
->curl_result
;
2391 freq
->http_code
= freq
->slot
->http_code
;
2395 int finish_http_object_request(struct http_object_request
*freq
)
2398 struct strbuf filename
= STRBUF_INIT
;
2400 close(freq
->localfile
);
2401 freq
->localfile
= -1;
2403 process_http_object_request(freq
);
2405 if (freq
->http_code
== 416) {
2406 warning("requested range invalid; we may already have all the data.");
2407 } else if (freq
->curl_result
!= CURLE_OK
) {
2408 if (stat(freq
->tmpfile
.buf
, &st
) == 0)
2409 if (st
.st_size
== 0)
2410 unlink_or_warn(freq
->tmpfile
.buf
);
2414 git_inflate_end(&freq
->stream
);
2415 the_hash_algo
->final_oid_fn(&freq
->real_oid
, &freq
->c
);
2416 if (freq
->zret
!= Z_STREAM_END
) {
2417 unlink_or_warn(freq
->tmpfile
.buf
);
2420 if (!oideq(&freq
->oid
, &freq
->real_oid
)) {
2421 unlink_or_warn(freq
->tmpfile
.buf
);
2424 loose_object_path(the_repository
, &filename
, &freq
->oid
);
2425 freq
->rename
= finalize_object_file(freq
->tmpfile
.buf
, filename
.buf
);
2426 strbuf_release(&filename
);
2428 return freq
->rename
;
2431 void abort_http_object_request(struct http_object_request
*freq
)
2433 unlink_or_warn(freq
->tmpfile
.buf
);
2435 release_http_object_request(freq
);
2438 void release_http_object_request(struct http_object_request
*freq
)
2440 if (freq
->localfile
!= -1) {
2441 close(freq
->localfile
);
2442 freq
->localfile
= -1;
2444 FREE_AND_NULL(freq
->url
);
2445 if (freq
->slot
!= NULL
) {
2446 freq
->slot
->callback_func
= NULL
;
2447 freq
->slot
->callback_data
= NULL
;
2448 release_active_slot(freq
->slot
);
2451 strbuf_release(&freq
->tmpfile
);