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 struct string_list cookies_to_redact
= STRING_LIST_INIT_DUP
;
22 #if LIBCURL_VERSION_NUM >= 0x070a08
23 long int git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
25 long int git_curl_ipresolve
;
29 ssize_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
31 #if LIBCURL_VERSION_NUM >= 0x070a06
32 #define LIBCURL_CAN_HANDLE_AUTH_ANY
35 static int min_curl_sessions
= 1;
36 static int curl_session_count
;
38 static int max_requests
= -1;
41 #ifndef NO_CURL_EASY_DUPHANDLE
42 static CURL
*curl_default
;
45 #define PREV_BUF_SIZE 4096
47 char curl_errorstr
[CURL_ERROR_SIZE
];
49 static int curl_ssl_verify
= -1;
50 static int curl_ssl_try
;
51 static const char *ssl_cert
;
52 static const char *ssl_cipherlist
;
53 static const char *ssl_version
;
58 { "sslv2", CURL_SSLVERSION_SSLv2
},
59 { "sslv3", CURL_SSLVERSION_SSLv3
},
60 { "tlsv1", CURL_SSLVERSION_TLSv1
},
61 #if LIBCURL_VERSION_NUM >= 0x072200
62 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0
},
63 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1
},
64 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2
},
66 #if LIBCURL_VERSION_NUM >= 0x073400
67 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3
},
70 #if LIBCURL_VERSION_NUM >= 0x070903
71 static const char *ssl_key
;
73 #if LIBCURL_VERSION_NUM >= 0x070908
74 static const char *ssl_capath
;
76 #if LIBCURL_VERSION_NUM >= 0x071304
77 static const char *curl_no_proxy
;
79 #if LIBCURL_VERSION_NUM >= 0x072c00
80 static const char *ssl_pinnedkey
;
82 static const char *ssl_cainfo
;
83 static long curl_low_speed_limit
= -1;
84 static long curl_low_speed_time
= -1;
85 static int curl_ftp_no_epsv
;
86 static const char *curl_http_proxy
;
87 static const char *http_proxy_authmethod
;
91 } proxy_authmethods
[] = {
92 { "basic", CURLAUTH_BASIC
},
93 { "digest", CURLAUTH_DIGEST
},
94 { "negotiate", CURLAUTH_GSSNEGOTIATE
},
95 { "ntlm", CURLAUTH_NTLM
},
96 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
97 { "anyauth", CURLAUTH_ANY
},
100 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
101 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
105 #ifdef CURLGSSAPI_DELEGATION_FLAG
106 static const char *curl_deleg
;
109 long curl_deleg_param
;
110 } curl_deleg_levels
[] = {
111 { "none", CURLGSSAPI_DELEGATION_NONE
},
112 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG
},
113 { "always", CURLGSSAPI_DELEGATION_FLAG
},
117 static struct credential proxy_auth
= CREDENTIAL_INIT
;
118 static const char *curl_proxyuserpwd
;
119 static const char *curl_cookie_file
;
120 static int curl_save_cookies
;
121 struct credential http_auth
= CREDENTIAL_INIT
;
122 static int http_proactive_auth
;
123 static const char *user_agent
;
124 static int curl_empty_auth
= -1;
126 enum http_follow_config http_follow_config
= HTTP_FOLLOW_INITIAL
;
128 #if LIBCURL_VERSION_NUM >= 0x071700
129 /* Use CURLOPT_KEYPASSWD as is */
130 #elif LIBCURL_VERSION_NUM >= 0x070903
131 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
133 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
136 static struct credential cert_auth
= CREDENTIAL_INIT
;
137 static int ssl_cert_password_required
;
138 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
139 static unsigned long http_auth_methods
= CURLAUTH_ANY
;
140 static int http_auth_methods_restricted
;
141 /* Modes for which empty_auth cannot actually help us. */
142 static unsigned long empty_auth_useless
=
144 #ifdef CURLAUTH_DIGEST_IE
150 static struct curl_slist
*pragma_header
;
151 static struct curl_slist
*no_pragma_header
;
152 static struct curl_slist
*extra_http_headers
;
154 static struct active_request_slot
*active_queue_head
;
156 static char *cached_accept_language
;
158 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
160 size_t size
= eltsize
* nmemb
;
161 struct buffer
*buffer
= buffer_
;
163 if (size
> buffer
->buf
.len
- buffer
->posn
)
164 size
= buffer
->buf
.len
- buffer
->posn
;
165 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
166 buffer
->posn
+= size
;
171 #ifndef NO_CURL_IOCTL
172 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
174 struct buffer
*buffer
= clientp
;
180 case CURLIOCMD_RESTARTREAD
:
185 return CURLIOE_UNKNOWNCMD
;
190 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
192 size_t size
= eltsize
* nmemb
;
193 struct strbuf
*buffer
= buffer_
;
195 strbuf_add(buffer
, ptr
, size
);
199 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
201 return eltsize
* nmemb
;
204 static void closedown_active_slot(struct active_request_slot
*slot
)
210 static void finish_active_slot(struct active_request_slot
*slot
)
212 closedown_active_slot(slot
);
213 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
215 if (slot
->finished
!= NULL
)
216 (*slot
->finished
) = 1;
218 /* Store slot results so they can be read after the slot is reused */
219 if (slot
->results
!= NULL
) {
220 slot
->results
->curl_result
= slot
->curl_result
;
221 slot
->results
->http_code
= slot
->http_code
;
222 #if LIBCURL_VERSION_NUM >= 0x070a08
223 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
224 &slot
->results
->auth_avail
);
226 slot
->results
->auth_avail
= 0;
229 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CONNECTCODE
,
230 &slot
->results
->http_connectcode
);
233 /* Run callback if appropriate */
234 if (slot
->callback_func
!= NULL
)
235 slot
->callback_func(slot
->callback_data
);
238 static void xmulti_remove_handle(struct active_request_slot
*slot
)
240 #ifdef USE_CURL_MULTI
241 curl_multi_remove_handle(curlm
, slot
->curl
);
245 #ifdef USE_CURL_MULTI
246 static void process_curl_messages(void)
249 struct active_request_slot
*slot
;
250 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
252 while (curl_message
!= NULL
) {
253 if (curl_message
->msg
== CURLMSG_DONE
) {
254 int curl_result
= curl_message
->data
.result
;
255 slot
= active_queue_head
;
256 while (slot
!= NULL
&&
257 slot
->curl
!= curl_message
->easy_handle
)
260 xmulti_remove_handle(slot
);
261 slot
->curl_result
= curl_result
;
262 finish_active_slot(slot
);
264 fprintf(stderr
, "Received DONE message for unknown request!\n");
267 fprintf(stderr
, "Unknown CURL message received: %d\n",
268 (int)curl_message
->msg
);
270 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
275 static int http_options(const char *var
, const char *value
, void *cb
)
277 if (!strcmp("http.sslverify", var
)) {
278 curl_ssl_verify
= git_config_bool(var
, value
);
281 if (!strcmp("http.sslcipherlist", var
))
282 return git_config_string(&ssl_cipherlist
, var
, value
);
283 if (!strcmp("http.sslversion", var
))
284 return git_config_string(&ssl_version
, var
, value
);
285 if (!strcmp("http.sslcert", var
))
286 return git_config_pathname(&ssl_cert
, var
, value
);
287 #if LIBCURL_VERSION_NUM >= 0x070903
288 if (!strcmp("http.sslkey", var
))
289 return git_config_pathname(&ssl_key
, var
, value
);
291 #if LIBCURL_VERSION_NUM >= 0x070908
292 if (!strcmp("http.sslcapath", var
))
293 return git_config_pathname(&ssl_capath
, var
, value
);
295 if (!strcmp("http.sslcainfo", var
))
296 return git_config_pathname(&ssl_cainfo
, var
, value
);
297 if (!strcmp("http.sslcertpasswordprotected", var
)) {
298 ssl_cert_password_required
= git_config_bool(var
, value
);
301 if (!strcmp("http.ssltry", var
)) {
302 curl_ssl_try
= git_config_bool(var
, value
);
305 if (!strcmp("http.minsessions", var
)) {
306 min_curl_sessions
= git_config_int(var
, value
);
307 #ifndef USE_CURL_MULTI
308 if (min_curl_sessions
> 1)
309 min_curl_sessions
= 1;
313 #ifdef USE_CURL_MULTI
314 if (!strcmp("http.maxrequests", var
)) {
315 max_requests
= git_config_int(var
, value
);
319 if (!strcmp("http.lowspeedlimit", var
)) {
320 curl_low_speed_limit
= (long)git_config_int(var
, value
);
323 if (!strcmp("http.lowspeedtime", var
)) {
324 curl_low_speed_time
= (long)git_config_int(var
, value
);
328 if (!strcmp("http.noepsv", var
)) {
329 curl_ftp_no_epsv
= git_config_bool(var
, value
);
332 if (!strcmp("http.proxy", var
))
333 return git_config_string(&curl_http_proxy
, var
, value
);
335 if (!strcmp("http.proxyauthmethod", var
))
336 return git_config_string(&http_proxy_authmethod
, var
, value
);
338 if (!strcmp("http.cookiefile", var
))
339 return git_config_pathname(&curl_cookie_file
, var
, value
);
340 if (!strcmp("http.savecookies", var
)) {
341 curl_save_cookies
= git_config_bool(var
, value
);
345 if (!strcmp("http.postbuffer", var
)) {
346 http_post_buffer
= git_config_ssize_t(var
, value
);
347 if (http_post_buffer
< 0)
348 warning(_("negative value for http.postbuffer; defaulting to %d"), LARGE_PACKET_MAX
);
349 if (http_post_buffer
< LARGE_PACKET_MAX
)
350 http_post_buffer
= LARGE_PACKET_MAX
;
354 if (!strcmp("http.useragent", var
))
355 return git_config_string(&user_agent
, var
, value
);
357 if (!strcmp("http.emptyauth", var
)) {
358 if (value
&& !strcmp("auto", value
))
359 curl_empty_auth
= -1;
361 curl_empty_auth
= git_config_bool(var
, value
);
365 if (!strcmp("http.delegation", var
)) {
366 #ifdef CURLGSSAPI_DELEGATION_FLAG
367 return git_config_string(&curl_deleg
, var
, value
);
369 warning(_("Delegation control is not supported with cURL < 7.22.0"));
374 if (!strcmp("http.pinnedpubkey", var
)) {
375 #if LIBCURL_VERSION_NUM >= 0x072c00
376 return git_config_pathname(&ssl_pinnedkey
, var
, value
);
378 warning(_("Public key pinning not supported with cURL < 7.44.0"));
383 if (!strcmp("http.extraheader", var
)) {
385 return config_error_nonbool(var
);
386 } else if (!*value
) {
387 curl_slist_free_all(extra_http_headers
);
388 extra_http_headers
= NULL
;
391 curl_slist_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
;
415 #ifndef LIBCURL_CAN_HANDLE_AUTH_ANY
417 * Our libcurl is too old to do AUTH_ANY in the first place;
418 * just default to turning the feature off.
422 * In the automatic case, kick in the empty-auth
423 * hack as long as we would potentially try some
424 * method more exotic than "Basic" or "Digest".
426 * But only do this when this is our second or
427 * subsequent request, as by then we know what
428 * methods are available.
430 if (http_auth_methods_restricted
&&
431 (http_auth_methods
& ~empty_auth_useless
))
437 static void init_curl_http_auth(CURL
*result
)
439 if (!http_auth
.username
|| !*http_auth
.username
) {
440 if (curl_empty_auth_enabled())
441 curl_easy_setopt(result
, CURLOPT_USERPWD
, ":");
445 credential_fill(&http_auth
);
447 #if LIBCURL_VERSION_NUM >= 0x071301
448 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
449 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
452 static struct strbuf up
= STRBUF_INIT
;
454 * Note that we assume we only ever have a single set of
455 * credentials in a given program run, so we do not have
456 * to worry about updating this buffer, only setting its
460 strbuf_addf(&up
, "%s:%s",
461 http_auth
.username
, http_auth
.password
);
462 curl_easy_setopt(result
, CURLOPT_USERPWD
, up
.buf
);
467 /* *var must be free-able */
468 static void var_override(const char **var
, char *value
)
472 *var
= xstrdup(value
);
476 static void set_proxyauth_name_password(CURL
*result
)
478 #if LIBCURL_VERSION_NUM >= 0x071301
479 curl_easy_setopt(result
, CURLOPT_PROXYUSERNAME
,
480 proxy_auth
.username
);
481 curl_easy_setopt(result
, CURLOPT_PROXYPASSWORD
,
482 proxy_auth
.password
);
484 struct strbuf s
= STRBUF_INIT
;
486 strbuf_addstr_urlencode(&s
, proxy_auth
.username
, 1);
487 strbuf_addch(&s
, ':');
488 strbuf_addstr_urlencode(&s
, proxy_auth
.password
, 1);
489 curl_proxyuserpwd
= strbuf_detach(&s
, NULL
);
490 curl_easy_setopt(result
, CURLOPT_PROXYUSERPWD
, curl_proxyuserpwd
);
494 static void init_curl_proxy_auth(CURL
*result
)
496 if (proxy_auth
.username
) {
497 if (!proxy_auth
.password
)
498 credential_fill(&proxy_auth
);
499 set_proxyauth_name_password(result
);
502 var_override(&http_proxy_authmethod
, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
504 #if LIBCURL_VERSION_NUM >= 0x070a07 /* CURLOPT_PROXYAUTH and CURLAUTH_ANY */
505 if (http_proxy_authmethod
) {
507 for (i
= 0; i
< ARRAY_SIZE(proxy_authmethods
); i
++) {
508 if (!strcmp(http_proxy_authmethod
, proxy_authmethods
[i
].name
)) {
509 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
,
510 proxy_authmethods
[i
].curlauth_param
);
514 if (i
== ARRAY_SIZE(proxy_authmethods
)) {
515 warning("unsupported proxy authentication method %s: using anyauth",
516 http_proxy_authmethod
);
517 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
521 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
525 static int has_cert_password(void)
527 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
529 if (!cert_auth
.password
) {
530 cert_auth
.protocol
= xstrdup("cert");
531 cert_auth
.username
= xstrdup("");
532 cert_auth
.path
= xstrdup(ssl_cert
);
533 credential_fill(&cert_auth
);
538 #if LIBCURL_VERSION_NUM >= 0x071900
539 static void set_curl_keepalive(CURL
*c
)
541 curl_easy_setopt(c
, CURLOPT_TCP_KEEPALIVE
, 1);
544 #elif LIBCURL_VERSION_NUM >= 0x071000
545 static int sockopt_callback(void *client
, curl_socket_t fd
, curlsocktype type
)
549 socklen_t len
= (socklen_t
)sizeof(ka
);
551 if (type
!= CURLSOCKTYPE_IPCXN
)
554 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
556 warning_errno("unable to set SO_KEEPALIVE on socket");
558 return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
561 static void set_curl_keepalive(CURL
*c
)
563 curl_easy_setopt(c
, CURLOPT_SOCKOPTFUNCTION
, sockopt_callback
);
567 static void set_curl_keepalive(CURL
*c
)
569 /* not supported on older curl versions */
573 static void redact_sensitive_header(struct strbuf
*header
)
575 const char *sensitive_header
;
577 if (skip_prefix(header
->buf
, "Authorization:", &sensitive_header
) ||
578 skip_prefix(header
->buf
, "Proxy-Authorization:", &sensitive_header
)) {
579 /* The first token is the type, which is OK to log */
580 while (isspace(*sensitive_header
))
582 while (*sensitive_header
&& !isspace(*sensitive_header
))
584 /* Everything else is opaque and possibly sensitive */
585 strbuf_setlen(header
, sensitive_header
- header
->buf
);
586 strbuf_addstr(header
, " <redacted>");
587 } else if (cookies_to_redact
.nr
&&
588 skip_prefix(header
->buf
, "Cookie:", &sensitive_header
)) {
589 struct strbuf redacted_header
= STRBUF_INIT
;
592 while (isspace(*sensitive_header
))
596 * The contents of header starting from sensitive_header will
597 * subsequently be overridden, so it is fine to mutate this
598 * string (hence the assignment to "char *").
600 cookie
= (char *) sensitive_header
;
604 char *semicolon
= strstr(cookie
, "; ");
607 equals
= strchrnul(cookie
, '=');
609 /* invalid cookie, just append and continue */
610 strbuf_addstr(&redacted_header
, cookie
);
613 *equals
= 0; /* temporarily set to NUL for lookup */
614 if (string_list_lookup(&cookies_to_redact
, cookie
)) {
615 strbuf_addstr(&redacted_header
, cookie
);
616 strbuf_addstr(&redacted_header
, "=<redacted>");
619 strbuf_addstr(&redacted_header
, cookie
);
623 * There are more cookies. (Or, for some
624 * reason, the input string ends in "; ".)
626 strbuf_addstr(&redacted_header
, "; ");
627 cookie
= semicolon
+ strlen("; ");
633 strbuf_setlen(header
, sensitive_header
- header
->buf
);
634 strbuf_addbuf(header
, &redacted_header
);
638 static void curl_dump_header(const char *text
, unsigned char *ptr
, size_t size
, int hide_sensitive_header
)
640 struct strbuf out
= STRBUF_INIT
;
641 struct strbuf
**headers
, **header
;
643 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
644 text
, (long)size
, (long)size
);
645 trace_strbuf(&trace_curl
, &out
);
647 strbuf_add(&out
, ptr
, size
);
648 headers
= strbuf_split_max(&out
, '\n', 0);
650 for (header
= headers
; *header
; header
++) {
651 if (hide_sensitive_header
)
652 redact_sensitive_header(*header
);
653 strbuf_insert((*header
), 0, text
, strlen(text
));
654 strbuf_insert((*header
), strlen(text
), ": ", 2);
655 strbuf_rtrim((*header
));
656 strbuf_addch((*header
), '\n');
657 trace_strbuf(&trace_curl
, (*header
));
659 strbuf_list_free(headers
);
660 strbuf_release(&out
);
663 static void curl_dump_data(const char *text
, unsigned char *ptr
, size_t size
)
666 struct strbuf out
= STRBUF_INIT
;
667 unsigned int width
= 60;
669 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
670 text
, (long)size
, (long)size
);
671 trace_strbuf(&trace_curl
, &out
);
673 for (i
= 0; i
< size
; i
+= width
) {
677 strbuf_addf(&out
, "%s: ", text
);
678 for (w
= 0; (w
< width
) && (i
+ w
< size
); w
++) {
679 unsigned char ch
= ptr
[i
+ w
];
682 (ch
>= 0x20) && (ch
< 0x80)
685 strbuf_addch(&out
, '\n');
686 trace_strbuf(&trace_curl
, &out
);
688 strbuf_release(&out
);
691 static int curl_trace(CURL
*handle
, curl_infotype type
, char *data
, size_t size
, void *userp
)
694 enum { NO_FILTER
= 0, DO_FILTER
= 1 };
698 trace_printf_key(&trace_curl
, "== Info: %s", data
);
700 case CURLINFO_HEADER_OUT
:
701 text
= "=> Send header";
702 curl_dump_header(text
, (unsigned char *)data
, size
, DO_FILTER
);
704 case CURLINFO_DATA_OUT
:
705 if (trace_curl_data
) {
706 text
= "=> Send data";
707 curl_dump_data(text
, (unsigned char *)data
, size
);
710 case CURLINFO_SSL_DATA_OUT
:
711 if (trace_curl_data
) {
712 text
= "=> Send SSL data";
713 curl_dump_data(text
, (unsigned char *)data
, size
);
716 case CURLINFO_HEADER_IN
:
717 text
= "<= Recv header";
718 curl_dump_header(text
, (unsigned char *)data
, size
, NO_FILTER
);
720 case CURLINFO_DATA_IN
:
721 if (trace_curl_data
) {
722 text
= "<= Recv data";
723 curl_dump_data(text
, (unsigned char *)data
, size
);
726 case CURLINFO_SSL_DATA_IN
:
727 if (trace_curl_data
) {
728 text
= "<= Recv SSL data";
729 curl_dump_data(text
, (unsigned char *)data
, size
);
733 default: /* we ignore unknown types by default */
739 void setup_curl_trace(CURL
*handle
)
741 if (!trace_want(&trace_curl
))
743 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
744 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, curl_trace
);
745 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, NULL
);
748 #ifdef CURLPROTO_HTTP
749 static long get_curl_allowed_protocols(int from_user
)
751 long allowed_protocols
= 0;
753 if (is_transport_allowed("http", from_user
))
754 allowed_protocols
|= CURLPROTO_HTTP
;
755 if (is_transport_allowed("https", from_user
))
756 allowed_protocols
|= CURLPROTO_HTTPS
;
757 if (is_transport_allowed("ftp", from_user
))
758 allowed_protocols
|= CURLPROTO_FTP
;
759 if (is_transport_allowed("ftps", from_user
))
760 allowed_protocols
|= CURLPROTO_FTPS
;
762 return allowed_protocols
;
766 static CURL
*get_curl_handle(void)
768 CURL
*result
= curl_easy_init();
771 die("curl_easy_init failed");
773 if (!curl_ssl_verify
) {
774 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
775 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
777 /* Verify authenticity of the peer's certificate */
778 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
779 /* The name in the cert must match whom we tried to connect */
780 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
783 #if LIBCURL_VERSION_NUM >= 0x070907
784 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
786 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
787 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_proactive_auth
)
807 init_curl_http_auth(result
);
809 if (getenv("GIT_SSL_VERSION"))
810 ssl_version
= getenv("GIT_SSL_VERSION");
811 if (ssl_version
&& *ssl_version
) {
813 for (i
= 0; i
< ARRAY_SIZE(sslversions
); i
++) {
814 if (!strcmp(ssl_version
, sslversions
[i
].name
)) {
815 curl_easy_setopt(result
, CURLOPT_SSLVERSION
,
816 sslversions
[i
].ssl_version
);
820 if (i
== ARRAY_SIZE(sslversions
))
821 warning("unsupported ssl version %s: using default",
825 if (getenv("GIT_SSL_CIPHER_LIST"))
826 ssl_cipherlist
= getenv("GIT_SSL_CIPHER_LIST");
827 if (ssl_cipherlist
!= NULL
&& *ssl_cipherlist
)
828 curl_easy_setopt(result
, CURLOPT_SSL_CIPHER_LIST
,
831 if (ssl_cert
!= NULL
)
832 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
833 if (has_cert_password())
834 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
835 #if LIBCURL_VERSION_NUM >= 0x070903
837 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
839 #if LIBCURL_VERSION_NUM >= 0x070908
840 if (ssl_capath
!= NULL
)
841 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
843 #if LIBCURL_VERSION_NUM >= 0x072c00
844 if (ssl_pinnedkey
!= NULL
)
845 curl_easy_setopt(result
, CURLOPT_PINNEDPUBLICKEY
, ssl_pinnedkey
);
847 if (ssl_cainfo
!= NULL
)
848 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
850 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
851 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
852 curl_low_speed_limit
);
853 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
854 curl_low_speed_time
);
857 curl_easy_setopt(result
, CURLOPT_MAXREDIRS
, 20);
858 #if LIBCURL_VERSION_NUM >= 0x071301
859 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
860 #elif LIBCURL_VERSION_NUM >= 0x071101
861 curl_easy_setopt(result
, CURLOPT_POST301
, 1);
863 #ifdef CURLPROTO_HTTP
864 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS
,
865 get_curl_allowed_protocols(0));
866 curl_easy_setopt(result
, CURLOPT_PROTOCOLS
,
867 get_curl_allowed_protocols(-1));
869 warning("protocol restrictions not applied to curl redirects because\n"
870 "your curl version is too old (>= 7.19.4)");
872 if (getenv("GIT_CURL_VERBOSE"))
873 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1L);
874 setup_curl_trace(result
);
875 if (getenv("GIT_TRACE_CURL_NO_DATA"))
877 if (getenv("GIT_REDACT_COOKIES")) {
878 string_list_split(&cookies_to_redact
,
879 getenv("GIT_REDACT_COOKIES"), ',', -1);
880 string_list_sort(&cookies_to_redact
);
883 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
884 user_agent
? user_agent
: git_user_agent());
886 if (curl_ftp_no_epsv
)
887 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
889 #ifdef CURLOPT_USE_SSL
891 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
895 * CURL also examines these variables as a fallback; but we need to query
896 * them here in order to decide whether to prompt for missing password (cf.
897 * init_curl_proxy_auth()).
899 * Unlike many other common environment variables, these are historically
900 * lowercase only. It appears that CURL did not know this and implemented
901 * only uppercase variants, which was later corrected to take both - with
902 * the exception of http_proxy, which is lowercase only also in CURL. As
903 * the lowercase versions are the historical quasi-standard, they take
904 * precedence here, as in CURL.
906 if (!curl_http_proxy
) {
907 if (http_auth
.protocol
&& !strcmp(http_auth
.protocol
, "https")) {
908 var_override(&curl_http_proxy
, getenv("HTTPS_PROXY"));
909 var_override(&curl_http_proxy
, getenv("https_proxy"));
911 var_override(&curl_http_proxy
, getenv("http_proxy"));
913 if (!curl_http_proxy
) {
914 var_override(&curl_http_proxy
, getenv("ALL_PROXY"));
915 var_override(&curl_http_proxy
, getenv("all_proxy"));
919 if (curl_http_proxy
&& curl_http_proxy
[0] == '\0') {
921 * Handle case with the empty http.proxy value here to keep
923 * NB: empty option disables proxying at all.
925 curl_easy_setopt(result
, CURLOPT_PROXY
, "");
926 } else if (curl_http_proxy
) {
927 #if LIBCURL_VERSION_NUM >= 0x071800
928 if (starts_with(curl_http_proxy
, "socks5h"))
929 curl_easy_setopt(result
,
930 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5_HOSTNAME
);
931 else if (starts_with(curl_http_proxy
, "socks5"))
932 curl_easy_setopt(result
,
933 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
934 else if (starts_with(curl_http_proxy
, "socks4a"))
935 curl_easy_setopt(result
,
936 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4A
);
937 else if (starts_with(curl_http_proxy
, "socks"))
938 curl_easy_setopt(result
,
939 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4
);
941 #if LIBCURL_VERSION_NUM >= 0x073400
942 else if (starts_with(curl_http_proxy
, "https"))
943 curl_easy_setopt(result
,
944 CURLOPT_PROXYTYPE
, CURLPROXY_HTTPS
);
946 if (strstr(curl_http_proxy
, "://"))
947 credential_from_url(&proxy_auth
, curl_http_proxy
);
949 struct strbuf url
= STRBUF_INIT
;
950 strbuf_addf(&url
, "http://%s", curl_http_proxy
);
951 credential_from_url(&proxy_auth
, url
.buf
);
952 strbuf_release(&url
);
955 if (!proxy_auth
.host
)
956 die("Invalid proxy URL '%s'", curl_http_proxy
);
958 curl_easy_setopt(result
, CURLOPT_PROXY
, proxy_auth
.host
);
959 #if LIBCURL_VERSION_NUM >= 0x071304
960 var_override(&curl_no_proxy
, getenv("NO_PROXY"));
961 var_override(&curl_no_proxy
, getenv("no_proxy"));
962 curl_easy_setopt(result
, CURLOPT_NOPROXY
, curl_no_proxy
);
965 init_curl_proxy_auth(result
);
967 set_curl_keepalive(result
);
972 static void set_from_env(const char **var
, const char *envname
)
974 const char *val
= getenv(envname
);
979 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
981 char *low_speed_limit
;
982 char *low_speed_time
;
983 char *normalized_url
;
984 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
986 config
.section
= "http";
988 config
.collect_fn
= http_options
;
989 config
.cascade_fn
= git_default_config
;
993 normalized_url
= url_normalize(url
, &config
.url
);
995 git_config(urlmatch_config_entry
, &config
);
996 free(normalized_url
);
998 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
999 die("curl_global_init failed");
1001 http_proactive_auth
= proactive_auth
;
1003 if (remote
&& remote
->http_proxy
)
1004 curl_http_proxy
= xstrdup(remote
->http_proxy
);
1007 var_override(&http_proxy_authmethod
, remote
->http_proxy_authmethod
);
1009 pragma_header
= curl_slist_append(http_copy_default_headers(),
1010 "Pragma: no-cache");
1011 no_pragma_header
= curl_slist_append(http_copy_default_headers(),
1014 #ifdef USE_CURL_MULTI
1016 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
1017 if (http_max_requests
!= NULL
)
1018 max_requests
= atoi(http_max_requests
);
1021 curlm
= curl_multi_init();
1023 die("curl_multi_init failed");
1026 if (getenv("GIT_SSL_NO_VERIFY"))
1027 curl_ssl_verify
= 0;
1029 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
1030 #if LIBCURL_VERSION_NUM >= 0x070903
1031 set_from_env(&ssl_key
, "GIT_SSL_KEY");
1033 #if LIBCURL_VERSION_NUM >= 0x070908
1034 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
1036 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
1038 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
1040 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1041 if (low_speed_limit
!= NULL
)
1042 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
1043 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
1044 if (low_speed_time
!= NULL
)
1045 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
1047 if (curl_ssl_verify
== -1)
1048 curl_ssl_verify
= 1;
1050 curl_session_count
= 0;
1051 #ifdef USE_CURL_MULTI
1052 if (max_requests
< 1)
1053 max_requests
= DEFAULT_MAX_REQUESTS
;
1056 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1057 curl_ftp_no_epsv
= 1;
1060 credential_from_url(&http_auth
, url
);
1061 if (!ssl_cert_password_required
&&
1062 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
1063 starts_with(url
, "https://"))
1064 ssl_cert_password_required
= 1;
1067 #ifndef NO_CURL_EASY_DUPHANDLE
1068 curl_default
= get_curl_handle();
1072 void http_cleanup(void)
1074 struct active_request_slot
*slot
= active_queue_head
;
1076 while (slot
!= NULL
) {
1077 struct active_request_slot
*next
= slot
->next
;
1078 if (slot
->curl
!= NULL
) {
1079 xmulti_remove_handle(slot
);
1080 curl_easy_cleanup(slot
->curl
);
1085 active_queue_head
= NULL
;
1087 #ifndef NO_CURL_EASY_DUPHANDLE
1088 curl_easy_cleanup(curl_default
);
1091 #ifdef USE_CURL_MULTI
1092 curl_multi_cleanup(curlm
);
1094 curl_global_cleanup();
1096 curl_slist_free_all(extra_http_headers
);
1097 extra_http_headers
= NULL
;
1099 curl_slist_free_all(pragma_header
);
1100 pragma_header
= NULL
;
1102 curl_slist_free_all(no_pragma_header
);
1103 no_pragma_header
= NULL
;
1105 if (curl_http_proxy
) {
1106 free((void *)curl_http_proxy
);
1107 curl_http_proxy
= NULL
;
1110 if (proxy_auth
.password
) {
1111 memset(proxy_auth
.password
, 0, strlen(proxy_auth
.password
));
1112 FREE_AND_NULL(proxy_auth
.password
);
1115 free((void *)curl_proxyuserpwd
);
1116 curl_proxyuserpwd
= NULL
;
1118 free((void *)http_proxy_authmethod
);
1119 http_proxy_authmethod
= NULL
;
1121 if (cert_auth
.password
!= NULL
) {
1122 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
1123 FREE_AND_NULL(cert_auth
.password
);
1125 ssl_cert_password_required
= 0;
1127 FREE_AND_NULL(cached_accept_language
);
1130 struct active_request_slot
*get_active_slot(void)
1132 struct active_request_slot
*slot
= active_queue_head
;
1133 struct active_request_slot
*newslot
;
1135 #ifdef USE_CURL_MULTI
1138 /* Wait for a slot to open up if the queue is full */
1139 while (active_requests
>= max_requests
) {
1140 curl_multi_perform(curlm
, &num_transfers
);
1141 if (num_transfers
< active_requests
)
1142 process_curl_messages();
1146 while (slot
!= NULL
&& slot
->in_use
)
1150 newslot
= xmalloc(sizeof(*newslot
));
1151 newslot
->curl
= NULL
;
1152 newslot
->in_use
= 0;
1153 newslot
->next
= NULL
;
1155 slot
= active_queue_head
;
1157 active_queue_head
= newslot
;
1159 while (slot
->next
!= NULL
)
1161 slot
->next
= newslot
;
1166 if (slot
->curl
== NULL
) {
1167 #ifdef NO_CURL_EASY_DUPHANDLE
1168 slot
->curl
= get_curl_handle();
1170 slot
->curl
= curl_easy_duphandle(curl_default
);
1172 curl_session_count
++;
1177 slot
->results
= NULL
;
1178 slot
->finished
= NULL
;
1179 slot
->callback_data
= NULL
;
1180 slot
->callback_func
= NULL
;
1181 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
1182 if (curl_save_cookies
)
1183 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
1184 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
1185 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
1186 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
1187 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
1188 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
1189 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
1190 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
1191 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1192 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
1193 curl_easy_setopt(slot
->curl
, CURLOPT_RANGE
, NULL
);
1196 * Default following to off unless "ALWAYS" is configured; this gives
1197 * callers a sane starting point, and they can tweak for individual
1198 * HTTP_FOLLOW_* cases themselves.
1200 if (http_follow_config
== HTTP_FOLLOW_ALWAYS
)
1201 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1203 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 0);
1205 #if LIBCURL_VERSION_NUM >= 0x070a08
1206 curl_easy_setopt(slot
->curl
, CURLOPT_IPRESOLVE
, git_curl_ipresolve
);
1208 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1209 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPAUTH
, http_auth_methods
);
1211 if (http_auth
.password
|| curl_empty_auth_enabled())
1212 init_curl_http_auth(slot
->curl
);
1217 int start_active_slot(struct active_request_slot
*slot
)
1219 #ifdef USE_CURL_MULTI
1220 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
1223 if (curlm_result
!= CURLM_OK
&&
1224 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
1225 warning("curl_multi_add_handle failed: %s",
1226 curl_multi_strerror(curlm_result
));
1233 * We know there must be something to do, since we just added
1236 curl_multi_perform(curlm
, &num_transfers
);
1241 #ifdef USE_CURL_MULTI
1244 int (*fill
)(void *);
1245 struct fill_chain
*next
;
1248 static struct fill_chain
*fill_cfg
;
1250 void add_fill_function(void *data
, int (*fill
)(void *))
1252 struct fill_chain
*new_fill
= xmalloc(sizeof(*new_fill
));
1253 struct fill_chain
**linkp
= &fill_cfg
;
1254 new_fill
->data
= data
;
1255 new_fill
->fill
= fill
;
1256 new_fill
->next
= NULL
;
1258 linkp
= &(*linkp
)->next
;
1262 void fill_active_slots(void)
1264 struct active_request_slot
*slot
= active_queue_head
;
1266 while (active_requests
< max_requests
) {
1267 struct fill_chain
*fill
;
1268 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
1269 if (fill
->fill(fill
->data
))
1276 while (slot
!= NULL
) {
1277 if (!slot
->in_use
&& slot
->curl
!= NULL
1278 && curl_session_count
> min_curl_sessions
) {
1279 curl_easy_cleanup(slot
->curl
);
1281 curl_session_count
--;
1287 void step_active_slots(void)
1290 CURLMcode curlm_result
;
1293 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
1294 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
1295 if (num_transfers
< active_requests
) {
1296 process_curl_messages();
1297 fill_active_slots();
1302 void run_active_slot(struct active_request_slot
*slot
)
1304 #ifdef USE_CURL_MULTI
1309 struct timeval select_timeout
;
1312 slot
->finished
= &finished
;
1314 step_active_slots();
1317 #if LIBCURL_VERSION_NUM >= 0x070f04
1319 curl_multi_timeout(curlm
, &curl_timeout
);
1320 if (curl_timeout
== 0) {
1322 } else if (curl_timeout
== -1) {
1323 select_timeout
.tv_sec
= 0;
1324 select_timeout
.tv_usec
= 50000;
1326 select_timeout
.tv_sec
= curl_timeout
/ 1000;
1327 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
1330 select_timeout
.tv_sec
= 0;
1331 select_timeout
.tv_usec
= 50000;
1338 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
1341 * It can happen that curl_multi_timeout returns a pathologically
1342 * long timeout when curl_multi_fdset returns no file descriptors
1343 * to read. See commit message for more details.
1346 (select_timeout
.tv_sec
> 0 ||
1347 select_timeout
.tv_usec
> 50000)) {
1348 select_timeout
.tv_sec
= 0;
1349 select_timeout
.tv_usec
= 50000;
1352 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
1356 while (slot
->in_use
) {
1357 slot
->curl_result
= curl_easy_perform(slot
->curl
);
1358 finish_active_slot(slot
);
1363 static void release_active_slot(struct active_request_slot
*slot
)
1365 closedown_active_slot(slot
);
1367 xmulti_remove_handle(slot
);
1368 if (curl_session_count
> min_curl_sessions
) {
1369 curl_easy_cleanup(slot
->curl
);
1371 curl_session_count
--;
1374 #ifdef USE_CURL_MULTI
1375 fill_active_slots();
1379 void finish_all_active_slots(void)
1381 struct active_request_slot
*slot
= active_queue_head
;
1383 while (slot
!= NULL
)
1385 run_active_slot(slot
);
1386 slot
= active_queue_head
;
1392 /* Helpers for modifying and creating URLs */
1393 static inline int needs_quote(int ch
)
1395 if (((ch
>= 'A') && (ch
<= 'Z'))
1396 || ((ch
>= 'a') && (ch
<= 'z'))
1397 || ((ch
>= '0') && (ch
<= '9'))
1405 static char *quote_ref_url(const char *base
, const char *ref
)
1407 struct strbuf buf
= STRBUF_INIT
;
1411 end_url_with_slash(&buf
, base
);
1413 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
1414 if (needs_quote(ch
))
1415 strbuf_addf(&buf
, "%%%02x", ch
);
1417 strbuf_addch(&buf
, *cp
);
1419 return strbuf_detach(&buf
, NULL
);
1422 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
1424 int only_two_digit_prefix
)
1426 end_url_with_slash(buf
, url
);
1428 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
1429 if (!only_two_digit_prefix
)
1430 strbuf_addstr(buf
, hex
+ 2);
1433 char *get_remote_object_url(const char *url
, const char *hex
,
1434 int only_two_digit_prefix
)
1436 struct strbuf buf
= STRBUF_INIT
;
1437 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
1438 return strbuf_detach(&buf
, NULL
);
1441 static int handle_curl_result(struct slot_results
*results
)
1444 * If we see a failing http code with CURLE_OK, we have turned off
1445 * FAILONERROR (to keep the server's custom error response), and should
1446 * translate the code into failure here.
1448 * Likewise, if we see a redirect (30x code), that means we turned off
1449 * redirect-following, and we should treat the result as an error.
1451 if (results
->curl_result
== CURLE_OK
&&
1452 results
->http_code
>= 300) {
1453 results
->curl_result
= CURLE_HTTP_RETURNED_ERROR
;
1455 * Normally curl will already have put the "reason phrase"
1456 * from the server into curl_errorstr; unfortunately without
1457 * FAILONERROR it is lost, so we can give only the numeric
1460 xsnprintf(curl_errorstr
, sizeof(curl_errorstr
),
1461 "The requested URL returned error: %ld",
1462 results
->http_code
);
1465 if (results
->curl_result
== CURLE_OK
) {
1466 credential_approve(&http_auth
);
1467 if (proxy_auth
.password
)
1468 credential_approve(&proxy_auth
);
1470 } else if (missing_target(results
))
1471 return HTTP_MISSING_TARGET
;
1472 else if (results
->http_code
== 401) {
1473 if (http_auth
.username
&& http_auth
.password
) {
1474 credential_reject(&http_auth
);
1477 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1478 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
1479 if (results
->auth_avail
) {
1480 http_auth_methods
&= results
->auth_avail
;
1481 http_auth_methods_restricted
= 1;
1487 if (results
->http_connectcode
== 407)
1488 credential_reject(&proxy_auth
);
1489 #if LIBCURL_VERSION_NUM >= 0x070c00
1490 if (!curl_errorstr
[0])
1491 strlcpy(curl_errorstr
,
1492 curl_easy_strerror(results
->curl_result
),
1493 sizeof(curl_errorstr
));
1499 int run_one_slot(struct active_request_slot
*slot
,
1500 struct slot_results
*results
)
1502 slot
->results
= results
;
1503 if (!start_active_slot(slot
)) {
1504 xsnprintf(curl_errorstr
, sizeof(curl_errorstr
),
1505 "failed to start HTTP request");
1506 return HTTP_START_FAILED
;
1509 run_active_slot(slot
);
1510 return handle_curl_result(results
);
1513 struct curl_slist
*http_copy_default_headers(void)
1515 struct curl_slist
*headers
= NULL
, *h
;
1517 for (h
= extra_http_headers
; h
; h
= h
->next
)
1518 headers
= curl_slist_append(headers
, h
->data
);
1523 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
1529 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
1531 strbuf_addstr(buf
, ptr
);
1536 * Check for and extract a content-type parameter. "raw"
1537 * should be positioned at the start of the potential
1538 * parameter, with any whitespace already removed.
1540 * "name" is the name of the parameter. The value is appended
1543 static int extract_param(const char *raw
, const char *name
,
1546 size_t len
= strlen(name
);
1548 if (strncasecmp(raw
, name
, len
))
1556 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
1557 strbuf_addch(out
, *raw
++);
1562 * Extract a normalized version of the content type, with any
1563 * spaces suppressed, all letters lowercased, and no trailing ";"
1566 * Note that we will silently remove even invalid whitespace. For
1567 * example, "text / plain" is specifically forbidden by RFC 2616,
1568 * but "text/plain" is the only reasonable output, and this keeps
1571 * If the "charset" argument is not NULL, store the value of any
1572 * charset parameter there.
1575 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1576 * "text / plain" -> "text/plain"
1578 static void extract_content_type(struct strbuf
*raw
, struct strbuf
*type
,
1579 struct strbuf
*charset
)
1584 strbuf_grow(type
, raw
->len
);
1585 for (p
= raw
->buf
; *p
; p
++) {
1592 strbuf_addch(type
, tolower(*p
));
1598 strbuf_reset(charset
);
1600 while (isspace(*p
) || *p
== ';')
1602 if (!extract_param(p
, "charset", charset
))
1604 while (*p
&& !isspace(*p
))
1608 if (!charset
->len
&& starts_with(type
->buf
, "text/"))
1609 strbuf_addstr(charset
, "ISO-8859-1");
1612 static void write_accept_language(struct strbuf
*buf
)
1615 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1616 * that, q-value will be smaller than 0.001, the minimum q-value the
1617 * HTTP specification allows. See
1618 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1620 const int MAX_DECIMAL_PLACES
= 3;
1621 const int MAX_LANGUAGE_TAGS
= 1000;
1622 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE
= 4000;
1623 char **language_tags
= NULL
;
1625 const char *s
= get_preferred_languages();
1627 struct strbuf tag
= STRBUF_INIT
;
1629 /* Don't add Accept-Language header if no language is preferred. */
1634 * Split the colon-separated string of preferred languages into
1635 * language_tags array.
1638 /* collect language tag */
1639 for (; *s
&& (isalnum(*s
) || *s
== '_'); s
++)
1640 strbuf_addch(&tag
, *s
== '_' ? '-' : *s
);
1642 /* skip .codeset, @modifier and any other unnecessary parts */
1643 while (*s
&& *s
!= ':')
1648 REALLOC_ARRAY(language_tags
, num_langs
);
1649 language_tags
[num_langs
- 1] = strbuf_detach(&tag
, NULL
);
1650 if (num_langs
>= MAX_LANGUAGE_TAGS
- 1) /* -1 for '*' */
1655 /* write Accept-Language header into buf */
1657 int last_buf_len
= 0;
1663 REALLOC_ARRAY(language_tags
, num_langs
+ 1);
1664 language_tags
[num_langs
++] = "*"; /* it's OK; this won't be freed */
1666 /* compute decimal_places */
1667 for (max_q
= 1, decimal_places
= 0;
1668 max_q
< num_langs
&& decimal_places
<= MAX_DECIMAL_PLACES
;
1669 decimal_places
++, max_q
*= 10)
1672 xsnprintf(q_format
, sizeof(q_format
), ";q=0.%%0%dd", decimal_places
);
1674 strbuf_addstr(buf
, "Accept-Language: ");
1676 for (i
= 0; i
< num_langs
; i
++) {
1678 strbuf_addstr(buf
, ", ");
1680 strbuf_addstr(buf
, language_tags
[i
]);
1683 strbuf_addf(buf
, q_format
, max_q
- i
);
1685 if (buf
->len
> MAX_ACCEPT_LANGUAGE_HEADER_SIZE
) {
1686 strbuf_remove(buf
, last_buf_len
, buf
->len
- last_buf_len
);
1690 last_buf_len
= buf
->len
;
1694 /* free language tags -- last one is a static '*' */
1695 for (i
= 0; i
< num_langs
- 1; i
++)
1696 free(language_tags
[i
]);
1697 free(language_tags
);
1701 * Get an Accept-Language header which indicates user's preferred languages.
1705 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1706 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1707 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1708 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1709 * LANGUAGE= LANG=C -> ""
1711 static const char *get_accept_language(void)
1713 if (!cached_accept_language
) {
1714 struct strbuf buf
= STRBUF_INIT
;
1715 write_accept_language(&buf
);
1717 cached_accept_language
= strbuf_detach(&buf
, NULL
);
1720 return cached_accept_language
;
1723 static void http_opt_request_remainder(CURL
*curl
, off_t pos
)
1726 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
"-", (uintmax_t)pos
);
1727 curl_easy_setopt(curl
, CURLOPT_RANGE
, buf
);
1730 /* http_request() targets */
1731 #define HTTP_REQUEST_STRBUF 0
1732 #define HTTP_REQUEST_FILE 1
1734 static int http_request(const char *url
,
1735 void *result
, int target
,
1736 const struct http_get_options
*options
)
1738 struct active_request_slot
*slot
;
1739 struct slot_results results
;
1740 struct curl_slist
*headers
= http_copy_default_headers();
1741 struct strbuf buf
= STRBUF_INIT
;
1742 const char *accept_language
;
1745 slot
= get_active_slot();
1746 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1748 if (result
== NULL
) {
1749 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
1751 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1752 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
1754 if (target
== HTTP_REQUEST_FILE
) {
1755 off_t posn
= ftello(result
);
1756 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1759 http_opt_request_remainder(slot
->curl
, posn
);
1761 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1765 accept_language
= get_accept_language();
1767 if (accept_language
)
1768 headers
= curl_slist_append(headers
, accept_language
);
1770 strbuf_addstr(&buf
, "Pragma:");
1771 if (options
&& options
->no_cache
)
1772 strbuf_addstr(&buf
, " no-cache");
1773 if (options
&& options
->keep_error
)
1774 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1775 if (options
&& options
->initial_request
&&
1776 http_follow_config
== HTTP_FOLLOW_INITIAL
)
1777 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1779 headers
= curl_slist_append(headers
, buf
.buf
);
1781 /* Add additional headers here */
1782 if (options
&& options
->extra_headers
) {
1783 const struct string_list_item
*item
;
1784 for_each_string_list_item(item
, options
->extra_headers
) {
1785 headers
= curl_slist_append(headers
, item
->string
);
1789 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1790 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1791 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
1793 ret
= run_one_slot(slot
, &results
);
1795 if (options
&& options
->content_type
) {
1796 struct strbuf raw
= STRBUF_INIT
;
1797 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
, &raw
);
1798 extract_content_type(&raw
, options
->content_type
,
1800 strbuf_release(&raw
);
1803 if (options
&& options
->effective_url
)
1804 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
1805 options
->effective_url
);
1807 curl_slist_free_all(headers
);
1808 strbuf_release(&buf
);
1814 * Update the "base" url to a more appropriate value, as deduced by
1815 * redirects seen when requesting a URL starting with "url".
1817 * The "asked" parameter is a URL that we asked curl to access, and must begin
1820 * The "got" parameter is the URL that curl reported to us as where we ended
1823 * Returns 1 if we updated the base url, 0 otherwise.
1825 * Our basic strategy is to compare "base" and "asked" to find the bits
1826 * specific to our request. We then strip those bits off of "got" to yield the
1827 * new base. So for example, if our base is "http://example.com/foo.git",
1828 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1829 * with "https://other.example.com/foo.git/info/refs". We would want the
1830 * new URL to become "https://other.example.com/foo.git".
1832 * Note that this assumes a sane redirect scheme. It's entirely possible
1833 * in the example above to end up at a URL that does not even end in
1834 * "info/refs". In such a case we die. There's not much we can do, such a
1835 * scheme is unlikely to represent a real git repository, and failing to
1836 * rewrite the base opens options for malicious redirects to do funny things.
1838 static int update_url_from_redirect(struct strbuf
*base
,
1840 const struct strbuf
*got
)
1845 if (!strcmp(asked
, got
->buf
))
1848 if (!skip_prefix(asked
, base
->buf
, &tail
))
1849 BUG("update_url_from_redirect: %s is not a superset of %s",
1853 if (!strip_suffix_mem(got
->buf
, &new_len
, tail
))
1854 die(_("unable to update url base from redirection:\n"
1860 strbuf_add(base
, got
->buf
, new_len
);
1865 static int http_request_reauth(const char *url
,
1866 void *result
, int target
,
1867 struct http_get_options
*options
)
1869 int ret
= http_request(url
, result
, target
, options
);
1871 if (ret
!= HTTP_OK
&& ret
!= HTTP_REAUTH
)
1874 if (options
&& options
->effective_url
&& options
->base_url
) {
1875 if (update_url_from_redirect(options
->base_url
,
1876 url
, options
->effective_url
)) {
1877 credential_from_url(&http_auth
, options
->base_url
->buf
);
1878 url
= options
->effective_url
->buf
;
1882 if (ret
!= HTTP_REAUTH
)
1886 * If we are using KEEP_ERROR, the previous request may have
1887 * put cruft into our output stream; we should clear it out before
1888 * making our next request. We only know how to do this for
1889 * the strbuf case, but that is enough to satisfy current callers.
1891 if (options
&& options
->keep_error
) {
1893 case HTTP_REQUEST_STRBUF
:
1894 strbuf_reset(result
);
1897 BUG("HTTP_KEEP_ERROR is only supported with strbufs");
1901 credential_fill(&http_auth
);
1903 return http_request(url
, result
, target
, options
);
1906 int http_get_strbuf(const char *url
,
1907 struct strbuf
*result
,
1908 struct http_get_options
*options
)
1910 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
1914 * Downloads a URL and stores the result in the given file.
1916 * If a previous interrupted download is detected (i.e. a previous temporary
1917 * file is still around) the download is resumed.
1919 static int http_get_file(const char *url
, const char *filename
,
1920 struct http_get_options
*options
)
1923 struct strbuf tmpfile
= STRBUF_INIT
;
1926 strbuf_addf(&tmpfile
, "%s.temp", filename
);
1927 result
= fopen(tmpfile
.buf
, "a");
1929 error("Unable to open local file %s", tmpfile
.buf
);
1934 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
1937 if (ret
== HTTP_OK
&& finalize_object_file(tmpfile
.buf
, filename
))
1940 strbuf_release(&tmpfile
);
1944 int http_fetch_ref(const char *base
, struct ref
*ref
)
1946 struct http_get_options options
= {0};
1948 struct strbuf buffer
= STRBUF_INIT
;
1951 options
.no_cache
= 1;
1953 url
= quote_ref_url(base
, ref
->name
);
1954 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
1955 strbuf_rtrim(&buffer
);
1956 if (buffer
.len
== 40)
1957 ret
= get_oid_hex(buffer
.buf
, &ref
->old_oid
);
1958 else if (starts_with(buffer
.buf
, "ref: ")) {
1959 ref
->symref
= xstrdup(buffer
.buf
+ 5);
1964 strbuf_release(&buffer
);
1969 /* Helpers for fetching packs */
1970 static char *fetch_pack_index(unsigned char *sha1
, const char *base_url
)
1973 struct strbuf buf
= STRBUF_INIT
;
1975 if (http_is_verbose
)
1976 fprintf(stderr
, "Getting index for pack %s\n", sha1_to_hex(sha1
));
1978 end_url_with_slash(&buf
, base_url
);
1979 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", sha1_to_hex(sha1
));
1980 url
= strbuf_detach(&buf
, NULL
);
1982 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(sha1
));
1983 tmp
= strbuf_detach(&buf
, NULL
);
1985 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
1986 error("Unable to get pack index %s", url
);
1994 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
1995 unsigned char *sha1
, const char *base_url
)
1997 struct packed_git
*new_pack
;
1998 char *tmp_idx
= NULL
;
2001 if (has_pack_index(sha1
)) {
2002 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
2004 return -1; /* parse_pack_index() already issued error message */
2008 tmp_idx
= fetch_pack_index(sha1
, base_url
);
2012 new_pack
= parse_pack_index(sha1
, tmp_idx
);
2017 return -1; /* parse_pack_index() already issued error message */
2020 ret
= verify_pack_index(new_pack
);
2022 close_pack_index(new_pack
);
2023 ret
= finalize_object_file(tmp_idx
, sha1_pack_index_name(sha1
));
2030 new_pack
->next
= *packs_head
;
2031 *packs_head
= new_pack
;
2035 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
2037 struct http_get_options options
= {0};
2040 struct strbuf buf
= STRBUF_INIT
;
2041 unsigned char sha1
[20];
2043 end_url_with_slash(&buf
, base_url
);
2044 strbuf_addstr(&buf
, "objects/info/packs");
2045 url
= strbuf_detach(&buf
, NULL
);
2047 options
.no_cache
= 1;
2048 ret
= http_get_strbuf(url
, &buf
, &options
);
2053 while (i
< buf
.len
) {
2057 if (i
+ 52 <= buf
.len
&&
2058 starts_with(data
+ i
, " pack-") &&
2059 starts_with(data
+ i
+ 46, ".pack\n")) {
2060 get_sha1_hex(data
+ i
+ 6, sha1
);
2061 fetch_and_setup_pack_index(packs_head
, sha1
,
2067 while (i
< buf
.len
&& data
[i
] != '\n')
2078 void release_http_pack_request(struct http_pack_request
*preq
)
2080 if (preq
->packfile
!= NULL
) {
2081 fclose(preq
->packfile
);
2082 preq
->packfile
= NULL
;
2089 int finish_http_pack_request(struct http_pack_request
*preq
)
2091 struct packed_git
**lst
;
2092 struct packed_git
*p
= preq
->target
;
2095 struct child_process ip
= CHILD_PROCESS_INIT
;
2097 close_pack_index(p
);
2099 fclose(preq
->packfile
);
2100 preq
->packfile
= NULL
;
2104 lst
= &((*lst
)->next
);
2105 *lst
= (*lst
)->next
;
2107 if (!strip_suffix(preq
->tmpfile
, ".pack.temp", &len
))
2108 BUG("pack tmpfile does not end in .pack.temp?");
2109 tmp_idx
= xstrfmt("%.*s.idx.temp", (int)len
, preq
->tmpfile
);
2111 argv_array_push(&ip
.args
, "index-pack");
2112 argv_array_pushl(&ip
.args
, "-o", tmp_idx
, NULL
);
2113 argv_array_push(&ip
.args
, preq
->tmpfile
);
2118 if (run_command(&ip
)) {
2119 unlink(preq
->tmpfile
);
2125 unlink(sha1_pack_index_name(p
->sha1
));
2127 if (finalize_object_file(preq
->tmpfile
, sha1_pack_name(p
->sha1
))
2128 || finalize_object_file(tmp_idx
, sha1_pack_index_name(p
->sha1
))) {
2133 install_packed_git(the_repository
, p
);
2138 struct http_pack_request
*new_http_pack_request(
2139 struct packed_git
*target
, const char *base_url
)
2141 off_t prev_posn
= 0;
2142 struct strbuf buf
= STRBUF_INIT
;
2143 struct http_pack_request
*preq
;
2145 preq
= xcalloc(1, sizeof(*preq
));
2146 preq
->target
= target
;
2148 end_url_with_slash(&buf
, base_url
);
2149 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
2150 sha1_to_hex(target
->sha1
));
2151 preq
->url
= strbuf_detach(&buf
, NULL
);
2153 snprintf(preq
->tmpfile
, sizeof(preq
->tmpfile
), "%s.temp",
2154 sha1_pack_name(target
->sha1
));
2155 preq
->packfile
= fopen(preq
->tmpfile
, "a");
2156 if (!preq
->packfile
) {
2157 error("Unable to open local file %s for pack",
2162 preq
->slot
= get_active_slot();
2163 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
2164 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
2165 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
2166 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
2170 * If there is data present from a previous transfer attempt,
2171 * resume where it left off
2173 prev_posn
= ftello(preq
->packfile
);
2175 if (http_is_verbose
)
2177 "Resuming fetch of pack %s at byte %"PRIuMAX
"\n",
2178 sha1_to_hex(target
->sha1
), (uintmax_t)prev_posn
);
2179 http_opt_request_remainder(preq
->slot
->curl
, prev_posn
);
2190 /* Helpers for fetching objects (loose) */
2191 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
2194 unsigned char expn
[4096];
2195 size_t size
= eltsize
* nmemb
;
2197 struct http_object_request
*freq
= data
;
2198 struct active_request_slot
*slot
= freq
->slot
;
2201 CURLcode c
= curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
,
2204 BUG("curl_easy_getinfo for HTTP code failed: %s",
2205 curl_easy_strerror(c
));
2206 if (slot
->http_code
>= 300)
2211 ssize_t retval
= xwrite(freq
->localfile
,
2212 (char *) ptr
+ posn
, size
- posn
);
2216 } while (posn
< size
);
2218 freq
->stream
.avail_in
= size
;
2219 freq
->stream
.next_in
= (void *)ptr
;
2221 freq
->stream
.next_out
= expn
;
2222 freq
->stream
.avail_out
= sizeof(expn
);
2223 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
2224 git_SHA1_Update(&freq
->c
, expn
,
2225 sizeof(expn
) - freq
->stream
.avail_out
);
2226 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
2230 struct http_object_request
*new_http_object_request(const char *base_url
,
2231 unsigned char *sha1
)
2233 char *hex
= sha1_to_hex(sha1
);
2234 struct strbuf filename
= STRBUF_INIT
;
2235 char prevfile
[PATH_MAX
];
2237 char prev_buf
[PREV_BUF_SIZE
];
2238 ssize_t prev_read
= 0;
2239 off_t prev_posn
= 0;
2240 struct http_object_request
*freq
;
2242 freq
= xcalloc(1, sizeof(*freq
));
2243 hashcpy(freq
->sha1
, sha1
);
2244 freq
->localfile
= -1;
2246 sha1_file_name(the_repository
, &filename
, sha1
);
2247 snprintf(freq
->tmpfile
, sizeof(freq
->tmpfile
),
2248 "%s.temp", filename
.buf
);
2250 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
.buf
);
2251 unlink_or_warn(prevfile
);
2252 rename(freq
->tmpfile
, prevfile
);
2253 unlink_or_warn(freq
->tmpfile
);
2254 strbuf_release(&filename
);
2256 if (freq
->localfile
!= -1)
2257 error("fd leakage in start: %d", freq
->localfile
);
2258 freq
->localfile
= open(freq
->tmpfile
,
2259 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2261 * This could have failed due to the "lazy directory creation";
2262 * try to mkdir the last path component.
2264 if (freq
->localfile
< 0 && errno
== ENOENT
) {
2265 char *dir
= strrchr(freq
->tmpfile
, '/');
2268 mkdir(freq
->tmpfile
, 0777);
2271 freq
->localfile
= open(freq
->tmpfile
,
2272 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2275 if (freq
->localfile
< 0) {
2276 error_errno("Couldn't create temporary file %s", freq
->tmpfile
);
2280 git_inflate_init(&freq
->stream
);
2282 git_SHA1_Init(&freq
->c
);
2284 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
2287 * If a previous temp file is present, process what was already
2290 prevlocal
= open(prevfile
, O_RDONLY
);
2291 if (prevlocal
!= -1) {
2293 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
2295 if (fwrite_sha1_file(prev_buf
,
2298 freq
) == prev_read
) {
2299 prev_posn
+= prev_read
;
2304 } while (prev_read
> 0);
2307 unlink_or_warn(prevfile
);
2310 * Reset inflate/SHA1 if there was an error reading the previous temp
2311 * file; also rewind to the beginning of the local file.
2313 if (prev_read
== -1) {
2314 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
2315 git_inflate_init(&freq
->stream
);
2316 git_SHA1_Init(&freq
->c
);
2319 lseek(freq
->localfile
, 0, SEEK_SET
);
2320 if (ftruncate(freq
->localfile
, 0) < 0) {
2321 error_errno("Couldn't truncate temporary file %s",
2328 freq
->slot
= get_active_slot();
2330 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
2331 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FAILONERROR
, 0);
2332 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
2333 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
2334 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
2335 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
2338 * If we have successfully processed data from a previous fetch
2339 * attempt, only fetch the data we don't already have.
2342 if (http_is_verbose
)
2344 "Resuming fetch of object %s at byte %"PRIuMAX
"\n",
2345 hex
, (uintmax_t)prev_posn
);
2346 http_opt_request_remainder(freq
->slot
->curl
, prev_posn
);
2357 void process_http_object_request(struct http_object_request
*freq
)
2359 if (freq
->slot
== NULL
)
2361 freq
->curl_result
= freq
->slot
->curl_result
;
2362 freq
->http_code
= freq
->slot
->http_code
;
2366 int finish_http_object_request(struct http_object_request
*freq
)
2369 struct strbuf filename
= STRBUF_INIT
;
2371 close(freq
->localfile
);
2372 freq
->localfile
= -1;
2374 process_http_object_request(freq
);
2376 if (freq
->http_code
== 416) {
2377 warning("requested range invalid; we may already have all the data.");
2378 } else if (freq
->curl_result
!= CURLE_OK
) {
2379 if (stat(freq
->tmpfile
, &st
) == 0)
2380 if (st
.st_size
== 0)
2381 unlink_or_warn(freq
->tmpfile
);
2385 git_inflate_end(&freq
->stream
);
2386 git_SHA1_Final(freq
->real_sha1
, &freq
->c
);
2387 if (freq
->zret
!= Z_STREAM_END
) {
2388 unlink_or_warn(freq
->tmpfile
);
2391 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
2392 unlink_or_warn(freq
->tmpfile
);
2395 sha1_file_name(the_repository
, &filename
, freq
->sha1
);
2396 freq
->rename
= finalize_object_file(freq
->tmpfile
, filename
.buf
);
2397 strbuf_release(&filename
);
2399 return freq
->rename
;
2402 void abort_http_object_request(struct http_object_request
*freq
)
2404 unlink_or_warn(freq
->tmpfile
);
2406 release_http_object_request(freq
);
2409 void release_http_object_request(struct http_object_request
*freq
)
2411 if (freq
->localfile
!= -1) {
2412 close(freq
->localfile
);
2413 freq
->localfile
= -1;
2415 if (freq
->url
!= NULL
) {
2416 FREE_AND_NULL(freq
->url
);
2418 if (freq
->slot
!= NULL
) {
2419 freq
->slot
->callback_func
= NULL
;
2420 freq
->slot
->callback_data
= NULL
;
2421 release_active_slot(freq
->slot
);