From 26deffe0c6a1e177e393416b94cdc0f340ca79bd Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 10 Mar 2020 17:42:14 -0400 Subject: [PATCH] Not to send 'EXCEPTION_WHAT' header to client --- httplib.h | 3 +++ test/test.cc | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/httplib.h b/httplib.h index b5fdbd4..e79ae3c 100644 --- a/httplib.h +++ b/httplib.h @@ -1923,6 +1923,9 @@ inline ssize_t write_headers(Stream &strm, const T &info, const Headers &headers) { ssize_t write_len = 0; for (const auto &x : info.headers) { + if (x.first == "EXCEPTION_WHAT") { + continue; + } auto len = strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str()); if (len < 0) { return len; } diff --git a/test/test.cc b/test/test.cc index 9c8eee9..3e9e2a3 100644 --- a/test/test.cc +++ b/test/test.cc @@ -2205,6 +2205,35 @@ TEST(MountTest, Unmount) { ASSERT_FALSE(svr.is_running()); } +TEST(ExceptionTest, ThrowExceptionInHandler) { + Server svr; + + svr.Get("/hi", + [&](const Request & /*req*/, Response &res) { + throw std::runtime_error("exception..."); + res.set_content("Hello World!", "text/plain"); + }); + + auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); }); + while (!svr.is_running()) { + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + + // Give GET time to get a few messages. + std::this_thread::sleep_for(std::chrono::seconds(1)); + + Client cli("localhost", PORT); + + auto res = cli.Get("/hi"); + ASSERT_TRUE(res != nullptr); + EXPECT_EQ(500, res->status); + ASSERT_FALSE(res->has_header("EXCEPTION_WHAT")); + + svr.stop(); + listen_thread.join(); + ASSERT_FALSE(svr.is_running()); +} + class ServerTestWithAI_PASSIVE : public ::testing::Test { protected: ServerTestWithAI_PASSIVE()