From 226388ae27374956df03a234fcc0fa3cb53415ce Mon Sep 17 00:00:00 2001 From: yhirose Date: Tue, 23 Nov 2021 10:47:30 -0500 Subject: [PATCH] Resolve #1100 --- httplib.h | 14 ++++++++++++++ test/Makefile | 4 ++-- test/test.cc | 10 ++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 2e0ac21..08f7c51 100644 --- a/httplib.h +++ b/httplib.h @@ -3611,7 +3611,11 @@ inline bool parse_multipart_boundary(const std::string &content_type, return !boundary.empty(); } +#ifdef CPPHTTPLIB_NO_EXCEPTIONS +inline bool parse_range_header(const std::string &s, Ranges &ranges) { +#else inline bool parse_range_header(const std::string &s, Ranges &ranges) try { +#endif static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))"); std::smatch m; if (std::regex_match(s, m, re_first_range)) { @@ -3643,7 +3647,11 @@ inline bool parse_range_header(const std::string &s, Ranges &ranges) try { return all_valid_ranges; } return false; +#ifdef CPPHTTPLIB_NO_EXCEPTIONS +} +#else } catch (...) { return false; } +#endif class MultipartFormDataParser { public: @@ -5505,6 +5513,9 @@ Server::process_request(Stream &strm, bool close_connection, // Rounting bool routed = false; +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + routed = routing(req, res, strm); +#else try { routed = routing(req, res, strm); } catch (std::exception &e) { @@ -5519,6 +5530,7 @@ Server::process_request(Stream &strm, bool close_connection, res.status = 500; res.set_header("EXCEPTION_WHAT", "UNKNOWN"); } +#endif if (routed) { if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; } @@ -7579,8 +7591,10 @@ inline Client::Client(const std::string &scheme_host_port, #else if (!scheme.empty() && scheme != "http") { #endif +#ifndef CPPHTTPLIB_NO_EXCEPTIONS std::string msg = "'" + scheme + "' scheme is not supported."; throw std::invalid_argument(msg); +#endif return; } diff --git a/test/Makefile b/test/Makefile index 52139e5..0b228ad 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,5 +1,5 @@ -#CXX = clang++ -CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion #-fsanitize=address +CXX = clang++ +CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address PREFIX = /usr/local #PREFIX = $(shell brew --prefix) diff --git a/test/test.cc b/test/test.cc index 68ee018..04c9efc 100644 --- a/test/test.cc +++ b/test/test.cc @@ -37,8 +37,12 @@ MultipartFormData &get_file_value(MultipartFormDataItems &files, auto it = std::find_if( files.begin(), files.end(), [&](const MultipartFormData &file) { return file.name == key; }); +#ifdef CPPHTTPLIB_NO_EXCEPTIONS + return *it; +#else if (it != files.end()) { return *it; } throw std::runtime_error("invalid mulitpart form data name error"); +#endif } TEST(ConstructorTest, MoveConstructible) { @@ -1187,6 +1191,7 @@ TEST(ErrorHandlerTest, ContentLength) { ASSERT_FALSE(svr.is_running()); } +#ifndef CPPHTTPLIB_NO_EXCEPTIONS TEST(ExceptionHandlerTest, ContentLength) { Server svr; @@ -1222,6 +1227,7 @@ TEST(ExceptionHandlerTest, ContentLength) { thread.join(); ASSERT_FALSE(svr.is_running()); } +#endif TEST(NoContentTest, ContentLength) { Server svr; @@ -3681,6 +3687,7 @@ TEST(MountTest, Unmount) { ASSERT_FALSE(svr.is_running()); } +#ifndef CPPHTTPLIB_NO_EXCEPTIONS TEST(ExceptionTest, ThrowExceptionInHandler) { Server svr; @@ -3709,6 +3716,7 @@ TEST(ExceptionTest, ThrowExceptionInHandler) { listen_thread.join(); ASSERT_FALSE(svr.is_running()); } +#endif TEST(KeepAliveTest, ReadTimeout) { Server svr; @@ -4515,9 +4523,11 @@ TEST(NoSSLSupport, SimpleInterface) { } #endif +#ifndef CPPHTTPLIB_NO_EXCEPTIONS TEST(InvalidScheme, SimpleInterface) { ASSERT_ANY_THROW(Client cli("scheme://yahoo.com")); } +#endif TEST(NoScheme, SimpleInterface) { Client cli("yahoo.com:80");