Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit 3e906a9

Browse files
committed
1 parent 110393e commit 3e906a9

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

httplib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4518,7 +4518,7 @@ inline bool ClientImpl::send(const Request &req, Response &res) {
45184518
return handle_request(strm, req, res, close_connection);
45194519
});
45204520

4521-
if (close_connection) { stop(); }
4521+
if (close_connection || !ret) { stop(); }
45224522

45234523
return ret;
45244524
}

test/test.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,43 @@ TEST(ExceptionTest, ThrowExceptionInHandler) {
27202720
ASSERT_FALSE(svr.is_running());
27212721
}
27222722

2723+
TEST(KeepAliveTest, ReadTimeout) {
2724+
Server svr;
2725+
2726+
svr.Get("/a", [&](const Request & /*req*/, Response &res) {
2727+
std::this_thread::sleep_for(std::chrono::seconds(2));
2728+
res.set_content("a", "text/plain");
2729+
});
2730+
2731+
svr.Get("/b", [&](const Request & /*req*/, Response &res) {
2732+
res.set_content("b", "text/plain");
2733+
});
2734+
2735+
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
2736+
while (!svr.is_running()) {
2737+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
2738+
}
2739+
2740+
// Give GET time to get a few messages.
2741+
std::this_thread::sleep_for(std::chrono::seconds(1));
2742+
2743+
Client cli("localhost", PORT);
2744+
cli.set_keep_alive(true);
2745+
cli.set_read_timeout(1);
2746+
2747+
auto resa = cli.Get("/a");
2748+
ASSERT_TRUE(resa == nullptr);
2749+
2750+
auto resb = cli.Get("/b");
2751+
ASSERT_TRUE(resb != nullptr);
2752+
EXPECT_EQ(200, resb->status);
2753+
EXPECT_EQ("b", resb->body);
2754+
2755+
svr.stop();
2756+
listen_thread.join();
2757+
ASSERT_FALSE(svr.is_running());
2758+
}
2759+
27232760
class ServerTestWithAI_PASSIVE : public ::testing::Test {
27242761
protected:
27252762
ServerTestWithAI_PASSIVE()

0 commit comments

Comments
 (0)