mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-26 20:48:24 +00:00
Decide the graceful close in close_served() itself
Both callers of close_served() worked out from the same connection whether to close gracefully, so pass what resume() returned and let the connection check turn.websocket_upgraded once. Also wait for the keep-alive timeout in both modes at once, which halves the test to about a second, and describe include_httplib.cc for what it now does.
This commit is contained in:
1 parent
8037789492
commit
bba6042bba
3 files changed
+28
-21
No files matched your search
@@ -14374,11 +14374,11 @@ struct Server::KeepAliveConnection {
|
|||||||
sock = INVALID_SOCKET;
|
sock = INVALID_SOCKET;
|
||||||
}
|
}
|
||||||
|
|
||||||
// From the worker that served it.
|
// From the worker that served it, with what resume() returned.
|
||||||
void close_served(bool graceful) {
|
void close_served(bool ok) {
|
||||||
assert(sock != INVALID_SOCKET);
|
assert(sock != INVALID_SOCKET);
|
||||||
if (opened) {
|
if (opened) {
|
||||||
shut_down(graceful);
|
shut_down(ok && !turn.websocket_upgraded);
|
||||||
detail::drain_and_close_socket(sock);
|
detail::drain_and_close_socket(sock);
|
||||||
} else {
|
} else {
|
||||||
// It never got as far as a request, e.g. a failed TLS handshake.
|
// It never got as far as a request, e.g. a failed TLS handshake.
|
||||||
@@ -16471,7 +16471,7 @@ inline bool Server::process_and_close_socket(socket_t sock) {
|
|||||||
auto conn = new_keep_alive_connection(sock);
|
auto conn = new_keep_alive_connection(sock);
|
||||||
conn->wait_in_place = true;
|
conn->wait_in_place = true;
|
||||||
auto ret = conn->resume();
|
auto ret = conn->resume();
|
||||||
conn->close_served(!conn->turn.websocket_upgraded && ret);
|
conn->close_served(ret);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16656,7 +16656,7 @@ Server::resume_connection(std::shared_ptr<KeepAliveConnection> conn) {
|
|||||||
c.release_buffers(); // it may stay idle for long
|
c.release_buffers(); // it may stay idle for long
|
||||||
if (st.give_back(conn)) { return; }
|
if (st.give_back(conn)) { return; }
|
||||||
}
|
}
|
||||||
c.close_served(!c.turn.websocket_upgraded && ret);
|
c.close_served(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Server::output_log(const Request &req, const Response &res) const {
|
inline void Server::output_log(const Request &req, const Response &res) const {
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
// The sole purpose of this file is to include httplib.h in a separate
|
// Includes httplib.h in a separate compilation unit, thus verifying that
|
||||||
// compilation unit, thus verifying that inline keywords have not been forgotten
|
// inline keywords have not been forgotten when linked together with test.cc.
|
||||||
// when linked together with test.cc.
|
|
||||||
|
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
|
|
||||||
// The library no longer calls these, but subclasses that override
|
// Nothing in the library calls these, but subclasses that override
|
||||||
// process_and_close_socket() do. Instantiating them here, where the split build
|
// process_and_close_socket() do. Instantiating them here, where the split build
|
||||||
// does not reach, keeps a change to process_server_socket_core() from breaking
|
// does not reach, keeps a change to process_server_socket_core() from breaking
|
||||||
// them unnoticed.
|
// them unnoticed.
|
||||||
|
|||||||
+20
-12
@@ -34496,19 +34496,27 @@ private:
|
|||||||
// read timeout, whether or not it is parked.
|
// read timeout, whether or not it is parked.
|
||||||
TEST(KeepAliveParkingTest, FirstRequestWaitsForTheKeepAliveTimeout) {
|
TEST(KeepAliveParkingTest, FirstRequestWaitsForTheKeepAliveTimeout) {
|
||||||
using namespace parking_test;
|
using namespace parking_test;
|
||||||
for (auto parking : {true, false}) {
|
// Both modes at once, since each waits a whole second.
|
||||||
SCOPED_TRACE(parking ? "parking" : "no parking");
|
Server svrs[2];
|
||||||
Server svr;
|
std::unique_ptr<RunningServer> running[2];
|
||||||
serve_ok(svr, threads(1), parking);
|
Sockets socks; // after `running`, so that the sockets close first
|
||||||
svr.set_read_timeout(10, 0);
|
for (auto i = 0; i < 2; i++) {
|
||||||
svr.set_keep_alive_timeout(1);
|
serve_ok(svrs[i], threads(1), /*parking=*/i == 0);
|
||||||
RunningServer running(svr);
|
svrs[i].set_read_timeout(10, 0);
|
||||||
Sockets socks;
|
svrs[i].set_keep_alive_timeout(1);
|
||||||
|
running[i].reset(new RunningServer(svrs[i]));
|
||||||
|
ASSERT_NE(INVALID_SOCKET, socks.connect(running[i]->port));
|
||||||
|
}
|
||||||
|
|
||||||
auto sock = socks.connect(running.port);
|
auto early = in_ms(500);
|
||||||
ASSERT_NE(INVALID_SOCKET, sock);
|
for (size_t i = 0; i < 2; i++) {
|
||||||
EXPECT_FALSE(sees_eof(sock, in_ms(500)));
|
SCOPED_TRACE(i == 0 ? "parking" : "no parking");
|
||||||
EXPECT_TRUE(sees_eof(sock, in_ms(2500)));
|
EXPECT_FALSE(sees_eof(socks.socks[i], early));
|
||||||
|
}
|
||||||
|
auto late = in_ms(2500);
|
||||||
|
for (size_t i = 0; i < 2; i++) {
|
||||||
|
SCOPED_TRACE(i == 0 ? "parking" : "no parking");
|
||||||
|
EXPECT_TRUE(sees_eof(socks.socks[i], late));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user