From 72d3f4896a46a9b820fbb6955d738fac0f31b371 Mon Sep 17 00:00:00 2001 From: Kai Aoki Date: Tue, 12 Jul 2022 00:10:57 +0900 Subject: [PATCH 1/3] Update httplib.h use std::exception_ptr --- httplib.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 1c4db98..111276a 100644 --- a/httplib.h +++ b/httplib.h @@ -616,7 +616,7 @@ public: using Handler = std::function; using ExceptionHandler = - std::function; + std::function; enum class HandlerResponse { Handled, @@ -5733,7 +5733,8 @@ Server::process_request(Stream &strm, bool close_connection, routed = routing(req, res, strm); } catch (std::exception &e) { if (exception_handler_) { - exception_handler_(req, res, e); + auto ep = std::current_exception(); + exception_handler_(req, res, ep); routed = true; } else { res.status = 500; From d4ab2fa0e6a1f903a5f4be761d1f1ed5e4aaee8c Mon Sep 17 00:00:00 2001 From: Kai Aoki Date: Fri, 15 Jul 2022 01:45:10 +0900 Subject: [PATCH 2/3] fix double ref and case of exceptions that are not std::exception --- httplib.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/httplib.h b/httplib.h index 111276a..7647fed 100644 --- a/httplib.h +++ b/httplib.h @@ -616,7 +616,7 @@ public: using Handler = std::function; using ExceptionHandler = - std::function; + std::function; enum class HandlerResponse { Handled, @@ -5741,8 +5741,14 @@ Server::process_request(Stream &strm, bool close_connection, res.set_header("EXCEPTION_WHAT", e.what()); } } catch (...) { - res.status = 500; - res.set_header("EXCEPTION_WHAT", "UNKNOWN"); + if (exception_handler_) { + auto ep = std::current_exception(); + exception_handler_(req, res, ep); + routed = true; + } else { + res.status = 500; + res.set_header("EXCEPTION_WHAT", "UNKNOWN"); + } } #endif From 869f5bb2794c0c2fb458fae898e20c0f179ffc88 Mon Sep 17 00:00:00 2001 From: Kai Aoki Date: Fri, 15 Jul 2022 11:50:26 +0900 Subject: [PATCH 3/3] fix ExceptionHandlerTest.ContentLength --- test/test.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/test.cc b/test/test.cc index 3f94e58..6bd3330 100644 --- a/test/test.cc +++ b/test/test.cc @@ -1249,8 +1249,13 @@ TEST(ExceptionHandlerTest, ContentLength) { Server svr; svr.set_exception_handler([](const Request & /*req*/, Response &res, - std::exception &e) { - EXPECT_EQ("abc", std::string(e.what())); + std::exception_ptr ep) { + EXPECT_FALSE(ep == nullptr); + try{ + std::rethrow_exception(ep); + }catch(std::exception& e){ + EXPECT_EQ("abc", std::string(e.what())); + } res.status = 500; res.set_content("abcdefghijklmnopqrstuvwxyz", "text/html"); // <= Content-Length still 13 at this point