From 98cc1ec344da3250e7dc8135853d0a5bef52d788 Mon Sep 17 00:00:00 2001 From: Sean Quinn Date: Sun, 26 May 2024 05:57:07 -0700 Subject: [PATCH] Allow hex for ip6 literal addr, fix #1800 (#1830) * Allow hex for ip6 literal addr, fix #1800 * Add UT for ipv6 + Universal client implementation * add /n at EOF --- httplib.h | 4 +++- test/test.cc | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/httplib.h b/httplib.h index ba6e66695..a7a561333 100644 --- a/httplib.h +++ b/httplib.h @@ -9213,7 +9213,7 @@ inline Client::Client(const std::string &scheme_host_port, const std::string &client_cert_path, const std::string &client_key_path) { const static std::regex re( - R"((?:([a-z]+):\/\/)?(?:\[([\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); + R"((?:([a-z]+):\/\/)?(?:\[([a-fA-F\d:]+)\]|([^:/?#]+))(?::(\d+))?)"); std::smatch m; if (std::regex_match(scheme_host_port, m, re)) { @@ -9250,6 +9250,8 @@ inline Client::Client(const std::string &scheme_host_port, client_key_path); } } else { + // NOTE: Update TEST(UniversalClientImplTest, Ipv6LiteralAddress) + // if port param below changes. cli_ = detail::make_unique(scheme_host_port, 80, client_cert_path, client_key_path); } diff --git a/test/test.cc b/test/test.cc index dce82abe4..aab9db057 100644 --- a/test/test.cc +++ b/test/test.cc @@ -7373,3 +7373,18 @@ TEST(PathParamsTest, SequenceOfParams) { EXPECT_EQ(request.path_params, expected_params); } + +TEST(UniversalClientImplTest, Ipv6LiteralAddress) { + // If ipv6 regex working, regex match codepath is taken. + // else port will default to 80 in Client impl + int clientImplMagicPort = 80; + int port = 4321; + // above ports must be different to avoid false negative + EXPECT_NE(clientImplMagicPort, port); + + std::string ipV6TestURL = "http://[ff06::c3]"; + + Client cli(ipV6TestURL + ":" + std::to_string(port), CLIENT_CERT_FILE, + CLIENT_PRIVATE_KEY_FILE); + EXPECT_EQ(cli.port(), port); +}