mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-21 20:01:46 +00:00
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.
This commit is contained in:
1 parent
a6ab971a3f
commit
726e4583ef
2 files changed
+59
-2
No files matched your search
@@ -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; }
|
||||
|
||||
+57
-1
@@ -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
|
||||
|
||||
Reference in new issue
Block a user