mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-15 17:28:29 +00:00
Resolve #1100
This commit is contained in:
parent
ea7548b4cc
commit
226388ae27
3 changed files with 26 additions and 2 deletions
14
httplib.h
14
httplib.h
|
@ -3611,7 +3611,11 @@ inline bool parse_multipart_boundary(const std::string &content_type,
|
||||||
return !boundary.empty();
|
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 {
|
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*)*))");
|
static auto re_first_range = std::regex(R"(bytes=(\d*-\d*(?:,\s*\d*-\d*)*))");
|
||||||
std::smatch m;
|
std::smatch m;
|
||||||
if (std::regex_match(s, m, re_first_range)) {
|
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 all_valid_ranges;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
#ifdef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
|
}
|
||||||
|
#else
|
||||||
} catch (...) { return false; }
|
} catch (...) { return false; }
|
||||||
|
#endif
|
||||||
|
|
||||||
class MultipartFormDataParser {
|
class MultipartFormDataParser {
|
||||||
public:
|
public:
|
||||||
|
@ -5505,6 +5513,9 @@ Server::process_request(Stream &strm, bool close_connection,
|
||||||
|
|
||||||
// Rounting
|
// Rounting
|
||||||
bool routed = false;
|
bool routed = false;
|
||||||
|
#ifdef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
|
routed = routing(req, res, strm);
|
||||||
|
#else
|
||||||
try {
|
try {
|
||||||
routed = routing(req, res, strm);
|
routed = routing(req, res, strm);
|
||||||
} catch (std::exception &e) {
|
} catch (std::exception &e) {
|
||||||
|
@ -5519,6 +5530,7 @@ Server::process_request(Stream &strm, bool close_connection,
|
||||||
res.status = 500;
|
res.status = 500;
|
||||||
res.set_header("EXCEPTION_WHAT", "UNKNOWN");
|
res.set_header("EXCEPTION_WHAT", "UNKNOWN");
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (routed) {
|
if (routed) {
|
||||||
if (res.status == -1) { res.status = req.ranges.empty() ? 200 : 206; }
|
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
|
#else
|
||||||
if (!scheme.empty() && scheme != "http") {
|
if (!scheme.empty() && scheme != "http") {
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
std::string msg = "'" + scheme + "' scheme is not supported.";
|
std::string msg = "'" + scheme + "' scheme is not supported.";
|
||||||
throw std::invalid_argument(msg);
|
throw std::invalid_argument(msg);
|
||||||
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#CXX = clang++
|
CXX = clang++
|
||||||
CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion #-fsanitize=address
|
CXXFLAGS = -g -std=c++11 -I. -Wall -Wextra -Wtype-limits -Wconversion # -fno-exceptions -DCPPHTTPLIB_NO_EXCEPTIONS -fsanitize=address
|
||||||
|
|
||||||
PREFIX = /usr/local
|
PREFIX = /usr/local
|
||||||
#PREFIX = $(shell brew --prefix)
|
#PREFIX = $(shell brew --prefix)
|
||||||
|
|
10
test/test.cc
10
test/test.cc
|
@ -37,8 +37,12 @@ MultipartFormData &get_file_value(MultipartFormDataItems &files,
|
||||||
auto it = std::find_if(
|
auto it = std::find_if(
|
||||||
files.begin(), files.end(),
|
files.begin(), files.end(),
|
||||||
[&](const MultipartFormData &file) { return file.name == key; });
|
[&](const MultipartFormData &file) { return file.name == key; });
|
||||||
|
#ifdef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
|
return *it;
|
||||||
|
#else
|
||||||
if (it != files.end()) { return *it; }
|
if (it != files.end()) { return *it; }
|
||||||
throw std::runtime_error("invalid mulitpart form data name error");
|
throw std::runtime_error("invalid mulitpart form data name error");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(ConstructorTest, MoveConstructible) {
|
TEST(ConstructorTest, MoveConstructible) {
|
||||||
|
@ -1187,6 +1191,7 @@ TEST(ErrorHandlerTest, ContentLength) {
|
||||||
ASSERT_FALSE(svr.is_running());
|
ASSERT_FALSE(svr.is_running());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
TEST(ExceptionHandlerTest, ContentLength) {
|
TEST(ExceptionHandlerTest, ContentLength) {
|
||||||
Server svr;
|
Server svr;
|
||||||
|
|
||||||
|
@ -1222,6 +1227,7 @@ TEST(ExceptionHandlerTest, ContentLength) {
|
||||||
thread.join();
|
thread.join();
|
||||||
ASSERT_FALSE(svr.is_running());
|
ASSERT_FALSE(svr.is_running());
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST(NoContentTest, ContentLength) {
|
TEST(NoContentTest, ContentLength) {
|
||||||
Server svr;
|
Server svr;
|
||||||
|
@ -3681,6 +3687,7 @@ TEST(MountTest, Unmount) {
|
||||||
ASSERT_FALSE(svr.is_running());
|
ASSERT_FALSE(svr.is_running());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
TEST(ExceptionTest, ThrowExceptionInHandler) {
|
TEST(ExceptionTest, ThrowExceptionInHandler) {
|
||||||
Server svr;
|
Server svr;
|
||||||
|
|
||||||
|
@ -3709,6 +3716,7 @@ TEST(ExceptionTest, ThrowExceptionInHandler) {
|
||||||
listen_thread.join();
|
listen_thread.join();
|
||||||
ASSERT_FALSE(svr.is_running());
|
ASSERT_FALSE(svr.is_running());
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST(KeepAliveTest, ReadTimeout) {
|
TEST(KeepAliveTest, ReadTimeout) {
|
||||||
Server svr;
|
Server svr;
|
||||||
|
@ -4515,9 +4523,11 @@ TEST(NoSSLSupport, SimpleInterface) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef CPPHTTPLIB_NO_EXCEPTIONS
|
||||||
TEST(InvalidScheme, SimpleInterface) {
|
TEST(InvalidScheme, SimpleInterface) {
|
||||||
ASSERT_ANY_THROW(Client cli("scheme://yahoo.com"));
|
ASSERT_ANY_THROW(Client cli("scheme://yahoo.com"));
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
TEST(NoScheme, SimpleInterface) {
|
TEST(NoScheme, SimpleInterface) {
|
||||||
Client cli("yahoo.com:80");
|
Client cli("yahoo.com:80");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue