Compare commits

...
Author SHA1 Message Date
yhirose a11f530494 Don't reuse a client connection with unread data on it
A kept-alive connection was reused while bytes that belong to no
request were waiting on it, and the next request read them as its
response. Such bytes are not a response (RFC 9112 9.2). A server may,
for one, send a 408 before it closes an idle connection.

Close the connection when they show up: after a response, if bytes
past it are already buffered in the stream or the TLS library, and
before reuse, if anything is readable on the socket. As in curl, the
reuse check no longer asks the TLS library whether the peer closed, so
it is the same for every TLS backend. A TLS message on an idle
connection, such as a late session ticket, now costs a new connection.
2026-11-16 23:31:31 -05:00
yhirose b23e16775d Close the tunnel when the TLS handshake through a proxy fails
SSLClient::setup_proxy_connection() left the socket open when the TLS
handshake inside the CONNECT tunnel failed, so the client kept a
connection with no TLS session on it. Only the next request's reuse
check closed it, because tls::is_peer_closed() reports a missing
session as closed. Close it where the handshake fails, as
ensure_socket_connection() does for a direct connection.
2026-11-16 23:30:26 -05:00
metsw24-maxandyhirose f57672b390 take the request lock in open_stream_ before reusing the socket (#2669)
* take the request lock in open_stream_ before reusing the socket

* Hold the request lock only while open_stream_ takes the socket

The handle owns the connection once the socket is transferred to it, so
holding request_mutex_ while the stream wrote its request and read the
response head only made requests on other threads wait for the stream.
Also shorten the comments.

---------

Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
2026-11-16 22:08:13 -05:00
metsw24-maxandyhirose 654fd3546c resolve junctions and symlinks in canonicalize_path on Windows (#2670)
* resolve junctions and symlinks in canonicalize_path on Windows

* Shorten the canonicalize_path comment

---------

Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
2026-11-16 21:19:39 -05:00
2 changed files with 325 additions and 15 deletions

No files matched your search

+40 -15
View File
@@ -3031,8 +3031,8 @@ protected:
void disconnect(bool gracefully);
// Under socket_mutex_: whether the connection kept alive can take another
// request. One the peer has closed, or one set up under TLS settings changed
// since, is closed instead.
// request. One with anything to read, or one set up under TLS settings
// changed since, is closed instead.
bool socket_reusable();
// TLS settings apply as a connection is set up, so a change makes the
@@ -6111,9 +6111,35 @@ inline bool is_valid_path(const std::string &path) {
inline bool canonicalize_path(const char *path, std::string &resolved) {
#if defined(_WIN32)
char buf[_MAX_PATH];
if (_fullpath(buf, path, _MAX_PATH) == nullptr) { return false; }
resolved = buf;
// Resolve junctions and symlinks, as realpath() does
auto wpath = u8string_to_wstring(path);
if (wpath.empty()) { return false; }
CREATEFILE2_EXTENDED_PARAMETERS params{};
params.dwSize = sizeof(params);
params.dwFileFlags = FILE_FLAG_BACKUP_SEMANTICS; // Needed to open a directory
auto h = ::CreateFile2(wpath.c_str(), 0,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
OPEN_EXISTING, &params);
if (h == INVALID_HANDLE_VALUE) { return false; }
auto guard = scope_exit([&]() { ::CloseHandle(h); });
std::wstring wbuf(MAX_PATH, L'\0');
auto len = ::GetFinalPathNameByHandleW(
h, &wbuf[0], static_cast<DWORD>(wbuf.size()), FILE_NAME_NORMALIZED);
if (len >= wbuf.size()) {
wbuf.resize(len);
len = ::GetFinalPathNameByHandleW(
h, &wbuf[0], static_cast<DWORD>(wbuf.size()), FILE_NAME_NORMALIZED);
}
if (len == 0 || len >= wbuf.size()) { return false; }
auto n = ::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast<int>(len),
nullptr, 0, nullptr, nullptr);
if (n <= 0) { return false; }
resolved.assign(static_cast<size_t>(n), '\0');
::WideCharToMultiByte(CP_UTF8, 0, wbuf.data(), static_cast<int>(len),
&resolved[0], n, nullptr, nullptr);
#elif defined(PATH_MAX)
char buf[PATH_MAX];
if (realpath(path, buf) == nullptr) { return false; }
@@ -15775,14 +15801,8 @@ inline void ClientImpl::disconnect(bool gracefully) {
inline bool ClientImpl::socket_reusable() {
if (!socket_.is_open()) { return false; }
auto is_alive = detail::is_socket_alive(socket_.sock);
#ifdef CPPHTTPLIB_SSL_ENABLED
if (is_alive && is_ssl() && tls::is_peer_closed(socket_.ssl, socket_.sock)) {
is_alive = false;
}
#endif
if (!is_alive) {
// Peer seems gone — non-graceful shutdown to avoid SIGPIPE.
// RFC 9112 9.2: data with no request outstanding is not a response
if (detail::select_read(socket_.sock, 0, 0) != 0) {
disconnect(/*gracefully=*/false);
return false;
}
@@ -16036,6 +16056,8 @@ inline ClientImpl::StreamHandle ClientImpl::open_stream_(Request &req) {
handle.connection_ = detail::make_unique<ClientConnection>();
{
// A request in flight uses socket_ without holding socket_mutex_
std::lock_guard<std::recursive_mutex> request_mutex_guard(request_mutex_);
std::lock_guard<std::mutex> guard(socket_mutex_);
if (!socket_reusable()) {
@@ -16431,9 +16453,10 @@ inline bool ClientImpl::handle_request(Stream &strm, Request &req,
// to do these things to the socket if another thread is using the socket.
std::lock_guard<std::mutex> guard(socket_mutex_);
// Also when the request asked for it (e.g. a body that was never sent,
// or "Connection: close"), so that a redirect or an auth retry below does
// not reuse the socket.
// or "Connection: close") or bytes follow the response (RFC 9112 9.2), so
// that a redirect or an auth retry below does not reuse the socket.
if (socket_should_be_closed_when_request_is_done_ || close_connection ||
strm.is_readable() ||
detail::has_header_token(res.headers, "Connection", "close") ||
(res.version == "HTTP/1.0" && res.reason != "Connection established")) {
disconnect(/*gracefully=*/true);
@@ -19299,6 +19322,8 @@ inline bool SSLClient::setup_proxy_connection(
}
if (!initialize_ssl(socket, error)) {
shutdown_socket(socket);
close_socket(socket);
success = false;
return false;
}
+285
View File
@@ -13415,6 +13415,175 @@ TEST(KeepAliveTest, MaxCountZeroSetsNoLimit) {
EXPECT_EQ(1u, remote_ports.size());
}
// Forward declaration (see the base64_encode note).
namespace httplib {
namespace detail {
ssize_t select_read(socket_t sock, time_t sec, time_t usec);
} // namespace detail
} // namespace httplib
// RFC 9112 9.2: an unsolicited 408 must not be read as the next response
static void expect_no_reuse_with_unread_data(bool use_tls, bool same_write) {
#ifndef _WIN32
signal(SIGPIPE, SIG_IGN);
#endif
auto srv = ::socket(AF_INET, SOCK_STREAM, 0);
ASSERT_NE(INVALID_SOCKET, srv);
auto srv_se = detail::scope_exit([&] { detail::close_socket(srv); });
default_socket_options(srv);
sockaddr_in addr{};
addr.sin_family = AF_INET;
addr.sin_port = 0;
::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
ASSERT_EQ(0, ::bind(srv, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)));
ASSERT_EQ(0, ::listen(srv, 2));
socklen_t addr_len = sizeof(addr);
ASSERT_EQ(0,
::getsockname(srv, reinterpret_cast<sockaddr *>(&addr), &addr_len));
auto port = ntohs(addr.sin_port);
struct Peer {
socket_t sock = INVALID_SOCKET;
#ifdef CPPHTTPLIB_SSL_ENABLED
tls::session_t session = nullptr;
#endif
};
#ifdef CPPHTTPLIB_SSL_ENABLED
tls::ctx_t ctx = nullptr;
if (use_tls) {
ctx = tls::create_server_context();
tls::set_server_cert_file(ctx, SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE,
nullptr);
}
auto ctx_se = detail::scope_exit([&] {
if (ctx) { tls::free_context(ctx); }
});
#endif
auto close_peer = [](Peer &peer) {
#ifdef CPPHTTPLIB_SSL_ENABLED
if (peer.session) { tls::free_session(peer.session); }
#endif
if (peer.sock != INVALID_SOCKET) { detail::close_socket(peer.sock); }
};
auto accept_peer = [&]() {
Peer peer;
if (detail::select_read(srv, 5, 0) <= 0) { return peer; }
peer.sock = ::accept(srv, nullptr, nullptr);
if (peer.sock == INVALID_SOCKET) { return peer; }
detail::set_socket_opt_time(peer.sock, SOL_SOCKET, SO_RCVTIMEO, 5, 0);
#ifdef CPPHTTPLIB_SSL_ENABLED
if (use_tls) {
peer.session = tls::create_session(ctx, peer.sock);
tls::TlsError err;
if (!tls::accept_nonblocking(peer.session, peer.sock, 5, 0, &err)) {
close_peer(peer);
return Peer();
}
}
#endif
return peer;
};
auto read_some = [](const Peer &peer, char *buf, size_t len) -> ssize_t {
#ifdef CPPHTTPLIB_SSL_ENABLED
tls::TlsError err;
if (peer.session) { return tls::read(peer.session, buf, len, err); }
#endif
return detail::read_socket(peer.sock, buf, len, 0);
};
auto read_head = [&](const Peer &peer) {
std::string head;
char buf[1024];
while (head.find("\r\n\r\n") == std::string::npos) {
auto n = read_some(peer, buf, sizeof(buf));
if (n <= 0) { break; }
head.append(buf, static_cast<size_t>(n));
}
};
auto send_text = [](const Peer &peer, const std::string &text) {
#ifdef CPPHTTPLIB_SSL_ENABLED
tls::TlsError err;
if (peer.session) {
tls::write(peer.session, text.data(), text.size(), err);
return;
}
#endif
detail::send_socket(peer.sock, text.data(), text.size(), 0);
};
const std::string first = "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nfirst";
const std::string stray = "HTTP/1.1 408 Request Timeout\r\n"
"Connection: close\r\nContent-Length: 0\r\n\r\n";
// Sent once the first response is read, so that it lands on an idle socket
std::promise<void> first_response_read;
auto first_response_read_future = first_response_read.get_future();
auto server_thread = std::thread([&] {
auto peer1 = accept_peer();
if (peer1.sock == INVALID_SOCKET) { return; }
read_head(peer1);
if (same_write) {
send_text(peer1, first + stray);
} else {
send_text(peer1, first);
first_response_read_future.wait_for(std::chrono::seconds(5));
send_text(peer1, stray);
}
// peer1 stays open, so only the unread data can tell the client
auto peer2 = accept_peer();
if (peer2.sock != INVALID_SOCKET) {
read_head(peer2);
send_text(peer2, "HTTP/1.1 200 OK\r\nContent-Length: 6\r\n"
"Connection: close\r\n\r\nsecond");
close_peer(peer2);
}
close_peer(peer1);
});
auto server_se = detail::scope_exit([&] { server_thread.join(); });
Client cli((use_tls ? "https://127.0.0.1:" : "http://127.0.0.1:") +
std::to_string(port));
#ifdef CPPHTTPLIB_SSL_ENABLED
cli.enable_server_certificate_verification(false);
#endif
cli.set_keep_alive(true);
cli.set_read_timeout(5, 0);
auto res = cli.Get("/1");
first_response_read.set_value();
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("first", res->body);
if (!same_write) { ASSERT_GT(detail::select_read(cli.socket(), 5, 0), 0); }
res = cli.Get("/2");
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status);
EXPECT_EQ("second", res->body);
}
TEST(KeepAliveTest, ClientDoesNotReuseConnectionWithUnreadData) {
expect_no_reuse_with_unread_data(/*use_tls=*/false, /*same_write=*/false);
}
TEST(KeepAliveTest, ClientDoesNotReuseConnectionWithDataPastResponse) {
expect_no_reuse_with_unread_data(/*use_tls=*/false, /*same_write=*/true);
}
#ifdef CPPHTTPLIB_SSL_ENABLED
TEST(KeepAliveTest, SSLClientDoesNotReuseConnectionWithUnreadData) {
expect_no_reuse_with_unread_data(/*use_tls=*/true, /*same_write=*/false);
}
TEST(KeepAliveTest, SSLClientDoesNotReuseConnectionWithDataPastResponse) {
expect_no_reuse_with_unread_data(/*use_tls=*/true, /*same_write=*/true);
}
#endif
TEST(KeepAliveTest, Issue1959) {
Server svr;
svr.set_keep_alive_timeout(5);
@@ -24347,6 +24516,45 @@ TEST(OpenStreamPartialWriteTest, BodyIsWrittenWhole) {
}
#endif
TEST(OpenStreamConcurrencyTest, RequestInFlightKeepsItsConnection) {
std::promise<void> slow_started;
auto slow_started_future = slow_started.get_future();
Server svr;
svr.Get("/slow", [&](const Request &, Response &res) {
slow_started.set_value();
std::this_thread::sleep_for(std::chrono::milliseconds(200));
res.set_content("slow", "text/plain");
});
svr.Get("/fast", [](const Request &, Response &res) {
res.set_content("fast", "text/plain");
});
auto port = svr.bind_to_any_port(HOST);
std::thread t([&]() { svr.listen_after_bind(); });
auto se = detail::scope_exit([&] {
svr.stop();
t.join();
});
svr.wait_until_ready();
Client cli(HOST, port);
std::string slow_body;
std::thread slow_thread([&] {
auto res = cli.Get("/slow");
if (res) { slow_body = res->body; }
});
slow_started_future.wait();
auto result = stream::Get(cli, "/fast");
auto fast_body = result.is_valid() ? result.read_all() : std::string();
slow_thread.join();
EXPECT_EQ("slow", slow_body);
EXPECT_EQ("fast", fast_body);
}
// Serves `response` to the single request `fn` makes with a fresh client.
template <typename Fn>
static void with_single_response(const std::string &response, Fn fn) {
@@ -30712,6 +30920,49 @@ TEST(SymlinkTest, SymlinkEscapeFromBaseDirectory) {
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::Forbidden_403, res->status);
}
#else
TEST(SymlinkTest, JunctionEscapeFromBaseDirectory) {
auto secret_dir = std::string("junction_test_secret");
auto served_dir = std::string("junction_test_served");
auto secret_file = secret_dir + "\\secret.txt";
auto junction_path = served_dir + "\\escape";
::CreateDirectoryA(secret_dir.c_str(), nullptr);
::CreateDirectoryA(served_dir.c_str(), nullptr);
{
std::ofstream ofs(secret_file);
ofs << "SECRET_DATA";
}
// A junction needs no privilege to create, unlike a symlink
auto cmd = "mklink /J " + junction_path + " " + secret_dir + " >nul";
ASSERT_EQ(0, std::system(cmd.c_str()));
auto se = detail::scope_exit([&] {
::RemoveDirectoryA(junction_path.c_str());
std::remove(secret_file.c_str());
::RemoveDirectoryA(served_dir.c_str());
::RemoveDirectoryA(secret_dir.c_str());
});
Server svr;
svr.set_mount_point("/", served_dir);
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
auto se2 = detail::scope_exit([&] {
svr.stop();
listen_thread.join();
});
svr.wait_until_ready();
Client cli("localhost", PORT);
// Junction pointing outside base dir should be blocked
auto res = cli.Get("/escape/secret.txt");
ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::Forbidden_403, res->status);
}
#endif
// Sends `outer_head` (with every "{len}" replaced by the length of an embedded
@@ -32403,6 +32654,40 @@ TEST(ProxyTunnelTest, RedirectKeepsTunnelHostnameAddrMap) {
EXPECT_NE(std::string::npos,
connect_req.find("\r\nHost: " + target + "\r\n"));
}
// A failed handshake inside the tunnel must not leave the tunnel for reuse
TEST(ProxyTunnelTest, FailedHandshakeClosesTunnel) {
std::atomic<int> origin_hits{0};
proxy_tunnel_test::ScopedSSLServer origin;
origin.svr().Get(".*", [&](const Request &, Response &res) {
origin_hits++;
res.set_content("ok", "text/plain");
});
origin.listen();
proxy_tunnel_test::ScopedConnectProxy proxy(origin.port());
ASSERT_NE(0, proxy.port());
SSLClient cli("127.0.0.1", origin.port());
cli.enable_server_certificate_verification(false);
cli.set_proxy("127.0.0.1", proxy.port());
cli.set_keep_alive(true);
cli.set_session_verifier(
[](tls::session_t) { return SSLVerifierResponse::CertificateRejected; });
auto res = cli.Get("/x");
EXPECT_FALSE(res);
ASSERT_FALSE(cli.is_socket_open());
res = cli.Get("/x");
EXPECT_FALSE(res);
EXPECT_EQ(2, proxy.connect_hits());
auto handle = cli.open_stream("GET", "/x");
EXPECT_FALSE(handle.is_valid());
EXPECT_EQ(3, proxy.connect_hits());
EXPECT_EQ(0, origin_hits.load());
}
#endif
namespace proxy_tunnel_test {