Browse Source

Not to send 'EXCEPTION_WHAT' header to client

yhirose 5 years ago
parent
commit
26deffe0c6
2 changed files with 32 additions and 0 deletions
  1. 3 0
      httplib.h
  2. 29 0
      test/test.cc

+ 3 - 0
httplib.h

@@ -1923,6 +1923,9 @@ inline ssize_t write_headers(Stream &strm, const T &info,
                              const Headers &headers) {
                              const Headers &headers) {
   ssize_t write_len = 0;
   ssize_t write_len = 0;
   for (const auto &x : info.headers) {
   for (const auto &x : info.headers) {
+    if (x.first == "EXCEPTION_WHAT") {
+      continue;
+    }
     auto len =
     auto len =
         strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str());
         strm.write_format("%s: %s\r\n", x.first.c_str(), x.second.c_str());
     if (len < 0) { return len; }
     if (len < 0) { return len; }

+ 29 - 0
test/test.cc

@@ -2205,6 +2205,35 @@ TEST(MountTest, Unmount) {
   ASSERT_FALSE(svr.is_running());
   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 {
 class ServerTestWithAI_PASSIVE : public ::testing::Test {
 protected:
 protected:
   ServerTestWithAI_PASSIVE()
   ServerTestWithAI_PASSIVE()