From 726e4583efc4e2a2f6a098fec4f6e88241011791 Mon Sep 17 00:00:00 2001 From: yhirose Date: Fri, 20 Nov 2026 17:47:57 -0500 Subject: [PATCH] Accept an uppercase URL scheme parse_url rejected any scheme containing an uppercase letter, so Client("HTTPS://..."), WebSocketClient("WSS://...") and a redirect to "Location: HTTPS://..." failed, although the scheme is case-insensitive (RFC 3986 3.1). Lowercase the scheme in parse_url so that every caller compares one canonical form and selects TLS for it. --- httplib.h | 3 ++- test/test.cc | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 99d10ec8..845366e3 100644 --- a/httplib.h +++ b/httplib.h @@ -914,7 +914,8 @@ inline bool parse_url(const std::string &url, UrlComponents &uc) { auto sep = url.find("://"); if (sep != std::string::npos) { - uc.scheme = url.substr(0, sep); + // Schemes are case-insensitive (RFC 3986 3.1) + uc.scheme = case_ignore::to_lower(url.substr(0, sep)); // Scheme must be [a-z]+ only if (uc.scheme.empty()) { return false; } diff --git a/test/test.cc b/test/test.cc index 4dc86b21..64f37dee 100644 --- a/test/test.cc +++ b/test/test.cc @@ -18838,6 +18838,49 @@ TEST(HttpToHttpsRedirectTest, CertFile) { ASSERT_EQ(StatusCode::OK_200, res->status); } +TEST(UppercaseSchemeTest, UsesTLS) { + SSLServer ssl_svr(SERVER_CERT2_FILE, SERVER_PRIVATE_KEY_FILE); + ASSERT_TRUE(ssl_svr.is_valid()); + ssl_svr.Get("/index", [](const Request &, Response &res) { + res.set_content("test", "text/plain"); + }); + auto ssl_port = ssl_svr.bind_to_any_port("127.0.0.1"); + auto https_url = "HTTPS://127.0.0.1:" + std::to_string(ssl_port); + + Server svr; + svr.Get("/index", [&](const Request &, Response &res) { + res.set_redirect(https_url + "/index"); + }); + auto port = svr.bind_to_any_port("127.0.0.1"); + + auto t = thread([&] { ssl_svr.listen_after_bind(); }); + auto t2 = thread([&] { svr.listen_after_bind(); }); + auto se = detail::scope_exit([&] { + svr.stop(); + ssl_svr.stop(); + t2.join(); + t.join(); + }); + ssl_svr.wait_until_ready(); + svr.wait_until_ready(); + + { + Client cli(https_url); + cli.set_ca_cert_path(SERVER_CERT2_FILE); + auto res = cli.Get("/index"); + ASSERT_TRUE(res) << "Error: " << to_string(res.error()); + EXPECT_EQ("test", res->body); + } + { + Client cli("127.0.0.1", port); + cli.set_ca_cert_path(SERVER_CERT2_FILE); + cli.set_follow_location(true); + auto res = cli.Get("/index"); + ASSERT_TRUE(res) << "Error: " << to_string(res.error()); + EXPECT_EQ("test", res->body); + } +} + TEST(SSLClientRedirectTest, CertFile) { auto ssl_port = PORT + 1; @@ -22143,7 +22186,8 @@ TEST(ParseUrlTest, VariousPatterns) { } { detail::UrlComponents uc; - ASSERT_FALSE(detail::parse_url("HTTP://example.com/path", uc)); + ASSERT_TRUE(detail::parse_url("HTTP://example.com/path", uc)); + EXPECT_EQ("http", uc.scheme); } { detail::UrlComponents uc; @@ -22219,6 +22263,11 @@ TEST(ParseUrlTest, SchemeEdgeCases) { detail::UrlComponents uc; ASSERT_FALSE(detail::parse_url("h.t://evil.com/path", uc)); } + { + detail::UrlComponents uc; + ASSERT_TRUE(detail::parse_url("httpS://example.com/path", uc)); + EXPECT_EQ("https", uc.scheme); + } } TEST(ParseUrlTest, PortEdgeCases) { @@ -31237,6 +31286,13 @@ TEST_F(WebSocketSSLIntegrationTest, TextEcho) { client.close(); } +TEST_F(WebSocketSSLIntegrationTest, UppercaseScheme) { + ws::WebSocketClient client("WSS://localhost:" + std::to_string(port_) + + "/ws-echo"); + client.enable_server_certificate_verification(false); + EXPECT_TRUE(client.connect()); +} + // Regression test (GHSA-w7p7-f35j-mw7q): shutdown_and_close() used to free the // TLS session before ws_->close() sent the close frame. Because the WebSocket's // SSLSocketStream keeps a raw pointer to that session, sending the close frame