1 #include "git-compat-util.h"
6 #include "run-command.h"
9 #include "credential.h"
13 #include "transport.h"
17 static struct trace_key trace_curl
= TRACE_KEY_INIT(CURL
);
18 #if LIBCURL_VERSION_NUM >= 0x070a08
19 long int git_curl_ipresolve
= CURL_IPRESOLVE_WHATEVER
;
21 long int git_curl_ipresolve
;
25 ssize_t http_post_buffer
= 16 * LARGE_PACKET_MAX
;
27 #if LIBCURL_VERSION_NUM >= 0x070a06
28 #define LIBCURL_CAN_HANDLE_AUTH_ANY
31 static int min_curl_sessions
= 1;
32 static int curl_session_count
;
34 static int max_requests
= -1;
37 #ifndef NO_CURL_EASY_DUPHANDLE
38 static CURL
*curl_default
;
41 #define PREV_BUF_SIZE 4096
43 char curl_errorstr
[CURL_ERROR_SIZE
];
45 static int curl_ssl_verify
= -1;
46 static int curl_ssl_try
;
47 static const char *ssl_cert
;
48 static const char *ssl_cipherlist
;
49 static const char *ssl_version
;
54 { "sslv2", CURL_SSLVERSION_SSLv2
},
55 { "sslv3", CURL_SSLVERSION_SSLv3
},
56 { "tlsv1", CURL_SSLVERSION_TLSv1
},
57 #if LIBCURL_VERSION_NUM >= 0x072200
58 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0
},
59 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1
},
60 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2
},
63 #if LIBCURL_VERSION_NUM >= 0x070903
64 static const char *ssl_key
;
66 #if LIBCURL_VERSION_NUM >= 0x070908
67 static const char *ssl_capath
;
69 #if LIBCURL_VERSION_NUM >= 0x072c00
70 static const char *ssl_pinnedkey
;
72 static const char *ssl_cainfo
;
73 static long curl_low_speed_limit
= -1;
74 static long curl_low_speed_time
= -1;
75 static int curl_ftp_no_epsv
;
76 static const char *curl_http_proxy
;
77 static const char *curl_no_proxy
;
78 static const char *http_proxy_authmethod
;
82 } proxy_authmethods
[] = {
83 { "basic", CURLAUTH_BASIC
},
84 { "digest", CURLAUTH_DIGEST
},
85 { "negotiate", CURLAUTH_GSSNEGOTIATE
},
86 { "ntlm", CURLAUTH_NTLM
},
87 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
88 { "anyauth", CURLAUTH_ANY
},
91 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
92 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
96 #ifdef CURLGSSAPI_DELEGATION_FLAG
97 static const char *curl_deleg
;
100 long curl_deleg_param
;
101 } curl_deleg_levels
[] = {
102 { "none", CURLGSSAPI_DELEGATION_NONE
},
103 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG
},
104 { "always", CURLGSSAPI_DELEGATION_FLAG
},
108 static struct credential proxy_auth
= CREDENTIAL_INIT
;
109 static const char *curl_proxyuserpwd
;
110 static const char *curl_cookie_file
;
111 static int curl_save_cookies
;
112 struct credential http_auth
= CREDENTIAL_INIT
;
113 static int http_proactive_auth
;
114 static const char *user_agent
;
115 static int curl_empty_auth
= -1;
117 enum http_follow_config http_follow_config
= HTTP_FOLLOW_INITIAL
;
119 #if LIBCURL_VERSION_NUM >= 0x071700
120 /* Use CURLOPT_KEYPASSWD as is */
121 #elif LIBCURL_VERSION_NUM >= 0x070903
122 #define CURLOPT_KEYPASSWD CURLOPT_SSLKEYPASSWD
124 #define CURLOPT_KEYPASSWD CURLOPT_SSLCERTPASSWD
127 static struct credential cert_auth
= CREDENTIAL_INIT
;
128 static int ssl_cert_password_required
;
129 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
130 static unsigned long http_auth_methods
= CURLAUTH_ANY
;
131 static int http_auth_methods_restricted
;
132 /* Modes for which empty_auth cannot actually help us. */
133 static unsigned long empty_auth_useless
=
135 #ifdef CURLAUTH_DIGEST_IE
141 static struct curl_slist
*pragma_header
;
142 static struct curl_slist
*no_pragma_header
;
143 static struct curl_slist
*extra_http_headers
;
145 static struct active_request_slot
*active_queue_head
;
147 static char *cached_accept_language
;
149 size_t fread_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
151 size_t size
= eltsize
* nmemb
;
152 struct buffer
*buffer
= buffer_
;
154 if (size
> buffer
->buf
.len
- buffer
->posn
)
155 size
= buffer
->buf
.len
- buffer
->posn
;
156 memcpy(ptr
, buffer
->buf
.buf
+ buffer
->posn
, size
);
157 buffer
->posn
+= size
;
162 #ifndef NO_CURL_IOCTL
163 curlioerr
ioctl_buffer(CURL
*handle
, int cmd
, void *clientp
)
165 struct buffer
*buffer
= clientp
;
171 case CURLIOCMD_RESTARTREAD
:
176 return CURLIOE_UNKNOWNCMD
;
181 size_t fwrite_buffer(char *ptr
, size_t eltsize
, size_t nmemb
, void *buffer_
)
183 size_t size
= eltsize
* nmemb
;
184 struct strbuf
*buffer
= buffer_
;
186 strbuf_add(buffer
, ptr
, size
);
190 size_t fwrite_null(char *ptr
, size_t eltsize
, size_t nmemb
, void *strbuf
)
192 return eltsize
* nmemb
;
195 static void closedown_active_slot(struct active_request_slot
*slot
)
201 static void finish_active_slot(struct active_request_slot
*slot
)
203 closedown_active_slot(slot
);
204 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
, &slot
->http_code
);
206 if (slot
->finished
!= NULL
)
207 (*slot
->finished
) = 1;
209 /* Store slot results so they can be read after the slot is reused */
210 if (slot
->results
!= NULL
) {
211 slot
->results
->curl_result
= slot
->curl_result
;
212 slot
->results
->http_code
= slot
->http_code
;
213 #if LIBCURL_VERSION_NUM >= 0x070a08
214 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTPAUTH_AVAIL
,
215 &slot
->results
->auth_avail
);
217 slot
->results
->auth_avail
= 0;
220 curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CONNECTCODE
,
221 &slot
->results
->http_connectcode
);
224 /* Run callback if appropriate */
225 if (slot
->callback_func
!= NULL
)
226 slot
->callback_func(slot
->callback_data
);
229 static void xmulti_remove_handle(struct active_request_slot
*slot
)
231 #ifdef USE_CURL_MULTI
232 curl_multi_remove_handle(curlm
, slot
->curl
);
236 #ifdef USE_CURL_MULTI
237 static void process_curl_messages(void)
240 struct active_request_slot
*slot
;
241 CURLMsg
*curl_message
= curl_multi_info_read(curlm
, &num_messages
);
243 while (curl_message
!= NULL
) {
244 if (curl_message
->msg
== CURLMSG_DONE
) {
245 int curl_result
= curl_message
->data
.result
;
246 slot
= active_queue_head
;
247 while (slot
!= NULL
&&
248 slot
->curl
!= curl_message
->easy_handle
)
251 xmulti_remove_handle(slot
);
252 slot
->curl_result
= curl_result
;
253 finish_active_slot(slot
);
255 fprintf(stderr
, "Received DONE message for unknown request!\n");
258 fprintf(stderr
, "Unknown CURL message received: %d\n",
259 (int)curl_message
->msg
);
261 curl_message
= curl_multi_info_read(curlm
, &num_messages
);
266 static int http_options(const char *var
, const char *value
, void *cb
)
268 if (!strcmp("http.sslverify", var
)) {
269 curl_ssl_verify
= git_config_bool(var
, value
);
272 if (!strcmp("http.sslcipherlist", var
))
273 return git_config_string(&ssl_cipherlist
, var
, value
);
274 if (!strcmp("http.sslversion", var
))
275 return git_config_string(&ssl_version
, var
, value
);
276 if (!strcmp("http.sslcert", var
))
277 return git_config_pathname(&ssl_cert
, var
, value
);
278 #if LIBCURL_VERSION_NUM >= 0x070903
279 if (!strcmp("http.sslkey", var
))
280 return git_config_pathname(&ssl_key
, var
, value
);
282 #if LIBCURL_VERSION_NUM >= 0x070908
283 if (!strcmp("http.sslcapath", var
))
284 return git_config_pathname(&ssl_capath
, var
, value
);
286 if (!strcmp("http.sslcainfo", var
))
287 return git_config_pathname(&ssl_cainfo
, var
, value
);
288 if (!strcmp("http.sslcertpasswordprotected", var
)) {
289 ssl_cert_password_required
= git_config_bool(var
, value
);
292 if (!strcmp("http.ssltry", var
)) {
293 curl_ssl_try
= git_config_bool(var
, value
);
296 if (!strcmp("http.minsessions", var
)) {
297 min_curl_sessions
= git_config_int(var
, value
);
298 #ifndef USE_CURL_MULTI
299 if (min_curl_sessions
> 1)
300 min_curl_sessions
= 1;
304 #ifdef USE_CURL_MULTI
305 if (!strcmp("http.maxrequests", var
)) {
306 max_requests
= git_config_int(var
, value
);
310 if (!strcmp("http.lowspeedlimit", var
)) {
311 curl_low_speed_limit
= (long)git_config_int(var
, value
);
314 if (!strcmp("http.lowspeedtime", var
)) {
315 curl_low_speed_time
= (long)git_config_int(var
, value
);
319 if (!strcmp("http.noepsv", var
)) {
320 curl_ftp_no_epsv
= git_config_bool(var
, value
);
323 if (!strcmp("http.proxy", var
))
324 return git_config_string(&curl_http_proxy
, var
, value
);
326 if (!strcmp("http.proxyauthmethod", var
))
327 return git_config_string(&http_proxy_authmethod
, var
, value
);
329 if (!strcmp("http.cookiefile", var
))
330 return git_config_pathname(&curl_cookie_file
, var
, value
);
331 if (!strcmp("http.savecookies", var
)) {
332 curl_save_cookies
= git_config_bool(var
, value
);
336 if (!strcmp("http.postbuffer", var
)) {
337 http_post_buffer
= git_config_ssize_t(var
, value
);
338 if (http_post_buffer
< 0)
339 warning(_("negative value for http.postbuffer; defaulting to %d"), LARGE_PACKET_MAX
);
340 if (http_post_buffer
< LARGE_PACKET_MAX
)
341 http_post_buffer
= LARGE_PACKET_MAX
;
345 if (!strcmp("http.useragent", var
))
346 return git_config_string(&user_agent
, var
, value
);
348 if (!strcmp("http.emptyauth", var
)) {
349 if (value
&& !strcmp("auto", value
))
350 curl_empty_auth
= -1;
352 curl_empty_auth
= git_config_bool(var
, value
);
356 if (!strcmp("http.delegation", var
)) {
357 #ifdef CURLGSSAPI_DELEGATION_FLAG
358 return git_config_string(&curl_deleg
, var
, value
);
360 warning(_("Delegation control is not supported with cURL < 7.22.0"));
365 if (!strcmp("http.pinnedpubkey", var
)) {
366 #if LIBCURL_VERSION_NUM >= 0x072c00
367 return git_config_pathname(&ssl_pinnedkey
, var
, value
);
369 warning(_("Public key pinning not supported with cURL < 7.44.0"));
374 if (!strcmp("http.extraheader", var
)) {
376 return config_error_nonbool(var
);
377 } else if (!*value
) {
378 curl_slist_free_all(extra_http_headers
);
379 extra_http_headers
= NULL
;
382 curl_slist_append(extra_http_headers
, value
);
387 if (!strcmp("http.followredirects", var
)) {
388 if (value
&& !strcmp(value
, "initial"))
389 http_follow_config
= HTTP_FOLLOW_INITIAL
;
390 else if (git_config_bool(var
, value
))
391 http_follow_config
= HTTP_FOLLOW_ALWAYS
;
393 http_follow_config
= HTTP_FOLLOW_NONE
;
397 /* Fall back on the default ones */
398 return git_default_config(var
, value
, cb
);
401 static int curl_empty_auth_enabled(void)
403 if (curl_empty_auth
>= 0)
404 return curl_empty_auth
;
406 #ifndef LIBCURL_CAN_HANDLE_AUTH_ANY
408 * Our libcurl is too old to do AUTH_ANY in the first place;
409 * just default to turning the feature off.
413 * In the automatic case, kick in the empty-auth
414 * hack as long as we would potentially try some
415 * method more exotic than "Basic" or "Digest".
417 * But only do this when this is our second or
418 * subsequent request, as by then we know what
419 * methods are available.
421 if (http_auth_methods_restricted
&&
422 (http_auth_methods
& ~empty_auth_useless
))
428 static void init_curl_http_auth(CURL
*result
)
430 if (!http_auth
.username
|| !*http_auth
.username
) {
431 if (curl_empty_auth_enabled())
432 curl_easy_setopt(result
, CURLOPT_USERPWD
, ":");
436 credential_fill(&http_auth
);
438 #if LIBCURL_VERSION_NUM >= 0x071301
439 curl_easy_setopt(result
, CURLOPT_USERNAME
, http_auth
.username
);
440 curl_easy_setopt(result
, CURLOPT_PASSWORD
, http_auth
.password
);
443 static struct strbuf up
= STRBUF_INIT
;
445 * Note that we assume we only ever have a single set of
446 * credentials in a given program run, so we do not have
447 * to worry about updating this buffer, only setting its
451 strbuf_addf(&up
, "%s:%s",
452 http_auth
.username
, http_auth
.password
);
453 curl_easy_setopt(result
, CURLOPT_USERPWD
, up
.buf
);
458 /* *var must be free-able */
459 static void var_override(const char **var
, char *value
)
463 *var
= xstrdup(value
);
467 static void set_proxyauth_name_password(CURL
*result
)
469 #if LIBCURL_VERSION_NUM >= 0x071301
470 curl_easy_setopt(result
, CURLOPT_PROXYUSERNAME
,
471 proxy_auth
.username
);
472 curl_easy_setopt(result
, CURLOPT_PROXYPASSWORD
,
473 proxy_auth
.password
);
475 struct strbuf s
= STRBUF_INIT
;
477 strbuf_addstr_urlencode(&s
, proxy_auth
.username
, 1);
478 strbuf_addch(&s
, ':');
479 strbuf_addstr_urlencode(&s
, proxy_auth
.password
, 1);
480 curl_proxyuserpwd
= strbuf_detach(&s
, NULL
);
481 curl_easy_setopt(result
, CURLOPT_PROXYUSERPWD
, curl_proxyuserpwd
);
485 static void init_curl_proxy_auth(CURL
*result
)
487 if (proxy_auth
.username
) {
488 if (!proxy_auth
.password
)
489 credential_fill(&proxy_auth
);
490 set_proxyauth_name_password(result
);
493 var_override(&http_proxy_authmethod
, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
495 #if LIBCURL_VERSION_NUM >= 0x070a07 /* CURLOPT_PROXYAUTH and CURLAUTH_ANY */
496 if (http_proxy_authmethod
) {
498 for (i
= 0; i
< ARRAY_SIZE(proxy_authmethods
); i
++) {
499 if (!strcmp(http_proxy_authmethod
, proxy_authmethods
[i
].name
)) {
500 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
,
501 proxy_authmethods
[i
].curlauth_param
);
505 if (i
== ARRAY_SIZE(proxy_authmethods
)) {
506 warning("unsupported proxy authentication method %s: using anyauth",
507 http_proxy_authmethod
);
508 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
512 curl_easy_setopt(result
, CURLOPT_PROXYAUTH
, CURLAUTH_ANY
);
516 static int has_cert_password(void)
518 if (ssl_cert
== NULL
|| ssl_cert_password_required
!= 1)
520 if (!cert_auth
.password
) {
521 cert_auth
.protocol
= xstrdup("cert");
522 cert_auth
.username
= xstrdup("");
523 cert_auth
.path
= xstrdup(ssl_cert
);
524 credential_fill(&cert_auth
);
529 #if LIBCURL_VERSION_NUM >= 0x071900
530 static void set_curl_keepalive(CURL
*c
)
532 curl_easy_setopt(c
, CURLOPT_TCP_KEEPALIVE
, 1);
535 #elif LIBCURL_VERSION_NUM >= 0x071000
536 static int sockopt_callback(void *client
, curl_socket_t fd
, curlsocktype type
)
540 socklen_t len
= (socklen_t
)sizeof(ka
);
542 if (type
!= CURLSOCKTYPE_IPCXN
)
545 rc
= setsockopt(fd
, SOL_SOCKET
, SO_KEEPALIVE
, (void *)&ka
, len
);
547 warning_errno("unable to set SO_KEEPALIVE on socket");
549 return 0; /* CURL_SOCKOPT_OK only exists since curl 7.21.5 */
552 static void set_curl_keepalive(CURL
*c
)
554 curl_easy_setopt(c
, CURLOPT_SOCKOPTFUNCTION
, sockopt_callback
);
558 static void set_curl_keepalive(CURL
*c
)
560 /* not supported on older curl versions */
564 static void redact_sensitive_header(struct strbuf
*header
)
566 const char *sensitive_header
;
568 if (skip_prefix(header
->buf
, "Authorization:", &sensitive_header
) ||
569 skip_prefix(header
->buf
, "Proxy-Authorization:", &sensitive_header
)) {
570 /* The first token is the type, which is OK to log */
571 while (isspace(*sensitive_header
))
573 while (*sensitive_header
&& !isspace(*sensitive_header
))
575 /* Everything else is opaque and possibly sensitive */
576 strbuf_setlen(header
, sensitive_header
- header
->buf
);
577 strbuf_addstr(header
, " <redacted>");
581 static void curl_dump_header(const char *text
, unsigned char *ptr
, size_t size
, int hide_sensitive_header
)
583 struct strbuf out
= STRBUF_INIT
;
584 struct strbuf
**headers
, **header
;
586 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
587 text
, (long)size
, (long)size
);
588 trace_strbuf(&trace_curl
, &out
);
590 strbuf_add(&out
, ptr
, size
);
591 headers
= strbuf_split_max(&out
, '\n', 0);
593 for (header
= headers
; *header
; header
++) {
594 if (hide_sensitive_header
)
595 redact_sensitive_header(*header
);
596 strbuf_insert((*header
), 0, text
, strlen(text
));
597 strbuf_insert((*header
), strlen(text
), ": ", 2);
598 strbuf_rtrim((*header
));
599 strbuf_addch((*header
), '\n');
600 trace_strbuf(&trace_curl
, (*header
));
602 strbuf_list_free(headers
);
603 strbuf_release(&out
);
606 static void curl_dump_data(const char *text
, unsigned char *ptr
, size_t size
)
609 struct strbuf out
= STRBUF_INIT
;
610 unsigned int width
= 60;
612 strbuf_addf(&out
, "%s, %10.10ld bytes (0x%8.8lx)\n",
613 text
, (long)size
, (long)size
);
614 trace_strbuf(&trace_curl
, &out
);
616 for (i
= 0; i
< size
; i
+= width
) {
620 strbuf_addf(&out
, "%s: ", text
);
621 for (w
= 0; (w
< width
) && (i
+ w
< size
); w
++) {
622 unsigned char ch
= ptr
[i
+ w
];
625 (ch
>= 0x20) && (ch
< 0x80)
628 strbuf_addch(&out
, '\n');
629 trace_strbuf(&trace_curl
, &out
);
631 strbuf_release(&out
);
634 static int curl_trace(CURL
*handle
, curl_infotype type
, char *data
, size_t size
, void *userp
)
637 enum { NO_FILTER
= 0, DO_FILTER
= 1 };
641 trace_printf_key(&trace_curl
, "== Info: %s", data
);
643 case CURLINFO_HEADER_OUT
:
644 text
= "=> Send header";
645 curl_dump_header(text
, (unsigned char *)data
, size
, DO_FILTER
);
647 case CURLINFO_DATA_OUT
:
648 text
= "=> Send data";
649 curl_dump_data(text
, (unsigned char *)data
, size
);
651 case CURLINFO_SSL_DATA_OUT
:
652 text
= "=> Send SSL data";
653 curl_dump_data(text
, (unsigned char *)data
, size
);
655 case CURLINFO_HEADER_IN
:
656 text
= "<= Recv header";
657 curl_dump_header(text
, (unsigned char *)data
, size
, NO_FILTER
);
659 case CURLINFO_DATA_IN
:
660 text
= "<= Recv data";
661 curl_dump_data(text
, (unsigned char *)data
, size
);
663 case CURLINFO_SSL_DATA_IN
:
664 text
= "<= Recv SSL data";
665 curl_dump_data(text
, (unsigned char *)data
, size
);
668 default: /* we ignore unknown types by default */
674 void setup_curl_trace(CURL
*handle
)
676 if (!trace_want(&trace_curl
))
678 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
679 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, curl_trace
);
680 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, NULL
);
683 #ifdef CURLPROTO_HTTP
684 static long get_curl_allowed_protocols(int from_user
)
686 long allowed_protocols
= 0;
688 if (is_transport_allowed("http", from_user
))
689 allowed_protocols
|= CURLPROTO_HTTP
;
690 if (is_transport_allowed("https", from_user
))
691 allowed_protocols
|= CURLPROTO_HTTPS
;
692 if (is_transport_allowed("ftp", from_user
))
693 allowed_protocols
|= CURLPROTO_FTP
;
694 if (is_transport_allowed("ftps", from_user
))
695 allowed_protocols
|= CURLPROTO_FTPS
;
697 return allowed_protocols
;
701 static CURL
*get_curl_handle(void)
703 CURL
*result
= curl_easy_init();
706 die("curl_easy_init failed");
708 if (!curl_ssl_verify
) {
709 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 0);
710 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 0);
712 /* Verify authenticity of the peer's certificate */
713 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYPEER
, 1);
714 /* The name in the cert must match whom we tried to connect */
715 curl_easy_setopt(result
, CURLOPT_SSL_VERIFYHOST
, 2);
718 #if LIBCURL_VERSION_NUM >= 0x070907
719 curl_easy_setopt(result
, CURLOPT_NETRC
, CURL_NETRC_OPTIONAL
);
721 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
722 curl_easy_setopt(result
, CURLOPT_HTTPAUTH
, CURLAUTH_ANY
);
725 #ifdef CURLGSSAPI_DELEGATION_FLAG
728 for (i
= 0; i
< ARRAY_SIZE(curl_deleg_levels
); i
++) {
729 if (!strcmp(curl_deleg
, curl_deleg_levels
[i
].name
)) {
730 curl_easy_setopt(result
, CURLOPT_GSSAPI_DELEGATION
,
731 curl_deleg_levels
[i
].curl_deleg_param
);
735 if (i
== ARRAY_SIZE(curl_deleg_levels
))
736 warning("Unknown delegation method '%s': using default",
741 if (http_proactive_auth
)
742 init_curl_http_auth(result
);
744 if (getenv("GIT_SSL_VERSION"))
745 ssl_version
= getenv("GIT_SSL_VERSION");
746 if (ssl_version
&& *ssl_version
) {
748 for (i
= 0; i
< ARRAY_SIZE(sslversions
); i
++) {
749 if (!strcmp(ssl_version
, sslversions
[i
].name
)) {
750 curl_easy_setopt(result
, CURLOPT_SSLVERSION
,
751 sslversions
[i
].ssl_version
);
755 if (i
== ARRAY_SIZE(sslversions
))
756 warning("unsupported ssl version %s: using default",
760 if (getenv("GIT_SSL_CIPHER_LIST"))
761 ssl_cipherlist
= getenv("GIT_SSL_CIPHER_LIST");
762 if (ssl_cipherlist
!= NULL
&& *ssl_cipherlist
)
763 curl_easy_setopt(result
, CURLOPT_SSL_CIPHER_LIST
,
766 if (ssl_cert
!= NULL
)
767 curl_easy_setopt(result
, CURLOPT_SSLCERT
, ssl_cert
);
768 if (has_cert_password())
769 curl_easy_setopt(result
, CURLOPT_KEYPASSWD
, cert_auth
.password
);
770 #if LIBCURL_VERSION_NUM >= 0x070903
772 curl_easy_setopt(result
, CURLOPT_SSLKEY
, ssl_key
);
774 #if LIBCURL_VERSION_NUM >= 0x070908
775 if (ssl_capath
!= NULL
)
776 curl_easy_setopt(result
, CURLOPT_CAPATH
, ssl_capath
);
778 #if LIBCURL_VERSION_NUM >= 0x072c00
779 if (ssl_pinnedkey
!= NULL
)
780 curl_easy_setopt(result
, CURLOPT_PINNEDPUBLICKEY
, ssl_pinnedkey
);
782 if (ssl_cainfo
!= NULL
)
783 curl_easy_setopt(result
, CURLOPT_CAINFO
, ssl_cainfo
);
785 if (curl_low_speed_limit
> 0 && curl_low_speed_time
> 0) {
786 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_LIMIT
,
787 curl_low_speed_limit
);
788 curl_easy_setopt(result
, CURLOPT_LOW_SPEED_TIME
,
789 curl_low_speed_time
);
792 curl_easy_setopt(result
, CURLOPT_MAXREDIRS
, 20);
793 #if LIBCURL_VERSION_NUM >= 0x071301
794 curl_easy_setopt(result
, CURLOPT_POSTREDIR
, CURL_REDIR_POST_ALL
);
795 #elif LIBCURL_VERSION_NUM >= 0x071101
796 curl_easy_setopt(result
, CURLOPT_POST301
, 1);
798 #ifdef CURLPROTO_HTTP
799 curl_easy_setopt(result
, CURLOPT_REDIR_PROTOCOLS
,
800 get_curl_allowed_protocols(0));
801 curl_easy_setopt(result
, CURLOPT_PROTOCOLS
,
802 get_curl_allowed_protocols(-1));
804 warning("protocol restrictions not applied to curl redirects because\n"
805 "your curl version is too old (>= 7.19.4)");
807 if (getenv("GIT_CURL_VERBOSE"))
808 curl_easy_setopt(result
, CURLOPT_VERBOSE
, 1L);
809 setup_curl_trace(result
);
811 curl_easy_setopt(result
, CURLOPT_USERAGENT
,
812 user_agent
? user_agent
: git_user_agent());
814 if (curl_ftp_no_epsv
)
815 curl_easy_setopt(result
, CURLOPT_FTP_USE_EPSV
, 0);
817 #ifdef CURLOPT_USE_SSL
819 curl_easy_setopt(result
, CURLOPT_USE_SSL
, CURLUSESSL_TRY
);
823 * CURL also examines these variables as a fallback; but we need to query
824 * them here in order to decide whether to prompt for missing password (cf.
825 * init_curl_proxy_auth()).
827 * Unlike many other common environment variables, these are historically
828 * lowercase only. It appears that CURL did not know this and implemented
829 * only uppercase variants, which was later corrected to take both - with
830 * the exception of http_proxy, which is lowercase only also in CURL. As
831 * the lowercase versions are the historical quasi-standard, they take
832 * precedence here, as in CURL.
834 if (!curl_http_proxy
) {
835 if (http_auth
.protocol
&& !strcmp(http_auth
.protocol
, "https")) {
836 var_override(&curl_http_proxy
, getenv("HTTPS_PROXY"));
837 var_override(&curl_http_proxy
, getenv("https_proxy"));
839 var_override(&curl_http_proxy
, getenv("http_proxy"));
841 if (!curl_http_proxy
) {
842 var_override(&curl_http_proxy
, getenv("ALL_PROXY"));
843 var_override(&curl_http_proxy
, getenv("all_proxy"));
847 if (curl_http_proxy
&& curl_http_proxy
[0] == '\0') {
849 * Handle case with the empty http.proxy value here to keep
851 * NB: empty option disables proxying at all.
853 curl_easy_setopt(result
, CURLOPT_PROXY
, "");
854 } else if (curl_http_proxy
) {
855 #if LIBCURL_VERSION_NUM >= 0x071800
856 if (starts_with(curl_http_proxy
, "socks5h"))
857 curl_easy_setopt(result
,
858 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5_HOSTNAME
);
859 else if (starts_with(curl_http_proxy
, "socks5"))
860 curl_easy_setopt(result
,
861 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS5
);
862 else if (starts_with(curl_http_proxy
, "socks4a"))
863 curl_easy_setopt(result
,
864 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4A
);
865 else if (starts_with(curl_http_proxy
, "socks"))
866 curl_easy_setopt(result
,
867 CURLOPT_PROXYTYPE
, CURLPROXY_SOCKS4
);
869 if (strstr(curl_http_proxy
, "://"))
870 credential_from_url(&proxy_auth
, curl_http_proxy
);
872 struct strbuf url
= STRBUF_INIT
;
873 strbuf_addf(&url
, "http://%s", curl_http_proxy
);
874 credential_from_url(&proxy_auth
, url
.buf
);
875 strbuf_release(&url
);
878 if (!proxy_auth
.host
)
879 die("Invalid proxy URL '%s'", curl_http_proxy
);
881 curl_easy_setopt(result
, CURLOPT_PROXY
, proxy_auth
.host
);
882 #if LIBCURL_VERSION_NUM >= 0x071304
883 var_override(&curl_no_proxy
, getenv("NO_PROXY"));
884 var_override(&curl_no_proxy
, getenv("no_proxy"));
885 curl_easy_setopt(result
, CURLOPT_NOPROXY
, curl_no_proxy
);
888 init_curl_proxy_auth(result
);
890 set_curl_keepalive(result
);
895 static void set_from_env(const char **var
, const char *envname
)
897 const char *val
= getenv(envname
);
902 static void protocol_http_header(void)
904 if (get_protocol_version_config() > 0) {
905 struct strbuf protocol_header
= STRBUF_INIT
;
907 strbuf_addf(&protocol_header
, GIT_PROTOCOL_HEADER
": version=%d",
908 get_protocol_version_config());
911 extra_http_headers
= curl_slist_append(extra_http_headers
,
912 protocol_header
.buf
);
913 strbuf_release(&protocol_header
);
917 void http_init(struct remote
*remote
, const char *url
, int proactive_auth
)
919 char *low_speed_limit
;
920 char *low_speed_time
;
921 char *normalized_url
;
922 struct urlmatch_config config
= { STRING_LIST_INIT_DUP
};
924 config
.section
= "http";
926 config
.collect_fn
= http_options
;
927 config
.cascade_fn
= git_default_config
;
931 normalized_url
= url_normalize(url
, &config
.url
);
933 git_config(urlmatch_config_entry
, &config
);
934 free(normalized_url
);
936 if (curl_global_init(CURL_GLOBAL_ALL
) != CURLE_OK
)
937 die("curl_global_init failed");
939 http_proactive_auth
= proactive_auth
;
941 if (remote
&& remote
->http_proxy
)
942 curl_http_proxy
= xstrdup(remote
->http_proxy
);
945 var_override(&http_proxy_authmethod
, remote
->http_proxy_authmethod
);
947 protocol_http_header();
949 pragma_header
= curl_slist_append(http_copy_default_headers(),
951 no_pragma_header
= curl_slist_append(http_copy_default_headers(),
954 #ifdef USE_CURL_MULTI
956 char *http_max_requests
= getenv("GIT_HTTP_MAX_REQUESTS");
957 if (http_max_requests
!= NULL
)
958 max_requests
= atoi(http_max_requests
);
961 curlm
= curl_multi_init();
963 die("curl_multi_init failed");
966 if (getenv("GIT_SSL_NO_VERIFY"))
969 set_from_env(&ssl_cert
, "GIT_SSL_CERT");
970 #if LIBCURL_VERSION_NUM >= 0x070903
971 set_from_env(&ssl_key
, "GIT_SSL_KEY");
973 #if LIBCURL_VERSION_NUM >= 0x070908
974 set_from_env(&ssl_capath
, "GIT_SSL_CAPATH");
976 set_from_env(&ssl_cainfo
, "GIT_SSL_CAINFO");
978 set_from_env(&user_agent
, "GIT_HTTP_USER_AGENT");
980 low_speed_limit
= getenv("GIT_HTTP_LOW_SPEED_LIMIT");
981 if (low_speed_limit
!= NULL
)
982 curl_low_speed_limit
= strtol(low_speed_limit
, NULL
, 10);
983 low_speed_time
= getenv("GIT_HTTP_LOW_SPEED_TIME");
984 if (low_speed_time
!= NULL
)
985 curl_low_speed_time
= strtol(low_speed_time
, NULL
, 10);
987 if (curl_ssl_verify
== -1)
990 curl_session_count
= 0;
991 #ifdef USE_CURL_MULTI
992 if (max_requests
< 1)
993 max_requests
= DEFAULT_MAX_REQUESTS
;
996 if (getenv("GIT_CURL_FTP_NO_EPSV"))
997 curl_ftp_no_epsv
= 1;
1000 credential_from_url(&http_auth
, url
);
1001 if (!ssl_cert_password_required
&&
1002 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
1003 starts_with(url
, "https://"))
1004 ssl_cert_password_required
= 1;
1007 #ifndef NO_CURL_EASY_DUPHANDLE
1008 curl_default
= get_curl_handle();
1012 void http_cleanup(void)
1014 struct active_request_slot
*slot
= active_queue_head
;
1016 while (slot
!= NULL
) {
1017 struct active_request_slot
*next
= slot
->next
;
1018 if (slot
->curl
!= NULL
) {
1019 xmulti_remove_handle(slot
);
1020 curl_easy_cleanup(slot
->curl
);
1025 active_queue_head
= NULL
;
1027 #ifndef NO_CURL_EASY_DUPHANDLE
1028 curl_easy_cleanup(curl_default
);
1031 #ifdef USE_CURL_MULTI
1032 curl_multi_cleanup(curlm
);
1034 curl_global_cleanup();
1036 curl_slist_free_all(extra_http_headers
);
1037 extra_http_headers
= NULL
;
1039 curl_slist_free_all(pragma_header
);
1040 pragma_header
= NULL
;
1042 curl_slist_free_all(no_pragma_header
);
1043 no_pragma_header
= NULL
;
1045 if (curl_http_proxy
) {
1046 free((void *)curl_http_proxy
);
1047 curl_http_proxy
= NULL
;
1050 if (proxy_auth
.password
) {
1051 memset(proxy_auth
.password
, 0, strlen(proxy_auth
.password
));
1052 FREE_AND_NULL(proxy_auth
.password
);
1055 free((void *)curl_proxyuserpwd
);
1056 curl_proxyuserpwd
= NULL
;
1058 free((void *)http_proxy_authmethod
);
1059 http_proxy_authmethod
= NULL
;
1061 if (cert_auth
.password
!= NULL
) {
1062 memset(cert_auth
.password
, 0, strlen(cert_auth
.password
));
1063 FREE_AND_NULL(cert_auth
.password
);
1065 ssl_cert_password_required
= 0;
1067 FREE_AND_NULL(cached_accept_language
);
1070 struct active_request_slot
*get_active_slot(void)
1072 struct active_request_slot
*slot
= active_queue_head
;
1073 struct active_request_slot
*newslot
;
1075 #ifdef USE_CURL_MULTI
1078 /* Wait for a slot to open up if the queue is full */
1079 while (active_requests
>= max_requests
) {
1080 curl_multi_perform(curlm
, &num_transfers
);
1081 if (num_transfers
< active_requests
)
1082 process_curl_messages();
1086 while (slot
!= NULL
&& slot
->in_use
)
1090 newslot
= xmalloc(sizeof(*newslot
));
1091 newslot
->curl
= NULL
;
1092 newslot
->in_use
= 0;
1093 newslot
->next
= NULL
;
1095 slot
= active_queue_head
;
1097 active_queue_head
= newslot
;
1099 while (slot
->next
!= NULL
)
1101 slot
->next
= newslot
;
1106 if (slot
->curl
== NULL
) {
1107 #ifdef NO_CURL_EASY_DUPHANDLE
1108 slot
->curl
= get_curl_handle();
1110 slot
->curl
= curl_easy_duphandle(curl_default
);
1112 curl_session_count
++;
1117 slot
->results
= NULL
;
1118 slot
->finished
= NULL
;
1119 slot
->callback_data
= NULL
;
1120 slot
->callback_func
= NULL
;
1121 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEFILE
, curl_cookie_file
);
1122 if (curl_save_cookies
)
1123 curl_easy_setopt(slot
->curl
, CURLOPT_COOKIEJAR
, curl_cookie_file
);
1124 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, pragma_header
);
1125 curl_easy_setopt(slot
->curl
, CURLOPT_ERRORBUFFER
, curl_errorstr
);
1126 curl_easy_setopt(slot
->curl
, CURLOPT_CUSTOMREQUEST
, NULL
);
1127 curl_easy_setopt(slot
->curl
, CURLOPT_READFUNCTION
, NULL
);
1128 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, NULL
);
1129 curl_easy_setopt(slot
->curl
, CURLOPT_POSTFIELDS
, NULL
);
1130 curl_easy_setopt(slot
->curl
, CURLOPT_UPLOAD
, 0);
1131 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1132 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 1);
1133 curl_easy_setopt(slot
->curl
, CURLOPT_RANGE
, NULL
);
1136 * Default following to off unless "ALWAYS" is configured; this gives
1137 * callers a sane starting point, and they can tweak for individual
1138 * HTTP_FOLLOW_* cases themselves.
1140 if (http_follow_config
== HTTP_FOLLOW_ALWAYS
)
1141 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1143 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 0);
1145 #if LIBCURL_VERSION_NUM >= 0x070a08
1146 curl_easy_setopt(slot
->curl
, CURLOPT_IPRESOLVE
, git_curl_ipresolve
);
1148 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1149 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPAUTH
, http_auth_methods
);
1151 if (http_auth
.password
|| curl_empty_auth_enabled())
1152 init_curl_http_auth(slot
->curl
);
1157 int start_active_slot(struct active_request_slot
*slot
)
1159 #ifdef USE_CURL_MULTI
1160 CURLMcode curlm_result
= curl_multi_add_handle(curlm
, slot
->curl
);
1163 if (curlm_result
!= CURLM_OK
&&
1164 curlm_result
!= CURLM_CALL_MULTI_PERFORM
) {
1165 warning("curl_multi_add_handle failed: %s",
1166 curl_multi_strerror(curlm_result
));
1173 * We know there must be something to do, since we just added
1176 curl_multi_perform(curlm
, &num_transfers
);
1181 #ifdef USE_CURL_MULTI
1184 int (*fill
)(void *);
1185 struct fill_chain
*next
;
1188 static struct fill_chain
*fill_cfg
;
1190 void add_fill_function(void *data
, int (*fill
)(void *))
1192 struct fill_chain
*new = xmalloc(sizeof(*new));
1193 struct fill_chain
**linkp
= &fill_cfg
;
1198 linkp
= &(*linkp
)->next
;
1202 void fill_active_slots(void)
1204 struct active_request_slot
*slot
= active_queue_head
;
1206 while (active_requests
< max_requests
) {
1207 struct fill_chain
*fill
;
1208 for (fill
= fill_cfg
; fill
; fill
= fill
->next
)
1209 if (fill
->fill(fill
->data
))
1216 while (slot
!= NULL
) {
1217 if (!slot
->in_use
&& slot
->curl
!= NULL
1218 && curl_session_count
> min_curl_sessions
) {
1219 curl_easy_cleanup(slot
->curl
);
1221 curl_session_count
--;
1227 void step_active_slots(void)
1230 CURLMcode curlm_result
;
1233 curlm_result
= curl_multi_perform(curlm
, &num_transfers
);
1234 } while (curlm_result
== CURLM_CALL_MULTI_PERFORM
);
1235 if (num_transfers
< active_requests
) {
1236 process_curl_messages();
1237 fill_active_slots();
1242 void run_active_slot(struct active_request_slot
*slot
)
1244 #ifdef USE_CURL_MULTI
1249 struct timeval select_timeout
;
1252 slot
->finished
= &finished
;
1254 step_active_slots();
1257 #if LIBCURL_VERSION_NUM >= 0x070f04
1259 curl_multi_timeout(curlm
, &curl_timeout
);
1260 if (curl_timeout
== 0) {
1262 } else if (curl_timeout
== -1) {
1263 select_timeout
.tv_sec
= 0;
1264 select_timeout
.tv_usec
= 50000;
1266 select_timeout
.tv_sec
= curl_timeout
/ 1000;
1267 select_timeout
.tv_usec
= (curl_timeout
% 1000) * 1000;
1270 select_timeout
.tv_sec
= 0;
1271 select_timeout
.tv_usec
= 50000;
1278 curl_multi_fdset(curlm
, &readfds
, &writefds
, &excfds
, &max_fd
);
1281 * It can happen that curl_multi_timeout returns a pathologically
1282 * long timeout when curl_multi_fdset returns no file descriptors
1283 * to read. See commit message for more details.
1286 (select_timeout
.tv_sec
> 0 ||
1287 select_timeout
.tv_usec
> 50000)) {
1288 select_timeout
.tv_sec
= 0;
1289 select_timeout
.tv_usec
= 50000;
1292 select(max_fd
+1, &readfds
, &writefds
, &excfds
, &select_timeout
);
1296 while (slot
->in_use
) {
1297 slot
->curl_result
= curl_easy_perform(slot
->curl
);
1298 finish_active_slot(slot
);
1303 static void release_active_slot(struct active_request_slot
*slot
)
1305 closedown_active_slot(slot
);
1307 xmulti_remove_handle(slot
);
1308 if (curl_session_count
> min_curl_sessions
) {
1309 curl_easy_cleanup(slot
->curl
);
1311 curl_session_count
--;
1314 #ifdef USE_CURL_MULTI
1315 fill_active_slots();
1319 void finish_all_active_slots(void)
1321 struct active_request_slot
*slot
= active_queue_head
;
1323 while (slot
!= NULL
)
1325 run_active_slot(slot
);
1326 slot
= active_queue_head
;
1332 /* Helpers for modifying and creating URLs */
1333 static inline int needs_quote(int ch
)
1335 if (((ch
>= 'A') && (ch
<= 'Z'))
1336 || ((ch
>= 'a') && (ch
<= 'z'))
1337 || ((ch
>= '0') && (ch
<= '9'))
1345 static char *quote_ref_url(const char *base
, const char *ref
)
1347 struct strbuf buf
= STRBUF_INIT
;
1351 end_url_with_slash(&buf
, base
);
1353 for (cp
= ref
; (ch
= *cp
) != 0; cp
++)
1354 if (needs_quote(ch
))
1355 strbuf_addf(&buf
, "%%%02x", ch
);
1357 strbuf_addch(&buf
, *cp
);
1359 return strbuf_detach(&buf
, NULL
);
1362 void append_remote_object_url(struct strbuf
*buf
, const char *url
,
1364 int only_two_digit_prefix
)
1366 end_url_with_slash(buf
, url
);
1368 strbuf_addf(buf
, "objects/%.*s/", 2, hex
);
1369 if (!only_two_digit_prefix
)
1370 strbuf_addstr(buf
, hex
+ 2);
1373 char *get_remote_object_url(const char *url
, const char *hex
,
1374 int only_two_digit_prefix
)
1376 struct strbuf buf
= STRBUF_INIT
;
1377 append_remote_object_url(&buf
, url
, hex
, only_two_digit_prefix
);
1378 return strbuf_detach(&buf
, NULL
);
1381 static int handle_curl_result(struct slot_results
*results
)
1384 * If we see a failing http code with CURLE_OK, we have turned off
1385 * FAILONERROR (to keep the server's custom error response), and should
1386 * translate the code into failure here.
1388 * Likewise, if we see a redirect (30x code), that means we turned off
1389 * redirect-following, and we should treat the result as an error.
1391 if (results
->curl_result
== CURLE_OK
&&
1392 results
->http_code
>= 300) {
1393 results
->curl_result
= CURLE_HTTP_RETURNED_ERROR
;
1395 * Normally curl will already have put the "reason phrase"
1396 * from the server into curl_errorstr; unfortunately without
1397 * FAILONERROR it is lost, so we can give only the numeric
1400 xsnprintf(curl_errorstr
, sizeof(curl_errorstr
),
1401 "The requested URL returned error: %ld",
1402 results
->http_code
);
1405 if (results
->curl_result
== CURLE_OK
) {
1406 credential_approve(&http_auth
);
1407 if (proxy_auth
.password
)
1408 credential_approve(&proxy_auth
);
1410 } else if (missing_target(results
))
1411 return HTTP_MISSING_TARGET
;
1412 else if (results
->http_code
== 401) {
1413 if (http_auth
.username
&& http_auth
.password
) {
1414 credential_reject(&http_auth
);
1417 #ifdef LIBCURL_CAN_HANDLE_AUTH_ANY
1418 http_auth_methods
&= ~CURLAUTH_GSSNEGOTIATE
;
1419 if (results
->auth_avail
) {
1420 http_auth_methods
&= results
->auth_avail
;
1421 http_auth_methods_restricted
= 1;
1427 if (results
->http_connectcode
== 407)
1428 credential_reject(&proxy_auth
);
1429 #if LIBCURL_VERSION_NUM >= 0x070c00
1430 if (!curl_errorstr
[0])
1431 strlcpy(curl_errorstr
,
1432 curl_easy_strerror(results
->curl_result
),
1433 sizeof(curl_errorstr
));
1439 int run_one_slot(struct active_request_slot
*slot
,
1440 struct slot_results
*results
)
1442 slot
->results
= results
;
1443 if (!start_active_slot(slot
)) {
1444 xsnprintf(curl_errorstr
, sizeof(curl_errorstr
),
1445 "failed to start HTTP request");
1446 return HTTP_START_FAILED
;
1449 run_active_slot(slot
);
1450 return handle_curl_result(results
);
1453 struct curl_slist
*http_copy_default_headers(void)
1455 struct curl_slist
*headers
= NULL
, *h
;
1457 for (h
= extra_http_headers
; h
; h
= h
->next
)
1458 headers
= curl_slist_append(headers
, h
->data
);
1463 static CURLcode
curlinfo_strbuf(CURL
*curl
, CURLINFO info
, struct strbuf
*buf
)
1469 ret
= curl_easy_getinfo(curl
, info
, &ptr
);
1471 strbuf_addstr(buf
, ptr
);
1476 * Check for and extract a content-type parameter. "raw"
1477 * should be positioned at the start of the potential
1478 * parameter, with any whitespace already removed.
1480 * "name" is the name of the parameter. The value is appended
1483 static int extract_param(const char *raw
, const char *name
,
1486 size_t len
= strlen(name
);
1488 if (strncasecmp(raw
, name
, len
))
1496 while (*raw
&& !isspace(*raw
) && *raw
!= ';')
1497 strbuf_addch(out
, *raw
++);
1502 * Extract a normalized version of the content type, with any
1503 * spaces suppressed, all letters lowercased, and no trailing ";"
1506 * Note that we will silently remove even invalid whitespace. For
1507 * example, "text / plain" is specifically forbidden by RFC 2616,
1508 * but "text/plain" is the only reasonable output, and this keeps
1511 * If the "charset" argument is not NULL, store the value of any
1512 * charset parameter there.
1515 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1516 * "text / plain" -> "text/plain"
1518 static void extract_content_type(struct strbuf
*raw
, struct strbuf
*type
,
1519 struct strbuf
*charset
)
1524 strbuf_grow(type
, raw
->len
);
1525 for (p
= raw
->buf
; *p
; p
++) {
1532 strbuf_addch(type
, tolower(*p
));
1538 strbuf_reset(charset
);
1540 while (isspace(*p
) || *p
== ';')
1542 if (!extract_param(p
, "charset", charset
))
1544 while (*p
&& !isspace(*p
))
1548 if (!charset
->len
&& starts_with(type
->buf
, "text/"))
1549 strbuf_addstr(charset
, "ISO-8859-1");
1552 static void write_accept_language(struct strbuf
*buf
)
1555 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1556 * that, q-value will be smaller than 0.001, the minimum q-value the
1557 * HTTP specification allows. See
1558 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1560 const int MAX_DECIMAL_PLACES
= 3;
1561 const int MAX_LANGUAGE_TAGS
= 1000;
1562 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE
= 4000;
1563 char **language_tags
= NULL
;
1565 const char *s
= get_preferred_languages();
1567 struct strbuf tag
= STRBUF_INIT
;
1569 /* Don't add Accept-Language header if no language is preferred. */
1574 * Split the colon-separated string of preferred languages into
1575 * language_tags array.
1578 /* collect language tag */
1579 for (; *s
&& (isalnum(*s
) || *s
== '_'); s
++)
1580 strbuf_addch(&tag
, *s
== '_' ? '-' : *s
);
1582 /* skip .codeset, @modifier and any other unnecessary parts */
1583 while (*s
&& *s
!= ':')
1588 REALLOC_ARRAY(language_tags
, num_langs
);
1589 language_tags
[num_langs
- 1] = strbuf_detach(&tag
, NULL
);
1590 if (num_langs
>= MAX_LANGUAGE_TAGS
- 1) /* -1 for '*' */
1595 /* write Accept-Language header into buf */
1597 int last_buf_len
= 0;
1603 REALLOC_ARRAY(language_tags
, num_langs
+ 1);
1604 language_tags
[num_langs
++] = "*"; /* it's OK; this won't be freed */
1606 /* compute decimal_places */
1607 for (max_q
= 1, decimal_places
= 0;
1608 max_q
< num_langs
&& decimal_places
<= MAX_DECIMAL_PLACES
;
1609 decimal_places
++, max_q
*= 10)
1612 xsnprintf(q_format
, sizeof(q_format
), ";q=0.%%0%dd", decimal_places
);
1614 strbuf_addstr(buf
, "Accept-Language: ");
1616 for (i
= 0; i
< num_langs
; i
++) {
1618 strbuf_addstr(buf
, ", ");
1620 strbuf_addstr(buf
, language_tags
[i
]);
1623 strbuf_addf(buf
, q_format
, max_q
- i
);
1625 if (buf
->len
> MAX_ACCEPT_LANGUAGE_HEADER_SIZE
) {
1626 strbuf_remove(buf
, last_buf_len
, buf
->len
- last_buf_len
);
1630 last_buf_len
= buf
->len
;
1634 /* free language tags -- last one is a static '*' */
1635 for (i
= 0; i
< num_langs
- 1; i
++)
1636 free(language_tags
[i
]);
1637 free(language_tags
);
1641 * Get an Accept-Language header which indicates user's preferred languages.
1645 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1646 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1647 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1648 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1649 * LANGUAGE= LANG=C -> ""
1651 static const char *get_accept_language(void)
1653 if (!cached_accept_language
) {
1654 struct strbuf buf
= STRBUF_INIT
;
1655 write_accept_language(&buf
);
1657 cached_accept_language
= strbuf_detach(&buf
, NULL
);
1660 return cached_accept_language
;
1663 static void http_opt_request_remainder(CURL
*curl
, off_t pos
)
1666 xsnprintf(buf
, sizeof(buf
), "%"PRIuMAX
"-", (uintmax_t)pos
);
1667 curl_easy_setopt(curl
, CURLOPT_RANGE
, buf
);
1670 /* http_request() targets */
1671 #define HTTP_REQUEST_STRBUF 0
1672 #define HTTP_REQUEST_FILE 1
1674 static int http_request(const char *url
,
1675 void *result
, int target
,
1676 const struct http_get_options
*options
)
1678 struct active_request_slot
*slot
;
1679 struct slot_results results
;
1680 struct curl_slist
*headers
= http_copy_default_headers();
1681 struct strbuf buf
= STRBUF_INIT
;
1682 const char *accept_language
;
1685 slot
= get_active_slot();
1686 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPGET
, 1);
1688 if (result
== NULL
) {
1689 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 1);
1691 curl_easy_setopt(slot
->curl
, CURLOPT_NOBODY
, 0);
1692 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, result
);
1694 if (target
== HTTP_REQUEST_FILE
) {
1695 off_t posn
= ftello(result
);
1696 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1699 http_opt_request_remainder(slot
->curl
, posn
);
1701 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
,
1705 accept_language
= get_accept_language();
1707 if (accept_language
)
1708 headers
= curl_slist_append(headers
, accept_language
);
1710 strbuf_addstr(&buf
, "Pragma:");
1711 if (options
&& options
->no_cache
)
1712 strbuf_addstr(&buf
, " no-cache");
1713 if (options
&& options
->keep_error
)
1714 curl_easy_setopt(slot
->curl
, CURLOPT_FAILONERROR
, 0);
1715 if (options
&& options
->initial_request
&&
1716 http_follow_config
== HTTP_FOLLOW_INITIAL
)
1717 curl_easy_setopt(slot
->curl
, CURLOPT_FOLLOWLOCATION
, 1);
1719 headers
= curl_slist_append(headers
, buf
.buf
);
1721 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, url
);
1722 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, headers
);
1723 curl_easy_setopt(slot
->curl
, CURLOPT_ENCODING
, "gzip");
1725 ret
= run_one_slot(slot
, &results
);
1727 if (options
&& options
->content_type
) {
1728 struct strbuf raw
= STRBUF_INIT
;
1729 curlinfo_strbuf(slot
->curl
, CURLINFO_CONTENT_TYPE
, &raw
);
1730 extract_content_type(&raw
, options
->content_type
,
1732 strbuf_release(&raw
);
1735 if (options
&& options
->effective_url
)
1736 curlinfo_strbuf(slot
->curl
, CURLINFO_EFFECTIVE_URL
,
1737 options
->effective_url
);
1739 curl_slist_free_all(headers
);
1740 strbuf_release(&buf
);
1746 * Update the "base" url to a more appropriate value, as deduced by
1747 * redirects seen when requesting a URL starting with "url".
1749 * The "asked" parameter is a URL that we asked curl to access, and must begin
1752 * The "got" parameter is the URL that curl reported to us as where we ended
1755 * Returns 1 if we updated the base url, 0 otherwise.
1757 * Our basic strategy is to compare "base" and "asked" to find the bits
1758 * specific to our request. We then strip those bits off of "got" to yield the
1759 * new base. So for example, if our base is "http://example.com/foo.git",
1760 * and we ask for "http://example.com/foo.git/info/refs", we might end up
1761 * with "https://other.example.com/foo.git/info/refs". We would want the
1762 * new URL to become "https://other.example.com/foo.git".
1764 * Note that this assumes a sane redirect scheme. It's entirely possible
1765 * in the example above to end up at a URL that does not even end in
1766 * "info/refs". In such a case we die. There's not much we can do, such a
1767 * scheme is unlikely to represent a real git repository, and failing to
1768 * rewrite the base opens options for malicious redirects to do funny things.
1770 static int update_url_from_redirect(struct strbuf
*base
,
1772 const struct strbuf
*got
)
1777 if (!strcmp(asked
, got
->buf
))
1780 if (!skip_prefix(asked
, base
->buf
, &tail
))
1781 die("BUG: update_url_from_redirect: %s is not a superset of %s",
1785 if (!strip_suffix_mem(got
->buf
, &new_len
, tail
))
1786 die(_("unable to update url base from redirection:\n"
1792 strbuf_add(base
, got
->buf
, new_len
);
1797 static int http_request_reauth(const char *url
,
1798 void *result
, int target
,
1799 struct http_get_options
*options
)
1801 int ret
= http_request(url
, result
, target
, options
);
1803 if (ret
!= HTTP_OK
&& ret
!= HTTP_REAUTH
)
1806 if (options
&& options
->effective_url
&& options
->base_url
) {
1807 if (update_url_from_redirect(options
->base_url
,
1808 url
, options
->effective_url
)) {
1809 credential_from_url(&http_auth
, options
->base_url
->buf
);
1810 url
= options
->effective_url
->buf
;
1814 if (ret
!= HTTP_REAUTH
)
1818 * If we are using KEEP_ERROR, the previous request may have
1819 * put cruft into our output stream; we should clear it out before
1820 * making our next request. We only know how to do this for
1821 * the strbuf case, but that is enough to satisfy current callers.
1823 if (options
&& options
->keep_error
) {
1825 case HTTP_REQUEST_STRBUF
:
1826 strbuf_reset(result
);
1829 die("BUG: HTTP_KEEP_ERROR is only supported with strbufs");
1833 credential_fill(&http_auth
);
1835 return http_request(url
, result
, target
, options
);
1838 int http_get_strbuf(const char *url
,
1839 struct strbuf
*result
,
1840 struct http_get_options
*options
)
1842 return http_request_reauth(url
, result
, HTTP_REQUEST_STRBUF
, options
);
1846 * Downloads a URL and stores the result in the given file.
1848 * If a previous interrupted download is detected (i.e. a previous temporary
1849 * file is still around) the download is resumed.
1851 static int http_get_file(const char *url
, const char *filename
,
1852 struct http_get_options
*options
)
1855 struct strbuf tmpfile
= STRBUF_INIT
;
1858 strbuf_addf(&tmpfile
, "%s.temp", filename
);
1859 result
= fopen(tmpfile
.buf
, "a");
1861 error("Unable to open local file %s", tmpfile
.buf
);
1866 ret
= http_request_reauth(url
, result
, HTTP_REQUEST_FILE
, options
);
1869 if (ret
== HTTP_OK
&& finalize_object_file(tmpfile
.buf
, filename
))
1872 strbuf_release(&tmpfile
);
1876 int http_fetch_ref(const char *base
, struct ref
*ref
)
1878 struct http_get_options options
= {0};
1880 struct strbuf buffer
= STRBUF_INIT
;
1883 options
.no_cache
= 1;
1885 url
= quote_ref_url(base
, ref
->name
);
1886 if (http_get_strbuf(url
, &buffer
, &options
) == HTTP_OK
) {
1887 strbuf_rtrim(&buffer
);
1888 if (buffer
.len
== 40)
1889 ret
= get_oid_hex(buffer
.buf
, &ref
->old_oid
);
1890 else if (starts_with(buffer
.buf
, "ref: ")) {
1891 ref
->symref
= xstrdup(buffer
.buf
+ 5);
1896 strbuf_release(&buffer
);
1901 /* Helpers for fetching packs */
1902 static char *fetch_pack_index(unsigned char *sha1
, const char *base_url
)
1905 struct strbuf buf
= STRBUF_INIT
;
1907 if (http_is_verbose
)
1908 fprintf(stderr
, "Getting index for pack %s\n", sha1_to_hex(sha1
));
1910 end_url_with_slash(&buf
, base_url
);
1911 strbuf_addf(&buf
, "objects/pack/pack-%s.idx", sha1_to_hex(sha1
));
1912 url
= strbuf_detach(&buf
, NULL
);
1914 strbuf_addf(&buf
, "%s.temp", sha1_pack_index_name(sha1
));
1915 tmp
= strbuf_detach(&buf
, NULL
);
1917 if (http_get_file(url
, tmp
, NULL
) != HTTP_OK
) {
1918 error("Unable to get pack index %s", url
);
1926 static int fetch_and_setup_pack_index(struct packed_git
**packs_head
,
1927 unsigned char *sha1
, const char *base_url
)
1929 struct packed_git
*new_pack
;
1930 char *tmp_idx
= NULL
;
1933 if (has_pack_index(sha1
)) {
1934 new_pack
= parse_pack_index(sha1
, sha1_pack_index_name(sha1
));
1936 return -1; /* parse_pack_index() already issued error message */
1940 tmp_idx
= fetch_pack_index(sha1
, base_url
);
1944 new_pack
= parse_pack_index(sha1
, tmp_idx
);
1949 return -1; /* parse_pack_index() already issued error message */
1952 ret
= verify_pack_index(new_pack
);
1954 close_pack_index(new_pack
);
1955 ret
= finalize_object_file(tmp_idx
, sha1_pack_index_name(sha1
));
1962 new_pack
->next
= *packs_head
;
1963 *packs_head
= new_pack
;
1967 int http_get_info_packs(const char *base_url
, struct packed_git
**packs_head
)
1969 struct http_get_options options
= {0};
1972 struct strbuf buf
= STRBUF_INIT
;
1973 unsigned char sha1
[20];
1975 end_url_with_slash(&buf
, base_url
);
1976 strbuf_addstr(&buf
, "objects/info/packs");
1977 url
= strbuf_detach(&buf
, NULL
);
1979 options
.no_cache
= 1;
1980 ret
= http_get_strbuf(url
, &buf
, &options
);
1985 while (i
< buf
.len
) {
1989 if (i
+ 52 <= buf
.len
&&
1990 starts_with(data
+ i
, " pack-") &&
1991 starts_with(data
+ i
+ 46, ".pack\n")) {
1992 get_sha1_hex(data
+ i
+ 6, sha1
);
1993 fetch_and_setup_pack_index(packs_head
, sha1
,
1999 while (i
< buf
.len
&& data
[i
] != '\n')
2010 void release_http_pack_request(struct http_pack_request
*preq
)
2012 if (preq
->packfile
!= NULL
) {
2013 fclose(preq
->packfile
);
2014 preq
->packfile
= NULL
;
2021 int finish_http_pack_request(struct http_pack_request
*preq
)
2023 struct packed_git
**lst
;
2024 struct packed_git
*p
= preq
->target
;
2027 struct child_process ip
= CHILD_PROCESS_INIT
;
2028 const char *ip_argv
[8];
2030 close_pack_index(p
);
2032 fclose(preq
->packfile
);
2033 preq
->packfile
= NULL
;
2037 lst
= &((*lst
)->next
);
2038 *lst
= (*lst
)->next
;
2040 if (!strip_suffix(preq
->tmpfile
, ".pack.temp", &len
))
2041 die("BUG: pack tmpfile does not end in .pack.temp?");
2042 tmp_idx
= xstrfmt("%.*s.idx.temp", (int)len
, preq
->tmpfile
);
2044 ip_argv
[0] = "index-pack";
2046 ip_argv
[2] = tmp_idx
;
2047 ip_argv
[3] = preq
->tmpfile
;
2055 if (run_command(&ip
)) {
2056 unlink(preq
->tmpfile
);
2062 unlink(sha1_pack_index_name(p
->sha1
));
2064 if (finalize_object_file(preq
->tmpfile
, sha1_pack_name(p
->sha1
))
2065 || finalize_object_file(tmp_idx
, sha1_pack_index_name(p
->sha1
))) {
2070 install_packed_git(p
);
2075 struct http_pack_request
*new_http_pack_request(
2076 struct packed_git
*target
, const char *base_url
)
2078 off_t prev_posn
= 0;
2079 struct strbuf buf
= STRBUF_INIT
;
2080 struct http_pack_request
*preq
;
2082 preq
= xcalloc(1, sizeof(*preq
));
2083 preq
->target
= target
;
2085 end_url_with_slash(&buf
, base_url
);
2086 strbuf_addf(&buf
, "objects/pack/pack-%s.pack",
2087 sha1_to_hex(target
->sha1
));
2088 preq
->url
= strbuf_detach(&buf
, NULL
);
2090 snprintf(preq
->tmpfile
, sizeof(preq
->tmpfile
), "%s.temp",
2091 sha1_pack_name(target
->sha1
));
2092 preq
->packfile
= fopen(preq
->tmpfile
, "a");
2093 if (!preq
->packfile
) {
2094 error("Unable to open local file %s for pack",
2099 preq
->slot
= get_active_slot();
2100 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_FILE
, preq
->packfile
);
2101 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite
);
2102 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_URL
, preq
->url
);
2103 curl_easy_setopt(preq
->slot
->curl
, CURLOPT_HTTPHEADER
,
2107 * If there is data present from a previous transfer attempt,
2108 * resume where it left off
2110 prev_posn
= ftello(preq
->packfile
);
2112 if (http_is_verbose
)
2114 "Resuming fetch of pack %s at byte %"PRIuMAX
"\n",
2115 sha1_to_hex(target
->sha1
), (uintmax_t)prev_posn
);
2116 http_opt_request_remainder(preq
->slot
->curl
, prev_posn
);
2127 /* Helpers for fetching objects (loose) */
2128 static size_t fwrite_sha1_file(char *ptr
, size_t eltsize
, size_t nmemb
,
2131 unsigned char expn
[4096];
2132 size_t size
= eltsize
* nmemb
;
2134 struct http_object_request
*freq
= data
;
2135 struct active_request_slot
*slot
= freq
->slot
;
2138 CURLcode c
= curl_easy_getinfo(slot
->curl
, CURLINFO_HTTP_CODE
,
2141 die("BUG: curl_easy_getinfo for HTTP code failed: %s",
2142 curl_easy_strerror(c
));
2143 if (slot
->http_code
>= 300)
2148 ssize_t retval
= xwrite(freq
->localfile
,
2149 (char *) ptr
+ posn
, size
- posn
);
2153 } while (posn
< size
);
2155 freq
->stream
.avail_in
= size
;
2156 freq
->stream
.next_in
= (void *)ptr
;
2158 freq
->stream
.next_out
= expn
;
2159 freq
->stream
.avail_out
= sizeof(expn
);
2160 freq
->zret
= git_inflate(&freq
->stream
, Z_SYNC_FLUSH
);
2161 git_SHA1_Update(&freq
->c
, expn
,
2162 sizeof(expn
) - freq
->stream
.avail_out
);
2163 } while (freq
->stream
.avail_in
&& freq
->zret
== Z_OK
);
2167 struct http_object_request
*new_http_object_request(const char *base_url
,
2168 unsigned char *sha1
)
2170 char *hex
= sha1_to_hex(sha1
);
2171 const char *filename
;
2172 char prevfile
[PATH_MAX
];
2174 char prev_buf
[PREV_BUF_SIZE
];
2175 ssize_t prev_read
= 0;
2176 off_t prev_posn
= 0;
2177 struct http_object_request
*freq
;
2179 freq
= xcalloc(1, sizeof(*freq
));
2180 hashcpy(freq
->sha1
, sha1
);
2181 freq
->localfile
= -1;
2183 filename
= sha1_file_name(sha1
);
2184 snprintf(freq
->tmpfile
, sizeof(freq
->tmpfile
),
2185 "%s.temp", filename
);
2187 snprintf(prevfile
, sizeof(prevfile
), "%s.prev", filename
);
2188 unlink_or_warn(prevfile
);
2189 rename(freq
->tmpfile
, prevfile
);
2190 unlink_or_warn(freq
->tmpfile
);
2192 if (freq
->localfile
!= -1)
2193 error("fd leakage in start: %d", freq
->localfile
);
2194 freq
->localfile
= open(freq
->tmpfile
,
2195 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2197 * This could have failed due to the "lazy directory creation";
2198 * try to mkdir the last path component.
2200 if (freq
->localfile
< 0 && errno
== ENOENT
) {
2201 char *dir
= strrchr(freq
->tmpfile
, '/');
2204 mkdir(freq
->tmpfile
, 0777);
2207 freq
->localfile
= open(freq
->tmpfile
,
2208 O_WRONLY
| O_CREAT
| O_EXCL
, 0666);
2211 if (freq
->localfile
< 0) {
2212 error_errno("Couldn't create temporary file %s", freq
->tmpfile
);
2216 git_inflate_init(&freq
->stream
);
2218 git_SHA1_Init(&freq
->c
);
2220 freq
->url
= get_remote_object_url(base_url
, hex
, 0);
2223 * If a previous temp file is present, process what was already
2226 prevlocal
= open(prevfile
, O_RDONLY
);
2227 if (prevlocal
!= -1) {
2229 prev_read
= xread(prevlocal
, prev_buf
, PREV_BUF_SIZE
);
2231 if (fwrite_sha1_file(prev_buf
,
2234 freq
) == prev_read
) {
2235 prev_posn
+= prev_read
;
2240 } while (prev_read
> 0);
2243 unlink_or_warn(prevfile
);
2246 * Reset inflate/SHA1 if there was an error reading the previous temp
2247 * file; also rewind to the beginning of the local file.
2249 if (prev_read
== -1) {
2250 memset(&freq
->stream
, 0, sizeof(freq
->stream
));
2251 git_inflate_init(&freq
->stream
);
2252 git_SHA1_Init(&freq
->c
);
2255 lseek(freq
->localfile
, 0, SEEK_SET
);
2256 if (ftruncate(freq
->localfile
, 0) < 0) {
2257 error_errno("Couldn't truncate temporary file %s",
2264 freq
->slot
= get_active_slot();
2266 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FILE
, freq
);
2267 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_FAILONERROR
, 0);
2268 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_sha1_file
);
2269 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_ERRORBUFFER
, freq
->errorstr
);
2270 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_URL
, freq
->url
);
2271 curl_easy_setopt(freq
->slot
->curl
, CURLOPT_HTTPHEADER
, no_pragma_header
);
2274 * If we have successfully processed data from a previous fetch
2275 * attempt, only fetch the data we don't already have.
2278 if (http_is_verbose
)
2280 "Resuming fetch of object %s at byte %"PRIuMAX
"\n",
2281 hex
, (uintmax_t)prev_posn
);
2282 http_opt_request_remainder(freq
->slot
->curl
, prev_posn
);
2293 void process_http_object_request(struct http_object_request
*freq
)
2295 if (freq
->slot
== NULL
)
2297 freq
->curl_result
= freq
->slot
->curl_result
;
2298 freq
->http_code
= freq
->slot
->http_code
;
2302 int finish_http_object_request(struct http_object_request
*freq
)
2306 close(freq
->localfile
);
2307 freq
->localfile
= -1;
2309 process_http_object_request(freq
);
2311 if (freq
->http_code
== 416) {
2312 warning("requested range invalid; we may already have all the data.");
2313 } else if (freq
->curl_result
!= CURLE_OK
) {
2314 if (stat(freq
->tmpfile
, &st
) == 0)
2315 if (st
.st_size
== 0)
2316 unlink_or_warn(freq
->tmpfile
);
2320 git_inflate_end(&freq
->stream
);
2321 git_SHA1_Final(freq
->real_sha1
, &freq
->c
);
2322 if (freq
->zret
!= Z_STREAM_END
) {
2323 unlink_or_warn(freq
->tmpfile
);
2326 if (hashcmp(freq
->sha1
, freq
->real_sha1
)) {
2327 unlink_or_warn(freq
->tmpfile
);
2331 finalize_object_file(freq
->tmpfile
, sha1_file_name(freq
->sha1
));
2333 return freq
->rename
;
2336 void abort_http_object_request(struct http_object_request
*freq
)
2338 unlink_or_warn(freq
->tmpfile
);
2340 release_http_object_request(freq
);
2343 void release_http_object_request(struct http_object_request
*freq
)
2345 if (freq
->localfile
!= -1) {
2346 close(freq
->localfile
);
2347 freq
->localfile
= -1;
2349 if (freq
->url
!= NULL
) {
2350 FREE_AND_NULL(freq
->url
);
2352 if (freq
->slot
!= NULL
) {
2353 freq
->slot
->callback_func
= NULL
;
2354 freq
->slot
->callback_data
= NULL
;
2355 release_active_slot(freq
->slot
);