Skip to content

Commit

Permalink
http: add support selecting http version
Browse files Browse the repository at this point in the history
Usually we don't need to set libcurl to choose which version of the
HTTP protocol to use to communicate with a server.
But different versions of libcurl, the default value is not the same.

CURL >= 7.62.0: CURL_HTTP_VERSION_2TLS
CURL < 7.62: CURL_HTTP_VERSION_1_1

In order to give users the freedom to control the HTTP version,
we need to add a setting to choose which HTTP version to use.

Signed-off-by: Force Charlie <charlieio@outlook.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
fcharlie authored and dscho committed Dec 15, 2018
1 parent 34e7f82 commit 5fae612
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Documentation/config/http.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ http.saveCookies::
If set, store cookies received during requests to the file specified by
http.cookieFile. Has no effect if http.cookieFile is unset.

http.version::
Use the specified HTTP protocol version when communicating with a server.
If you want to force the default. The available and default version depend
on libcurl. Actually the possible values of
this option are:

- HTTP/2
- HTTP/1.1

http.sslVersion::
The SSL version to use when negotiating an SSL connection, if you
want to force the default. The available and default version
Expand Down
39 changes: 39 additions & 0 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ char curl_errorstr[CURL_ERROR_SIZE];

static int curl_ssl_verify = -1;
static int curl_ssl_try;
static const char *curl_http_version = NULL;
static const char *ssl_cert;
static const char *ssl_cipherlist;
static const char *ssl_version;
Expand Down Expand Up @@ -284,6 +285,9 @@ static void process_curl_messages(void)

static int http_options(const char *var, const char *value, void *cb)
{
if (!strcmp("http.version", var)) {
return git_config_string(&curl_http_version, var, value);
}
if (!strcmp("http.sslverify", var)) {
curl_ssl_verify = git_config_bool(var, value);
return 0;
Expand Down Expand Up @@ -789,6 +793,31 @@ static long get_curl_allowed_protocols(int from_user)
}
#endif

#if LIBCURL_VERSION_NUM >=0x072f00
static int get_curl_http_version_opt(const char *version_string, long *opt)
{
int i;
static struct {
const char *name;
long opt_token;
} choice[] = {
{ "HTTP/1.1", CURL_HTTP_VERSION_1_1 },
{ "HTTP/2", CURL_HTTP_VERSION_2 }
};

for (i = 0; i < ARRAY_SIZE(choice); i++) {
if (!strcmp(version_string, choice[i].name)) {
*opt = choice[i].opt_token;
return 0;
}
}

warning("unknown value given to http.version: '%s'", version_string);
return -1; /* not found */
}

#endif

static CURL *get_curl_handle(void)
{
CURL *result = curl_easy_init();
Expand All @@ -806,6 +835,16 @@ static CURL *get_curl_handle(void)
curl_easy_setopt(result, CURLOPT_SSL_VERIFYHOST, 2);
}

#if LIBCURL_VERSION_NUM >= 0x072f00 // 7.47.0
if (curl_http_version) {
long opt;
if (!get_curl_http_version_opt(curl_http_version, &opt)) {
/* Set request use http version */
curl_easy_setopt(result, CURLOPT_HTTP_VERSION, opt);
}
}
#endif

#if LIBCURL_VERSION_NUM >= 0x070907
curl_easy_setopt(result, CURLOPT_NETRC, CURL_NETRC_OPTIONAL);
#endif
Expand Down

0 comments on commit 5fae612

Please sign in to comment.