mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-12-05 22:18:27 +00:00
Compare the redirect host case-insensitively (#2708)
* Compare the redirect host case-insensitively * Test a Unix socket redirect to LOCALHOST --------- Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
This commit is contained in:
1 parent
c438858d92
commit
5fd66e46df
2 files changed
+55
-4
No files matched your search
@@ -17787,10 +17787,12 @@ inline bool ClientImpl::redirect(Request &req, Response &res, Error &error) {
|
||||
|
||||
auto path = std::move(next_path) + next_query;
|
||||
|
||||
// A Unix socket client sends "Host: localhost", so a Location naming
|
||||
// localhost names this client too.
|
||||
auto is_same_host = next_host == host_ ||
|
||||
(address_family_ == AF_UNIX && next_host == "localhost");
|
||||
// Host names are case-insensitive (RFC 3986 3.2.2). A Unix socket client
|
||||
// sends "Host: localhost", so a Location naming localhost names this client
|
||||
// too.
|
||||
auto is_same_host = detail::case_ignore::equal(next_host, host_) ||
|
||||
(address_family_ == AF_UNIX &&
|
||||
detail::case_ignore::equal(next_host, "localhost"));
|
||||
|
||||
// Same host redirect - use current client
|
||||
if (next_scheme == scheme && is_same_host && next_port == port_) {
|
||||
|
||||
@@ -347,6 +347,9 @@ TEST_F(UnixSocketTest, RedirectStaysOnSocket) {
|
||||
// An absolute Location naming the Host this client sends
|
||||
res.set_redirect("http://localhost" + pattern_);
|
||||
});
|
||||
svr.Get("/upper", [&](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_redirect("http://LOCALHOST" + pattern_);
|
||||
});
|
||||
svr.Get("/cross", [&](const httplib::Request &, httplib::Response &res) {
|
||||
res.set_redirect("http://" + other_path + pattern_);
|
||||
});
|
||||
@@ -399,6 +402,13 @@ TEST_F(UnixSocketTest, RedirectStaysOnSocket) {
|
||||
EXPECT_EQ(content_, res->body);
|
||||
EXPECT_EQ("Bearer token", res->get_header_value("X-Auth"));
|
||||
|
||||
// in any case
|
||||
res = cli.Get("/upper");
|
||||
ASSERT_TRUE(res) << "error: " << res.error();
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
EXPECT_EQ(content_, res->body);
|
||||
EXPECT_EQ("Bearer token", res->get_header_value("X-Auth"));
|
||||
|
||||
// A host the caller mapped to a socket goes there
|
||||
cli.set_direct_hostname_addr_map({{"mapped.test", other_path}});
|
||||
res = cli.Get("/mapped");
|
||||
@@ -4744,6 +4754,45 @@ TEST(RedirectToSamePort, ForwardCookie) {
|
||||
EXPECT_EQ("session_id=SECRET", captured_cookie);
|
||||
}
|
||||
|
||||
TEST(RedirectToSamePort, HostNameIsCaseInsensitive) {
|
||||
// "LOCALHOST" names the same host as "localhost" (RFC 3986 3.2.2), so the
|
||||
// redirect stays on this origin and keeps its credentials and cookies.
|
||||
Server svr;
|
||||
std::string captured_authorization;
|
||||
std::string captured_cookie;
|
||||
int port = 0;
|
||||
svr.Get("/redir", [&](const Request & /*req*/, Response &res) {
|
||||
res.set_redirect("http://LOCALHOST:" + std::to_string(port) + "/target",
|
||||
302);
|
||||
});
|
||||
svr.Get("/target", [&](const Request &req, Response &res) {
|
||||
captured_authorization = req.get_header_value("Authorization");
|
||||
captured_cookie = req.get_header_value("Cookie");
|
||||
res.set_content("OK", "text/plain");
|
||||
});
|
||||
|
||||
port = svr.bind_to_any_port(HOST);
|
||||
auto thread = std::thread([&]() { svr.listen_after_bind(); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
thread.join();
|
||||
ASSERT_FALSE(svr.is_running());
|
||||
});
|
||||
|
||||
svr.wait_until_ready();
|
||||
|
||||
Client cli(HOST, port);
|
||||
cli.set_follow_location(true);
|
||||
cli.set_basic_auth("admin", "secret");
|
||||
|
||||
Headers headers = {{"Cookie", "session_id=SECRET"}};
|
||||
auto res = cli.Get("/redir", headers);
|
||||
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
|
||||
EXPECT_EQ(StatusCode::OK_200, res->status);
|
||||
EXPECT_EQ("Basic YWRtaW46c2VjcmV0", captured_authorization);
|
||||
EXPECT_EQ("session_id=SECRET", captured_cookie);
|
||||
}
|
||||
|
||||
TEST(RedirectTest, DoNotCarryRequestParamsToLocation) {
|
||||
// The Location alone names the next target, on any origin.
|
||||
Server target_svr;
|
||||
|
||||
Reference in new issue
Block a user