Skip to content

Commit 511cfd3

Browse files
chriscoolgitster
authored andcommitted
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>
1 parent 6cd33dc commit 511cfd3

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

Documentation/config/http.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ http.version::
9898
- HTTP/2
9999
- HTTP/1.1
100100

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+
101117
http.sslVersion::
102118
The SSL version to use when negotiating an SSL connection, if you
103119
want to force the default. The available and default version

http.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ static struct curl_slist *pragma_header;
128128
static struct curl_slist *no_pragma_header;
129129
static struct string_list extra_http_headers = STRING_LIST_INIT_DUP;
130130

131+
static struct curl_slist *host_resolutions;
132+
131133
static struct active_request_slot *active_queue_head;
132134

133135
static char *cached_accept_language;
@@ -393,6 +395,18 @@ static int http_options(const char *var, const char *value, void *cb)
393395
return 0;
394396
}
395397

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+
396410
if (!strcmp("http.followredirects", var)) {
397411
if (value && !strcmp(value, "initial"))
398412
http_follow_config = HTTP_FOLLOW_INITIAL;
@@ -1131,6 +1145,9 @@ void http_cleanup(void)
11311145
curl_slist_free_all(no_pragma_header);
11321146
no_pragma_header = NULL;
11331147

1148+
curl_slist_free_all(host_resolutions);
1149+
host_resolutions = NULL;
1150+
11341151
if (curl_http_proxy) {
11351152
free((void *)curl_http_proxy);
11361153
curl_http_proxy = NULL;
@@ -1211,6 +1228,7 @@ struct active_request_slot *get_active_slot(void)
12111228
if (curl_save_cookies)
12121229
curl_easy_setopt(slot->curl, CURLOPT_COOKIEJAR, curl_cookie_file);
12131230
curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, pragma_header);
1231+
curl_easy_setopt(slot->curl, CURLOPT_RESOLVE, host_resolutions);
12141232
curl_easy_setopt(slot->curl, CURLOPT_ERRORBUFFER, curl_errorstr);
12151233
curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, NULL);
12161234
curl_easy_setopt(slot->curl, CURLOPT_READFUNCTION, NULL);

t/t5551-http-fetch-smart.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,4 +567,11 @@ test_expect_success 'client falls back from v2 to v0 to match server' '
567567
grep symref=HEAD:refs/heads/ trace
568568
'
569569

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+
570577
test_done

0 commit comments

Comments
 (0)