am, rebase: fix arghelp syntax of --empty
[git.git] / http.c
blobe138b4b96fb9e201623339eada35f9499b5ba371
1 #include "git-compat-util.h"
2 #include "git-curl-compat.h"
3 #include "hex.h"
4 #include "http.h"
5 #include "config.h"
6 #include "pack.h"
7 #include "sideband.h"
8 #include "run-command.h"
9 #include "url.h"
10 #include "urlmatch.h"
11 #include "credential.h"
12 #include "version.h"
13 #include "pkt-line.h"
14 #include "gettext.h"
15 #include "trace.h"
16 #include "transport.h"
17 #include "packfile.h"
18 #include "protocol.h"
19 #include "string-list.h"
20 #include "object-file.h"
21 #include "object-store-ll.h"
23 static struct trace_key trace_curl = TRACE_KEY_INIT(CURL);
24 static int trace_curl_data = 1;
25 static int trace_curl_redact = 1;
26 long int git_curl_ipresolve = CURL_IPRESOLVE_WHATEVER;
27 int active_requests;
28 int http_is_verbose;
29 ssize_t http_post_buffer = 16 * LARGE_PACKET_MAX;
31 static int min_curl_sessions = 1;
32 static int curl_session_count;
33 static int max_requests = -1;
34 static CURLM *curlm;
35 static CURL *curl_default;
37 #define PREV_BUF_SIZE 4096
39 char curl_errorstr[CURL_ERROR_SIZE];
41 static int curl_ssl_verify = -1;
42 static int curl_ssl_try;
43 static const char *curl_http_version = NULL;
44 static const char *ssl_cert;
45 static const char *ssl_cert_type;
46 static const char *ssl_cipherlist;
47 static const char *ssl_version;
48 static struct {
49 const char *name;
50 long ssl_version;
51 } sslversions[] = {
52 { "sslv2", CURL_SSLVERSION_SSLv2 },
53 { "sslv3", CURL_SSLVERSION_SSLv3 },
54 { "tlsv1", CURL_SSLVERSION_TLSv1 },
55 #ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_0
56 { "tlsv1.0", CURL_SSLVERSION_TLSv1_0 },
57 { "tlsv1.1", CURL_SSLVERSION_TLSv1_1 },
58 { "tlsv1.2", CURL_SSLVERSION_TLSv1_2 },
59 #endif
60 #ifdef GIT_CURL_HAVE_CURL_SSLVERSION_TLSv1_3
61 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3 },
62 #endif
64 static const char *ssl_key;
65 static const char *ssl_key_type;
66 static const char *ssl_capath;
67 static const char *curl_no_proxy;
68 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
69 static const char *ssl_pinnedkey;
70 #endif
71 static const char *ssl_cainfo;
72 static long curl_low_speed_limit = -1;
73 static long curl_low_speed_time = -1;
74 static int curl_ftp_no_epsv;
75 static const char *curl_http_proxy;
76 static const char *http_proxy_authmethod;
78 static const char *http_proxy_ssl_cert;
79 static const char *http_proxy_ssl_key;
80 static const char *http_proxy_ssl_ca_info;
81 static struct credential proxy_cert_auth = CREDENTIAL_INIT;
82 static int proxy_ssl_cert_password_required;
84 static struct {
85 const char *name;
86 long curlauth_param;
87 } proxy_authmethods[] = {
88 { "basic", CURLAUTH_BASIC },
89 { "digest", CURLAUTH_DIGEST },
90 { "negotiate", CURLAUTH_GSSNEGOTIATE },
91 { "ntlm", CURLAUTH_NTLM },
92 { "anyauth", CURLAUTH_ANY },
94 * CURLAUTH_DIGEST_IE has no corresponding command-line option in
95 * curl(1) and is not included in CURLAUTH_ANY, so we leave it out
96 * here, too
99 #ifdef CURLGSSAPI_DELEGATION_FLAG
100 static const char *curl_deleg;
101 static struct {
102 const char *name;
103 long curl_deleg_param;
104 } curl_deleg_levels[] = {
105 { "none", CURLGSSAPI_DELEGATION_NONE },
106 { "policy", CURLGSSAPI_DELEGATION_POLICY_FLAG },
107 { "always", CURLGSSAPI_DELEGATION_FLAG },
109 #endif
111 static struct credential proxy_auth = CREDENTIAL_INIT;
112 static const char *curl_proxyuserpwd;
113 static const char *curl_cookie_file;
114 static int curl_save_cookies;
115 struct credential http_auth = CREDENTIAL_INIT;
116 static int http_proactive_auth;
117 static const char *user_agent;
118 static int curl_empty_auth = -1;
120 enum http_follow_config http_follow_config = HTTP_FOLLOW_INITIAL;
122 static struct credential cert_auth = CREDENTIAL_INIT;
123 static int ssl_cert_password_required;
124 static unsigned long http_auth_methods = CURLAUTH_ANY;
125 static int http_auth_methods_restricted;
126 /* Modes for which empty_auth cannot actually help us. */
127 static unsigned long empty_auth_useless =
128 CURLAUTH_BASIC
129 | CURLAUTH_DIGEST_IE
130 | CURLAUTH_DIGEST;
132 static struct curl_slist *pragma_header;
133 static struct curl_slist *no_pragma_header;
134 static struct string_list extra_http_headers = STRING_LIST_INIT_DUP;
136 static struct curl_slist *host_resolutions;
138 static struct active_request_slot *active_queue_head;
140 static char *cached_accept_language;
142 static char *http_ssl_backend;
144 static int http_schannel_check_revoke = 1;
146 * With the backend being set to `schannel`, setting sslCAinfo would override
147 * the Certificate Store in cURL v7.60.0 and later, which is not what we want
148 * by default.
150 static int http_schannel_use_ssl_cainfo;
152 size_t fread_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
154 size_t size = eltsize * nmemb;
155 struct buffer *buffer = buffer_;
157 if (size > buffer->buf.len - buffer->posn)
158 size = buffer->buf.len - buffer->posn;
159 memcpy(ptr, buffer->buf.buf + buffer->posn, size);
160 buffer->posn += size;
162 return size / eltsize;
165 int seek_buffer(void *clientp, curl_off_t offset, int origin)
167 struct buffer *buffer = clientp;
169 if (origin != SEEK_SET)
170 BUG("seek_buffer only handles SEEK_SET");
171 if (offset < 0 || offset >= buffer->buf.len) {
172 error("curl seek would be outside of buffer");
173 return CURL_SEEKFUNC_FAIL;
176 buffer->posn = offset;
177 return CURL_SEEKFUNC_OK;
180 size_t fwrite_buffer(char *ptr, size_t eltsize, size_t nmemb, void *buffer_)
182 size_t size = eltsize * nmemb;
183 struct strbuf *buffer = buffer_;
185 strbuf_add(buffer, ptr, size);
186 return nmemb;
190 * A folded header continuation line starts with any number of spaces or
191 * horizontal tab characters (SP or HTAB) as per RFC 7230 section 3.2.
192 * It is not a continuation line if the line starts with any other character.
194 static inline int is_hdr_continuation(const char *ptr, const size_t size)
196 return size && (*ptr == ' ' || *ptr == '\t');
199 static size_t fwrite_wwwauth(char *ptr, size_t eltsize, size_t nmemb, void *p UNUSED)
201 size_t size = eltsize * nmemb;
202 struct strvec *values = &http_auth.wwwauth_headers;
203 struct strbuf buf = STRBUF_INIT;
204 const char *val;
205 size_t val_len;
208 * Header lines may not come NULL-terminated from libcurl so we must
209 * limit all scans to the maximum length of the header line, or leverage
210 * strbufs for all operations.
212 * In addition, it is possible that header values can be split over
213 * multiple lines as per RFC 7230. 'Line folding' has been deprecated
214 * but older servers may still emit them. A continuation header field
215 * value is identified as starting with a space or horizontal tab.
217 * The formal definition of a header field as given in RFC 7230 is:
219 * header-field = field-name ":" OWS field-value OWS
221 * field-name = token
222 * field-value = *( field-content / obs-fold )
223 * field-content = field-vchar [ 1*( SP / HTAB ) field-vchar ]
224 * field-vchar = VCHAR / obs-text
226 * obs-fold = CRLF 1*( SP / HTAB )
227 * ; obsolete line folding
228 * ; see Section 3.2.4
231 /* Start of a new WWW-Authenticate header */
232 if (skip_iprefix_mem(ptr, size, "www-authenticate:", &val, &val_len)) {
233 strbuf_add(&buf, val, val_len);
236 * Strip the CRLF that should be present at the end of each
237 * field as well as any trailing or leading whitespace from the
238 * value.
240 strbuf_trim(&buf);
242 strvec_push(values, buf.buf);
243 http_auth.header_is_last_match = 1;
244 goto exit;
248 * This line could be a continuation of the previously matched header
249 * field. If this is the case then we should append this value to the
250 * end of the previously consumed value.
252 if (http_auth.header_is_last_match && is_hdr_continuation(ptr, size)) {
254 * Trim the CRLF and any leading or trailing from this line.
256 strbuf_add(&buf, ptr, size);
257 strbuf_trim(&buf);
260 * At this point we should always have at least one existing
261 * value, even if it is empty. Do not bother appending the new
262 * value if this continuation header is itself empty.
264 if (!values->nr) {
265 BUG("should have at least one existing header value");
266 } else if (buf.len) {
267 char *prev = xstrdup(values->v[values->nr - 1]);
269 /* Join two non-empty values with a single space. */
270 const char *const sp = *prev ? " " : "";
272 strvec_pop(values);
273 strvec_pushf(values, "%s%s%s", prev, sp, buf.buf);
274 free(prev);
277 goto exit;
280 /* Not a continuation of a previously matched auth header line. */
281 http_auth.header_is_last_match = 0;
284 * If this is a HTTP status line and not a header field, this signals
285 * a different HTTP response. libcurl writes all the output of all
286 * response headers of all responses, including redirects.
287 * We only care about the last HTTP request response's headers so clear
288 * the existing array.
290 if (skip_iprefix_mem(ptr, size, "http/", &val, &val_len))
291 strvec_clear(values);
293 exit:
294 strbuf_release(&buf);
295 return size;
298 size_t fwrite_null(char *ptr UNUSED, size_t eltsize UNUSED, size_t nmemb,
299 void *data UNUSED)
301 return nmemb;
304 static void closedown_active_slot(struct active_request_slot *slot)
306 active_requests--;
307 slot->in_use = 0;
310 static void finish_active_slot(struct active_request_slot *slot)
312 closedown_active_slot(slot);
313 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE, &slot->http_code);
315 if (slot->finished)
316 (*slot->finished) = 1;
318 /* Store slot results so they can be read after the slot is reused */
319 if (slot->results) {
320 slot->results->curl_result = slot->curl_result;
321 slot->results->http_code = slot->http_code;
322 curl_easy_getinfo(slot->curl, CURLINFO_HTTPAUTH_AVAIL,
323 &slot->results->auth_avail);
325 curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CONNECTCODE,
326 &slot->results->http_connectcode);
329 /* Run callback if appropriate */
330 if (slot->callback_func)
331 slot->callback_func(slot->callback_data);
334 static void xmulti_remove_handle(struct active_request_slot *slot)
336 curl_multi_remove_handle(curlm, slot->curl);
339 static void process_curl_messages(void)
341 int num_messages;
342 struct active_request_slot *slot;
343 CURLMsg *curl_message = curl_multi_info_read(curlm, &num_messages);
345 while (curl_message != NULL) {
346 if (curl_message->msg == CURLMSG_DONE) {
347 int curl_result = curl_message->data.result;
348 slot = active_queue_head;
349 while (slot != NULL &&
350 slot->curl != curl_message->easy_handle)
351 slot = slot->next;
352 if (slot) {
353 xmulti_remove_handle(slot);
354 slot->curl_result = curl_result;
355 finish_active_slot(slot);
356 } else {
357 fprintf(stderr, "Received DONE message for unknown request!\n");
359 } else {
360 fprintf(stderr, "Unknown CURL message received: %d\n",
361 (int)curl_message->msg);
363 curl_message = curl_multi_info_read(curlm, &num_messages);
367 static int http_options(const char *var, const char *value,
368 const struct config_context *ctx, void *data)
370 if (!strcmp("http.version", var)) {
371 return git_config_string(&curl_http_version, var, value);
373 if (!strcmp("http.sslverify", var)) {
374 curl_ssl_verify = git_config_bool(var, value);
375 return 0;
377 if (!strcmp("http.sslcipherlist", var))
378 return git_config_string(&ssl_cipherlist, var, value);
379 if (!strcmp("http.sslversion", var))
380 return git_config_string(&ssl_version, var, value);
381 if (!strcmp("http.sslcert", var))
382 return git_config_pathname(&ssl_cert, var, value);
383 if (!strcmp("http.sslcerttype", var))
384 return git_config_string(&ssl_cert_type, var, value);
385 if (!strcmp("http.sslkey", var))
386 return git_config_pathname(&ssl_key, var, value);
387 if (!strcmp("http.sslkeytype", var))
388 return git_config_string(&ssl_key_type, var, value);
389 if (!strcmp("http.sslcapath", var))
390 return git_config_pathname(&ssl_capath, var, value);
391 if (!strcmp("http.sslcainfo", var))
392 return git_config_pathname(&ssl_cainfo, var, value);
393 if (!strcmp("http.sslcertpasswordprotected", var)) {
394 ssl_cert_password_required = git_config_bool(var, value);
395 return 0;
397 if (!strcmp("http.ssltry", var)) {
398 curl_ssl_try = git_config_bool(var, value);
399 return 0;
401 if (!strcmp("http.sslbackend", var)) {
402 free(http_ssl_backend);
403 http_ssl_backend = xstrdup_or_null(value);
404 return 0;
407 if (!strcmp("http.schannelcheckrevoke", var)) {
408 http_schannel_check_revoke = git_config_bool(var, value);
409 return 0;
412 if (!strcmp("http.schannelusesslcainfo", var)) {
413 http_schannel_use_ssl_cainfo = git_config_bool(var, value);
414 return 0;
417 if (!strcmp("http.minsessions", var)) {
418 min_curl_sessions = git_config_int(var, value, ctx->kvi);
419 if (min_curl_sessions > 1)
420 min_curl_sessions = 1;
421 return 0;
423 if (!strcmp("http.maxrequests", var)) {
424 max_requests = git_config_int(var, value, ctx->kvi);
425 return 0;
427 if (!strcmp("http.lowspeedlimit", var)) {
428 curl_low_speed_limit = (long)git_config_int(var, value, ctx->kvi);
429 return 0;
431 if (!strcmp("http.lowspeedtime", var)) {
432 curl_low_speed_time = (long)git_config_int(var, value, ctx->kvi);
433 return 0;
436 if (!strcmp("http.noepsv", var)) {
437 curl_ftp_no_epsv = git_config_bool(var, value);
438 return 0;
440 if (!strcmp("http.proxy", var))
441 return git_config_string(&curl_http_proxy, var, value);
443 if (!strcmp("http.proxyauthmethod", var))
444 return git_config_string(&http_proxy_authmethod, var, value);
446 if (!strcmp("http.proxysslcert", var))
447 return git_config_string(&http_proxy_ssl_cert, var, value);
449 if (!strcmp("http.proxysslkey", var))
450 return git_config_string(&http_proxy_ssl_key, var, value);
452 if (!strcmp("http.proxysslcainfo", var))
453 return git_config_string(&http_proxy_ssl_ca_info, var, value);
455 if (!strcmp("http.proxysslcertpasswordprotected", var)) {
456 proxy_ssl_cert_password_required = git_config_bool(var, value);
457 return 0;
460 if (!strcmp("http.cookiefile", var))
461 return git_config_pathname(&curl_cookie_file, var, value);
462 if (!strcmp("http.savecookies", var)) {
463 curl_save_cookies = git_config_bool(var, value);
464 return 0;
467 if (!strcmp("http.postbuffer", var)) {
468 http_post_buffer = git_config_ssize_t(var, value, ctx->kvi);
469 if (http_post_buffer < 0)
470 warning(_("negative value for http.postBuffer; defaulting to %d"), LARGE_PACKET_MAX);
471 if (http_post_buffer < LARGE_PACKET_MAX)
472 http_post_buffer = LARGE_PACKET_MAX;
473 return 0;
476 if (!strcmp("http.useragent", var))
477 return git_config_string(&user_agent, var, value);
479 if (!strcmp("http.emptyauth", var)) {
480 if (value && !strcmp("auto", value))
481 curl_empty_auth = -1;
482 else
483 curl_empty_auth = git_config_bool(var, value);
484 return 0;
487 if (!strcmp("http.delegation", var)) {
488 #ifdef CURLGSSAPI_DELEGATION_FLAG
489 return git_config_string(&curl_deleg, var, value);
490 #else
491 warning(_("Delegation control is not supported with cURL < 7.22.0"));
492 return 0;
493 #endif
496 if (!strcmp("http.pinnedpubkey", var)) {
497 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
498 return git_config_pathname(&ssl_pinnedkey, var, value);
499 #else
500 warning(_("Public key pinning not supported with cURL < 7.39.0"));
501 return 0;
502 #endif
505 if (!strcmp("http.extraheader", var)) {
506 if (!value) {
507 return config_error_nonbool(var);
508 } else if (!*value) {
509 string_list_clear(&extra_http_headers, 0);
510 } else {
511 string_list_append(&extra_http_headers, value);
513 return 0;
516 if (!strcmp("http.curloptresolve", var)) {
517 if (!value) {
518 return config_error_nonbool(var);
519 } else if (!*value) {
520 curl_slist_free_all(host_resolutions);
521 host_resolutions = NULL;
522 } else {
523 host_resolutions = curl_slist_append(host_resolutions, value);
525 return 0;
528 if (!strcmp("http.followredirects", var)) {
529 if (value && !strcmp(value, "initial"))
530 http_follow_config = HTTP_FOLLOW_INITIAL;
531 else if (git_config_bool(var, value))
532 http_follow_config = HTTP_FOLLOW_ALWAYS;
533 else
534 http_follow_config = HTTP_FOLLOW_NONE;
535 return 0;
538 /* Fall back on the default ones */
539 return git_default_config(var, value, ctx, data);
542 static int curl_empty_auth_enabled(void)
544 if (curl_empty_auth >= 0)
545 return curl_empty_auth;
548 * In the automatic case, kick in the empty-auth
549 * hack as long as we would potentially try some
550 * method more exotic than "Basic" or "Digest".
552 * But only do this when this is our second or
553 * subsequent request, as by then we know what
554 * methods are available.
556 if (http_auth_methods_restricted &&
557 (http_auth_methods & ~empty_auth_useless))
558 return 1;
559 return 0;
562 static void init_curl_http_auth(CURL *result)
564 if (!http_auth.username || !*http_auth.username) {
565 if (curl_empty_auth_enabled())
566 curl_easy_setopt(result, CURLOPT_USERPWD, ":");
567 return;
570 credential_fill(&http_auth);
572 curl_easy_setopt(result, CURLOPT_USERNAME, http_auth.username);
573 curl_easy_setopt(result, CURLOPT_PASSWORD, http_auth.password);
576 /* *var must be free-able */
577 static void var_override(const char **var, char *value)
579 if (value) {
580 free((void *)*var);
581 *var = xstrdup(value);
585 static void set_proxyauth_name_password(CURL *result)
587 curl_easy_setopt(result, CURLOPT_PROXYUSERNAME,
588 proxy_auth.username);
589 curl_easy_setopt(result, CURLOPT_PROXYPASSWORD,
590 proxy_auth.password);
593 static void init_curl_proxy_auth(CURL *result)
595 if (proxy_auth.username) {
596 if (!proxy_auth.password)
597 credential_fill(&proxy_auth);
598 set_proxyauth_name_password(result);
601 var_override(&http_proxy_authmethod, getenv("GIT_HTTP_PROXY_AUTHMETHOD"));
603 if (http_proxy_authmethod) {
604 int i;
605 for (i = 0; i < ARRAY_SIZE(proxy_authmethods); i++) {
606 if (!strcmp(http_proxy_authmethod, proxy_authmethods[i].name)) {
607 curl_easy_setopt(result, CURLOPT_PROXYAUTH,
608 proxy_authmethods[i].curlauth_param);
609 break;
612 if (i == ARRAY_SIZE(proxy_authmethods)) {
613 warning("unsupported proxy authentication method %s: using anyauth",
614 http_proxy_authmethod);
615 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
618 else
619 curl_easy_setopt(result, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
622 static int has_cert_password(void)
624 if (ssl_cert == NULL || ssl_cert_password_required != 1)
625 return 0;
626 if (!cert_auth.password) {
627 cert_auth.protocol = xstrdup("cert");
628 cert_auth.host = xstrdup("");
629 cert_auth.username = xstrdup("");
630 cert_auth.path = xstrdup(ssl_cert);
631 credential_fill(&cert_auth);
633 return 1;
636 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
637 static int has_proxy_cert_password(void)
639 if (http_proxy_ssl_cert == NULL || proxy_ssl_cert_password_required != 1)
640 return 0;
641 if (!proxy_cert_auth.password) {
642 proxy_cert_auth.protocol = xstrdup("cert");
643 proxy_cert_auth.host = xstrdup("");
644 proxy_cert_auth.username = xstrdup("");
645 proxy_cert_auth.path = xstrdup(http_proxy_ssl_cert);
646 credential_fill(&proxy_cert_auth);
648 return 1;
650 #endif
652 #ifdef GITCURL_HAVE_CURLOPT_TCP_KEEPALIVE
653 static void set_curl_keepalive(CURL *c)
655 curl_easy_setopt(c, CURLOPT_TCP_KEEPALIVE, 1);
658 #else
659 static int sockopt_callback(void *client, curl_socket_t fd, curlsocktype type)
661 int ka = 1;
662 int rc;
663 socklen_t len = (socklen_t)sizeof(ka);
665 if (type != CURLSOCKTYPE_IPCXN)
666 return 0;
668 rc = setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void *)&ka, len);
669 if (rc < 0)
670 warning_errno("unable to set SO_KEEPALIVE on socket");
672 return CURL_SOCKOPT_OK;
675 static void set_curl_keepalive(CURL *c)
677 curl_easy_setopt(c, CURLOPT_SOCKOPTFUNCTION, sockopt_callback);
679 #endif
681 /* Return 1 if redactions have been made, 0 otherwise. */
682 static int redact_sensitive_header(struct strbuf *header, size_t offset)
684 int ret = 0;
685 const char *sensitive_header;
687 if (trace_curl_redact &&
688 (skip_iprefix(header->buf + offset, "Authorization:", &sensitive_header) ||
689 skip_iprefix(header->buf + offset, "Proxy-Authorization:", &sensitive_header))) {
690 /* The first token is the type, which is OK to log */
691 while (isspace(*sensitive_header))
692 sensitive_header++;
693 while (*sensitive_header && !isspace(*sensitive_header))
694 sensitive_header++;
695 /* Everything else is opaque and possibly sensitive */
696 strbuf_setlen(header, sensitive_header - header->buf);
697 strbuf_addstr(header, " <redacted>");
698 ret = 1;
699 } else if (trace_curl_redact &&
700 skip_iprefix(header->buf + offset, "Cookie:", &sensitive_header)) {
701 struct strbuf redacted_header = STRBUF_INIT;
702 const char *cookie;
704 while (isspace(*sensitive_header))
705 sensitive_header++;
707 cookie = sensitive_header;
709 while (cookie) {
710 char *equals;
711 char *semicolon = strstr(cookie, "; ");
712 if (semicolon)
713 *semicolon = 0;
714 equals = strchrnul(cookie, '=');
715 if (!equals) {
716 /* invalid cookie, just append and continue */
717 strbuf_addstr(&redacted_header, cookie);
718 continue;
720 strbuf_add(&redacted_header, cookie, equals - cookie);
721 strbuf_addstr(&redacted_header, "=<redacted>");
722 if (semicolon) {
724 * There are more cookies. (Or, for some
725 * reason, the input string ends in "; ".)
727 strbuf_addstr(&redacted_header, "; ");
728 cookie = semicolon + strlen("; ");
729 } else {
730 cookie = NULL;
734 strbuf_setlen(header, sensitive_header - header->buf);
735 strbuf_addbuf(header, &redacted_header);
736 ret = 1;
738 return ret;
741 /* Redact headers in info */
742 static void redact_sensitive_info_header(struct strbuf *header)
744 const char *sensitive_header;
747 * curl's h2h3 prints headers in info, e.g.:
748 * h2h3 [<header-name>: <header-val>]
750 if (trace_curl_redact &&
751 (skip_iprefix(header->buf, "h2h3 [", &sensitive_header) ||
752 skip_iprefix(header->buf, "h2 [", &sensitive_header))) {
753 if (redact_sensitive_header(header, sensitive_header - header->buf)) {
754 /* redaction ate our closing bracket */
755 strbuf_addch(header, ']');
760 static void curl_dump_header(const char *text, unsigned char *ptr, size_t size, int hide_sensitive_header)
762 struct strbuf out = STRBUF_INIT;
763 struct strbuf **headers, **header;
765 strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
766 text, (long)size, (long)size);
767 trace_strbuf(&trace_curl, &out);
768 strbuf_reset(&out);
769 strbuf_add(&out, ptr, size);
770 headers = strbuf_split_max(&out, '\n', 0);
772 for (header = headers; *header; header++) {
773 if (hide_sensitive_header)
774 redact_sensitive_header(*header, 0);
775 strbuf_insertstr((*header), 0, text);
776 strbuf_insertstr((*header), strlen(text), ": ");
777 strbuf_rtrim((*header));
778 strbuf_addch((*header), '\n');
779 trace_strbuf(&trace_curl, (*header));
781 strbuf_list_free(headers);
782 strbuf_release(&out);
785 static void curl_dump_data(const char *text, unsigned char *ptr, size_t size)
787 size_t i;
788 struct strbuf out = STRBUF_INIT;
789 unsigned int width = 60;
791 strbuf_addf(&out, "%s, %10.10ld bytes (0x%8.8lx)\n",
792 text, (long)size, (long)size);
793 trace_strbuf(&trace_curl, &out);
795 for (i = 0; i < size; i += width) {
796 size_t w;
798 strbuf_reset(&out);
799 strbuf_addf(&out, "%s: ", text);
800 for (w = 0; (w < width) && (i + w < size); w++) {
801 unsigned char ch = ptr[i + w];
803 strbuf_addch(&out,
804 (ch >= 0x20) && (ch < 0x80)
805 ? ch : '.');
807 strbuf_addch(&out, '\n');
808 trace_strbuf(&trace_curl, &out);
810 strbuf_release(&out);
813 static void curl_dump_info(char *data, size_t size)
815 struct strbuf buf = STRBUF_INIT;
817 strbuf_add(&buf, data, size);
819 redact_sensitive_info_header(&buf);
820 trace_printf_key(&trace_curl, "== Info: %s", buf.buf);
822 strbuf_release(&buf);
825 static int curl_trace(CURL *handle UNUSED, curl_infotype type,
826 char *data, size_t size,
827 void *userp UNUSED)
829 const char *text;
830 enum { NO_FILTER = 0, DO_FILTER = 1 };
832 switch (type) {
833 case CURLINFO_TEXT:
834 curl_dump_info(data, size);
835 break;
836 case CURLINFO_HEADER_OUT:
837 text = "=> Send header";
838 curl_dump_header(text, (unsigned char *)data, size, DO_FILTER);
839 break;
840 case CURLINFO_DATA_OUT:
841 if (trace_curl_data) {
842 text = "=> Send data";
843 curl_dump_data(text, (unsigned char *)data, size);
845 break;
846 case CURLINFO_SSL_DATA_OUT:
847 if (trace_curl_data) {
848 text = "=> Send SSL data";
849 curl_dump_data(text, (unsigned char *)data, size);
851 break;
852 case CURLINFO_HEADER_IN:
853 text = "<= Recv header";
854 curl_dump_header(text, (unsigned char *)data, size, NO_FILTER);
855 break;
856 case CURLINFO_DATA_IN:
857 if (trace_curl_data) {
858 text = "<= Recv data";
859 curl_dump_data(text, (unsigned char *)data, size);
861 break;
862 case CURLINFO_SSL_DATA_IN:
863 if (trace_curl_data) {
864 text = "<= Recv SSL data";
865 curl_dump_data(text, (unsigned char *)data, size);
867 break;
869 default: /* we ignore unknown types by default */
870 return 0;
872 return 0;
875 void http_trace_curl_no_data(void)
877 trace_override_envvar(&trace_curl, "1");
878 trace_curl_data = 0;
881 void setup_curl_trace(CURL *handle)
883 if (!trace_want(&trace_curl))
884 return;
885 curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
886 curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, curl_trace);
887 curl_easy_setopt(handle, CURLOPT_DEBUGDATA, NULL);
890 static void proto_list_append(struct strbuf *list, const char *proto)
892 if (!list)
893 return;
894 if (list->len)
895 strbuf_addch(list, ',');
896 strbuf_addstr(list, proto);
899 static long get_curl_allowed_protocols(int from_user, struct strbuf *list)
901 long bits = 0;
903 if (is_transport_allowed("http", from_user)) {
904 bits |= CURLPROTO_HTTP;
905 proto_list_append(list, "http");
907 if (is_transport_allowed("https", from_user)) {
908 bits |= CURLPROTO_HTTPS;
909 proto_list_append(list, "https");
911 if (is_transport_allowed("ftp", from_user)) {
912 bits |= CURLPROTO_FTP;
913 proto_list_append(list, "ftp");
915 if (is_transport_allowed("ftps", from_user)) {
916 bits |= CURLPROTO_FTPS;
917 proto_list_append(list, "ftps");
920 return bits;
923 #ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
924 static int get_curl_http_version_opt(const char *version_string, long *opt)
926 int i;
927 static struct {
928 const char *name;
929 long opt_token;
930 } choice[] = {
931 { "HTTP/1.1", CURL_HTTP_VERSION_1_1 },
932 { "HTTP/2", CURL_HTTP_VERSION_2 }
935 for (i = 0; i < ARRAY_SIZE(choice); i++) {
936 if (!strcmp(version_string, choice[i].name)) {
937 *opt = choice[i].opt_token;
938 return 0;
942 warning("unknown value given to http.version: '%s'", version_string);
943 return -1; /* not found */
946 #endif
948 static CURL *get_curl_handle(void)
950 CURL *result = curl_easy_init();
952 if (!result)
953 die("curl_easy_init failed");
955 if (!curl_ssl_verify) {
956 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 0);
957 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 0);
958 } else {
959 /* Verify authenticity of the peer's certificate */
960 curl_easy_setopt(result, CURLOPT_SSL_VERIFYPEER, 1);
961 /* The name in the cert must match whom we tried to connect */
962 curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
965 #ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
966 if (curl_http_version) {
967 long opt;
968 if (!get_curl_http_version_opt(curl_http_version, &opt)) {
969 /* Set request use http version */
970 curl_easy_setopt(result, CURLOPT_HTTP_VERSION, opt);
973 #endif
975 curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
976 curl_easy_setopt(result, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
978 #ifdef CURLGSSAPI_DELEGATION_FLAG
979 if (curl_deleg) {
980 int i;
981 for (i = 0; i < ARRAY_SIZE(curl_deleg_levels); i++) {
982 if (!strcmp(curl_deleg, curl_deleg_levels[i].name)) {
983 curl_easy_setopt(result, CURLOPT_GSSAPI_DELEGATION,
984 curl_deleg_levels[i].curl_deleg_param);
985 break;
988 if (i == ARRAY_SIZE(curl_deleg_levels))
989 warning("Unknown delegation method '%s': using default",
990 curl_deleg);
992 #endif
994 if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
995 !http_schannel_check_revoke) {
996 #ifdef GIT_CURL_HAVE_CURLSSLOPT_NO_REVOKE
997 curl_easy_setopt(result, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
998 #else
999 warning(_("CURLSSLOPT_NO_REVOKE not supported with cURL < 7.44.0"));
1000 #endif
1003 if (http_proactive_auth)
1004 init_curl_http_auth(result);
1006 if (getenv("GIT_SSL_VERSION"))
1007 ssl_version = getenv("GIT_SSL_VERSION");
1008 if (ssl_version && *ssl_version) {
1009 int i;
1010 for (i = 0; i < ARRAY_SIZE(sslversions); i++) {
1011 if (!strcmp(ssl_version, sslversions[i].name)) {
1012 curl_easy_setopt(result, CURLOPT_SSLVERSION,
1013 sslversions[i].ssl_version);
1014 break;
1017 if (i == ARRAY_SIZE(sslversions))
1018 warning("unsupported ssl version %s: using default",
1019 ssl_version);
1022 if (getenv("GIT_SSL_CIPHER_LIST"))
1023 ssl_cipherlist = getenv("GIT_SSL_CIPHER_LIST");
1024 if (ssl_cipherlist != NULL && *ssl_cipherlist)
1025 curl_easy_setopt(result, CURLOPT_SSL_CIPHER_LIST,
1026 ssl_cipherlist);
1028 if (ssl_cert)
1029 curl_easy_setopt(result, CURLOPT_SSLCERT, ssl_cert);
1030 if (ssl_cert_type)
1031 curl_easy_setopt(result, CURLOPT_SSLCERTTYPE, ssl_cert_type);
1032 if (has_cert_password())
1033 curl_easy_setopt(result, CURLOPT_KEYPASSWD, cert_auth.password);
1034 if (ssl_key)
1035 curl_easy_setopt(result, CURLOPT_SSLKEY, ssl_key);
1036 if (ssl_key_type)
1037 curl_easy_setopt(result, CURLOPT_SSLKEYTYPE, ssl_key_type);
1038 if (ssl_capath)
1039 curl_easy_setopt(result, CURLOPT_CAPATH, ssl_capath);
1040 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
1041 if (ssl_pinnedkey)
1042 curl_easy_setopt(result, CURLOPT_PINNEDPUBLICKEY, ssl_pinnedkey);
1043 #endif
1044 if (http_ssl_backend && !strcmp("schannel", http_ssl_backend) &&
1045 !http_schannel_use_ssl_cainfo) {
1046 curl_easy_setopt(result, CURLOPT_CAINFO, NULL);
1047 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
1048 curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, NULL);
1049 #endif
1050 } else if (ssl_cainfo != NULL || http_proxy_ssl_ca_info != NULL) {
1051 if (ssl_cainfo)
1052 curl_easy_setopt(result, CURLOPT_CAINFO, ssl_cainfo);
1053 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_CAINFO
1054 if (http_proxy_ssl_ca_info)
1055 curl_easy_setopt(result, CURLOPT_PROXY_CAINFO, http_proxy_ssl_ca_info);
1056 #endif
1059 if (curl_low_speed_limit > 0 && curl_low_speed_time > 0) {
1060 curl_easy_setopt(result, CURLOPT_LOW_SPEED_LIMIT,
1061 curl_low_speed_limit);
1062 curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
1063 curl_low_speed_time);
1066 curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
1067 curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
1069 #ifdef GIT_CURL_HAVE_CURLOPT_PROTOCOLS_STR
1071 struct strbuf buf = STRBUF_INIT;
1073 get_curl_allowed_protocols(0, &buf);
1074 curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS_STR, buf.buf);
1075 strbuf_reset(&buf);
1077 get_curl_allowed_protocols(-1, &buf);
1078 curl_easy_setopt(result, CURLOPT_PROTOCOLS_STR, buf.buf);
1079 strbuf_release(&buf);
1081 #else
1082 curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS,
1083 get_curl_allowed_protocols(0, NULL));
1084 curl_easy_setopt(result, CURLOPT_PROTOCOLS,
1085 get_curl_allowed_protocols(-1, NULL));
1086 #endif
1088 if (getenv("GIT_CURL_VERBOSE"))
1089 http_trace_curl_no_data();
1090 setup_curl_trace(result);
1091 if (getenv("GIT_TRACE_CURL_NO_DATA"))
1092 trace_curl_data = 0;
1093 if (!git_env_bool("GIT_TRACE_REDACT", 1))
1094 trace_curl_redact = 0;
1096 curl_easy_setopt(result, CURLOPT_USERAGENT,
1097 user_agent ? user_agent : git_user_agent());
1099 if (curl_ftp_no_epsv)
1100 curl_easy_setopt(result, CURLOPT_FTP_USE_EPSV, 0);
1102 if (curl_ssl_try)
1103 curl_easy_setopt(result, CURLOPT_USE_SSL, CURLUSESSL_TRY);
1106 * CURL also examines these variables as a fallback; but we need to query
1107 * them here in order to decide whether to prompt for missing password (cf.
1108 * init_curl_proxy_auth()).
1110 * Unlike many other common environment variables, these are historically
1111 * lowercase only. It appears that CURL did not know this and implemented
1112 * only uppercase variants, which was later corrected to take both - with
1113 * the exception of http_proxy, which is lowercase only also in CURL. As
1114 * the lowercase versions are the historical quasi-standard, they take
1115 * precedence here, as in CURL.
1117 if (!curl_http_proxy) {
1118 if (http_auth.protocol && !strcmp(http_auth.protocol, "https")) {
1119 var_override(&curl_http_proxy, getenv("HTTPS_PROXY"));
1120 var_override(&curl_http_proxy, getenv("https_proxy"));
1121 } else {
1122 var_override(&curl_http_proxy, getenv("http_proxy"));
1124 if (!curl_http_proxy) {
1125 var_override(&curl_http_proxy, getenv("ALL_PROXY"));
1126 var_override(&curl_http_proxy, getenv("all_proxy"));
1130 if (curl_http_proxy && curl_http_proxy[0] == '\0') {
1132 * Handle case with the empty http.proxy value here to keep
1133 * common code clean.
1134 * NB: empty option disables proxying at all.
1136 curl_easy_setopt(result, CURLOPT_PROXY, "");
1137 } else if (curl_http_proxy) {
1138 if (starts_with(curl_http_proxy, "socks5h"))
1139 curl_easy_setopt(result,
1140 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
1141 else if (starts_with(curl_http_proxy, "socks5"))
1142 curl_easy_setopt(result,
1143 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
1144 else if (starts_with(curl_http_proxy, "socks4a"))
1145 curl_easy_setopt(result,
1146 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4A);
1147 else if (starts_with(curl_http_proxy, "socks"))
1148 curl_easy_setopt(result,
1149 CURLOPT_PROXYTYPE, CURLPROXY_SOCKS4);
1150 #ifdef GIT_CURL_HAVE_CURLOPT_PROXY_KEYPASSWD
1151 else if (starts_with(curl_http_proxy, "https")) {
1152 curl_easy_setopt(result, CURLOPT_PROXYTYPE, CURLPROXY_HTTPS);
1154 if (http_proxy_ssl_cert)
1155 curl_easy_setopt(result, CURLOPT_PROXY_SSLCERT, http_proxy_ssl_cert);
1157 if (http_proxy_ssl_key)
1158 curl_easy_setopt(result, CURLOPT_PROXY_SSLKEY, http_proxy_ssl_key);
1160 if (has_proxy_cert_password())
1161 curl_easy_setopt(result, CURLOPT_PROXY_KEYPASSWD, proxy_cert_auth.password);
1163 #endif
1164 if (strstr(curl_http_proxy, "://"))
1165 credential_from_url(&proxy_auth, curl_http_proxy);
1166 else {
1167 struct strbuf url = STRBUF_INIT;
1168 strbuf_addf(&url, "http://%s", curl_http_proxy);
1169 credential_from_url(&proxy_auth, url.buf);
1170 strbuf_release(&url);
1173 if (!proxy_auth.host)
1174 die("Invalid proxy URL '%s'", curl_http_proxy);
1176 curl_easy_setopt(result, CURLOPT_PROXY, proxy_auth.host);
1177 var_override(&curl_no_proxy, getenv("NO_PROXY"));
1178 var_override(&curl_no_proxy, getenv("no_proxy"));
1179 curl_easy_setopt(result, CURLOPT_NOPROXY, curl_no_proxy);
1181 init_curl_proxy_auth(result);
1183 set_curl_keepalive(result);
1185 return result;
1188 static void set_from_env(const char **var, const char *envname)
1190 const char *val = getenv(envname);
1191 if (val)
1192 *var = val;
1195 void http_init(struct remote *remote, const char *url, int proactive_auth)
1197 char *low_speed_limit;
1198 char *low_speed_time;
1199 char *normalized_url;
1200 struct urlmatch_config config = URLMATCH_CONFIG_INIT;
1202 config.section = "http";
1203 config.key = NULL;
1204 config.collect_fn = http_options;
1205 config.cascade_fn = git_default_config;
1206 config.cb = NULL;
1208 http_is_verbose = 0;
1209 normalized_url = url_normalize(url, &config.url);
1211 git_config(urlmatch_config_entry, &config);
1212 free(normalized_url);
1213 string_list_clear(&config.vars, 1);
1215 #ifdef GIT_CURL_HAVE_CURLSSLSET_NO_BACKENDS
1216 if (http_ssl_backend) {
1217 const curl_ssl_backend **backends;
1218 struct strbuf buf = STRBUF_INIT;
1219 int i;
1221 switch (curl_global_sslset(-1, http_ssl_backend, &backends)) {
1222 case CURLSSLSET_UNKNOWN_BACKEND:
1223 strbuf_addf(&buf, _("Unsupported SSL backend '%s'. "
1224 "Supported SSL backends:"),
1225 http_ssl_backend);
1226 for (i = 0; backends[i]; i++)
1227 strbuf_addf(&buf, "\n\t%s", backends[i]->name);
1228 die("%s", buf.buf);
1229 case CURLSSLSET_NO_BACKENDS:
1230 die(_("Could not set SSL backend to '%s': "
1231 "cURL was built without SSL backends"),
1232 http_ssl_backend);
1233 case CURLSSLSET_TOO_LATE:
1234 die(_("Could not set SSL backend to '%s': already set"),
1235 http_ssl_backend);
1236 case CURLSSLSET_OK:
1237 break; /* Okay! */
1240 #endif
1242 if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
1243 die("curl_global_init failed");
1245 http_proactive_auth = proactive_auth;
1247 if (remote && remote->http_proxy)
1248 curl_http_proxy = xstrdup(remote->http_proxy);
1250 if (remote)
1251 var_override(&http_proxy_authmethod, remote->http_proxy_authmethod);
1253 pragma_header = curl_slist_append(http_copy_default_headers(),
1254 "Pragma: no-cache");
1255 no_pragma_header = curl_slist_append(http_copy_default_headers(),
1256 "Pragma:");
1259 char *http_max_requests = getenv("GIT_HTTP_MAX_REQUESTS");
1260 if (http_max_requests)
1261 max_requests = atoi(http_max_requests);
1264 curlm = curl_multi_init();
1265 if (!curlm)
1266 die("curl_multi_init failed");
1268 if (getenv("GIT_SSL_NO_VERIFY"))
1269 curl_ssl_verify = 0;
1271 set_from_env(&ssl_cert, "GIT_SSL_CERT");
1272 set_from_env(&ssl_cert_type, "GIT_SSL_CERT_TYPE");
1273 set_from_env(&ssl_key, "GIT_SSL_KEY");
1274 set_from_env(&ssl_key_type, "GIT_SSL_KEY_TYPE");
1275 set_from_env(&ssl_capath, "GIT_SSL_CAPATH");
1276 set_from_env(&ssl_cainfo, "GIT_SSL_CAINFO");
1278 set_from_env(&user_agent, "GIT_HTTP_USER_AGENT");
1280 low_speed_limit = getenv("GIT_HTTP_LOW_SPEED_LIMIT");
1281 if (low_speed_limit)
1282 curl_low_speed_limit = strtol(low_speed_limit, NULL, 10);
1283 low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
1284 if (low_speed_time)
1285 curl_low_speed_time = strtol(low_speed_time, NULL, 10);
1287 if (curl_ssl_verify == -1)
1288 curl_ssl_verify = 1;
1290 curl_session_count = 0;
1291 if (max_requests < 1)
1292 max_requests = DEFAULT_MAX_REQUESTS;
1294 set_from_env(&http_proxy_ssl_cert, "GIT_PROXY_SSL_CERT");
1295 set_from_env(&http_proxy_ssl_key, "GIT_PROXY_SSL_KEY");
1296 set_from_env(&http_proxy_ssl_ca_info, "GIT_PROXY_SSL_CAINFO");
1298 if (getenv("GIT_PROXY_SSL_CERT_PASSWORD_PROTECTED"))
1299 proxy_ssl_cert_password_required = 1;
1301 if (getenv("GIT_CURL_FTP_NO_EPSV"))
1302 curl_ftp_no_epsv = 1;
1304 if (url) {
1305 credential_from_url(&http_auth, url);
1306 if (!ssl_cert_password_required &&
1307 getenv("GIT_SSL_CERT_PASSWORD_PROTECTED") &&
1308 starts_with(url, "https://"))
1309 ssl_cert_password_required = 1;
1312 curl_default = get_curl_handle();
1315 void http_cleanup(void)
1317 struct active_request_slot *slot = active_queue_head;
1319 while (slot != NULL) {
1320 struct active_request_slot *next = slot->next;
1321 if (slot->curl) {
1322 xmulti_remove_handle(slot);
1323 curl_easy_cleanup(slot->curl);
1325 free(slot);
1326 slot = next;
1328 active_queue_head = NULL;
1330 curl_easy_cleanup(curl_default);
1332 curl_multi_cleanup(curlm);
1333 curl_global_cleanup();
1335 string_list_clear(&extra_http_headers, 0);
1337 curl_slist_free_all(pragma_header);
1338 pragma_header = NULL;
1340 curl_slist_free_all(no_pragma_header);
1341 no_pragma_header = NULL;
1343 curl_slist_free_all(host_resolutions);
1344 host_resolutions = NULL;
1346 if (curl_http_proxy) {
1347 free((void *)curl_http_proxy);
1348 curl_http_proxy = NULL;
1351 if (proxy_auth.password) {
1352 memset(proxy_auth.password, 0, strlen(proxy_auth.password));
1353 FREE_AND_NULL(proxy_auth.password);
1356 free((void *)curl_proxyuserpwd);
1357 curl_proxyuserpwd = NULL;
1359 free((void *)http_proxy_authmethod);
1360 http_proxy_authmethod = NULL;
1362 if (cert_auth.password) {
1363 memset(cert_auth.password, 0, strlen(cert_auth.password));
1364 FREE_AND_NULL(cert_auth.password);
1366 ssl_cert_password_required = 0;
1368 if (proxy_cert_auth.password) {
1369 memset(proxy_cert_auth.password, 0, strlen(proxy_cert_auth.password));
1370 FREE_AND_NULL(proxy_cert_auth.password);
1372 proxy_ssl_cert_password_required = 0;
1374 FREE_AND_NULL(cached_accept_language);
1377 struct active_request_slot *get_active_slot(void)
1379 struct active_request_slot *slot = active_queue_head;
1380 struct active_request_slot *newslot;
1382 int num_transfers;
1384 /* Wait for a slot to open up if the queue is full */
1385 while (active_requests >= max_requests) {
1386 curl_multi_perform(curlm, &num_transfers);
1387 if (num_transfers < active_requests)
1388 process_curl_messages();
1391 while (slot != NULL && slot->in_use)
1392 slot = slot->next;
1394 if (!slot) {
1395 newslot = xmalloc(sizeof(*newslot));
1396 newslot->curl = NULL;
1397 newslot->in_use = 0;
1398 newslot->next = NULL;
1400 slot = active_queue_head;
1401 if (!slot) {
1402 active_queue_head = newslot;
1403 } else {
1404 while (slot->next != NULL)
1405 slot = slot->next;
1406 slot->next = newslot;
1408 slot = newslot;
1411 if (!slot->curl) {
1412 slot->curl = curl_easy_duphandle(curl_default);
1413 curl_session_count++;
1416 active_requests++;
1417 slot->in_use = 1;
1418 slot->results = NULL;
1419 slot->finished = NULL;
1420 slot->callback_data = NULL;
1421 slot->callback_func = NULL;
1422 curl_easy_setopt(slot->curl, CURLOPT_COOKIEFILE, curl_cookie_file);
1423 if (curl_save_cookies)
1424 curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
1425 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
1426 curl_easy_setopt(slot->curl, CURLOPT_RESOLVE, host_resolutions);
1427 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
1428 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
1429 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
1430 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, NULL);
1431 curl_easy_setopt(slot->curl, CURLOPT_POSTFIELDS, NULL);
1432 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 0);
1433 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
1434 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 1);
1435 curl_easy_setopt(slot->curl, CURLOPT_RANGE, NULL);
1438 * Default following to off unless "ALWAYS" is configured; this gives
1439 * callers a sane starting point, and they can tweak for individual
1440 * HTTP_FOLLOW_* cases themselves.
1442 if (http_follow_config == HTTP_FOLLOW_ALWAYS)
1443 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
1444 else
1445 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 0);
1447 curl_easy_setopt(slot->curl, CURLOPT_IPRESOLVE, git_curl_ipresolve);
1448 curl_easy_setopt(slot->curl, CURLOPT_HTTPAUTH, http_auth_methods);
1449 if (http_auth.password || curl_empty_auth_enabled())
1450 init_curl_http_auth(slot->curl);
1452 return slot;
1455 int start_active_slot(struct active_request_slot *slot)
1457 CURLMcode curlm_result = curl_multi_add_handle(curlm, slot->curl);
1458 int num_transfers;
1460 if (curlm_result != CURLM_OK &&
1461 curlm_result != CURLM_CALL_MULTI_PERFORM) {
1462 warning("curl_multi_add_handle failed: %s",
1463 curl_multi_strerror(curlm_result));
1464 active_requests--;
1465 slot->in_use = 0;
1466 return 0;
1470 * We know there must be something to do, since we just added
1471 * something.
1473 curl_multi_perform(curlm, &num_transfers);
1474 return 1;
1477 struct fill_chain {
1478 void *data;
1479 int (*fill)(void *);
1480 struct fill_chain *next;
1483 static struct fill_chain *fill_cfg;
1485 void add_fill_function(void *data, int (*fill)(void *))
1487 struct fill_chain *new_fill = xmalloc(sizeof(*new_fill));
1488 struct fill_chain **linkp = &fill_cfg;
1489 new_fill->data = data;
1490 new_fill->fill = fill;
1491 new_fill->next = NULL;
1492 while (*linkp)
1493 linkp = &(*linkp)->next;
1494 *linkp = new_fill;
1497 void fill_active_slots(void)
1499 struct active_request_slot *slot = active_queue_head;
1501 while (active_requests < max_requests) {
1502 struct fill_chain *fill;
1503 for (fill = fill_cfg; fill; fill = fill->next)
1504 if (fill->fill(fill->data))
1505 break;
1507 if (!fill)
1508 break;
1511 while (slot != NULL) {
1512 if (!slot->in_use && slot->curl != NULL
1513 && curl_session_count > min_curl_sessions) {
1514 curl_easy_cleanup(slot->curl);
1515 slot->curl = NULL;
1516 curl_session_count--;
1518 slot = slot->next;
1522 void step_active_slots(void)
1524 int num_transfers;
1525 CURLMcode curlm_result;
1527 do {
1528 curlm_result = curl_multi_perform(curlm, &num_transfers);
1529 } while (curlm_result == CURLM_CALL_MULTI_PERFORM);
1530 if (num_transfers < active_requests) {
1531 process_curl_messages();
1532 fill_active_slots();
1536 void run_active_slot(struct active_request_slot *slot)
1538 fd_set readfds;
1539 fd_set writefds;
1540 fd_set excfds;
1541 int max_fd;
1542 struct timeval select_timeout;
1543 int finished = 0;
1545 slot->finished = &finished;
1546 while (!finished) {
1547 step_active_slots();
1549 if (slot->in_use) {
1550 long curl_timeout;
1551 curl_multi_timeout(curlm, &curl_timeout);
1552 if (curl_timeout == 0) {
1553 continue;
1554 } else if (curl_timeout == -1) {
1555 select_timeout.tv_sec = 0;
1556 select_timeout.tv_usec = 50000;
1557 } else {
1558 select_timeout.tv_sec = curl_timeout / 1000;
1559 select_timeout.tv_usec = (curl_timeout % 1000) * 1000;
1562 max_fd = -1;
1563 FD_ZERO(&readfds);
1564 FD_ZERO(&writefds);
1565 FD_ZERO(&excfds);
1566 curl_multi_fdset(curlm, &readfds, &writefds, &excfds, &max_fd);
1569 * It can happen that curl_multi_timeout returns a pathologically
1570 * long timeout when curl_multi_fdset returns no file descriptors
1571 * to read. See commit message for more details.
1573 if (max_fd < 0 &&
1574 (select_timeout.tv_sec > 0 ||
1575 select_timeout.tv_usec > 50000)) {
1576 select_timeout.tv_sec = 0;
1577 select_timeout.tv_usec = 50000;
1580 select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
1585 * The value of slot->finished we set before the loop was used
1586 * to set our "finished" variable when our request completed.
1588 * 1. The slot may not have been reused for another requst
1589 * yet, in which case it still has &finished.
1591 * 2. The slot may already be in-use to serve another request,
1592 * which can further be divided into two cases:
1594 * (a) If call run_active_slot() hasn't been called for that
1595 * other request, slot->finished would have been cleared
1596 * by get_active_slot() and has NULL.
1598 * (b) If the request did call run_active_slot(), then the
1599 * call would have updated slot->finished at the beginning
1600 * of this function, and with the clearing of the member
1601 * below, we would find that slot->finished is now NULL.
1603 * In all cases, slot->finished has no useful information to
1604 * anybody at this point. Some compilers warn us for
1605 * attempting to smuggle a pointer that is about to become
1606 * invalid, i.e. &finished. We clear it here to assure them.
1608 slot->finished = NULL;
1611 static void release_active_slot(struct active_request_slot *slot)
1613 closedown_active_slot(slot);
1614 if (slot->curl) {
1615 xmulti_remove_handle(slot);
1616 if (curl_session_count > min_curl_sessions) {
1617 curl_easy_cleanup(slot->curl);
1618 slot->curl = NULL;
1619 curl_session_count--;
1622 fill_active_slots();
1625 void finish_all_active_slots(void)
1627 struct active_request_slot *slot = active_queue_head;
1629 while (slot != NULL)
1630 if (slot->in_use) {
1631 run_active_slot(slot);
1632 slot = active_queue_head;
1633 } else {
1634 slot = slot->next;
1638 /* Helpers for modifying and creating URLs */
1639 static inline int needs_quote(int ch)
1641 if (((ch >= 'A') && (ch <= 'Z'))
1642 || ((ch >= 'a') && (ch <= 'z'))
1643 || ((ch >= '0') && (ch <= '9'))
1644 || (ch == '/')
1645 || (ch == '-')
1646 || (ch == '.'))
1647 return 0;
1648 return 1;
1651 static char *quote_ref_url(const char *base, const char *ref)
1653 struct strbuf buf = STRBUF_INIT;
1654 const char *cp;
1655 int ch;
1657 end_url_with_slash(&buf, base);
1659 for (cp = ref; (ch = *cp) != 0; cp++)
1660 if (needs_quote(ch))
1661 strbuf_addf(&buf, "%%%02x", ch);
1662 else
1663 strbuf_addch(&buf, *cp);
1665 return strbuf_detach(&buf, NULL);
1668 void append_remote_object_url(struct strbuf *buf, const char *url,
1669 const char *hex,
1670 int only_two_digit_prefix)
1672 end_url_with_slash(buf, url);
1674 strbuf_addf(buf, "objects/%.*s/", 2, hex);
1675 if (!only_two_digit_prefix)
1676 strbuf_addstr(buf, hex + 2);
1679 char *get_remote_object_url(const char *url, const char *hex,
1680 int only_two_digit_prefix)
1682 struct strbuf buf = STRBUF_INIT;
1683 append_remote_object_url(&buf, url, hex, only_two_digit_prefix);
1684 return strbuf_detach(&buf, NULL);
1687 void normalize_curl_result(CURLcode *result, long http_code,
1688 char *errorstr, size_t errorlen)
1691 * If we see a failing http code with CURLE_OK, we have turned off
1692 * FAILONERROR (to keep the server's custom error response), and should
1693 * translate the code into failure here.
1695 * Likewise, if we see a redirect (30x code), that means we turned off
1696 * redirect-following, and we should treat the result as an error.
1698 if (*result == CURLE_OK && http_code >= 300) {
1699 *result = CURLE_HTTP_RETURNED_ERROR;
1701 * Normally curl will already have put the "reason phrase"
1702 * from the server into curl_errorstr; unfortunately without
1703 * FAILONERROR it is lost, so we can give only the numeric
1704 * status code.
1706 xsnprintf(errorstr, errorlen,
1707 "The requested URL returned error: %ld",
1708 http_code);
1712 static int handle_curl_result(struct slot_results *results)
1714 normalize_curl_result(&results->curl_result, results->http_code,
1715 curl_errorstr, sizeof(curl_errorstr));
1717 if (results->curl_result == CURLE_OK) {
1718 credential_approve(&http_auth);
1719 credential_approve(&proxy_auth);
1720 credential_approve(&cert_auth);
1721 return HTTP_OK;
1722 } else if (results->curl_result == CURLE_SSL_CERTPROBLEM) {
1724 * We can't tell from here whether it's a bad path, bad
1725 * certificate, bad password, or something else wrong
1726 * with the certificate. So we reject the credential to
1727 * avoid caching or saving a bad password.
1729 credential_reject(&cert_auth);
1730 return HTTP_NOAUTH;
1731 #ifdef GIT_CURL_HAVE_CURLE_SSL_PINNEDPUBKEYNOTMATCH
1732 } else if (results->curl_result == CURLE_SSL_PINNEDPUBKEYNOTMATCH) {
1733 return HTTP_NOMATCHPUBLICKEY;
1734 #endif
1735 } else if (missing_target(results))
1736 return HTTP_MISSING_TARGET;
1737 else if (results->http_code == 401) {
1738 if (http_auth.username && http_auth.password) {
1739 credential_reject(&http_auth);
1740 return HTTP_NOAUTH;
1741 } else {
1742 http_auth_methods &= ~CURLAUTH_GSSNEGOTIATE;
1743 if (results->auth_avail) {
1744 http_auth_methods &= results->auth_avail;
1745 http_auth_methods_restricted = 1;
1747 return HTTP_REAUTH;
1749 } else {
1750 if (results->http_connectcode == 407)
1751 credential_reject(&proxy_auth);
1752 if (!curl_errorstr[0])
1753 strlcpy(curl_errorstr,
1754 curl_easy_strerror(results->curl_result),
1755 sizeof(curl_errorstr));
1756 return HTTP_ERROR;
1760 int run_one_slot(struct active_request_slot *slot,
1761 struct slot_results *results)
1763 slot->results = results;
1764 if (!start_active_slot(slot)) {
1765 xsnprintf(curl_errorstr, sizeof(curl_errorstr),
1766 "failed to start HTTP request");
1767 return HTTP_START_FAILED;
1770 run_active_slot(slot);
1771 return handle_curl_result(results);
1774 struct curl_slist *http_copy_default_headers(void)
1776 struct curl_slist *headers = NULL;
1777 const struct string_list_item *item;
1779 for_each_string_list_item(item, &extra_http_headers)
1780 headers = curl_slist_append(headers, item->string);
1782 return headers;
1785 static CURLcode curlinfo_strbuf(CURL *curl, CURLINFO info, struct strbuf *buf)
1787 char *ptr;
1788 CURLcode ret;
1790 strbuf_reset(buf);
1791 ret = curl_easy_getinfo(curl, info, &ptr);
1792 if (!ret && ptr)
1793 strbuf_addstr(buf, ptr);
1794 return ret;
1798 * Check for and extract a content-type parameter. "raw"
1799 * should be positioned at the start of the potential
1800 * parameter, with any whitespace already removed.
1802 * "name" is the name of the parameter. The value is appended
1803 * to "out".
1805 static int extract_param(const char *raw, const char *name,
1806 struct strbuf *out)
1808 size_t len = strlen(name);
1810 if (strncasecmp(raw, name, len))
1811 return -1;
1812 raw += len;
1814 if (*raw != '=')
1815 return -1;
1816 raw++;
1818 while (*raw && !isspace(*raw) && *raw != ';')
1819 strbuf_addch(out, *raw++);
1820 return 0;
1824 * Extract a normalized version of the content type, with any
1825 * spaces suppressed, all letters lowercased, and no trailing ";"
1826 * or parameters.
1828 * Note that we will silently remove even invalid whitespace. For
1829 * example, "text / plain" is specifically forbidden by RFC 2616,
1830 * but "text/plain" is the only reasonable output, and this keeps
1831 * our code simple.
1833 * If the "charset" argument is not NULL, store the value of any
1834 * charset parameter there.
1836 * Example:
1837 * "TEXT/PLAIN; charset=utf-8" -> "text/plain", "utf-8"
1838 * "text / plain" -> "text/plain"
1840 static void extract_content_type(struct strbuf *raw, struct strbuf *type,
1841 struct strbuf *charset)
1843 const char *p;
1845 strbuf_reset(type);
1846 strbuf_grow(type, raw->len);
1847 for (p = raw->buf; *p; p++) {
1848 if (isspace(*p))
1849 continue;
1850 if (*p == ';') {
1851 p++;
1852 break;
1854 strbuf_addch(type, tolower(*p));
1857 if (!charset)
1858 return;
1860 strbuf_reset(charset);
1861 while (*p) {
1862 while (isspace(*p) || *p == ';')
1863 p++;
1864 if (!extract_param(p, "charset", charset))
1865 return;
1866 while (*p && !isspace(*p))
1867 p++;
1870 if (!charset->len && starts_with(type->buf, "text/"))
1871 strbuf_addstr(charset, "ISO-8859-1");
1874 static void write_accept_language(struct strbuf *buf)
1877 * MAX_DECIMAL_PLACES must not be larger than 3. If it is larger than
1878 * that, q-value will be smaller than 0.001, the minimum q-value the
1879 * HTTP specification allows. See
1880 * http://tools.ietf.org/html/rfc7231#section-5.3.1 for q-value.
1882 const int MAX_DECIMAL_PLACES = 3;
1883 const int MAX_LANGUAGE_TAGS = 1000;
1884 const int MAX_ACCEPT_LANGUAGE_HEADER_SIZE = 4000;
1885 char **language_tags = NULL;
1886 int num_langs = 0;
1887 const char *s = get_preferred_languages();
1888 int i;
1889 struct strbuf tag = STRBUF_INIT;
1891 /* Don't add Accept-Language header if no language is preferred. */
1892 if (!s)
1893 return;
1896 * Split the colon-separated string of preferred languages into
1897 * language_tags array.
1899 do {
1900 /* collect language tag */
1901 for (; *s && (isalnum(*s) || *s == '_'); s++)
1902 strbuf_addch(&tag, *s == '_' ? '-' : *s);
1904 /* skip .codeset, @modifier and any other unnecessary parts */
1905 while (*s && *s != ':')
1906 s++;
1908 if (tag.len) {
1909 num_langs++;
1910 REALLOC_ARRAY(language_tags, num_langs);
1911 language_tags[num_langs - 1] = strbuf_detach(&tag, NULL);
1912 if (num_langs >= MAX_LANGUAGE_TAGS - 1) /* -1 for '*' */
1913 break;
1915 } while (*s++);
1917 /* write Accept-Language header into buf */
1918 if (num_langs) {
1919 int last_buf_len = 0;
1920 int max_q;
1921 int decimal_places;
1922 char q_format[32];
1924 /* add '*' */
1925 REALLOC_ARRAY(language_tags, num_langs + 1);
1926 language_tags[num_langs++] = "*"; /* it's OK; this won't be freed */
1928 /* compute decimal_places */
1929 for (max_q = 1, decimal_places = 0;
1930 max_q < num_langs && decimal_places <= MAX_DECIMAL_PLACES;
1931 decimal_places++, max_q *= 10)
1934 xsnprintf(q_format, sizeof(q_format), ";q=0.%%0%dd", decimal_places);
1936 strbuf_addstr(buf, "Accept-Language: ");
1938 for (i = 0; i < num_langs; i++) {
1939 if (i > 0)
1940 strbuf_addstr(buf, ", ");
1942 strbuf_addstr(buf, language_tags[i]);
1944 if (i > 0)
1945 strbuf_addf(buf, q_format, max_q - i);
1947 if (buf->len > MAX_ACCEPT_LANGUAGE_HEADER_SIZE) {
1948 strbuf_remove(buf, last_buf_len, buf->len - last_buf_len);
1949 break;
1952 last_buf_len = buf->len;
1956 /* free language tags -- last one is a static '*' */
1957 for (i = 0; i < num_langs - 1; i++)
1958 free(language_tags[i]);
1959 free(language_tags);
1963 * Get an Accept-Language header which indicates user's preferred languages.
1965 * Examples:
1966 * LANGUAGE= -> ""
1967 * LANGUAGE=ko:en -> "Accept-Language: ko, en; q=0.9, *; q=0.1"
1968 * LANGUAGE=ko_KR.UTF-8:sr@latin -> "Accept-Language: ko-KR, sr; q=0.9, *; q=0.1"
1969 * LANGUAGE=ko LANG=en_US.UTF-8 -> "Accept-Language: ko, *; q=0.1"
1970 * LANGUAGE= LANG=en_US.UTF-8 -> "Accept-Language: en-US, *; q=0.1"
1971 * LANGUAGE= LANG=C -> ""
1973 const char *http_get_accept_language_header(void)
1975 if (!cached_accept_language) {
1976 struct strbuf buf = STRBUF_INIT;
1977 write_accept_language(&buf);
1978 if (buf.len > 0)
1979 cached_accept_language = strbuf_detach(&buf, NULL);
1982 return cached_accept_language;
1985 static void http_opt_request_remainder(CURL *curl, off_t pos)
1987 char buf[128];
1988 xsnprintf(buf, sizeof(buf), "%"PRIuMAX"-", (uintmax_t)pos);
1989 curl_easy_setopt(curl, CURLOPT_RANGE, buf);
1992 /* http_request() targets */
1993 #define HTTP_REQUEST_STRBUF 0
1994 #define HTTP_REQUEST_FILE 1
1996 static int http_request(const char *url,
1997 void *result, int target,
1998 const struct http_get_options *options)
2000 struct active_request_slot *slot;
2001 struct slot_results results;
2002 struct curl_slist *headers = http_copy_default_headers();
2003 struct strbuf buf = STRBUF_INIT;
2004 const char *accept_language;
2005 int ret;
2007 slot = get_active_slot();
2008 curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
2010 if (!result) {
2011 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 1);
2012 } else {
2013 curl_easy_setopt(slot->curl, CURLOPT_NOBODY, 0);
2014 curl_easy_setopt(slot->curl, CURLOPT_WRITEDATA, result);
2016 if (target == HTTP_REQUEST_FILE) {
2017 off_t posn = ftello(result);
2018 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
2019 fwrite);
2020 if (posn > 0)
2021 http_opt_request_remainder(slot->curl, posn);
2022 } else
2023 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION,
2024 fwrite_buffer);
2027 curl_easy_setopt(slot->curl, CURLOPT_HEADERFUNCTION, fwrite_wwwauth);
2029 accept_language = http_get_accept_language_header();
2031 if (accept_language)
2032 headers = curl_slist_append(headers, accept_language);
2034 strbuf_addstr(&buf, "Pragma:");
2035 if (options && options->no_cache)
2036 strbuf_addstr(&buf, " no-cache");
2037 if (options && options->initial_request &&
2038 http_follow_config == HTTP_FOLLOW_INITIAL)
2039 curl_easy_setopt(slot->curl, CURLOPT_FOLLOWLOCATION, 1);
2041 headers = curl_slist_append(headers, buf.buf);
2043 /* Add additional headers here */
2044 if (options && options->extra_headers) {
2045 const struct string_list_item *item;
2046 for_each_string_list_item(item, options->extra_headers) {
2047 headers = curl_slist_append(headers, item->string);
2051 curl_easy_setopt(slot->curl, CURLOPT_URL, url);
2052 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, headers);
2053 curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
2054 curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
2056 ret = run_one_slot(slot, &results);
2058 if (options && options->content_type) {
2059 struct strbuf raw = STRBUF_INIT;
2060 curlinfo_strbuf(slot->curl, CURLINFO_CONTENT_TYPE, &raw);
2061 extract_content_type(&raw, options->content_type,
2062 options->charset);
2063 strbuf_release(&raw);
2066 if (options && options->effective_url)
2067 curlinfo_strbuf(slot->curl, CURLINFO_EFFECTIVE_URL,
2068 options->effective_url);
2070 curl_slist_free_all(headers);
2071 strbuf_release(&buf);
2073 return ret;
2077 * Update the "base" url to a more appropriate value, as deduced by
2078 * redirects seen when requesting a URL starting with "url".
2080 * The "asked" parameter is a URL that we asked curl to access, and must begin
2081 * with "base".
2083 * The "got" parameter is the URL that curl reported to us as where we ended
2084 * up.
2086 * Returns 1 if we updated the base url, 0 otherwise.
2088 * Our basic strategy is to compare "base" and "asked" to find the bits
2089 * specific to our request. We then strip those bits off of "got" to yield the
2090 * new base. So for example, if our base is "http://example.com/foo.git",
2091 * and we ask for "http://example.com/foo.git/info/refs", we might end up
2092 * with "https://other.example.com/foo.git/info/refs". We would want the
2093 * new URL to become "https://other.example.com/foo.git".
2095 * Note that this assumes a sane redirect scheme. It's entirely possible
2096 * in the example above to end up at a URL that does not even end in
2097 * "info/refs". In such a case we die. There's not much we can do, such a
2098 * scheme is unlikely to represent a real git repository, and failing to
2099 * rewrite the base opens options for malicious redirects to do funny things.
2101 static int update_url_from_redirect(struct strbuf *base,
2102 const char *asked,
2103 const struct strbuf *got)
2105 const char *tail;
2106 size_t new_len;
2108 if (!strcmp(asked, got->buf))
2109 return 0;
2111 if (!skip_prefix(asked, base->buf, &tail))
2112 BUG("update_url_from_redirect: %s is not a superset of %s",
2113 asked, base->buf);
2115 new_len = got->len;
2116 if (!strip_suffix_mem(got->buf, &new_len, tail))
2117 die(_("unable to update url base from redirection:\n"
2118 " asked for: %s\n"
2119 " redirect: %s"),
2120 asked, got->buf);
2122 strbuf_reset(base);
2123 strbuf_add(base, got->buf, new_len);
2125 return 1;
2128 static int http_request_reauth(const char *url,
2129 void *result, int target,
2130 struct http_get_options *options)
2132 int ret = http_request(url, result, target, options);
2134 if (ret != HTTP_OK && ret != HTTP_REAUTH)
2135 return ret;
2137 if (options && options->effective_url && options->base_url) {
2138 if (update_url_from_redirect(options->base_url,
2139 url, options->effective_url)) {
2140 credential_from_url(&http_auth, options->base_url->buf);
2141 url = options->effective_url->buf;
2145 if (ret != HTTP_REAUTH)
2146 return ret;
2149 * The previous request may have put cruft into our output stream; we
2150 * should clear it out before making our next request.
2152 switch (target) {
2153 case HTTP_REQUEST_STRBUF:
2154 strbuf_reset(result);
2155 break;
2156 case HTTP_REQUEST_FILE:
2157 if (fflush(result)) {
2158 error_errno("unable to flush a file");
2159 return HTTP_START_FAILED;
2161 rewind(result);
2162 if (ftruncate(fileno(result), 0) < 0) {
2163 error_errno("unable to truncate a file");
2164 return HTTP_START_FAILED;
2166 break;
2167 default:
2168 BUG("Unknown http_request target");
2171 credential_fill(&http_auth);
2173 return http_request(url, result, target, options);
2176 int http_get_strbuf(const char *url,
2177 struct strbuf *result,
2178 struct http_get_options *options)
2180 return http_request_reauth(url, result, HTTP_REQUEST_STRBUF, options);
2184 * Downloads a URL and stores the result in the given file.
2186 * If a previous interrupted download is detected (i.e. a previous temporary
2187 * file is still around) the download is resumed.
2189 int http_get_file(const char *url, const char *filename,
2190 struct http_get_options *options)
2192 int ret;
2193 struct strbuf tmpfile = STRBUF_INIT;
2194 FILE *result;
2196 strbuf_addf(&tmpfile, "%s.temp", filename);
2197 result = fopen(tmpfile.buf, "a");
2198 if (!result) {
2199 error("Unable to open local file %s", tmpfile.buf);
2200 ret = HTTP_ERROR;
2201 goto cleanup;
2204 ret = http_request_reauth(url, result, HTTP_REQUEST_FILE, options);
2205 fclose(result);
2207 if (ret == HTTP_OK && finalize_object_file(tmpfile.buf, filename))
2208 ret = HTTP_ERROR;
2209 cleanup:
2210 strbuf_release(&tmpfile);
2211 return ret;
2214 int http_fetch_ref(const char *base, struct ref *ref)
2216 struct http_get_options options = {0};
2217 char *url;
2218 struct strbuf buffer = STRBUF_INIT;
2219 int ret = -1;
2221 options.no_cache = 1;
2223 url = quote_ref_url(base, ref->name);
2224 if (http_get_strbuf(url, &buffer, &options) == HTTP_OK) {
2225 strbuf_rtrim(&buffer);
2226 if (buffer.len == the_hash_algo->hexsz)
2227 ret = get_oid_hex(buffer.buf, &ref->old_oid);
2228 else if (starts_with(buffer.buf, "ref: ")) {
2229 ref->symref = xstrdup(buffer.buf + 5);
2230 ret = 0;
2234 strbuf_release(&buffer);
2235 free(url);
2236 return ret;
2239 /* Helpers for fetching packs */
2240 static char *fetch_pack_index(unsigned char *hash, const char *base_url)
2242 char *url, *tmp;
2243 struct strbuf buf = STRBUF_INIT;
2245 if (http_is_verbose)
2246 fprintf(stderr, "Getting index for pack %s\n", hash_to_hex(hash));
2248 end_url_with_slash(&buf, base_url);
2249 strbuf_addf(&buf, "objects/pack/pack-%s.idx", hash_to_hex(hash));
2250 url = strbuf_detach(&buf, NULL);
2252 strbuf_addf(&buf, "%s.temp", sha1_pack_index_name(hash));
2253 tmp = strbuf_detach(&buf, NULL);
2255 if (http_get_file(url, tmp, NULL) != HTTP_OK) {
2256 error("Unable to get pack index %s", url);
2257 FREE_AND_NULL(tmp);
2260 free(url);
2261 return tmp;
2264 static int fetch_and_setup_pack_index(struct packed_git **packs_head,
2265 unsigned char *sha1, const char *base_url)
2267 struct packed_git *new_pack;
2268 char *tmp_idx = NULL;
2269 int ret;
2271 if (has_pack_index(sha1)) {
2272 new_pack = parse_pack_index(sha1, sha1_pack_index_name(sha1));
2273 if (!new_pack)
2274 return -1; /* parse_pack_index() already issued error message */
2275 goto add_pack;
2278 tmp_idx = fetch_pack_index(sha1, base_url);
2279 if (!tmp_idx)
2280 return -1;
2282 new_pack = parse_pack_index(sha1, tmp_idx);
2283 if (!new_pack) {
2284 unlink(tmp_idx);
2285 free(tmp_idx);
2287 return -1; /* parse_pack_index() already issued error message */
2290 ret = verify_pack_index(new_pack);
2291 if (!ret) {
2292 close_pack_index(new_pack);
2293 ret = finalize_object_file(tmp_idx, sha1_pack_index_name(sha1));
2295 free(tmp_idx);
2296 if (ret)
2297 return -1;
2299 add_pack:
2300 new_pack->next = *packs_head;
2301 *packs_head = new_pack;
2302 return 0;
2305 int http_get_info_packs(const char *base_url, struct packed_git **packs_head)
2307 struct http_get_options options = {0};
2308 int ret = 0;
2309 char *url;
2310 const char *data;
2311 struct strbuf buf = STRBUF_INIT;
2312 struct object_id oid;
2314 end_url_with_slash(&buf, base_url);
2315 strbuf_addstr(&buf, "objects/info/packs");
2316 url = strbuf_detach(&buf, NULL);
2318 options.no_cache = 1;
2319 ret = http_get_strbuf(url, &buf, &options);
2320 if (ret != HTTP_OK)
2321 goto cleanup;
2323 data = buf.buf;
2324 while (*data) {
2325 if (skip_prefix(data, "P pack-", &data) &&
2326 !parse_oid_hex(data, &oid, &data) &&
2327 skip_prefix(data, ".pack", &data) &&
2328 (*data == '\n' || *data == '\0')) {
2329 fetch_and_setup_pack_index(packs_head, oid.hash, base_url);
2330 } else {
2331 data = strchrnul(data, '\n');
2333 if (*data)
2334 data++; /* skip past newline */
2337 cleanup:
2338 free(url);
2339 return ret;
2342 void release_http_pack_request(struct http_pack_request *preq)
2344 if (preq->packfile) {
2345 fclose(preq->packfile);
2346 preq->packfile = NULL;
2348 preq->slot = NULL;
2349 strbuf_release(&preq->tmpfile);
2350 free(preq->url);
2351 free(preq);
2354 static const char *default_index_pack_args[] =
2355 {"index-pack", "--stdin", NULL};
2357 int finish_http_pack_request(struct http_pack_request *preq)
2359 struct child_process ip = CHILD_PROCESS_INIT;
2360 int tmpfile_fd;
2361 int ret = 0;
2363 fclose(preq->packfile);
2364 preq->packfile = NULL;
2366 tmpfile_fd = xopen(preq->tmpfile.buf, O_RDONLY);
2368 ip.git_cmd = 1;
2369 ip.in = tmpfile_fd;
2370 strvec_pushv(&ip.args, preq->index_pack_args ?
2371 preq->index_pack_args :
2372 default_index_pack_args);
2374 if (preq->preserve_index_pack_stdout)
2375 ip.out = 0;
2376 else
2377 ip.no_stdout = 1;
2379 if (run_command(&ip)) {
2380 ret = -1;
2381 goto cleanup;
2384 cleanup:
2385 close(tmpfile_fd);
2386 unlink(preq->tmpfile.buf);
2387 return ret;
2390 void http_install_packfile(struct packed_git *p,
2391 struct packed_git **list_to_remove_from)
2393 struct packed_git **lst = list_to_remove_from;
2395 while (*lst != p)
2396 lst = &((*lst)->next);
2397 *lst = (*lst)->next;
2399 install_packed_git(the_repository, p);
2402 struct http_pack_request *new_http_pack_request(
2403 const unsigned char *packed_git_hash, const char *base_url) {
2405 struct strbuf buf = STRBUF_INIT;
2407 end_url_with_slash(&buf, base_url);
2408 strbuf_addf(&buf, "objects/pack/pack-%s.pack",
2409 hash_to_hex(packed_git_hash));
2410 return new_direct_http_pack_request(packed_git_hash,
2411 strbuf_detach(&buf, NULL));
2414 struct http_pack_request *new_direct_http_pack_request(
2415 const unsigned char *packed_git_hash, char *url)
2417 off_t prev_posn = 0;
2418 struct http_pack_request *preq;
2420 CALLOC_ARRAY(preq, 1);
2421 strbuf_init(&preq->tmpfile, 0);
2423 preq->url = url;
2425 strbuf_addf(&preq->tmpfile, "%s.temp", sha1_pack_name(packed_git_hash));
2426 preq->packfile = fopen(preq->tmpfile.buf, "a");
2427 if (!preq->packfile) {
2428 error("Unable to open local file %s for pack",
2429 preq->tmpfile.buf);
2430 goto abort;
2433 preq->slot = get_active_slot();
2434 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEDATA, preq->packfile);
2435 curl_easy_setopt(preq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite);
2436 curl_easy_setopt(preq->slot->curl, CURLOPT_URL, preq->url);
2437 curl_easy_setopt(preq->slot->curl, CURLOPT_HTTPHEADER,
2438 no_pragma_header);
2441 * If there is data present from a previous transfer attempt,
2442 * resume where it left off
2444 prev_posn = ftello(preq->packfile);
2445 if (prev_posn>0) {
2446 if (http_is_verbose)
2447 fprintf(stderr,
2448 "Resuming fetch of pack %s at byte %"PRIuMAX"\n",
2449 hash_to_hex(packed_git_hash),
2450 (uintmax_t)prev_posn);
2451 http_opt_request_remainder(preq->slot->curl, prev_posn);
2454 return preq;
2456 abort:
2457 strbuf_release(&preq->tmpfile);
2458 free(preq->url);
2459 free(preq);
2460 return NULL;
2463 /* Helpers for fetching objects (loose) */
2464 static size_t fwrite_sha1_file(char *ptr, size_t eltsize, size_t nmemb,
2465 void *data)
2467 unsigned char expn[4096];
2468 size_t size = eltsize * nmemb;
2469 int posn = 0;
2470 struct http_object_request *freq = data;
2471 struct active_request_slot *slot = freq->slot;
2473 if (slot) {
2474 CURLcode c = curl_easy_getinfo(slot->curl, CURLINFO_HTTP_CODE,
2475 &slot->http_code);
2476 if (c != CURLE_OK)
2477 BUG("curl_easy_getinfo for HTTP code failed: %s",
2478 curl_easy_strerror(c));
2479 if (slot->http_code >= 300)
2480 return nmemb;
2483 do {
2484 ssize_t retval = xwrite(freq->localfile,
2485 (char *) ptr + posn, size - posn);
2486 if (retval < 0)
2487 return posn / eltsize;
2488 posn += retval;
2489 } while (posn < size);
2491 freq->stream.avail_in = size;
2492 freq->stream.next_in = (void *)ptr;
2493 do {
2494 freq->stream.next_out = expn;
2495 freq->stream.avail_out = sizeof(expn);
2496 freq->zret = git_inflate(&freq->stream, Z_SYNC_FLUSH);
2497 the_hash_algo->update_fn(&freq->c, expn,
2498 sizeof(expn) - freq->stream.avail_out);
2499 } while (freq->stream.avail_in && freq->zret == Z_OK);
2500 return nmemb;
2503 struct http_object_request *new_http_object_request(const char *base_url,
2504 const struct object_id *oid)
2506 char *hex = oid_to_hex(oid);
2507 struct strbuf filename = STRBUF_INIT;
2508 struct strbuf prevfile = STRBUF_INIT;
2509 int prevlocal;
2510 char prev_buf[PREV_BUF_SIZE];
2511 ssize_t prev_read = 0;
2512 off_t prev_posn = 0;
2513 struct http_object_request *freq;
2515 CALLOC_ARRAY(freq, 1);
2516 strbuf_init(&freq->tmpfile, 0);
2517 oidcpy(&freq->oid, oid);
2518 freq->localfile = -1;
2520 loose_object_path(the_repository, &filename, oid);
2521 strbuf_addf(&freq->tmpfile, "%s.temp", filename.buf);
2523 strbuf_addf(&prevfile, "%s.prev", filename.buf);
2524 unlink_or_warn(prevfile.buf);
2525 rename(freq->tmpfile.buf, prevfile.buf);
2526 unlink_or_warn(freq->tmpfile.buf);
2527 strbuf_release(&filename);
2529 if (freq->localfile != -1)
2530 error("fd leakage in start: %d", freq->localfile);
2531 freq->localfile = open(freq->tmpfile.buf,
2532 O_WRONLY | O_CREAT | O_EXCL, 0666);
2534 * This could have failed due to the "lazy directory creation";
2535 * try to mkdir the last path component.
2537 if (freq->localfile < 0 && errno == ENOENT) {
2538 char *dir = strrchr(freq->tmpfile.buf, '/');
2539 if (dir) {
2540 *dir = 0;
2541 mkdir(freq->tmpfile.buf, 0777);
2542 *dir = '/';
2544 freq->localfile = open(freq->tmpfile.buf,
2545 O_WRONLY | O_CREAT | O_EXCL, 0666);
2548 if (freq->localfile < 0) {
2549 error_errno("Couldn't create temporary file %s",
2550 freq->tmpfile.buf);
2551 goto abort;
2554 git_inflate_init(&freq->stream);
2556 the_hash_algo->init_fn(&freq->c);
2558 freq->url = get_remote_object_url(base_url, hex, 0);
2561 * If a previous temp file is present, process what was already
2562 * fetched.
2564 prevlocal = open(prevfile.buf, O_RDONLY);
2565 if (prevlocal != -1) {
2566 do {
2567 prev_read = xread(prevlocal, prev_buf, PREV_BUF_SIZE);
2568 if (prev_read>0) {
2569 if (fwrite_sha1_file(prev_buf,
2571 prev_read,
2572 freq) == prev_read) {
2573 prev_posn += prev_read;
2574 } else {
2575 prev_read = -1;
2578 } while (prev_read > 0);
2579 close(prevlocal);
2581 unlink_or_warn(prevfile.buf);
2582 strbuf_release(&prevfile);
2585 * Reset inflate/SHA1 if there was an error reading the previous temp
2586 * file; also rewind to the beginning of the local file.
2588 if (prev_read == -1) {
2589 memset(&freq->stream, 0, sizeof(freq->stream));
2590 git_inflate_init(&freq->stream);
2591 the_hash_algo->init_fn(&freq->c);
2592 if (prev_posn>0) {
2593 prev_posn = 0;
2594 lseek(freq->localfile, 0, SEEK_SET);
2595 if (ftruncate(freq->localfile, 0) < 0) {
2596 error_errno("Couldn't truncate temporary file %s",
2597 freq->tmpfile.buf);
2598 goto abort;
2603 freq->slot = get_active_slot();
2605 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEDATA, freq);
2606 curl_easy_setopt(freq->slot->curl, CURLOPT_FAILONERROR, 0);
2607 curl_easy_setopt(freq->slot->curl, CURLOPT_WRITEFUNCTION, fwrite_sha1_file);
2608 curl_easy_setopt(freq->slot->curl, CURLOPT_ERRORBUFFER, freq->errorstr);
2609 curl_easy_setopt(freq->slot->curl, CURLOPT_URL, freq->url);
2610 curl_easy_setopt(freq->slot->curl, CURLOPT_HTTPHEADER, no_pragma_header);
2613 * If we have successfully processed data from a previous fetch
2614 * attempt, only fetch the data we don't already have.
2616 if (prev_posn>0) {
2617 if (http_is_verbose)
2618 fprintf(stderr,
2619 "Resuming fetch of object %s at byte %"PRIuMAX"\n",
2620 hex, (uintmax_t)prev_posn);
2621 http_opt_request_remainder(freq->slot->curl, prev_posn);
2624 return freq;
2626 abort:
2627 strbuf_release(&prevfile);
2628 free(freq->url);
2629 free(freq);
2630 return NULL;
2633 void process_http_object_request(struct http_object_request *freq)
2635 if (!freq->slot)
2636 return;
2637 freq->curl_result = freq->slot->curl_result;
2638 freq->http_code = freq->slot->http_code;
2639 freq->slot = NULL;
2642 int finish_http_object_request(struct http_object_request *freq)
2644 struct stat st;
2645 struct strbuf filename = STRBUF_INIT;
2647 close(freq->localfile);
2648 freq->localfile = -1;
2650 process_http_object_request(freq);
2652 if (freq->http_code == 416) {
2653 warning("requested range invalid; we may already have all the data.");
2654 } else if (freq->curl_result != CURLE_OK) {
2655 if (stat(freq->tmpfile.buf, &st) == 0)
2656 if (st.st_size == 0)
2657 unlink_or_warn(freq->tmpfile.buf);
2658 return -1;
2661 git_inflate_end(&freq->stream);
2662 the_hash_algo->final_oid_fn(&freq->real_oid, &freq->c);
2663 if (freq->zret != Z_STREAM_END) {
2664 unlink_or_warn(freq->tmpfile.buf);
2665 return -1;
2667 if (!oideq(&freq->oid, &freq->real_oid)) {
2668 unlink_or_warn(freq->tmpfile.buf);
2669 return -1;
2671 loose_object_path(the_repository, &filename, &freq->oid);
2672 freq->rename = finalize_object_file(freq->tmpfile.buf, filename.buf);
2673 strbuf_release(&filename);
2675 return freq->rename;
2678 void abort_http_object_request(struct http_object_request *freq)
2680 unlink_or_warn(freq->tmpfile.buf);
2682 release_http_object_request(freq);
2685 void release_http_object_request(struct http_object_request *freq)
2687 if (freq->localfile != -1) {
2688 close(freq->localfile);
2689 freq->localfile = -1;
2691 FREE_AND_NULL(freq->url);
2692 if (freq->slot) {
2693 freq->slot->callback_func = NULL;
2694 freq->slot->callback_data = NULL;
2695 release_active_slot(freq->slot);
2696 freq->slot = NULL;
2698 strbuf_release(&freq->tmpfile);