http: add custom hostname to IP address resolutions

Libcurl has a CURLOPT_RESOLVE easy option that allows the result of hostname resolution in the following format to be passed: [+]HOST:PORT:ADDRESS[,ADDRESS] This way, redirects and everything operating against the HOST+PORT will use the provided ADDRESS(s). The following format is also allowed to stop using hostname resolutions that have already been passed: -HOST:PORT See https://curl.se/libcurl/c/CURLOPT_RESOLVE.html for more details. Let's add a corresponding "http.curloptResolve" config option that takes advantage of CURLOPT_RESOLVE. Each value configured for the "http.curloptResolve" key is passed "as is" to libcurl through CURLOPT_RESOLVE, so it should be in one of the above 2 formats. This keeps the implementation simple and makes us consistent with libcurl's CURLOPT_RESOLVE, and with curl's corresponding `--resolve` command line option. The implementation uses CURLOPT_RESOLVE only in get_active_slot() which is called by all the HTTP request sending functions. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed May 16, 2022 at 10:38 UTC 511cfd3bffa685fda0e7c25bfa08082aa0de3a30
3 files changed +41
Documentation/config/http.txt
+16
@@ -98,6 +98,22 @@ http.version::
98 - HTTP/2
99 - HTTP/1.1
100
101 +http.curloptResolve::
102 + Hostname resolution information that will be used first by
103 + libcurl when sending HTTP requests. This information should
104 + be in one of the following formats:
105 +
106 + - [+]HOST:PORT:ADDRESS[,ADDRESS]
107 + - -HOST:PORT
108 +
109 ++
110 +The first format redirects all requests to the given `HOST:PORT`
111 +to the provided `ADDRESS`(s). The second format clears all
112 +previous config values for that `HOST:PORT` combination. To
113 +allow easy overriding of all the settings inherited from the
114 +system config, an empty value will reset all resolution
115 +information to the empty list.
116 +
117 http.sslVersion::
118 The SSL version to use when negotiating an SSL connection, if you
119 want to force the default. The available and default version
http.c
+18
@@ -128,6 +128,8 @@ static struct curl_slist *pragma_header;
128 static struct curl_slist *no_pragma_header;
129 static struct string_list extra_http_headers = STRING_LIST_INIT_DUP;
130
131 +static struct curl_slist *host_resolutions;
132 +
133 static struct active_request_slot *active_queue_head;
134
135 static char *cached_accept_language;
@@ -393,6 +395,18 @@ static int http_options(const char *var, const char *value, void *cb)
395 return 0;
396 }
397
398 + if (!strcmp("http.curloptresolve", var)) {
399 + if (!value) {
400 + return config_error_nonbool(var);
401 + } else if (!*value) {
402 + curl_slist_free_all(host_resolutions);
403 + host_resolutions = NULL;
404 + } else {
405 + host_resolutions = curl_slist_append(host_resolutions, value);
406 + }
407 + return 0;
408 + }
409 +
410 if (!strcmp("http.followredirects", var)) {
411 if (value && !strcmp(value, "initial"))
412 http_follow_config = HTTP_FOLLOW_INITIAL;
@@ -1131,6 +1145,9 @@ void http_cleanup(void)
1145 curl_slist_free_all(no_pragma_header);
1146 no_pragma_header = NULL;
1147
1148 + curl_slist_free_all(host_resolutions);
1149 + host_resolutions = NULL;
1150 +
1151 if (curl_http_proxy) {
1152 free((void *)curl_http_proxy);
1153 curl_http_proxy = NULL;
@@ -1211,6 +1228,7 @@ struct active_request_slot *get_active_slot(void)
1228 if (curl_save_cookies)
1229 curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
1230 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
1231 + curl_easy_setopt(slot->curl, CURLOPT_RESOLVE, host_resolutions);
1232 curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
1233 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
1234 curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);
t/t5551-http-fetch-smart.sh
+7
@@ -567,4 +567,11 @@ test_expect_success 'client falls back from v2 to v0 to match server' '
567 grep symref=HEAD:refs/heads/ trace
568 '
569
570 +test_expect_success 'passing hostname resolution information works' '
571 + BOGUS_HOST=gitbogusexamplehost.invalid &&
572 + BOGUS_HTTPD_URL=$HTTPD_PROTO://$BOGUS_HOST:$LIB_HTTPD_PORT &&
573 + test_must_fail git ls-remote "$BOGUS_HTTPD_URL/smart/repo.git" >/dev/null &&
574 + git -c "http.curloptResolve=$BOGUS_HOST:$LIB_HTTPD_PORT:127.0.0.1" ls-remote "$BOGUS_HTTPD_URL/smart/repo.git" >/dev/null
575 +'
576 +
577 test_done