mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-17 19:30:54 +00:00
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.
This commit is contained in:
1 parent
1b40ce850b
commit
4af42e1907
2 files changed
+176
-12
No files matched your search
@@ -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
|
||||
@@ -15801,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;
|
||||
}
|
||||
@@ -16459,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);
|
||||
|
||||
Reference in new issue
Block a user