Skip to content

Commit 9d0a9d4

Browse files
committed
Fix #1437
1 parent 5758769 commit 9d0a9d4

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

httplib.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5855,7 +5855,16 @@ Server::process_request(Stream &strm, bool close_connection,
58555855
routed = true;
58565856
} else {
58575857
res.status = 500;
5858-
res.set_header("EXCEPTION_WHAT", e.what());
5858+
std::string val;
5859+
auto s = e.what();
5860+
for (size_t i = 0; s[i]; i++) {
5861+
switch (s[i]) {
5862+
case '\r': val += "\\r"; break;
5863+
case '\n': val += "\\n"; break;
5864+
default: val += s[i]; break;
5865+
}
5866+
}
5867+
res.set_header("EXCEPTION_WHAT", val);
58595868
}
58605869
} catch (...) {
58615870
if (exception_handler_) {

test/test.cc

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3839,9 +3839,12 @@ TEST(MountTest, Unmount) {
38393839
TEST(ExceptionTest, ThrowExceptionInHandler) {
38403840
Server svr;
38413841

3842-
svr.Get("/hi", [&](const Request & /*req*/, Response & /*res*/) {
3842+
svr.Get("/exception", [&](const Request & /*req*/, Response & /*res*/) {
38433843
throw std::runtime_error("exception...");
3844-
// res.set_content("Hello World!", "text/plain");
3844+
});
3845+
3846+
svr.Get("/unknown", [&](const Request & /*req*/, Response & /*res*/) {
3847+
throw std::runtime_error("exception\r\n...");
38453848
});
38463849

38473850
auto listen_thread = std::thread([&svr]() { svr.listen("localhost", PORT); });
@@ -3854,11 +3857,21 @@ TEST(ExceptionTest, ThrowExceptionInHandler) {
38543857

38553858
Client cli("localhost", PORT);
38563859

3857-
auto res = cli.Get("/hi");
3858-
ASSERT_TRUE(res);
3859-
EXPECT_EQ(500, res->status);
3860-
ASSERT_TRUE(res->has_header("EXCEPTION_WHAT"));
3861-
EXPECT_EQ("exception...", res->get_header_value("EXCEPTION_WHAT"));
3860+
{
3861+
auto res = cli.Get("/exception");
3862+
ASSERT_TRUE(res);
3863+
EXPECT_EQ(500, res->status);
3864+
ASSERT_TRUE(res->has_header("EXCEPTION_WHAT"));
3865+
EXPECT_EQ("exception...", res->get_header_value("EXCEPTION_WHAT"));
3866+
}
3867+
3868+
{
3869+
auto res = cli.Get("/unknown");
3870+
ASSERT_TRUE(res);
3871+
EXPECT_EQ(500, res->status);
3872+
ASSERT_TRUE(res->has_header("EXCEPTION_WHAT"));
3873+
EXPECT_EQ("exception\\r\\n...", res->get_header_value("EXCEPTION_WHAT"));
3874+
}
38623875

38633876
svr.stop();
38643877
listen_thread.join();

0 commit comments

Comments
 (0)