Skip to content

Commit de848e3

Browse files
peffdscho
authored andcommitted
http: support CURLOPT_PROTOCOLS_STR
The CURLOPT_PROTOCOLS (and matching CURLOPT_REDIR_PROTOCOLS) flag was deprecated in curl 7.85.0, and using it generate compiler warnings as of curl 7.87.0. The path forward is to use CURLOPT_PROTOCOLS_STR, but we can't just do so unilaterally, as it was only introduced less than a year ago in 7.85.0. Until that version becomes ubiquitous, we have to either disable the deprecation warning or conditionally use the "STR" variant on newer versions of libcurl. This patch switches to the new variant, which is nice for two reasons: - we don't have to worry that silencing curl's deprecation warnings might cause us to miss other more useful ones - we'd eventually want to move to the new variant anyway, so this gets us set up (albeit with some extra ugly boilerplate for the conditional) There are a lot of ways to split up the two cases. One way would be to abstract the storage type (strbuf versus a long), how to append (strbuf_addstr vs bitwise OR), how to initialize, which CURLOPT to use, and so on. But the resulting code looks pretty magical: GIT_CURL_PROTOCOL_TYPE allowed = GIT_CURL_PROTOCOL_TYPE_INIT; if (...http is allowed...) GIT_CURL_PROTOCOL_APPEND(&allowed, "http", CURLOPT_HTTP); and you end up with more "#define GIT_CURL_PROTOCOL_TYPE" macros than actual code. On the other end of the spectrum, we could just implement two separate functions, one that handles a string list and one that handles bits. But then we end up repeating our list of protocols (http, https, ftp, ftp). This patch takes the middle ground. The run-time code is always there to handle both types, and we just choose which one to feed to curl. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]>
1 parent ee08d7b commit de848e3

File tree

2 files changed

+54
-13
lines changed

2 files changed

+54
-13
lines changed

git-curl-compat.h

+8
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,14 @@
127127
#endif
128128

129129
/**
130+
* CURLOPT_PROTOCOLS_STR and CURLOPT_REDIR_PROTOCOLS_STR were added in 7.85.0,
131+
* released in August 2022.
132+
*/
133+
#if LIBCURL_VERSION_NUM >= 0x075500
134+
#define GIT_CURL_HAVE_CURLOPT_PROTOCOLS_STR 1
135+
#endif
136+
137+
/*
130138
* CURLSSLOPT_AUTO_CLIENT_CERT was added in 7.77.0, released in May
131139
* 2021.
132140
*/

http.c

+46-13
Original file line numberDiff line numberDiff line change
@@ -789,20 +789,37 @@ void setup_curl_trace(CURL *handle)
789789
curl_easy_setopt(handle, CURLOPT_DEBUGDATA, NULL);
790790
}
791791

792-
static long get_curl_allowed_protocols(int from_user)
792+
static void proto_list_append(struct strbuf *list, const char *proto)
793793
{
794-
long allowed_protocols = 0;
794+
if (!list)
795+
return;
796+
if (list->len)
797+
strbuf_addch(list, ',');
798+
strbuf_addstr(list, proto);
799+
}
795800

796-
if (is_transport_allowed("http", from_user))
797-
allowed_protocols |= CURLPROTO_HTTP;
798-
if (is_transport_allowed("https", from_user))
799-
allowed_protocols |= CURLPROTO_HTTPS;
800-
if (is_transport_allowed("ftp", from_user))
801-
allowed_protocols |= CURLPROTO_FTP;
802-
if (is_transport_allowed("ftps", from_user))
803-
allowed_protocols |= CURLPROTO_FTPS;
801+
static long get_curl_allowed_protocols(int from_user, struct strbuf *list)
802+
{
803+
long bits = 0;
804804

805-
return allowed_protocols;
805+
if (is_transport_allowed("http", from_user)) {
806+
bits |= CURLPROTO_HTTP;
807+
proto_list_append(list, "http");
808+
}
809+
if (is_transport_allowed("https", from_user)) {
810+
bits |= CURLPROTO_HTTPS;
811+
proto_list_append(list, "https");
812+
}
813+
if (is_transport_allowed("ftp", from_user)) {
814+
bits |= CURLPROTO_FTP;
815+
proto_list_append(list, "ftp");
816+
}
817+
if (is_transport_allowed("ftps", from_user)) {
818+
bits |= CURLPROTO_FTPS;
819+
proto_list_append(list, "ftps");
820+
}
821+
822+
return bits;
806823
}
807824

808825
#ifdef GIT_CURL_HAVE_CURL_HTTP_VERSION_2
@@ -957,10 +974,26 @@ static CURL *get_curl_handle(void)
957974

958975
curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
959976
curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
977+
978+
#ifdef GIT_CURL_HAVE_CURLOPT_PROTOCOLS_STR
979+
{
980+
struct strbuf buf = STRBUF_INIT;
981+
982+
get_curl_allowed_protocols(0, &buf);
983+
curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS_STR, buf.buf);
984+
strbuf_reset(&buf);
985+
986+
get_curl_allowed_protocols(-1, &buf);
987+
curl_easy_setopt(result, CURLOPT_PROTOCOLS_STR, buf.buf);
988+
strbuf_release(&buf);
989+
}
990+
#else
960991
curl_easy_setopt(result, CURLOPT_REDIR_PROTOCOLS,
961-
get_curl_allowed_protocols(0));
992+
get_curl_allowed_protocols(0, NULL));
962993
curl_easy_setopt(result, CURLOPT_PROTOCOLS,
963-
get_curl_allowed_protocols(-1));
994+
get_curl_allowed_protocols(-1, NULL));
995+
#endif
996+
964997
if (getenv("GIT_CURL_VERBOSE"))
965998
http_trace_curl_no_data();
966999
setup_curl_trace(result);

0 commit comments

Comments
 (0)